import React from "react";
import { render, fireEvent } from "@testing-library/react-native";
import PlanningScreen from "../../../src/features/home/PlanningScreen";
import type { MobileIntervention } from "../../../src/types/intervention";

let nextId = 1;
const makeItem = (over: Partial<MobileIntervention> = {}): MobileIntervention => ({
  id: nextId++,
  titre: "Mission",
  statut: "planifiee",
  date_prevue: "2026-06-12 09:00:00",
  site_nom: "Site Alpha",
  machine_nom: "Compresseur 1",
  ...over,
});

const baseProps = {
  brandColor: "#1E56A8",
  onOpenDetail: jest.fn(),
};

describe("PlanningScreen", () => {
  beforeEach(() => {
    nextId = 1;
    baseProps.onOpenDetail = jest.fn();
    // "Maintenant" = vendredi 12 juin 2026.
    jest.useFakeTimers().setSystemTime(new Date("2026-06-12T10:00:00"));
  });

  afterEach(() => {
    jest.useRealTimers();
  });

  it("affiche le mois courant et la liste du jour selectionne (aujourd'hui)", () => {
    const { getByText } = render(
      <PlanningScreen
        {...baseProps}
        interventions={[makeItem({ site_nom: "Site Alpha", date_prevue: "2026-06-12 09:00:00" })]}
      />
    );
    expect(getByText("Juin 2026")).toBeTruthy();
    expect(getByText(/1 intervention$/)).toBeTruthy(); // singulier, pas de "s"
    // Jour selectionne par defaut = aujourd'hui (12) -> son intervention listee.
    expect(getByText("Site Alpha")).toBeTruthy();
    expect(getByText("09:00")).toBeTruthy();
  });

  it("filtre les statuts non-todo et les interventions sans date", () => {
    const { getByText, queryByText } = render(
      <PlanningScreen
        {...baseProps}
        interventions={[
          makeItem({ statut: "terminee", date_prevue: "2026-06-12 09:00:00", site_nom: "Terminee" }),
          makeItem({ statut: "planifiee", date_prevue: undefined, site_nom: "SansDate" }),
          makeItem({ statut: "planifiee", date_prevue: "date-invalide", site_nom: "DateKO" }),
        ]}
      />
    );
    // Aucune des deux ne doit compter dans le calendrier.
    expect(getByText(/^0 intervention$/)).toBeTruthy();
    expect(queryByText("Terminee")).toBeNull();
    expect(queryByText("SansDate")).toBeNull();
    expect(getByText("Aucune intervention ce jour-la.")).toBeTruthy();
  });

  it("pluriel: 2 interventions affiche un 's'", () => {
    const { getByText } = render(
      <PlanningScreen
        {...baseProps}
        interventions={[
          makeItem({ date_prevue: "2026-06-12 09:00:00" }),
          makeItem({ date_prevue: "2026-06-18 14:00:00" }),
        ]}
      />
    );
    expect(getByText(/2 interventions$/)).toBeTruthy();
  });

  it("navigue vers le mois suivant et precedent", () => {
    // Interventions en juin : apres passage a juillet, monthCount itere des
    // entrees d'un AUTRE mois (branche startsWith=false) -> 0.
    const { getByText } = render(
      <PlanningScreen {...baseProps} interventions={[makeItem({ date_prevue: "2026-06-12 09:00:00" })]} />
    );
    fireEvent.press(getByText("›"));
    expect(getByText("Juillet 2026")).toBeTruthy();
    expect(getByText(/^0 intervention$/)).toBeTruthy();
    fireEvent.press(getByText("‹"));
    fireEvent.press(getByText("‹"));
    expect(getByText("Mai 2026")).toBeTruthy();
  });

  it("selectionne un autre jour -> affiche sa liste (vide ici)", () => {
    const { getByText } = render(
      <PlanningScreen
        {...baseProps}
        interventions={[makeItem({ date_prevue: "2026-06-12 09:00:00" })]}
      />
    );
    // Le 20 n'a aucune intervention.
    fireEvent.press(getByText("20"));
    expect(getByText("Aucune intervention ce jour-la.")).toBeTruthy();
  });

  it("tap sur une intervention -> onOpenDetail(id)", () => {
    const { getByText } = render(
      <PlanningScreen
        {...baseProps}
        interventions={[makeItem({ id: 42, date_prevue: "2026-06-12 09:00:00", site_nom: "Site Alpha" })]}
      />
    );
    fireEvent.press(getByText("Site Alpha"));
    expect(baseProps.onOpenDetail).toHaveBeenCalledWith(42);
  });

  it("initialDay=tomorrow -> selectionne le lendemain (13)", () => {
    const { getByText } = render(
      <PlanningScreen
        {...baseProps}
        initialDay="tomorrow"
        interventions={[makeItem({ date_prevue: "2026-06-13 08:30:00", site_nom: "Demain" })]}
      />
    );
    // L'en-tete du jour selectionne contient le 13, et son intervention est listee.
    expect(getByText("Demain")).toBeTruthy();
    expect(getByText("08:30")).toBeTruthy();
  });
});
