/**
 * ANTAI vehicle-management model.
 *
 * `vehicles.antai_status` (S/N/E) is the single source of truth on the backend,
 * surfaced here as BOTH "ANTAI visibility" (S=Visible, N=Hidden, E=Exception)
 * and "binding status" (S=Bound, N=Unbound, E=Exception). Status-like fields all
 * follow the codebase convention `{ value, label, color }` with a semantic color
 * key ("success" | "warning" | "failure" | …), matching {@link AccessStatus}.
 */

/** A label+color status descriptor, optionally carrying its enum `value`. */
export interface AntaiStatus {
  value: string;
  label: string;
  /** Semantic badge color key ("success" | "warning" | "failure" | "info" | …) or a hex string. */
  color: string;
}

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

export interface AntaiArchiveStatus {
  archived: boolean;
  label: string;
  color: string;
}

export interface AntaiUnbindingReason {
  /** manual | company_disabled | external_removal | exception */
  value: string;
  label: string;
}

/** Exception block attached to a vehicle stuck in the `E` state. */
export interface AntaiException {
  /** already_linked_other_client | removed_externally | sync_failure | antai_rejection | other */
  type: string | null;
  type_label: string | null;
  /** open | in_progress | resolved */
  status: string;
  status_label: string;
  status_color: string;
  /** internal | external — whether the issue can be resolved in-house or needs ANTAI. */
  resolution: string | null;
  resolution_label: string | null;
  detail: string | null;
  resolved_at: string | null;
}

/** One row of the ANTAI vehicle-management list (includes archived/trashed vehicles). */
export interface AntaiVehicle {
  id: number;
  vehicle_name: string;
  numberplate: string | null;
  vin: string | null;
  company: AntaiCompanyRef | null;
  vehicle_status: AntaiStatus | null;
  /** Owning fleet account state (active / disabled). */
  company_account_status: AntaiStatus;
  /** S=Visible · N=Hidden · E=Exception. */
  antai_visibility: AntaiStatus;
  /** S=Bound · N=Unbound · E=Exception. */
  binding_status: AntaiStatus;
  archive_status: AntaiArchiveStatus;
  unbinding_reason: AntaiUnbindingReason | null;
  exception: AntaiException | null;
  last_antai_updated_date: string | null;
}

/** Full detail view. Extends the row with optional descriptive fields when present. */
export interface AntaiVehicleDetail extends AntaiVehicle {
  mark?: string | null;
  model?: string | null;
  motor_type?: string | null;
  antai_client_code?: string | null;
  created_at?: string | null;
  updated_at?: string | null;
}

export interface AntaiVehicleStats {
  total: number;
  visible: number;
  hidden: number;
  exceptions: number;
  open_exceptions: number;
  archived: number;
}

export interface AntaiOption {
  value: string | number;
  label: string;
}

/** Value/label option lists backing the filter dropdowns. */
export interface AntaiVehicleOptions {
  companies: AntaiOption[];
  company_statuses: AntaiOption[];
  vehicle_statuses: AntaiOption[];
  antai_visibility: AntaiOption[];
  binding_statuses: AntaiOption[];
  archived: AntaiOption[];
  unbinding_reasons: AntaiOption[];
  exception_types: AntaiOption[];
  exception_resolutions: AntaiOption[];
  exception_statuses: AntaiOption[];
}

/** Reason carried by single + bulk disable (defaults to `manual` server-side). */
export type AntaiDisableReason = "manual" | "external_removal" | "company_disabled";

export type AntaiExceptionStatus = "open" | "in_progress" | "resolved";
export type AntaiExceptionType =
  | "already_linked_other_client"
  | "removed_externally"
  | "sync_failure"
  | "antai_rejection"
  | "other";
export type AntaiExceptionResolution = "internal" | "external";

export interface UpdateAntaiExceptionPayload {
  status: AntaiExceptionStatus;
  exception_type?: AntaiExceptionType;
  resolution?: AntaiExceptionResolution;
  detail?: string;
}

/** Returned by single enable/disable (`{ vehicle, summary }`). Bodies aren't consumed — we refetch. */
export interface AntaiActionResult {
  vehicle?: AntaiVehicleDetail;
  summary?: unknown;
}

/** Returned by the queued bulk + company-cascade actions. */
export interface AntaiQueuedResult {
  queued: number;
}
