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

const makeItem = (over: Partial<MobileIntervention> = {}): MobileIntervention => ({
  id: Math.floor(Math.random() * 1e9),
  titre: "Mission",
  statut: "planifiee",
  date_prevue: "2026-06-12 09:00:00",
  ...over,
});

describe("StatsScreen", () => {
  beforeEach(() => {
    // Horloge figee : "maintenant" = vendredi 12 juin 2026 10:00 (mois 5, 2026).
    jest.useFakeTimers().setSystemTime(new Date("2026-06-12T10:00:00"));
  });

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

  it("liste vide -> tous les compteurs a 0 et derniere = 'Aucune'", () => {
    const { getByText, getAllByText } = render(
      <StatsScreen interventions={[]} brandColor="#1E56A8" />
    );
    // 4 tuiles + total = 5 fois la valeur "0".
    expect(getAllByText("0").length).toBe(5);
    expect(getByText("Aucune")).toBeTruthy();
  });

  it("calcule chaque compteur en exercant toutes les branches", () => {
    // Ordre choisi pour que `last` soit d'abord pose (!last), puis mis a jour
    // (date > last), puis laisse inchange (date < last).
    const interventions: MobileIntervention[] = [
      // --- TODO (5) ---
      makeItem({ statut: "planifiee", date_prevue: "2026-06-12 09:00:00" }), // todo + ce mois + aujourd'hui
      makeItem({ statut: "planifiee", date_prevue: "2026-07-15 09:00:00" }), // todo, juillet -> hors mois
      makeItem({ statut: "reportee", date_prevue: undefined }), // todo, parseDate null
      makeItem({ statut: "en_cours", date_prevue: "pas-une-date" }), // todo, parseDate invalide
      makeItem({ statut: "en_attente", date_prevue: "2026-05-01 09:00:00" }), // todo, mai -> hors mois
      // --- DONE (3) ---
      makeItem({ statut: "terminee", date_prevue: "2026-04-10 09:00:00" }), // done, !last -> last=04-10
      makeItem({ statut: "terminee", date_prevue: "2026-06-05 09:00:00" }), // done + ce mois, 06-05 > 04-10 -> last
      makeItem({ statut: "cloturee", date_prevue: "2026-03-01 09:00:00" }), // done, 03-01 < last -> inchange
      // --- annulee : counts=false (ni todo ni done, exclue de ce-mois/aujourd'hui) ---
      makeItem({ statut: "annulee", date_prevue: "2026-06-12 09:00:00" }),
    ];

    const { getByText, queryByText } = render(
      <StatsScreen interventions={interventions} brandColor="#1E56A8" />
    );

    // Valeurs volontairement toutes distinctes (1/2/5/3/9) -> getByText non ambigu.
    expect(getByText("1")).toBeTruthy(); // aujourd'hui
    expect(getByText("2")).toBeTruthy(); // ce mois (06-12 todo + 06-05 done)
    expect(getByText("5")).toBeTruthy(); // a faire
    expect(getByText("3")).toBeTruthy(); // terminees
    expect(getByText("9")).toBeTruthy(); // total
    // last a ete pose (la derniere terminee n'est pas "Aucune").
    expect(queryByText("Aucune")).toBeNull();
  });

  it("annulee avec date du jour n'incremente ni aujourd'hui ni ce mois", () => {
    const interventions = [
      makeItem({ statut: "annulee", date_prevue: "2026-06-12 09:00:00" }),
    ];
    const { getAllByText, getByText } = render(
      <StatsScreen interventions={interventions} brandColor="#1E56A8" />
    );
    // today=0, thisMonth=0, todo=0, done=0 -> 4 tuiles a 0 ; total=1.
    expect(getAllByText("0").length).toBe(4);
    expect(getByText("1")).toBeTruthy(); // total
    expect(getByText("Aucune")).toBeTruthy();
  });
});
