// MultiChildTabBar 组件单测
// 依据：02-architecture-design.md §15.2 MultiChildTabBar 设计、ISSUE-009 纯前端切换
// 覆盖：单子女不渲染 / 多子女渲染标签 / 点击切换 / 归档子女过滤 / 当前选中高亮

import { describe, it, expect, beforeEach, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MultiChildTabBar } from "./MultiChildTabBar";
import type { ChildInfo } from "@/types";

// mock useChildSwitcher hook
vi.mock("@/hooks/useChildSwitcher", () => ({
  useChildSwitcher: vi.fn(),
}));

import { useChildSwitcher } from "@/hooks/useChildSwitcher";

const mockChildren: ChildInfo[] = [
  {
    id: "student-001",
    name: "张小明",
    grade: "grade.7",
    schoolName: "实验中学",
    classId: "class-7-1",
    className: "初一(1)班",
  },
  {
    id: "student-002",
    name: "张小红",
    grade: "grade.5",
    schoolName: "实验小学",
    classId: "class-5-2",
    className: "五年级(2)班",
  },
  {
    id: "student-003",
    name: "张小强",
    grade: "grade.3",
    schoolName: "实验小学",
    classId: "class-3-1",
    className: "三年级(1)班",
  },
];

const mockUseChildSwitcher = vi.mocked(useChildSwitcher);

beforeEach(() => {
  mockUseChildSwitcher.mockReset();
});

describe("MultiChildTabBar 单子女 / 无子女", () => {
  it("hasMultipleChildren=false 时不渲染", () => {
    mockUseChildSwitcher.mockReturnValue({
      children: [mockChildren[0]!],
      currentChild: mockChildren[0]!,
      currentChildId: "student-001",
      switchChild: vi.fn(),
      loading: false,
      error: undefined,
      hasMultipleChildren: false,
    });
    const { container } = render(<MultiChildTabBar />);
    expect(container.firstChild).toBeNull();
  });

  it("空子女列表时不渲染", () => {
    mockUseChildSwitcher.mockReturnValue({
      children: [],
      currentChild: null,
      currentChildId: null,
      switchChild: vi.fn(),
      loading: false,
      error: undefined,
      hasMultipleChildren: false,
    });
    const { container } = render(<MultiChildTabBar />);
    expect(container.firstChild).toBeNull();
  });
});

describe("MultiChildTabBar 多子女", () => {
  beforeEach(() => {
    mockUseChildSwitcher.mockReturnValue({
      children: mockChildren,
      currentChild: mockChildren[0]!,
      currentChildId: "student-001",
      switchChild: vi.fn(),
      loading: false,
      error: undefined,
      hasMultipleChildren: true,
    });
  });

  it("渲染 tablist 导航", () => {
    render(<MultiChildTabBar />);
    expect(screen.getByRole("tablist")).toBeInTheDocument();
    expect(screen.getByRole("tablist")).toHaveAttribute(
      "aria-label",
      "选择子女",
    );
  });

  it("渲染所有子女标签", () => {
    render(<MultiChildTabBar />);
    expect(screen.getByText("张小明")).toBeInTheDocument();
    expect(screen.getByText("张小红")).toBeInTheDocument();
    expect(screen.getByText("张小强")).toBeInTheDocument();
  });

  it("每个标签为 role=tab", () => {
    render(<MultiChildTabBar />);
    const tabs = screen.getAllByRole("tab");
    expect(tabs).toHaveLength(3);
  });

  it("当前选中标签 aria-selected=true", () => {
    render(<MultiChildTabBar />);
    const tabs = screen.getAllByRole("tab");
    expect(tabs[0]).toHaveAttribute("aria-selected", "true");
    expect(tabs[1]).toHaveAttribute("aria-selected", "false");
    expect(tabs[2]).toHaveAttribute("aria-selected", "false");
  });

  it("点击标签调用 switchChild", async () => {
    const user = userEvent.setup();
    const switchChild = vi.fn();
    mockUseChildSwitcher.mockReturnValue({
      children: mockChildren,
      currentChild: mockChildren[0]!,
      currentChildId: "student-001",
      switchChild,
      loading: false,
      error: undefined,
      hasMultipleChildren: true,
    });
    render(<MultiChildTabBar />);
    await user.click(screen.getByText("张小红"));
    expect(switchChild).toHaveBeenCalledWith("student-002");
  });
});

describe("MultiChildTabBar 归档子女过滤", () => {
  it("归档子女不渲染标签", () => {
    const withArchived: ChildInfo[] = [
      mockChildren[0]!,
      { ...mockChildren[1]!, isArchived: true },
      mockChildren[2]!,
    ];
    mockUseChildSwitcher.mockReturnValue({
      children: withArchived,
      currentChild: mockChildren[0]!,
      currentChildId: "student-001",
      switchChild: vi.fn(),
      loading: false,
      error: undefined,
      hasMultipleChildren: true,
    });
    render(<MultiChildTabBar />);
    expect(screen.getByText("张小明")).toBeInTheDocument();
    expect(screen.queryByText("张小红")).not.toBeInTheDocument();
    expect(screen.getByText("张小强")).toBeInTheDocument();
    // 仅 2 个 tab（归档的不算）
    expect(screen.getAllByRole("tab")).toHaveLength(2);
  });
});
