// ParentAttentionBanner 组件单测
// 依据：参考项目差距补齐 - 仪表盘关注横幅
// 覆盖：无预警不渲染 / 逾期作业预警 / 出勤率低预警 / 关闭横幅 / 多子女预警

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ParentAttentionBanner } from "./ParentAttentionBanner";

interface AttentionItem {
  childId: string;
  childName: string;
  pendingHomeworkCount: number;
  attendanceRate: number;
}

function makeItem(overrides: Partial<AttentionItem> = {}): AttentionItem {
  return {
    childId: "student-001",
    childName: "张小明",
    pendingHomeworkCount: 0,
    attendanceRate: 1,
    ...overrides,
  };
}

describe("ParentAttentionBanner 无预警", () => {
  it("所有子女均正常时不渲染横幅", () => {
    const { container } = render(
      <ParentAttentionBanner
        items={[makeItem({ pendingHomeworkCount: 0, attendanceRate: 0.96 })]}
      />,
    );
    expect(container.firstChild).toBeNull();
  });

  it("空 items 数组不渲染横幅", () => {
    const { container } = render(<ParentAttentionBanner items={[]} />);
    expect(container.firstChild).toBeNull();
  });
});

describe("ParentAttentionBanner 逾期作业预警", () => {
  it("pendingHomeworkCount > 0 时显示待办作业预警", () => {
    render(
      <ParentAttentionBanner
        items={[makeItem({ pendingHomeworkCount: 3 })]}
      />,
    );
    expect(screen.getByText(/张小明有 3 份待办作业/)).toBeInTheDocument();
  });

  it("预警级别为 warning（温馨提醒）", () => {
    render(
      <ParentAttentionBanner
        items={[makeItem({ pendingHomeworkCount: 2 })]}
      />,
    );
    expect(screen.getByText("温馨提醒")).toBeInTheDocument();
  });
});

describe("ParentAttentionBanner 出勤率低预警", () => {
  it("attendanceRate < 0.9 时显示出勤率偏低预警", () => {
    render(
      <ParentAttentionBanner
        items={[makeItem({ attendanceRate: 0.85 })]}
      />,
    );
    expect(screen.getByText(/张小明出勤率偏低/)).toBeInTheDocument();
    expect(screen.getByText(/85/)).toBeInTheDocument();
  });

  it("出勤率低预警级别为 danger（需要关注）", () => {
    render(
      <ParentAttentionBanner
        items={[makeItem({ attendanceRate: 0.85 })]}
      />,
    );
    expect(screen.getByText("需要关注")).toBeInTheDocument();
  });

  it("attendanceRate = 0.9 时不预警（边界值）", () => {
    const { container } = render(
      <ParentAttentionBanner
        items={[makeItem({ attendanceRate: 0.9 })]}
      />,
    );
    expect(container.firstChild).toBeNull();
  });
});

describe("ParentAttentionBanner 多子女预警", () => {
  it("多个子女同时预警时显示多条信息", () => {
    const items = [
      makeItem({
        childId: "student-001",
        childName: "张小明",
        pendingHomeworkCount: 3,
      }),
      makeItem({
        childId: "student-002",
        childName: "张小红",
        attendanceRate: 0.8,
      }),
    ];
    render(<ParentAttentionBanner items={items} />);
    expect(screen.getByText(/张小明有 3 份待办作业/)).toBeInTheDocument();
    expect(screen.getByText(/张小红出勤率偏低/)).toBeInTheDocument();
  });

  it("有 danger 预警时标题为需要关注", () => {
    const items = [
      makeItem({
        childId: "student-001",
        childName: "张小明",
        pendingHomeworkCount: 1,
      }),
      makeItem({
        childId: "student-002",
        childName: "张小红",
        attendanceRate: 0.85,
      }),
    ];
    render(<ParentAttentionBanner items={items} />);
    expect(screen.getByText("需要关注")).toBeInTheDocument();
  });
});

describe("ParentAttentionBanner 关闭功能", () => {
  it("点击关闭按钮后横幅消失", async () => {
    const user = userEvent.setup();
    render(
      <ParentAttentionBanner
        items={[makeItem({ pendingHomeworkCount: 1 })]}
      />,
    );
    expect(screen.getByText(/份待办作业/)).toBeInTheDocument();
    const closeBtn = screen.getByRole("button", { name: "关闭横幅" });
    await user.click(closeBtn);
    expect(screen.queryByText(/份待办作业/)).not.toBeInTheDocument();
  });

  it("关闭按钮有 aria-label", () => {
    render(
      <ParentAttentionBanner
        items={[makeItem({ pendingHomeworkCount: 1 })]}
      />,
    );
    expect(screen.getByRole("button", { name: "关闭横幅" })).toBeInTheDocument();
  });
});
