export type MobileIntervention = {
  id: number;
  titre?: string;
  statut?: string;
  date_prevue?: string;
  site_nom?: string;
  machine_nom?: string;
  machine_marque?: string;
  machine_modele?: string;
  type_intervention?: string;
  priorite?: string;
  numero_r?: string;
  [key: string]: unknown;
};

export type StepType =
  | "checklist"
  | "measurement"
  | "text"
  | "photo"
  | "signature";

export type StepStatus = "todo" | "in_progress" | "done" | "blocked";

export type StepConstraints = {
  min?: number;
  max?: number;
  precision?: number;
  max_length?: number;
  [key: string]: unknown;
};

export type StepPhotoValue = {
  id: string;
  uri: string;
  name?: string;
  mime_type?: string;
  uploaded?: boolean;
};

export type StepSignatureValue = {
  signed: boolean;
  signed_by: "technicien" | "client";
  uri: string;
  name?: string;
  mime_type?: string;
};

export type StepValue =
  | null
  | boolean
  | number
  | string
  | string[]
  | StepPhotoValue[]
  | StepSignatureValue;

export type MobileStepFile = {
  id: number;
  path: string;
  mime_type?: string;
  original_name?: string;
  uploaded_at?: string;
};

export type MobileStep = {
  id: string;
  intervention_id: number;
  order_index: number;
  type: StepType;
  label: string;
  required: boolean;
  status: StepStatus;
  value: StepValue;
  unit?: string | null;
  constraints?: StepConstraints;
  options?: string[];
  metadata?: Record<string, unknown>;
  files?: MobileStepFile[];
  // Visibilite resolue cote serveur (booleen JSON top-level, contrat handoff
  // #168 : les endpoints mobiles renvoient desormais TOUTES les etapes, y
  // compris les masquees, avec ce flag). Le workflow en cours recalcule la
  // visibilite dynamiquement (computeVisibleStepIds) pour fonctionner offline ;
  // ce flag sert au RECAP lecture seule d'une mission terminee (etat fige
  // cote serveur) pour ne pas afficher les extensions jamais declenchees.
  // undefined = ancien backend / ancienne donnee locale -> traite comme visible.
  is_visible?: boolean;
  updated_at: string;
  version: number;
};

export type MobileInterventionDetails = MobileIntervention & {
  description?: string;
  recommandations?: string;
  type_intervention?: string;
  priorite?: string;
  date_debut?: string;
  date_fin?: string;
  site_adresse?: string;
  machine_marque?: string;
  machine_modele?: string;
  technicien_nom?: string;
  technicien_prenom?: string;
  site_details?: Record<string, unknown>;
  machine_details?: Record<string, unknown>;
  steps?: MobileStep[];
};

export type MobilePrecheckRequirements = {
  needs_cerfa: boolean;
  needs_prelevement: boolean;
  prelevement_info: string;
};

export type MobileInterventionPrecheck = {
  intervention_id: number;
  technicien_id: number;
  cerfa_enabled: boolean;
  cerfa_number: string;
  prelevement_number: string;
  status: "draft" | "started";
  started_at: string | null;
  created_at: string | null;
  updated_at: string | null;
};

export type MobilePrecheckState = {
  intervention_id: number;
  technicien_id: number;
  intervention_status: string;
  requirements: MobilePrecheckRequirements;
  precheck: MobileInterventionPrecheck | null;
};

export type MobilePrecheckPayload = {
  intervention_id: number;
  cerfa_enabled: boolean;
  cerfa_number: string;
  prelevement_number: string;
};

export type InterventionsPayload = {
  items: MobileIntervention[];
  count: number;
  technicien_id: number;
  date_debut: string;
  date_fin: string;
};
