// ═══════════════════════════════════════════════════════════════════════
// Previous NC — design tokens
// Identical scheme to the certificate report so visuals stay consistent
// across modules.
// ═══════════════════════════════════════════════════════════════════════

export const TOKENS = {
  // Brand
  brand: "#0f766e",
  brandLight: "#14b8a6",
  brandDark: "#0d544c",

  // Neutral scale
  ink: "#0b1220",
  ink2: "#1f2937",
  ink3: "#475569",
  ink4: "#64748b",
  ink5: "#94a3b8",
  line: "#e2e8f0",
  line2: "#f1f5f9",
  bg: "#f6f8fa",
  surface: "#ffffff",

  // Semantic
  success: "#16a34a",
  warning: "#f59e0b",
  danger: "#dc2626",
  info: "#2563eb",

  // NC type colors
  major: "#dc2626",
  minor: "#f59e0b",
  observation: "#2563eb",

  // Source colors
  qrs: "#6366f1",
  tqs: "#06b6d4",

  // Radii & Shadow
  rSm: "6px",
  rMd: "10px",
  rLg: "14px",
  shadow: "0 1px 3px rgba(15,23,42,0.06), 0 8px 24px rgba(15,23,42,0.04)",
  shadowLg: "0 4px 12px rgba(15,23,42,0.08), 0 16px 40px rgba(15,23,42,0.06)",
};

// ─── Small helpers reused everywhere ───────────────────────────────────

export const monthName = (m: number) =>
  ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][m];

export const todayStr = () =>
  new Date().toLocaleDateString("en-GB", {
    day: "2-digit",
    month: "long",
    year: "numeric",
  });

export const fmt = (n: number) => n.toLocaleString();

export const pct = (n: number, total: number) =>
  total > 0 ? `${((n / total) * 100).toFixed(1)}%` : "0%";

export const pctNum = (n: number, total: number) =>
  total > 0 ? (n / total) * 100 : 0;

export function formatDate(d?: string | Date | null): string {
  if (!d) return "—";
  const dt = typeof d === "string" ? new Date(d) : d;
  if (isNaN(dt.getTime())) return "—";
  return dt.toLocaleDateString("en-GB", {
    day: "2-digit",
    month: "short",
    year: "numeric",
  });
}

export function formatMonthOption(ym: string): string {
  if (!ym) return "";
  const [y, m] = ym.split("-");
  const mi = parseInt(m, 10) - 1;
  if (isNaN(mi) || mi < 0 || mi > 11) return ym;
  return `${monthName(mi)} ${y}`;
}

export function getYearMonth(d: string | null | undefined): string {
  if (!d) return "";
  const dt = new Date(d);
  if (isNaN(dt.getTime())) return "";
  return `${dt.getFullYear()}-${String(dt.getMonth() + 1).padStart(2, "0")}`;
}

export function getYear(d: string | null | undefined): string {
  if (!d) return "";
  const dt = new Date(d);
  if (isNaN(dt.getTime())) return "";
  return String(dt.getFullYear());
}

/**
 * Normalize an NC type string to a chip color.
 * Tolerates messy values like "minor", "Minor", "MINOR " etc.
 */
export function ncTypeColor(t: string | null | undefined): string {
  const v = (t || "").toLowerCase();
  if (v.includes("major")) return TOKENS.major;
  if (v.includes("minor")) return TOKENS.minor;
  if (v.includes("observ")) return TOKENS.observation;
  return TOKENS.ink4;
}

export function ncStatusColor(s: string | null | undefined): string {
  const v = (s || "").toLowerCase();
  if (v === "open") return TOKENS.danger;
  if (v === "closed") return TOKENS.success;
  return TOKENS.ink4;
}

export function sourceColor(s: string | null | undefined): string {
  return (s || "").toUpperCase() === "TQS" ? TOKENS.tqs : TOKENS.qrs;
}
