import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { DeadlineTrafficLights } from "@/components/dashboard/DeadlineTrafficLights";
import type { DeadlineSummary } from "@/lib/types";

describe("DeadlineTrafficLights", () => {
  const defaultData: DeadlineSummary = {
    overdue_count: 3,
    due_this_week: 5,
    due_next_week: 2,
    ok_count: 10,
  };

  it("renders all three traffic light cards", () => {
    render(<DeadlineTrafficLights data={defaultData} />);

    expect(screen.getByText("Überfällig")).toBeInTheDocument();
    expect(screen.getByText("Diese Woche")).toBeInTheDocument();
    expect(screen.getByText("Im Zeitplan")).toBeInTheDocument();
  });

  it("displays correct counts", () => {
    render(<DeadlineTrafficLights data={defaultData} />);

    // Overdue: 3
    expect(screen.getByText("3")).toBeInTheDocument();
    // This week: 5
    expect(screen.getByText("5")).toBeInTheDocument();
    // OK: ok_count + due_next_week = 10 + 2 = 12
    expect(screen.getByText("12")).toBeInTheDocument();
  });

  it("displays zero counts correctly", () => {
    const zeroData: DeadlineSummary = {
      overdue_count: 0,
      due_this_week: 0,
      due_next_week: 0,
      ok_count: 0,
    };

    render(<DeadlineTrafficLights data={zeroData} />);

    const zeros = screen.getAllByText("0");
    expect(zeros).toHaveLength(3);
  });

  it("calls onFilter with correct key when clicked", () => {
    const onFilter = vi.fn();
    render(<DeadlineTrafficLights data={defaultData} onFilter={onFilter} />);

    fireEvent.click(screen.getByText("Überfällig"));
    expect(onFilter).toHaveBeenCalledWith("overdue");

    fireEvent.click(screen.getByText("Diese Woche"));
    expect(onFilter).toHaveBeenCalledWith("this_week");

    fireEvent.click(screen.getByText("Im Zeitplan"));
    expect(onFilter).toHaveBeenCalledWith("ok");
  });

  it("renders without onFilter prop (no crash)", () => {
    expect(() => {
      render(<DeadlineTrafficLights data={defaultData} />);
      fireEvent.click(screen.getByText("Überfällig"));
    }).not.toThrow();
  });
});
