// AttendanceWarningBanner 组件单测
// 依据：参考项目差距补齐 - 考勤异常预警
// 覆盖：无预警不渲染 / 缺勤高危 / 迟到中危 / 出勤率低高危 / 关闭

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

function makeRecords(
  overrides: Partial<AttendanceRecord>[] = [],
): AttendanceRecord[] {
  const records: AttendanceRecord[] = [];
  for (let d = 1; d <= 10; d++) {
    records.push({
      id: `att-${d}`,
      date: `2026-06-${String(d).padStart(2, "0")}`,
      status: "present",
    });
  }
  for (const o of overrides) {
    records.push(o as AttendanceRecord);
  }
  return records;
}

describe("AttendanceWarningBanner 无预警", () => {
  it("所有指标正常时不渲染", () => {
    const records = makeRecords();
    const { container } = render(<AttendanceWarningBanner records={records} />);
    expect(container.firstChild).toBeNull();
  });

  it("空记录不渲染", () => {
    const { container } = render(<AttendanceWarningBanner records={[]} />);
    expect(container.firstChild).toBeNull();
  });
});

describe("AttendanceWarningBanner 缺勤预警", () => {
  it("缺勤≥3 时显示高危预警", () => {
    const records: AttendanceRecord[] = [
      { id: "1", date: "2026-06-01", status: "present" },
      { id: "2", date: "2026-06-02", status: "absent" },
      { id: "3", date: "2026-06-03", status: "absent" },
      { id: "4", date: "2026-06-04", status: "absent" },
      { id: "5", date: "2026-06-05", status: "present" },
    ];
    render(<AttendanceWarningBanner records={records} />);
    expect(screen.getByText(/缺勤 3 次/)).toBeInTheDocument();
    expect(screen.getByText("考勤异常预警")).toBeInTheDocument();
  });

  it("缺勤<3 时不显示缺勤预警", () => {
    // 9 present + 1 absent = 10 条，rate = 9/10 = 90% ≥ 90% 不触发出勤率预警
    // absent=1 < 3 不触发缺勤预警，late=0 < 3 不触发迟到预警
    const records: AttendanceRecord[] = [
      { id: "1", date: "2026-06-01", status: "present" },
      { id: "2", date: "2026-06-02", status: "present" },
      { id: "3", date: "2026-06-03", status: "present" },
      { id: "4", date: "2026-06-04", status: "present" },
      { id: "5", date: "2026-06-05", status: "present" },
      { id: "6", date: "2026-06-06", status: "present" },
      { id: "7", date: "2026-06-07", status: "present" },
      { id: "8", date: "2026-06-08", status: "present" },
      { id: "9", date: "2026-06-09", status: "present" },
      { id: "10", date: "2026-06-10", status: "absent" },
    ];
    const { container } = render(
      <AttendanceWarningBanner records={records} />,
    );
    expect(container.firstChild).toBeNull();
  });
});

describe("AttendanceWarningBanner 迟到预警", () => {
  it("迟到≥3 时显示中危预警", () => {
    const records: AttendanceRecord[] = [
      { id: "1", date: "2026-06-01", status: "present" },
      { id: "2", date: "2026-06-02", status: "late" },
      { id: "3", date: "2026-06-03", status: "late" },
      { id: "4", date: "2026-06-04", status: "late" },
      { id: "5", date: "2026-06-05", status: "present" },
      { id: "6", date: "2026-06-06", status: "present" },
      { id: "7", date: "2026-06-07", status: "present" },
    ];
    render(<AttendanceWarningBanner records={records} />);
    expect(screen.getByText(/迟到 3 次/)).toBeInTheDocument();
    expect(screen.getByText("考勤提醒")).toBeInTheDocument();
  });
});

describe("AttendanceWarningBanner 出勤率低预警", () => {
  it("出勤率<90% 时显示高危预警", () => {
    const records: AttendanceRecord[] = [
      { id: "1", date: "2026-06-01", status: "present" },
      { id: "2", date: "2026-06-02", status: "present" },
      { id: "3", date: "2026-06-03", status: "absent" },
      { id: "4", date: "2026-06-04", status: "absent" },
    ];
    render(<AttendanceWarningBanner records={records} />);
    // rate = 2/4 = 50% < 90%
    expect(screen.getByText(/出勤率偏低/)).toBeInTheDocument();
    expect(screen.getByText(/50/)).toBeInTheDocument();
  });
});

describe("AttendanceWarningBanner 关闭功能", () => {
  it("点击关闭按钮后横幅消失", async () => {
    const user = userEvent.setup();
    const records: AttendanceRecord[] = [
      { id: "1", date: "2026-06-01", status: "present" },
      { id: "2", date: "2026-06-02", status: "absent" },
      { id: "3", date: "2026-06-03", status: "absent" },
      { id: "4", date: "2026-06-04", status: "absent" },
    ];
    render(<AttendanceWarningBanner records={records} />);
    expect(screen.getByText(/缺勤/)).toBeInTheDocument();
    const closeBtn = screen.getByRole("button", { name: "关闭预警" });
    await user.click(closeBtn);
    expect(screen.queryByText(/缺勤/)).not.toBeInTheDocument();
  });
});
