// ============================================
// TYPES
// ============================================

import { cn } from "@/lib/utils";
import { SageDocumentType } from "@/types/sageTypes";

interface StatusInfo {
  label: string;
  color: string;
}

type StatusLabels = Record<number, StatusInfo>;

// ============================================
// COULEURS RÉUTILISABLES
// ============================================

const COLORS = {
  gray: "bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300",
  yellow: "bg-yellow-100 dark:bg-yellow-900/20 text-yellow-700 dark:text-yellow-300",
  green: "bg-green-100 text-green-700 border-green-200 dark:bg-green-900/30 dark:text-green-400 dark:border-green-800",
  red: "bg-red-100 dark:bg-red-900/20 text-red-700 dark:text-red-300",
  orange: "bg-orange-100 dark:bg-orange-900/20 text-orange-700 dark:text-orange-300",
  blue: "bg-blue-100 dark:bg-blue-900/20 text-blue-700 dark:text-blue-300",
  purple: "bg-purple-100 dark:bg-purple-900/20 text-purple-700 dark:text-purple-300",
  greenRegle: "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400",
};

// ============================================
// CONFIGURATION DES STATUTS PAR TYPE DE DOCUMENT
// ============================================

export const STATUS_LABELS: Record<SageDocumentType, StatusLabels> = {
  [SageDocumentType.DEVIS]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "Accepté", color: COLORS.green },
    3: { label: "Perdu", color: COLORS.red },
    4: { label: "Archivé", color: COLORS.orange },
    5: { label: "Transformé", color: COLORS.purple },
    6: { label: "Signé", color: COLORS.orange },
  },
  [SageDocumentType.BON_COMMANDE]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "A préparer", color: COLORS.blue },
    3: { label: "Préparé", color: COLORS.green },
    4: { label: "Soldé", color: COLORS.purple },
  },
  [SageDocumentType.PREPARATION]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "Terminé", color: COLORS.green },
  },
  [SageDocumentType.BON_LIVRAISON]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "A Facturer", color: COLORS.blue },
    3: { label: "Facturé", color: COLORS.green },
  },
  [SageDocumentType.BON_RETOUR]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "Traité", color: COLORS.green },
  },
  [SageDocumentType.BON_AVOIR]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "A Facturer", color: COLORS.blue },
    3: { label: "Facturé", color: COLORS.green },
  },
  [SageDocumentType.FACTURE]: {
    0: { label: "Saisi", color: COLORS.gray },
    1: { label: "Confirmé", color: COLORS.yellow },
    2: { label: "A comptabiliser", color: COLORS.orange },
    3: { label: "Comptabilisé", color: COLORS.green },
    4: { label: "Payé", color: COLORS.purple },
    5: { label: "Validé", color: COLORS.blue },
    6: { label: "Payé", color: COLORS.green },
    7: { label: "Partielement payé", color: COLORS.orange },
  },
};

// Statuts par défaut (fallback)
const DEFAULT_LABELS: StatusLabels = {
  0: { label: "Saisi", color: COLORS.gray },
  1: { label: "Confirmé", color: COLORS.yellow },
  2: { label: "Validé", color: COLORS.green },
};

// ============================================
// COMPOSANT
// ============================================

interface StatusBadgeProps {
  status: number;
  type_doc?: SageDocumentType;
  className?: string;
}

const StatusBadge = ({ 
  status, 
  type_doc = SageDocumentType.DEVIS,
  className 
}: StatusBadgeProps) => {
  const labels = STATUS_LABELS[type_doc] || DEFAULT_LABELS;
  const info = labels[status];

  return (
    <span
      className={cn(
        "inline-flex items-center px-2.5 py-0.5 rounded-lg text-xs font-medium",
        info?.color ?? COLORS.gray,
        className
      )}
    >
      {info?.label ?? `Statut ${status}`}
    </span>
  );
};

export default StatusBadge;