// ElectiveSelectionList 组件单测
// 依据：02-architecture-design.md §15 组件设计
// 覆盖：空态 / 列表渲染 / 分类标签 / 状态徽标 / 成绩（如有）

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { ElectiveSelectionList } from "./ElectiveSelectionList";
import type { ElectiveSelection } from "@/types";

const mockSelections: ElectiveSelection[] = [
  {
    id: "ele-001",
    childId: "student-001",
    courseName: "创意写作",
    courseCode: "ELECTIVE-A001",
    teacherName: "陈老师",
    category: "language",
    credits: 2,
    schedule: "周三 15:00-16:30",
    status: "enrolled",
    enrolledAt: "2026-02-25T09:00:00Z",
  },
  {
    id: "ele-002",
    childId: "student-001",
    courseName: "趣味物理实验",
    courseCode: "ELECTIVE-S012",
    teacherName: "周老师",
    category: "science",
    credits: 2,
    schedule: "周五 15:00-16:30",
    status: "enrolled",
    enrolledAt: "2026-02-25T09:00:00Z",
  },
  {
    id: "ele-003",
    childId: "student-001",
    courseName: "篮球基础",
    courseCode: "ELECTIVE-P005",
    teacherName: "孙老师",
    category: "sports",
    credits: 1,
    schedule: "周一 16:30-18:00",
    status: "completed",
    enrolledAt: "2025-09-05T09:00:00Z",
    score: 88,
  },
];

describe("ElectiveSelectionList 空态", () => {
  it("空数组渲染暂无选修课记录", () => {
    render(<ElectiveSelectionList electiveSelections={[]} />);
    expect(screen.getByText("暂无选修课记录")).toBeInTheDocument();
  });
});

describe("ElectiveSelectionList 渲染", () => {
  it("渲染全部课程名", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getByText("创意写作")).toBeInTheDocument();
    expect(screen.getByText("趣味物理实验")).toBeInTheDocument();
    expect(screen.getByText("篮球基础")).toBeInTheDocument();
  });

  it("渲染课程代码", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getByText("ELECTIVE-A001")).toBeInTheDocument();
    expect(screen.getByText("ELECTIVE-S012")).toBeInTheDocument();
    expect(screen.getByText("ELECTIVE-P005")).toBeInTheDocument();
  });

  it("渲染分类标签", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getByText("语言")).toBeInTheDocument();
    expect(screen.getByText("科学")).toBeInTheDocument();
    expect(screen.getByText("体育")).toBeInTheDocument();
  });

  it("渲染状态徽标（已选/已完成）", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getAllByText("已选")).toHaveLength(2);
    expect(screen.getAllByText("已完成")).toHaveLength(1);
  });

  it("渲染教师、学分与上课时间", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getByText("陈老师")).toBeInTheDocument();
    expect(screen.getByText("周老师")).toBeInTheDocument();
    expect(screen.getByText("孙老师")).toBeInTheDocument();
    expect(screen.getByText("周三 15:00-16:30")).toBeInTheDocument();
    expect(screen.getByText("周一 16:30-18:00")).toBeInTheDocument();
  });

  it("渲染学分", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getAllByText("2")).toHaveLength(2);
    expect(screen.getByText("1")).toBeInTheDocument();
  });
});

describe("ElectiveSelectionList 成绩", () => {
  it("有成绩时渲染成绩", () => {
    render(<ElectiveSelectionList electiveSelections={mockSelections} />);
    expect(screen.getByText("成绩：")).toBeInTheDocument();
    expect(screen.getByText("88")).toBeInTheDocument();
  });

  it("无成绩时不渲染成绩区块", () => {
    const noScore: ElectiveSelection[] = [
      {
        ...mockSelections[0]!,
        status: "enrolled",
        score: undefined,
      },
    ];
    render(<ElectiveSelectionList electiveSelections={noScore} />);
    expect(screen.queryByText("成绩：")).not.toBeInTheDocument();
  });

  it("已退选状态渲染已退选徽标", () => {
    const dropped: ElectiveSelection[] = [
      {
        ...mockSelections[0]!,
        status: "dropped",
        score: undefined,
      },
    ];
    render(<ElectiveSelectionList electiveSelections={dropped} />);
    expect(screen.getByText("已退选")).toBeInTheDocument();
  });
});
