import { Vehicle } from './Vehicle';
import { Device } from './Device';
import { Wiring } from './Wiring';
import { Photo } from './Photo';
import { FuelLevel, InstallationValidation } from './InstallationValidation';

export type InstallationStatus = 'in_progress' | 'validated' | 'gps_error' | 'pending';

export interface Installation {
  id: number;
  status: InstallationStatus;
  intervention_date?: string;
  installer_name?: string;
  location?: string;
  notes?: string;
  /** Numéro de programme du boîtier (obligatoire quand le boîtier a un bus CAN). */
  program_number?: string;
  vehicle?: Vehicle;
  device?: Device;
  wirings?: Wiring[];
  accessories?: number[];
  /** GET response field name */
  installation_validation?: InstallationValidation;
  /** PATCH payload field name */
  validation?: Partial<InstallationValidation>;
  photos?: Photo[];
  created_at?: string;
  updated_at?: string;
}

/**
 * Lightweight shape returned by the installations list endpoint
 * (`GET /installations`) and rendered in the history view. Mirrors
 * `InstallationListResource` on the backend rather than the full record.
 */
export interface InstallationSummary {
  id: number;
  status: InstallationStatus;
  installer_name?: string | null;
  intervention_date?: string | null;
  location?: string | null;
  vehicle?: {
    fleet_vehicle_id?: number | null;
    license_plate?: string | null;
    vin?: string | null;
  } | null;
  device?: {
    telematics_box_id?: number | null;
    product_line?: string | null;
    model?: string | null;
    imei?: string | null;
  } | null;
  photos_count?: number;
  created_at?: string | null;
}

interface RefLabel {
  id: number;
  label: string;
  color?: string;
}

/**
 * Full installation shape returned by `GET /installations/{id}`
 * (`InstallationResource`), rendered in the detail modal. Relations are
 * nested objects (vs. the `*_id` fields used in the wizard write models).
 */
export interface InstallationDetail {
  id: number;
  status: InstallationStatus;
  installer_name?: string | null;
  intervention_date?: string | null;
  location?: string | null;
  notes?: string | null;
  /** Numéro de programme du boîtier (obligatoire quand le boîtier a un bus CAN). */
  program_number?: string | null;
  vehicle?: {
    fleet_vehicle_id?: number | null;
    license_plate?: string | null;
    vin?: string | null;
    odometer_km?: number | null;
    hour_meter?: number | null;
    fuel_level?: FuelLevel | null;
  } | null;
  device?: {
    stock_origin?: string | null;
    telematics_box_id?: number | null;
    product_line?: string | null;
    model?: string | null;
    imei?: string | null;
    has_can_module?: boolean | null;
    can_module_type?: RefLabel | null;
    sim_card?: { iccid?: string | null; operator?: string | null; apn?: string | null } | null;
  } | null;
  wirings?: {
    id: number;
    wire?: RefLabel | null;
    wire_accessory?: RefLabel | null;
  }[];
  accessories?: RefLabel[];
  installation_validation?: InstallationValidation | null;
  photos?: Photo[];
  created_at?: string | null;
  /** Demande d'origine quand la pose a été démarrée depuis une demande confirmée. */
  installation_request_id?: number | null;
  /** Champs verrouillés (pose issue d'une demande) : immatriculation / VIN / boîtier. */
  locked?: { license_plate?: boolean; vin?: boolean; telematics_box_id?: boolean } | null;
  /** Boîtier verrouillé exposé pour pré-remplir le wizard avant création de la ligne device. */
  locked_telematics_box_id?: number | null;
}
