import { api } from "@/auth/http";

export interface GpsDevice {
  id: string;
  imei: string;
  name?: string | null;
  vehicleId?: string | null;
  vehicle?: {
    id: string;
    plateNumber?: string;
    code?: string | null;
    manufacturer?: string;
    model?: string;
  } | null;
  status: string;
  online: boolean;
  lastSeenAt?: string | null;
  lastLat?: number | string | null;
  lastLng?: number | string | null;
  lastSpeed?: number | string | null;
  lastCourse?: number | null;
  lastFixAt?: string | null;
  voltageLevel?: number | null;
  gsmLevel?: number | null;
}

export interface GpsPosition {
  id: string;
  lat: number | string;
  lng: number | string;
  speed: number | string;
  course: number;
  satellites: number;
  gpsTime: string;
  alarm: number;
}

export const gpsTrackingService = {
  latest: () => api.get<GpsDevice[]>("/gps/positions/latest"),
  listDevices: () => api.get<GpsDevice[]>("/gps/devices"),
  history: (vehicleId: string, limit = 200) =>
    api.get<GpsPosition[]>(`/gps/positions/${vehicleId}/history?limit=${limit}`),
  register: (data: { imei: string; name?: string; vehicleId?: string | null }) =>
    api.post<GpsDevice>("/gps/devices", data),
  update: (id: string, data: { name?: string; vehicleId?: string | null }) =>
    api.patch<GpsDevice>(`/gps/devices/${id}`, data),
  remove: (id: string) => api.delete<void>(`/gps/devices/${id}`),
};
