import type { MobileIntervention } from "../types/intervention";

export type InterventionStatusStyle = {
  label: string;
  color: string;
  backgroundColor: string;
  borderColor: string;
};

// Vocabulaire des statuts d'intervention — source unique cote mobile.
// Le backend est la source de verite : l'enum reel de `intervention.statut`
// (coolcare + missioflow-app, identique) = planifiee, en_cours, terminee,
// reportee, annulee (cf. database/schema.sql). Le mobile s'aligne dessus :
//   - TODO  : planifiee, en_cours, reportee (les "a faire / en cours")
//   - DONE  : terminee
//   - annulee : volontairement dans AUCUN onglet (ni a faire, ni terminee)
// "" / en_attente / cloturee sont des valeurs DEFENSIVES : impossibles pour
// une intervention (hors enum) mais tolerees au cas ou (donnee legacy, autre
// table, IN-list defensive du backend). Inoffensives car jamais produites.
export const TODO_STATUSES = new Set(["", "planifiee", "reportee", "en_cours", "en_attente"]);
export const DONE_STATUSES = new Set(["terminee", "cloturee"]);

function normalizeStatut(statut: string | undefined | null): string {
  return String(statut ?? "").toLowerCase();
}

// Mission "a faire / en cours" : a planifier, planifiee, reportee, en cours,
// en attente (le calendrier n'affiche que ces statuts, miroir de planning.php).
export function isTodoStatus(statut: string | undefined | null): boolean {
  return TODO_STATUSES.has(normalizeStatut(statut));
}

// Mission terminee / cloturee (consultation en lecture seule cote mobile).
export function isDoneStatus(statut: string | undefined | null): boolean {
  return DONE_STATUSES.has(normalizeStatut(statut));
}

const TYPE_LABELS: Record<string, string> = {
  maintenance_preventive: "Maintenance preventive",
  maintenance_corrective: "Maintenance corrective",
  installation: "Installation",
  mise_en_service: "Mise en service",
  controle_reglementaire: "Controle reglementaire",
  arret_definitif: "Arret definitif",
  fuite: "Reparation fuite",
  recharge: "Recharge fluide",
};

const STATUS_STYLES: Record<string, InterventionStatusStyle> = {
  en_cours: {
    label: "En cours",
    color: "#0f766e",
    backgroundColor: "#ccfbf1",
    borderColor: "#5eead4",
  },
  terminee: {
    label: "Terminee",
    color: "#166534",
    backgroundColor: "#dcfce7",
    borderColor: "#86efac",
  },
  planifiee: {
    label: "Planifiee",
    color: "#1d4ed8",
    backgroundColor: "#dbeafe",
    borderColor: "#93c5fd",
  },
  reportee: {
    label: "Reportee",
    color: "#92400e",
    backgroundColor: "#ffedd5",
    borderColor: "#fdba74",
  },
  en_attente: {
    label: "En attente",
    color: "#374151",
    backgroundColor: "#f3f4f6",
    borderColor: "#d1d5db",
  },
  annulee: {
    label: "Annulee",
    color: "#991b1b",
    backgroundColor: "#fee2e2",
    borderColor: "#fca5a5",
  },
};

export function formatInterventionDate(value: string | undefined): string {
  if (!value) {
    return "-";
  }

  const parsed = new Date(value.replace(" ", "T"));
  if (Number.isNaN(parsed.getTime())) {
    return value;
  }

  return parsed.toLocaleString();
}

export function detailValue(value: unknown): string {
  if (value === null || value === undefined) {
    return "-";
  }
  let text: string;
  if (typeof value === "string") text = value;
  else if (typeof value === "number" || typeof value === "boolean") text = String(value);
  else text = JSON.stringify(value);
  text = text.trim();
  return text === "" ? "-" : text;
}

export function getTypeLabel(type: string | undefined): string {
  if (!type) {
    return "-";
  }

  return TYPE_LABELS[type] || type;
}

export function getFluideLabel(intervention: MobileIntervention): string {
  if (intervention.machine_marque && intervention.machine_modele) {
    return `${intervention.machine_marque} ${intervention.machine_modele}`;
  }

  if (intervention.machine_marque) {
    return intervention.machine_marque;
  }

  return intervention.machine_nom ? "Fluide frigorifique" : "-";
}

export function getStatusStyle(statut: string | undefined): InterventionStatusStyle {
  if (!statut) {
    return {
      label: "Inconnu",
      color: "#374151",
      backgroundColor: "#f3f4f6",
      borderColor: "#d1d5db",
    };
  }

  return (
    STATUS_STYLES[statut] || {
      label: statut,
      color: "#374151",
      backgroundColor: "#f3f4f6",
      borderColor: "#d1d5db",
    }
  );
}