/**
 * Vehicle compliance ("Conformité du Parc") — every vehicle of every client scored
 * against the compliance rules (identification, CO2/fiscal, telematics, financing,
 * documents, driver, deadlines…). Field names are snake_case to match
 * /api/admin/compliance/* exactly. See docs/vehicle-compliance/IMPLEMENTATION_PLAN.md.
 */

export type ComplianceCategory =
  | "identification"
  | "fiscal"
  | "telematics"
  | "finance"
  | "documents"
  | "driver"
  | "deadlines"
  | "extras"
  | "antai";

export type ComplianceSeverity = "critical" | "high" | "medium" | "low";

export type ComplianceStatus = "critical" | "incomplete" | "compliant";

/** One rule of the dictionary (`GET /compliance/rules`). */
export interface ComplianceRule {
  key: string;
  category: ComplianceCategory;
  severity: ComplianceSeverity;
  label: string;
  /** Human-readable condition, e.g. "leasing", "age >= 4 years". Null = always. */
  applies_when: string | null;
}

export interface ComplianceCategoryCount {
  complete: number;
  missing: number;
}

export interface ComplianceStats {
  total_vehicles: number;
  average_score: number;
  by_status: Record<ComplianceStatus, number>;
  by_category: Record<ComplianceCategory, ComplianceCategoryCount>;
  /** Most frequently missing rules, sorted by count DESC. */
  by_rule: { key: string; missing: number }[];
  deadlines: { overdue: number; within_30: number; within_60: number; within_90: number };
  /** Vehicles without CO2: the TAI/TVS computation is blocked for them. */
  tax_blocked: number;
  alerts: { sent_30d: number; resolved_after_alert_30d: number };
  computed_at: string;
}

export type ComplianceDeadlineType =
  | "technical_control"
  | "maintenance"
  | "contract_end"
  | "insurance"
  | "licence";

export type ComplianceDeadlineStatus = "overdue" | "due_soon" | "upcoming";

export interface ComplianceDeadline {
  type: ComplianceDeadlineType;
  date: string;
  status: ComplianceDeadlineStatus;
}

export interface ComplianceRef {
  id: number;
  name: string;
}

/** One row of `GET /compliance/vehicles`. */
export interface ComplianceVehicle {
  vehicle_id: number;
  numberplate: string;
  vin: string | null;
  make_model: string;
  fuel_type: string | null;
  company: ComplianceRef;
  site: ComplianceRef | null;
  score: number;
  status: ComplianceStatus;
  /** Rule keys that fail. */
  missing: string[];
  missing_count: { critical: number; high: number; medium: number };
  /** Rule keys that pass but need attention soon (e.g. contract ending < 90 days). */
  warnings: string[];
  category_scores: Partial<Record<ComplianceCategory, number>>;
  next_deadline: { type: ComplianceDeadlineType; date: string } | null;
  last_alert_at: string | null;
  computed_at: string;
}

export interface ComplianceCheck {
  key: string;
  category: ComplianceCategory;
  severity: ComplianceSeverity;
  passed: boolean;
  /** False when the rule does not apply to this vehicle (e.g. leasing rules on a purchased car). */
  applicable: boolean;
  /** Current value, when there is one to show (e.g. "128 g/km", "Arval"). */
  value: string | null;
  detail: string | null;
}

/** Document types of the backend `uploaded_documents` + derived ones. */
export type ComplianceDocumentType =
  | "CG"
  | "CA"
  | "CL"
  | "CT"
  | "CJ"
  | "invoice";

export interface ComplianceDocument {
  type: ComplianceDocumentType;
  present: boolean;
  /** False when the document is not required for this vehicle. */
  required: boolean;
  file_name: string | null;
  expiration_date: string | null;
  expiration_status: ComplianceDeadlineStatus | null;
}

export type ComplianceFinancingMode = "lld" | "loa" | "credit" | "cash";

export interface ComplianceFinancing {
  mode: ComplianceFinancingMode | null;
  lessor: string | null;
  contract_number: string | null;
  start_date: string | null;
  end_date: string | null;
  km_limit: number | null;
  monthly_rent: number | null;
  duration_months: number | null;
}

export interface ComplianceDriver {
  name: string | null;
  licence_status: "active" | "expiring_soon" | "expired" | null;
  licence_expiry: string | null;
}

export type ComplianceAlertChannel = "email" | "in_app";

export interface ComplianceAlert {
  id: number;
  company: ComplianceRef;
  /** Null for a company digest. */
  vehicle: { id: number; numberplate: string } | null;
  sent_by: string;
  channels: ComplianceAlertChannel[];
  recipients: string[];
  missing: string[];
  score_at_send: number | null;
  sent_at: string;
  resolved_at: string | null;
}

/** `GET /compliance/vehicles/{id}`. */
export interface ComplianceVehicleDetail extends ComplianceVehicle {
  checks: ComplianceCheck[];
  documents: ComplianceDocument[];
  deadlines: ComplianceDeadline[];
  financing: ComplianceFinancing;
  driver: ComplianceDriver;
  alerts: ComplianceAlert[];
  /** Deep link to the vehicle in Dadycar Fleet. */
  fleet_url: string;
}

/** One row of `GET /compliance/companies`. */
export interface ComplianceCompany {
  company: ComplianceRef;
  vehicles: number;
  average_score: number;
  critical: number;
  incomplete: number;
  compliant: number;
  top_missing: string[];
  last_alert_at: string | null;
}

export interface ComplianceAlertPreview {
  subject: string;
  body: string;
  recipients: { name: string; email: string }[];
  /** Vehicles covered (1 for a vehicle alert, n for a digest). */
  vehicles_count: number;
  /** Set when an alert was sent recently: sending again needs `force`. */
  next_allowed_at: string | null;
}

export interface SendComplianceAlertPayload {
  channels: ComplianceAlertChannel[];
  message_override?: string;
  force?: boolean;
}

export interface SendComplianceAlertResult {
  alert_id: number;
  sent_to: string[];
  channels: ComplianceAlertChannel[];
}
