import type { MobileIntervention } from "../../src/types/intervention";
import {
  detailValue,
  formatInterventionDate,
  getFluideLabel,
  getStatusStyle,
  getTypeLabel,
} from "../../src/shared/interventionUtils";

describe("interventionUtils", () => {
  describe("formatInterventionDate", () => {
    it("retourne - si valeur absente", () => {
      expect(formatInterventionDate(undefined)).toBe("-");
      expect(formatInterventionDate("")).toBe("-");
    });

    it("retourne la valeur brute si date invalide", () => {
      expect(formatInterventionDate("pas-une-date")).toBe("pas-une-date");
    });

    it("formatte une date ISO avec espace", () => {
      const result = formatInterventionDate("2026-04-18 10:30:00");
      expect(typeof result).toBe("string");
      expect(result).not.toBe("-");
      expect(result).not.toBe("2026-04-18 10:30:00");
    });
  });

  describe("detailValue", () => {
    it("retourne - pour null ou undefined", () => {
      expect(detailValue(null)).toBe("-");
      expect(detailValue(undefined)).toBe("-");
    });

    it("retourne - pour chaine vide apres trim", () => {
      expect(detailValue("   ")).toBe("-");
      expect(detailValue("")).toBe("-");
    });

    it("retourne la valeur stringifiee", () => {
      expect(detailValue("valeur")).toBe("valeur");
      expect(detailValue(42)).toBe("42");
      expect(detailValue(true)).toBe("true");
    });

    it("stringifie un objet via JSON au lieu de [object Object]", () => {
      expect(detailValue({ a: 1 })).toBe('{"a":1}');
      expect(detailValue([1, 2])).toBe("[1,2]");
    });
  });

  describe("getTypeLabel", () => {
    it("retourne - si type absent", () => {
      expect(getTypeLabel(undefined)).toBe("-");
      expect(getTypeLabel("")).toBe("-");
    });

    it("retourne le label mappe pour les types connus", () => {
      expect(getTypeLabel("maintenance_preventive")).toBe("Maintenance preventive");
      expect(getTypeLabel("maintenance_corrective")).toBe("Maintenance corrective");
      expect(getTypeLabel("installation")).toBe("Installation");
      expect(getTypeLabel("mise_en_service")).toBe("Mise en service");
      expect(getTypeLabel("controle_reglementaire")).toBe("Controle reglementaire");
      expect(getTypeLabel("arret_definitif")).toBe("Arret definitif");
      expect(getTypeLabel("fuite")).toBe("Reparation fuite");
      expect(getTypeLabel("recharge")).toBe("Recharge fluide");
    });

    it("retourne la cle brute si type inconnu", () => {
      expect(getTypeLabel("inconnu")).toBe("inconnu");
    });
  });

  describe("getFluideLabel", () => {
    const base: MobileIntervention = { id: 1 };

    it("combine marque et modele quand les deux sont presents", () => {
      expect(
        getFluideLabel({ ...base, machine_marque: "Daikin", machine_modele: "RXM35" })
      ).toBe("Daikin RXM35");
    });

    it("retourne la marque seule si pas de modele", () => {
      expect(getFluideLabel({ ...base, machine_marque: "Daikin" })).toBe("Daikin");
    });

    it("retourne un label generique si seulement machine_nom", () => {
      expect(getFluideLabel({ ...base, machine_nom: "Groupe froid" })).toBe(
        "Fluide frigorifique"
      );
    });

    it("retourne - si rien n'est renseigne", () => {
      expect(getFluideLabel(base)).toBe("-");
    });
  });

  describe("getStatusStyle", () => {
    it("retourne le style Inconnu quand le statut est absent", () => {
      const style = getStatusStyle(undefined);
      expect(style.label).toBe("Inconnu");
      expect(style.color).toBe("#374151");
    });

    it("retourne le style mappe pour chaque statut connu", () => {
      expect(getStatusStyle("en_cours").label).toBe("En cours");
      expect(getStatusStyle("terminee").label).toBe("Terminee");
      expect(getStatusStyle("planifiee").label).toBe("Planifiee");
      expect(getStatusStyle("reportee").label).toBe("Reportee");
      expect(getStatusStyle("en_attente").label).toBe("En attente");
      expect(getStatusStyle("annulee").label).toBe("Annulee");
    });

    it("retourne un style fallback avec la cle brute comme label si statut inconnu", () => {
      const style = getStatusStyle("zzz");
      expect(style.label).toBe("zzz");
      expect(style.color).toBe("#374151");
    });
  });
});
