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

import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { LessonPlanList } from "./LessonPlanList";
import type { LessonPlan } 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: LessonPlan[] = [
  {
    id: "lp-001",
    title: "一元二次方程的概念",
    subject: "数学",
    className: "初一(1)班",
    teacherName: "王老师",
    textbookTitle: "人教版七年级数学下册",
    chapterTitle: "第一章 一元二次方程",
    status: "published",
    publishedAt: "2026-02-18T10:00:00Z",
    estimatedMinutes: 45,
  },
  {
    id: "lp-003",
    title: "文言文虚词专题（一）",
    subject: "语文",
    className: "初一(1)班",
    teacherName: "李老师",
    textbookTitle: "人教版七年级语文下册",
    chapterTitle: "第二单元 文言文阅读",
    status: "published",
    publishedAt: "2026-03-05T10:00:00Z",
    estimatedMinutes: 45,
  },
];

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

describe("LessonPlanList 渲染", () => {
  it("渲染全部备课标题", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    expect(screen.getByText("一元二次方程的概念")).toBeInTheDocument();
    expect(screen.getByText("文言文虚词专题（一）")).toBeInTheDocument();
  });

  it("渲染学科标签", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    // 学科标签 + 文本中均出现，至少 1 次
    expect(screen.getAllByText("数学").length).toBeGreaterThanOrEqual(1);
    expect(screen.getAllByText("语文").length).toBeGreaterThanOrEqual(1);
  });

  it("渲染已发布状态徽标", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    expect(screen.getAllByText("已发布")).toHaveLength(2);
  });

  it("渲染教师、教材与章节信息", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    expect(screen.getByText("王老师")).toBeInTheDocument();
    expect(screen.getByText("李老师")).toBeInTheDocument();
    expect(screen.getByText("人教版七年级数学下册")).toBeInTheDocument();
    expect(screen.getByText("人教版七年级语文下册")).toBeInTheDocument();
    expect(screen.getByText("第一章 一元二次方程")).toBeInTheDocument();
  });

  it("渲染预计时长", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    expect(screen.getAllByText("预计 45 分钟")).toHaveLength(2);
  });

  it("跳转链接指向只读详情页", () => {
    render(<LessonPlanList lessonPlans={mockPlans} />);
    const mathLink = screen.getByRole("link", {
      name: "一元二次方程的概念 - 查看备课",
    });
    expect(mathLink).toHaveAttribute(
      "href",
      "/parent/lesson-plans/lp-001/view",
    );

    const chineseLink = screen.getByRole("link", {
      name: "文言文虚词专题（一） - 查看备课",
    });
    expect(chineseLink).toHaveAttribute(
      "href",
      "/parent/lesson-plans/lp-003/view",
    );
  });
});
