import {
  REQUEST_STATUS_LABELS,
  WORK_ORDER_PRIORITY_LABELS,
  WORK_ORDER_STATUS_LABELS,
  type RequestStatus,
  type WorkOrderPriority,
  type WorkOrderStatus,
} from '@siop/shared';
import { cn } from '@/lib/cn';

const ST_CLASSES: Record<WorkOrderStatus, string> = {
  OPEN: 'ouvert',
  IN_PROGRESS: 'encours',
  ON_HOLD: 'attente',
  DONE: 'termine',
  CANCELLED: 'annule',
};

/** Statut OT — les couleurs réservées de la charte. */
export function StatutOT({ statut }: { statut: WorkOrderStatus }) {
  return (
    <span className={cn('st', ST_CLASSES[statut])}>
      {WORK_ORDER_STATUS_LABELS[statut]}
    </span>
  );
}

const PRIO_CLASSES: Record<WorkOrderPriority, string | null> = {
  NONE: null,
  LOW: 'basse',
  MEDIUM: 'moyenne',
  HIGH: 'haute',
  PERSON_TRAPPED: 'bloque',
};

/** Priorité — pastille + libellé, jamais la couleur seule (charte). */
export function PrioriteOT({ priorite }: { priorite: WorkOrderPriority }) {
  const classe = PRIO_CLASSES[priorite];
  if (!classe) return <span className="text-encre-3">—</span>;
  return (
    <span className={cn('prio', classe)}>
      <i />
      {WORK_ORDER_PRIORITY_LABELS[priorite]}
    </span>
  );
}

const DEM_CLASSES: Record<RequestStatus, string> = {
  RECEIVED: 'recue',
  APPROVED: 'approuvee',
  REJECTED: 'rejetee',
};

/** Statut de demande — « Résolue » quand l'OT lié est terminé. */
export function StatutDemande({
  statut,
  otStatut,
}: {
  statut: RequestStatus;
  otStatut?: WorkOrderStatus | null;
}) {
  if (statut === 'APPROVED' && otStatut === 'DONE') {
    return <span className="st resolue">Résolue</span>;
  }
  return <span className={cn('st', DEM_CLASSES[statut])}>{REQUEST_STATUS_LABELS[statut]}</span>;
}
