import type { SalesOpportunity } from "../lib/optima-api/modules/sales";

/**
 * Status ID → CSS class tier mapping.
 * Maps both canonical IDs and equivalency IDs to their tier's CSS class.
 */
const STATUS_TIER: Record<number, string> = (() => {
  const map: Record<number, string> = {};
  // FutureLead (id 51) + equivalencies
  for (const id of [51, 35, 36]) map[id] = "status-future";
  // PendingNew (id 37)
  map[37] = "status-pending-new";
  // New (id 24) + equivalencies
  for (const id of [24, 1, 13]) map[id] = "status-new";
  // Internal Review (id 56) + equivalencies
  for (const id of [56, 10, 26, 27, 28, 41, 54]) map[id] = "status-review";
  // QuoteSent (id 43)
  map[43] = "status-quote-sent";
  // ConfirmedQuote (id 57)
  map[57] = "status-quote-confirmed";
  // ReadyToSend (id 63)
  map[63] = "status-ready-to-send";
  // PendingSent (id 60)
  map[60] = "status-pending-sent";
  // PendingRevision (id 61)
  map[61] = "status-pending-revision";
  // Active (id 58) + equivalencies
  for (const id of [
    58, 9, 15, 16, 17, 18, 19, 20, 25, 38, 39, 40, 42, 44, 45, 46, 47, 48, 52,
    55,
  ])
    map[id] = "status-active";
  // PendingWon (id 49)
  map[49] = "status-pending-won";
  // Won (id 29) + equivalencies
  for (const id of [29, 2]) map[id] = "status-won";
  // PendingLost (id 50)
  map[50] = "status-pending-lost";
  // Lost (id 53) + equivalencies
  for (const id of [53, 3, 4, 12, 30, 31, 32, 33, 34]) map[id] = "status-lost";
  // Canceled (id 59)
  map[59] = "status-canceled";
  return map;
})();

/**
 * Returns a CSS class name corresponding to an opportunity's workflow status.
 * This is the single canonical implementation — do not re-declare this function
 * in any component or page file.
 */
export function statusColorClass(
  opportunity: SalesOpportunity | undefined | null,
): string {
  if (opportunity?.closedFlag) {
    const sid = opportunity.status?.id;
    if (sid != null && STATUS_TIER[sid]) return STATUS_TIER[sid];
    return "status-closed";
  }
  const sid = opportunity?.status?.id;
  if (sid == null) return "status-unknown";
  return STATUS_TIER[sid] ?? "status-open";
}

/** Canonical display name for each tier */
const CANONICAL_NAMES: Record<number, string> = {
  51: "FutureLead",
  37: "Pending New",
  24: "New",
  56: "Internal Review",
  43: "Quote Sent",
  57: "Confirmed Quote",
  63: "Ready to Send",
  60: "Pending Sent",
  61: "Pending Revision",
  58: "Active",
  49: "Pending Won",
  29: "Won",
  50: "Pending Lost",
  53: "Lost",
  59: "Canceled",
};

/** IDs that are canonical (not equivalency-mapped) */
const CANONICAL_IDS = new Set([
  51, 37, 24, 56, 43, 57, 63, 60, 61, 58, 49, 29, 50, 53, 59,
]);

/**
 * Returns a human-readable label for an opportunity's workflow status.
 * Resolves equivalency-mapped statuses to their canonical display names.
 */
export function statusLabel(
  opportunity: SalesOpportunity | undefined | null,
): string {
  if (!opportunity?.status?.id) return "Unknown";

  const sid = opportunity.status.id;

  // If it IS the canonical ID, use its name
  if (CANONICAL_IDS.has(sid)) {
    return CANONICAL_NAMES[sid] ?? opportunity.status?.name ?? "Open";
  }

  // Otherwise it's an equivalency — return the canonical name for the tier
  const tier = STATUS_TIER[sid];
  if (tier) {
    for (const [canonId, name] of Object.entries(CANONICAL_NAMES)) {
      if (STATUS_TIER[Number(canonId)] === tier) return name;
    }
  }

  return opportunity.status?.name ?? "Unknown";
}

/** Whether this status is an equivalency-mapped status (not canonical). */
export function isEquivalencyStatus(
  opportunity: SalesOpportunity | undefined | null,
): boolean {
  const sid = opportunity?.status?.id;
  if (sid == null) return false;
  return STATUS_TIER[sid] != null && !CANONICAL_IDS.has(sid);
}

/** The original CW status name (for tooltip on equivalency statuses). */
export function originalStatusName(
  opportunity: SalesOpportunity | undefined | null,
): string {
  return opportunity?.status?.name ?? "Unknown";
}
