import { api } from "./client";
import type { InspectionSummary } from "./vehicles";

export type InspectionType =
  | "handover"
  | "return"
  | "periodic"
  | "ad-hoc"
  | "initial"
  | "seizure"
  | "equipment-change"
  | "pre-repair"
  | "post-repair";

export type InspectionStatus = "in_progress" | "completed" | "cancelled";

export interface InspectionCreateInput {
  vehicle_id: number;
  type: InspectionType;
  driver_id?: number;
  mileage?: number;
}

export interface PhotoSummary {
  id: number;
  side: string;
  slot_index: number;
  storage_key: string;
  thumb_key?: string | null;
  annotated_key?: string | null;
  width?: number | null;
  height?: number | null;
  status: string;
  taken_at: string;
}

export interface MarkerSummary {
  id: number;
  inspection_id: number;
  photo_id?: number | null;
  tt_image_id?: string | null;
  side?: string | null;
  x?: number | null;
  y?: number | null;
  polygon?: { x: number; y: number }[] | null;
  damage_type?: string | null;
  severity?: string | null;
  description?: string | null;
  carried_over_from_id?: number | null;
  resolved: boolean;
  created_at: string;
}

export interface VehicleBrief {
  id: number;
  license_plate: string;
  vin?: string | null;
  make?: string | null;
  model?: string | null;
  year?: number | null;
}

export interface InspectionDetail {
  id: number;
  vehicle_id: number;
  /** Сводка по машине (гос.номер / марка / модель / год). Backend attaches её
      в get_inspection_by_id_with_relations, чтобы карточка осмотра могла
      показать машину без второго запроса. */
  vehicle?: VehicleBrief | null;
  type: InspectionType;
  performed_by: number;
  driver_id?: number | null;
  started_at: string;
  finished_at?: string | null;
  status: InspectionStatus;
  notes?: string | null;
  prev_inspection_id?: number | null;
  meta: Record<string, unknown>;
  photos: PhotoSummary[];
  markers: MarkerSummary[];
  mileage?: number | null;
  /** ФИО механика, выполнившего осмотр (lookup users.full_name по performed_by). */
  inspector_name?: string | null;
}

export async function createInspection(
  input: InspectionCreateInput
): Promise<InspectionDetail> {
  return api.post("inspections", { json: input }).json<InspectionDetail>();
}

export async function getInspection(id: number): Promise<InspectionDetail> {
  return api.get(`inspections/${id}`).json<InspectionDetail>();
}

export async function patchInspection(
  id: number,
  body: { status?: InspectionStatus; notes?: string; finished_at?: string; mileage?: number }
): Promise<InspectionDetail> {
  return api.patch(`inspections/${id}`, { json: body }).json<InspectionDetail>();
}

export interface MarkerCreateInput {
  photo_id?: number | null;
  side?: string | null;
  x?: number | null;
  y?: number | null;
  polygon?: { x: number; y: number }[] | null;
  damage_type?: string | null;
  severity?: string | null;
  description?: string | null;
}

export async function createMarker(
  inspectionId: number,
  body: MarkerCreateInput
): Promise<MarkerSummary> {
  return api
    .post(`inspections/${inspectionId}/markers`, { json: body })
    .json<MarkerSummary>();
}

export async function deleteInspection(inspectionId: number): Promise<void> {
  await api.delete(`inspections/${inspectionId}`);
}

export async function deleteMarker(markerId: number): Promise<void> {
  await api.delete(`markers/${markerId}`);
}

export type { InspectionSummary };
