import type {
  Ancre,
  Role,
  StatutCommercial,
  StatutFinancier,
  StatutOperationnel,
  StatutTache,
  TypeContact,
  TypeDossier,
} from "@prisma/client";

/** Ton visuel d'un badge : neutre, info, succès, attention, danger. */
export type Ton = "neutre" | "info" | "succes" | "attention" | "danger";

export const ROLES: Record<Role, string> = {
  DIRECTEUR: "Direction",
  ADMIN: "Administrateur",
  CHARGE_COMPTE: "Chargé(e) de compte",
  PLANIFICATION: "Planification",
  STOCK: "Stock / magasin",
  STUDIO: "Studio",
  TERRAIN: "Terrain",
};

export const TYPES_DOSSIER: Record<TypeDossier, string> = {
  EVENEMENT: "Événement",
  LOCATION: "Location sèche",
  CONTRAT_CADRE: "Contrat cadre",
  APPEL_OFFRES: "Appel d'offres",
  INTERNE: "Dossier interne",
  GRATUIT: "Gratuit",
};

export const TYPES_CONTACT: Record<TypeContact, string> = {
  CLIENT: "Client",
  FOURNISSEUR: "Fournisseur",
  LIEU: "Lieu d'événement",
};

export const STATUTS_COMMERCIAUX: Record<StatutCommercial, { libelle: string; ton: Ton }> = {
  LEAD_FROID: { libelle: "Lead froid", ton: "neutre" },
  LEAD_CHAUD: { libelle: "Lead chaud", ton: "info" },
  A_TRAITER: { libelle: "À traiter", ton: "attention" },
  DEVIS_ENVOYE: { libelle: "Devis envoyé", ton: "info" },
  EN_INSTANCE: { libelle: "En instance", ton: "attention" },
  EN_BONNE_VOIE: { libelle: "En bonne voie", ton: "info" },
  CONFIRME: { libelle: "Confirmé", ton: "succes" },
  PERDU: { libelle: "Perdu", ton: "danger" },
  ANNULE_CLIENT: { libelle: "Annulé par le client", ton: "danger" },
  ABANDONNE_AGENCE: { libelle: "Abandonné par l'agence", ton: "neutre" },
  REPORTE: { libelle: "Reporté", ton: "neutre" },
};

export const STATUTS_OPERATIONNELS: Record<StatutOperationnel, { libelle: string; ton: Ton }> = {
  NON_DEMARRE: { libelle: "Non démarré", ton: "neutre" },
  EN_PREPARATION: { libelle: "En préparation", ton: "info" },
  EN_EXPLOITATION: { libelle: "En exploitation", ton: "attention" },
  PRE_CLOTURE: { libelle: "Pré-clôture", ton: "info" },
  CLOTURE: { libelle: "Clôturé", ton: "succes" },
};

export const STATUTS_FINANCIERS: Record<StatutFinancier, { libelle: string; ton: Ton }> = {
  NON_FACTURE: { libelle: "Non facturé", ton: "neutre" },
  A_FACTURER_PROFORMA: { libelle: "À facturer (proforma)", ton: "attention" },
  A_FACTURER_FINALE: { libelle: "À facturer (finale)", ton: "attention" },
  FACTURE: { libelle: "Facturé", ton: "info" },
  PAYE: { libelle: "Payé", ton: "succes" },
  IMPAYE: { libelle: "Impayé", ton: "danger" },
  A_REGULARISER: { libelle: "À régulariser", ton: "attention" },
};

export const STATUTS_TACHE: Record<StatutTache, string> = {
  A_FAIRE: "À faire",
  FAIT: "Fait",
  NON_APPLICABLE: "N/A",
};

export const ANCRES: Record<Ancre, string> = {
  RECEPTION: "Réception de la demande",
  REPERAGE: "Repérage",
  CONFIRMATION: "Confirmation",
  EVENEMENT: "Événement",
  PERTE: "Perte du dossier",
  HEBDO: "Chaque samedi",
};

/** Abréviations utilisées dans l'Excel (Recp+1, CONF+3, EVENT-1…). */
export function libelleEcheance(ancre: Ancre | null, decalage: number) {
  if (!ancre) return "Indéterminée";
  if (ancre === "HEBDO") return "Chaque samedi";
  const court = { RECEPTION: "Récep.", REPERAGE: "Repérage", CONFIRMATION: "Conf.", EVENEMENT: "Événement", PERTE: "Perte" }[ancre];
  return decalage === 0 ? court : `${court} ${decalage > 0 ? "+" : "−"}${Math.abs(decalage)} j`;
}

/** Statuts commerciaux d'un dossier encore actif (ni perdu, ni annulé, ni reporté). */
export const COMMERCIAUX_ACTIFS: StatutCommercial[] = [
  "LEAD_FROID",
  "LEAD_CHAUD",
  "A_TRAITER",
  "DEVIS_ENVOYE",
  "EN_INSTANCE",
  "EN_BONNE_VOIE",
  "CONFIRME",
];

export const COMMERCIAUX_PIPELINE: StatutCommercial[] = [
  "LEAD_FROID",
  "LEAD_CHAUD",
  "A_TRAITER",
  "DEVIS_ENVOYE",
  "EN_INSTANCE",
  "EN_BONNE_VOIE",
];

export const COMMERCIAUX_PERDUS: StatutCommercial[] = ["PERDU", "ANNULE_CLIENT", "ABANDONNE_AGENCE"];
