// AttendanceRateCard 组件单测
// 依据：参考项目差距补齐 - 考勤聚合统计
// 覆盖：四指标渲染 / 出勤率颜色分级 / 空数据 / 仅缺勤 / 仅迟到

import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { AttendanceRateCard } from "./AttendanceRateCard";
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("AttendanceRateCard 四指标渲染", () => {
  it("渲染总出勤率 / 出勤天数 / 缺勤天数 / 迟到天数 标签", () => {
    render(<AttendanceRateCard records={makeRecords()} />);
    expect(screen.getByText("总出勤率")).toBeInTheDocument();
    expect(screen.getByText("出勤天数")).toBeInTheDocument();
    expect(screen.getByText("缺勤天数")).toBeInTheDocument();
    expect(screen.getByText("迟到天数")).toBeInTheDocument();
  });

  it("全勤场景渲染正确的出勤天数（10）", () => {
    render(<AttendanceRateCard records={makeRecords()} />);
    // 10 天出勤 → 出勤天数 10
    expect(screen.getByText("10")).toBeInTheDocument();
  });

  it("渲染总出勤率百分比", () => {
    render(<AttendanceRateCard records={makeRecords()} />);
    // 10 天全勤 → 100%
    expect(screen.getByText(/100/)).toBeInTheDocument();
  });
});

describe("AttendanceRateCard 出勤率颜色分级", () => {
  it("出勤率≥95% 总出勤率显示成功色（text-success）", () => {
    const { container } = render(
      <AttendanceRateCard records={makeRecords()} />,
    );
    // 100% ≥ 95%，text-success 应出现在总出勤率数值上
    const successEls = container.querySelectorAll(".text-success");
    expect(successEls.length).toBeGreaterThan(0);
  });

  it("出勤率 90-95% 总出勤率显示警告色（text-warning）", () => {
    // 10 条记录，1 条缺勤 → rate = 9/10 = 90%（边界，落入 warning 区间）
    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(<AttendanceRateCard records={records} />);
    // rate=90% 落入 [0.9, 0.95) 区间 → text-warning
    const warningEls = container.querySelectorAll(".text-warning");
    expect(warningEls.length).toBeGreaterThan(0);
  });

  it("出勤率<90% 总出勤率显示危险色（text-danger）", () => {
    // 5 条记录，2 条缺勤 → rate = 3/5 = 60% < 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: "present" },
      { id: "4", date: "2026-06-04", status: "absent" },
      { id: "5", date: "2026-06-05", status: "absent" },
    ];
    const { container } = render(<AttendanceRateCard records={records} />);
    // rate=60% < 90% → text-danger
    const dangerEls = container.querySelectorAll(".text-danger");
    expect(dangerEls.length).toBeGreaterThan(0);
  });
});

describe("AttendanceRateCard 空数据", () => {
  it("空记录数组时总出勤率显示 100%（默认值）", () => {
    render(<AttendanceRateCard records={[]} />);
    // 空数组时 rate=1 → 100%
    expect(screen.getByText(/100/)).toBeInTheDocument();
  });

  it("空记录数组时所有天数为 0", () => {
    render(<AttendanceRateCard records={[]} />);
    // 出勤/缺勤/迟到 天数均为 0
    const zeros = screen.getAllByText("0");
    expect(zeros.length).toBeGreaterThanOrEqual(3);
  });
});

describe("AttendanceRateCard 状态统计", () => {
  it("统计缺勤天数", () => {
    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: "present" },
    ];
    render(<AttendanceRateCard records={records} />);
    // 缺勤 2 天（用 text-danger 选择器区分出勤天数 2）
    expect(screen.getByText("2", { selector: ".text-danger" })).toBeInTheDocument();
  });

  it("统计迟到天数", () => {
    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" },
    ];
    render(<AttendanceRateCard records={records} />);
    // 迟到 3 天
    expect(screen.getByText("3")).toBeInTheDocument();
  });

  it("请假不计入出勤率（present + late 计入，absent + leave 不计入）", () => {
    // 4 条记录：2 present + 1 late + 1 leave
    // attended = present(2) + late(1) = 3，total = 4，rate = 3/4 = 75%
    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: "late" },
      { id: "4", date: "2026-06-04", status: "leave" },
    ];
    const { container } = render(<AttendanceRateCard records={records} />);
    // rate=75% < 90% → 总出勤率使用 text-danger
    const dangerEls = container.querySelectorAll(".text-danger");
    expect(dangerEls.length).toBeGreaterThan(0);
    // 验证出勤天数 = 2（present）
    expect(screen.getByText("2")).toBeInTheDocument();
  });
});
