export interface InstallationVehicle {
  /** Référence distante vers fleet.vehicles (null = non rattaché au parc). */
  fleet_vehicle_id: number | null;
  license_plate: string | null;
}

export interface InstallationDevice {
  /** Produit du catalogue fleet.telematics_boxes posé. */
  telematics_box_id: number | null;
  model: string | null;
  product_line: string | null;
  imei: string | null;
}

export interface Installation {
  id: number;
  status: string;
  status_label: string;
  status_color: string;
  installer_name: string | null;
  intervention_date: string | null;
  location: string | null;
  vehicle: InstallationVehicle;
  device: InstallationDevice;
  photos_count: number;
  duration_minutes: number | null;
  created_at: string;
}

export interface InstallationVehicleDetail {
  fleet_vehicle_id: number | null;
  license_plate: string | null;
  vin: string | null;
  odometer_km: number | null;
  hour_meter: number | null;
  fuel_level: string | null;
}

export interface InstallationDeviceDetail {
  telematics_box_id: number | null;
  model: string | null;
  product_line: string | null;
  imei: string | null;
  stock_origin: string | null;
  has_can_module: boolean;
  can_module_type_id: number | null;
  can_module_type: string | null;
  sim_iccid: string | null;
  sim_operator: string | null;
  sim_apn: string | null;
}

export interface InstallationWiring {
  id: number;
  wire_id: number | null;
  wire: string | null;
  wire_color: string | null;
  wire_accessory_id: number | null;
  accessory: string | null;
}

export interface InstallationAccessory {
  id: number;
  label: string;
}

export interface InstallationValidation {
  contact_detected: boolean | null;
  gps_position_date: string | null;
  gps_address: string | null;
  satellite_count: number | null;
  odometer_km: number | null;
  hour_meter: number | null;
  fuel_level: string | null;
}

export interface InstallationPhoto {
  id: number;
  type: string;
  filename: string | null;
  url: string;
}

export interface InstallationDetail {
  id: number;
  status: string;
  status_label: string;
  status_color: string;
  installer_name: string | null;
  intervention_date: string | null;
  location: string | null;
  notes: string | null;
  duration_minutes: number | null;
  created_at: string;
  updated_at: string;
  vehicle: InstallationVehicleDetail | null;
  device: InstallationDeviceDetail | null;
  accessories: InstallationAccessory[];
  wirings: InstallationWiring[];
  validation: InstallationValidation | null;
  photos: InstallationPhoto[];
}

export interface ReferenceOption {
  id: number;
  label: string;
  code?: string | null;
  color?: string | null;
}

export interface TelematicsBoxOption {
  id: number;
  code: string | null;
  name: string | null;
  install_method: string | null;
  product_line: string;
  model: string | null;
}

/** Référentiels servis par le service installer pour le formulaire d'édition. */
export interface InstallationEditOptions {
  telematics_boxes: TelematicsBoxOption[];
  can_module_types: ReferenceOption[];
  accessories: ReferenceOption[];
  wires: ReferenceOption[];
  wire_accessories: ReferenceOption[];
}

export interface InstallationStats {
  total: number;
  en_cours: number;
  validees: number;
  a_corriger: number;
  en_attente: number;
  avg_minutes: number | null;
}

/**
 * Payload d'édition admin — même contrat que l'app installateur. Quand
 * `device` est envoyé, telematics_box_id / stock_origin / imei sont requis
 * par le backend : toujours renvoyer les valeurs existantes.
 */
export interface UpdateInstallationPayload {
  installer_name?: string | null;
  intervention_date?: string | null;
  location?: string | null;
  notes?: string | null;
  vehicle?: {
    license_plate: string;
    vin?: string | null;
    fleet_vehicle_id?: number | null;
    odometer_km?: number | null;
    hour_meter?: number | null;
    fuel_level?: string | null;
  };
  device?: {
    telematics_box_id: number;
    stock_origin: string;
    imei: string;
    product_line?: string | null;
    model?: string | null;
    has_can_module?: boolean;
    can_module_type_id?: number | null;
    sim_card?: { iccid?: string | null; apn?: string | null };
  };
  accessories?: number[];
  wirings?: { wire_id: number; wire_accessory_id?: number | null }[];
}

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