import { Badge } from "@mantine/core";
import type {
  KevoEntryStatus,
  KevoFlagCategory,
  KevoFlagSource,
  KevoFlagStatus,
} from "@/lib/features/kevo/types";

/**
 * Karma DS accents (docs: DESIGN_SYSTEM.md): bronze is the neutral accent,
 * red strictly means "needs action", green strictly means "done".
 */
export const KARMA = {
  bronze: "#8c7451",
  bronzeLt: "#c2a878",
  black: "#0d0c0a",
  gold: "#a8781a",
  sage: "#5f6b5a",
  sand: "#d9c7a3",
  stone: "#b9b5aa",
  crit: "#d03b3b",
  good: "#1e7a43",
} as const;

export const CATEGORY_LABEL: Record<KevoFlagCategory, string> = {
  KNOWLEDGE_GAP: "Knowledge gap",
  WRONG_INFO: "Wrong info",
  DATA_UPDATE_REQUEST: "Data update",
  ESCALATION: "Escalation",
  OTHER: "Other",
};

export const CATEGORY_COLOR: Record<KevoFlagCategory, string> = {
  KNOWLEDGE_GAP: KARMA.bronze,
  WRONG_INFO: KARMA.crit,
  DATA_UPDATE_REQUEST: KARMA.black,
  ESCALATION: KARMA.gold,
  OTHER: KARMA.stone,
};

export const STATUS_LABEL: Record<KevoFlagStatus, string> = {
  NEW: "New",
  IN_REVIEW: "In review",
  APPROVED: "Approved",
  REJECTED: "Rejected",
  PUBLISHED: "Published",
  ROUTED: "Routed",
  RESOLVED: "Resolved",
};

export const STATUS_COLOR: Record<KevoFlagStatus, string> = {
  NEW: KARMA.crit, // the only state that demands action
  IN_REVIEW: KARMA.bronze,
  APPROVED: KARMA.bronzeLt,
  REJECTED: KARMA.stone,
  PUBLISHED: KARMA.good,
  ROUTED: KARMA.black,
  RESOLVED: KARMA.good,
};

export const SOURCE_LABEL: Record<KevoFlagSource, string> = {
  IN_CALL: "Flagged by Kevo",
  POST_CALL: "Post-call analysis",
  MANUAL: "Manual",
};

export const ENTRY_STATUS_COLOR: Record<KevoEntryStatus, string> = {
  DRAFT: KARMA.bronze,
  APPROVED: KARMA.bronzeLt,
  PUBLISHED: KARMA.good,
  RETIRED: KARMA.stone,
};

export function StatusBadge({ status }: { status: KevoFlagStatus }) {
  return (
    <Badge color={STATUS_COLOR[status]} variant="filled" radius="xl" size="sm">
      {STATUS_LABEL[status]}
    </Badge>
  );
}

export function CategoryBadge({ category }: { category: KevoFlagCategory }) {
  return (
    <Badge
      color={CATEGORY_COLOR[category]}
      variant="light"
      radius="xl"
      size="sm"
    >
      {CATEGORY_LABEL[category]}
    </Badge>
  );
}

/** "2d ago" style relative time; falls back to a short date past 45 days. */
export function timeAgo(iso: string): string {
  const then = new Date(iso).getTime();
  const mins = Math.floor((Date.now() - then) / 60000);
  if (mins < 1) return "just now";
  if (mins < 60) return `${mins} min ago`;
  const hours = Math.floor(mins / 60);
  if (hours < 24) return `${hours}h ago`;
  const days = Math.floor(hours / 24);
  if (days <= 45) return `${days}d ago`;
  return new Date(iso).toLocaleDateString("en-GB", {
    day: "numeric",
    month: "short",
    year: "numeric",
  });
}

export function formatDate(iso: string | null): string {
  if (!iso) return "–";
  return new Date(iso).toLocaleDateString("en-GB", {
    day: "numeric",
    month: "short",
    year: "numeric",
  });
}
