import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { AvailabilityCard } from "./AvailabilityCard";

describe("AvailabilityCard", () => {
  const defaultProps = {
    day: 0, // Monday
    enabled: true,
    onEnabledChange: vi.fn(),
    timeRanges: [{ start: "09:00", end: "17:00" }],
    onTimeRangesChange: vi.fn(),
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("renders without crashing", () => {
    render(<AvailabilityCard {...defaultProps} />);
    expect(screen.getByText("Lundi")).toBeInTheDocument();
  });

  it("displays the correct day name", () => {
    render(<AvailabilityCard {...defaultProps} day={2} />);
    expect(screen.getByText("Mercredi")).toBeInTheDocument();
  });

  it("shows enabled state correctly", () => {
    render(<AvailabilityCard {...defaultProps} />);
    expect(screen.getByText("Disponible")).toBeInTheDocument();
  });

  it("shows disabled state correctly", () => {
    render(<AvailabilityCard {...defaultProps} enabled={false} />);
    expect(screen.getByText("Indisponible")).toBeInTheDocument();
  });

  it("calls onEnabledChange when switch is toggled", () => {
    const onEnabledChange = vi.fn();
    render(<AvailabilityCard {...defaultProps} onEnabledChange={onEnabledChange} />);
    const switchElement = screen.getByRole("switch");
    fireEvent.click(switchElement);
    expect(onEnabledChange).toHaveBeenCalled();
  });

  it("displays time ranges", () => {
    render(<AvailabilityCard {...defaultProps} />);
    expect(screen.getByDisplayValue("09:00")).toBeInTheDocument();
    expect(screen.getByDisplayValue("17:00")).toBeInTheDocument();
  });

  it("displays multiple time ranges", () => {
    const props = {
      ...defaultProps,
      timeRanges: [
        { start: "09:00", end: "12:00" },
        { start: "14:00", end: "17:00" },
      ],
    };
    render(<AvailabilityCard {...props} />);
    expect(screen.getByDisplayValue("09:00")).toBeInTheDocument();
    expect(screen.getByDisplayValue("12:00")).toBeInTheDocument();
    expect(screen.getByDisplayValue("14:00")).toBeInTheDocument();
    expect(screen.getByDisplayValue("17:00")).toBeInTheDocument();
  });

  it("shows add button when less than 3 time ranges", () => {
    render(<AvailabilityCard {...defaultProps} />);
    expect(screen.getByText("Ajouter une plage horaire")).toBeInTheDocument();
  });

  it("adds time range when add button is clicked", () => {
    const onTimeRangesChange = vi.fn();
    render(<AvailabilityCard {...defaultProps} onTimeRangesChange={onTimeRangesChange} />);
    fireEvent.click(screen.getByText("Ajouter une plage horaire"));
    expect(onTimeRangesChange).toHaveBeenCalled();
  });

  it("shows delete button when multiple time ranges", () => {
    const props = {
      ...defaultProps,
      timeRanges: [
        { start: "09:00", end: "12:00" },
        { start: "14:00", end: "17:00" },
      ],
    };
    const { container } = render(<AvailabilityCard {...props} />);
    const deleteButtons = container.querySelectorAll("button svg");
    // Should have delete buttons for time ranges
    expect(deleteButtons.length).toBeGreaterThan(0);
  });

  it("shows copy button when onCopyToOtherDays is provided", () => {
    const props = {
      ...defaultProps,
      onCopyToOtherDays: vi.fn(),
    };
    render(<AvailabilityCard {...props} />);
    expect(screen.getByText("Copier")).toBeInTheDocument();
  });

  it("calls onCopyToOtherDays when copy button is clicked", () => {
    const onCopyToOtherDays = vi.fn();
    const props = {
      ...defaultProps,
      onCopyToOtherDays,
    };
    render(<AvailabilityCard {...props} />);
    fireEvent.click(screen.getByText("Copier"));
    expect(onCopyToOtherDays).toHaveBeenCalledWith(0, true, defaultProps.timeRanges);
  });

  it("disables inputs when not enabled", () => {
    const props = {
      ...defaultProps,
      enabled: false,
    };
    render(<AvailabilityCard {...props} />);
    const startInput = screen.getByDisplayValue("09:00");
    expect(startInput).toBeDisabled();
  });
});
