// CoursePlanList 组件单测
// 依据：02-architecture-design.md §15 组件设计
// 覆盖：空态 / 列表渲染 / 状态徽标 / 跳转链接

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

// mock next/link 为普通 <a>
vi.mock("next/link", () => ({
  default: ({
    href,
    children,
    ...rest
  }: {
    href: string;
    children: React.ReactNode;
    [key: string]: unknown;
  }) => (
    <a href={href} {...rest}>
      {children}
    </a>
  ),
}));

const mockPlans: CoursePlan[] = [
  {
    id: "cp-001",
    title: "初一数学下学期课程计划",
    description: "数学描述",
    subject: "数学",
    className: "初一(1)班",
    teacherName: "王老师",
    startDate: "2026-02-15",
    endDate: "2026-07-10",
    status: "active",
    chapterCount: 8,
  },
  {
    id: "cp-002",
    title: "初一语文下学期课程计划",
    description: "语文描述",
    subject: "语文",
    className: "初一(1)班",
    teacherName: "李老师",
    startDate: "2026-02-15",
    endDate: "2026-07-10",
    status: "active",
    chapterCount: 6,
  },
  {
    id: "cp-003",
    title: "初一英语上学期课程计划",
    description: "英语描述",
    subject: "英语",
    className: "初一(1)班",
    teacherName: "张老师",
    startDate: "2025-09-01",
    endDate: "2026-01-15",
    status: "completed",
    chapterCount: 10,
  },
];

describe("CoursePlanList 空态", () => {
  it("空数组渲染暂无课程计划", () => {
    render(<CoursePlanList coursePlans={[]} />);
    expect(screen.getByText("暂无课程计划")).toBeInTheDocument();
  });
});

describe("CoursePlanList 渲染", () => {
  it("渲染全部 3 条课程计划标题", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    expect(
      screen.getByText("初一数学下学期课程计划"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("初一语文下学期课程计划"),
    ).toBeInTheDocument();
    expect(
      screen.getByText("初一英语上学期课程计划"),
    ).toBeInTheDocument();
  });

  it("渲染学科标签", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    expect(screen.getByText("数学")).toBeInTheDocument();
    expect(screen.getByText("语文")).toBeInTheDocument();
    expect(screen.getByText("英语")).toBeInTheDocument();
  });

  it("渲染状态徽标（进行中/已完成）", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    expect(screen.getAllByText("进行中")).toHaveLength(2);
    expect(screen.getAllByText("已完成")).toHaveLength(1);
  });

  it("渲染教师与班级信息", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    expect(screen.getAllByText("王老师")).toHaveLength(1);
    expect(screen.getAllByText("李老师")).toHaveLength(1);
    expect(screen.getAllByText("张老师")).toHaveLength(1);
  });

  it("渲染章节数", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    expect(screen.getByText("8")).toBeInTheDocument();
    expect(screen.getByText("6")).toBeInTheDocument();
    expect(screen.getByText("10")).toBeInTheDocument();
  });

  it("每条课程计划跳转链接指向详情页", () => {
    render(<CoursePlanList coursePlans={mockPlans} />);
    const mathLink = screen.getByRole("link", {
      name: "初一数学下学期课程计划 - 查看详情",
    });
    expect(mathLink).toHaveAttribute("href", "/parent/course-plans/cp-001");

    const englishLink = screen.getByRole("link", {
      name: "初一英语上学期课程计划 - 查看详情",
    });
    expect(englishLink).toHaveAttribute(
      "href",
      "/parent/course-plans/cp-003",
    );
  });
});
