import { api as apiClient } from "../auth/http";

export type FleetEventType =
  | "DRIVER_REGISTERED"
  | "VEHICLE_REGISTERED"
  | "DRIVER_ASSIGNED"
  | "DRIVER_UNASSIGNED"
  | "VEHICLE_STATUS_CHANGED"
  | "VEHICLE_AVAILABILITY_CHANGED"
  | "MILE_VEHICLE_ASSIGNED"
  | "MILE_VEHICLE_RELEASED"
  | "MILE_STATUS_CHANGED";

export interface FleetHistoryEvent {
  id: string;
  eventType: FleetEventType;
  vehicleId?: string | null;
  driverId?: string | null;
  firstMileId?: string | null;
  lastMileId?: string | null;
  fromValue?: string | null;
  toValue?: string | null;
  label?: string | null;
  metadata?: Record<string, unknown> | null;
  createdAt: string;
}

/** Timeline of fleet events for a driver or a vehicle (newest first). */
export const fleetHistoryService = {
  driver: (id: string) =>
    apiClient
      .get<FleetHistoryEvent[]>(`/drivers/${id}/history`)
      .then((r) => r.data),
  vehicle: (id: string) =>
    apiClient
      .get<FleetHistoryEvent[]>(`/vehicles/${id}/history`)
      .then((r) => r.data),
};
