import type {
  ComplianceCategory,
  ComplianceSeverity,
  ComplianceStatus,
  ComplianceDeadlineStatus,
} from "@/domain/entities/compliance";

/** Display order of the categories (cards, sheet sections, filters). */
export const COMPLIANCE_CATEGORIES: ComplianceCategory[] = [
  "identification",
  "fiscal",
  "telematics",
  "finance",
  "documents",
  "driver",
  "deadlines",
  "antai",
  "extras",
];

/** Categories shown as KPI cards (extras are informational only). */
export const COMPLIANCE_KPI_CATEGORIES: ComplianceCategory[] = [
  "identification",
  "fiscal",
  "telematics",
  "finance",
  "documents",
  "driver",
  "deadlines",
  "antai",
];

/** Score weight of each severity. LOW is shown but does not move the score. */
export const COMPLIANCE_SEVERITY_WEIGHT: Record<ComplianceSeverity, number> = {
  critical: 3,
  high: 2,
  medium: 1,
  low: 0,
};

export const COMPLIANCE_SEVERITY_ORDER: ComplianceSeverity[] = ["critical", "high", "medium", "low"];

/** Score thresholds, mirrored from the backend evaluator. */
export const COMPLIANCE_CRITICAL_BELOW = 60;
export const COMPLIANCE_COMPLIANT_FROM = 90;

/**
 * The v1 rule dictionary. The backend is the source of truth (`GET /compliance/rules`);
 * this copy gives the UI the category/severity of a rule key without a round trip,
 * and feeds the mock repository. Labels live in i18n (`t.compliance.rules`).
 */
export const COMPLIANCE_RULES: {
  key: string;
  category: ComplianceCategory;
  severity: ComplianceSeverity;
  applies_when: string | null;
}[] = [
  { key: "identity.numberplate", category: "identification", severity: "critical", applies_when: null },
  { key: "identity.vin", category: "identification", severity: "critical", applies_when: null },
  { key: "identity.make_model", category: "identification", severity: "high", applies_when: null },
  { key: "identity.category", category: "identification", severity: "high", applies_when: null },
  { key: "identity.first_registration", category: "identification", severity: "high", applies_when: null },
  { key: "identity.park_entry", category: "identification", severity: "medium", applies_when: null },
  { key: "identity.site", category: "identification", severity: "high", applies_when: null },
  { key: "identity.odometer", category: "identification", severity: "high", applies_when: null },

  { key: "fiscal.co2", category: "fiscal", severity: "critical", applies_when: "combustion" },
  { key: "fiscal.energy", category: "fiscal", severity: "critical", applies_when: null },
  { key: "fiscal.crit_air", category: "fiscal", severity: "high", applies_when: null },
  { key: "fiscal.fiscal_power", category: "fiscal", severity: "high", applies_when: null },
  { key: "fiscal.aen_mode", category: "fiscal", severity: "medium", applies_when: "driver" },

  { key: "telematics.connected", category: "telematics", severity: "high", applies_when: null },
  { key: "telematics.device", category: "telematics", severity: "medium", applies_when: null },

  { key: "finance.mode", category: "finance", severity: "critical", applies_when: null },
  { key: "finance.lessor", category: "finance", severity: "high", applies_when: "leasing" },
  { key: "finance.contract_number", category: "finance", severity: "high", applies_when: "leasing" },
  { key: "finance.contract_dates", category: "finance", severity: "high", applies_when: "leasing" },
  { key: "finance.km_limit", category: "finance", severity: "high", applies_when: "leasing" },
  { key: "finance.monthly_rent", category: "finance", severity: "high", applies_when: "leasing" },
  { key: "finance.duration", category: "finance", severity: "medium", applies_when: "leasing" },

  { key: "documents.carte_grise", category: "documents", severity: "critical", applies_when: null },
  { key: "documents.insurance", category: "documents", severity: "critical", applies_when: null },
  { key: "documents.insurance_valid", category: "documents", severity: "critical", applies_when: "insurance" },
  { key: "documents.leasing_contract", category: "documents", severity: "high", applies_when: "leasing" },
  { key: "documents.purchase_invoice", category: "documents", severity: "high", applies_when: "purchase" },
  { key: "documents.technical_control", category: "documents", severity: "critical", applies_when: "age_4" },

  { key: "driver.assigned", category: "driver", severity: "high", applies_when: null },
  { key: "driver.licence", category: "driver", severity: "critical", applies_when: "driver" },

  { key: "deadline.technical_control", category: "deadlines", severity: "critical", applies_when: "age_4" },
  { key: "deadline.maintenance", category: "deadlines", severity: "high", applies_when: null },
  { key: "deadline.contract_end", category: "deadlines", severity: "high", applies_when: "leasing" },

  { key: "antai.binding", category: "antai", severity: "high", applies_when: "antai" },

  { key: "extras.fuel_card", category: "extras", severity: "low", applies_when: "combustion" },
  { key: "extras.toll_badge", category: "extras", severity: "low", applies_when: null },
  { key: "extras.duplicate_keys", category: "extras", severity: "low", applies_when: null },
];

export const COMPLIANCE_RULE_BY_KEY = Object.fromEntries(
  COMPLIANCE_RULES.map((rule) => [rule.key, rule]),
) as Record<string, (typeof COMPLIANCE_RULES)[number]>;

/** Tailwind classes per severity (tags, dots). */
export const COMPLIANCE_SEVERITY_CLASSES: Record<ComplianceSeverity, string> = {
  critical: "bg-red-50 text-red-700 border-red-200",
  high: "bg-amber-50 text-amber-700 border-amber-200",
  medium: "bg-blue-50 text-blue-700 border-blue-200",
  low: "bg-slate-50 text-slate-600 border-slate-200",
};

export const COMPLIANCE_STATUS_CLASSES: Record<ComplianceStatus, string> = {
  compliant: "bg-emerald-50 text-emerald-700 border-emerald-200",
  incomplete: "bg-amber-50 text-amber-700 border-amber-200",
  critical: "bg-red-50 text-red-700 border-red-200",
};

/** Colour of the score bar for a given score. */
export function complianceScoreColor(score: number): string {
  if (score >= COMPLIANCE_COMPLIANT_FROM) return "bg-emerald-500";
  if (score >= COMPLIANCE_CRITICAL_BELOW) return "bg-amber-500";
  return "bg-red-500";
}

export const COMPLIANCE_DEADLINE_CLASSES: Record<ComplianceDeadlineStatus, string> = {
  overdue: "text-red-600",
  due_soon: "text-amber-600",
  upcoming: "text-muted-foreground",
};
