import { api as apiClient } from '../auth/http';
import { URL_CONSTANTS } from '@/constants/URLS';

export type VehicleType = 'TRUCK' | 'VAN' | 'CAR' | 'BUS' | 'TRAILER' | 'TANKER' | 'FLATBED';
export type FuelType = 'PETROL' | 'DIESEL' | 'ELECTRIC' | 'HYBRID';
export type VehicleStatus = 'ACTIVE' | 'MAINTENANCE' | 'RETIRED' | 'OUT_OF_SERVICE';
export type VehicleAvailability = 'FREE' | 'BUSY';

export interface VehicleListFilters {
  status?: VehicleStatus;
  availability?: VehicleAvailability;
  search?: string;
  page?: number;
  limit?: number;
  sortBy?: string;
  sortOrder?: 'ASC' | 'DESC';
}

export interface Vehicle {
  id: string;
  plateNumber: string;
  registrationNumber: string;
  /** Denormalised truck-type code, written server-side. Register with `truckTypeId`. */
  vehicleType: VehicleType;
  /** Truck configuration from the managed truck types. */
  truckTypeId?: string | null;
  /** Vehicle Identification Number — unique across the fleet. */
  vin?: string | null;
  /** OWNED | OUTSOURCED. */
  ownership?: string | null;
  manufacturer: string;
  model: string;
  year: number;
  fuelType: FuelType;
  capacity: number;
  status: VehicleStatus;
  availability: VehicleAvailability;
  description?: string | null;
  assignedDriverId?: string | null;
  assignedDriverName?: string | null;
  code?: string | null;
  powerPlateNo?: string | null;
  trailerPlateNo?: string | null;
  locationId?: string | null;
  /** Odometer-derived distances (API sends numeric strings; coerce with Number). */
  estimatedDistanceKm?: number | null;
  actualDistanceKm?: number | null;
  /** Haulage rate per km + its currency (ETB | USD). */
  pricePerKm?: number | null;
  currency?: string | null;
  createdAt: string;
  updatedAt: string;
}

export type SaveVehiclePayload = Omit<
  Vehicle,
  'id' | 'createdAt' | 'updatedAt' | 'registrationNumber'
>;

export const vehiclesService = {
  getAll: (filters: VehicleListFilters = {}) => {
    const params = new URLSearchParams();
    if (filters.status) params.set('status', filters.status);
    if (filters.availability) params.set('availability', filters.availability);
    if (filters.search) params.set('search', filters.search);
    if (filters.page) params.set('page', filters.page.toString());
    if (filters.limit) params.set('limit', filters.limit.toString());
    if (filters.sortBy) params.set('sortBy', filters.sortBy);
    if (filters.sortOrder) params.set('sortOrder', filters.sortOrder);
    const qs = params.toString();
    return apiClient.get<Vehicle[]>(
      `${URL_CONSTANTS.VEHICLES.BASE}${qs ? `?${qs}` : ''}`,
    );
  },
  getById: (id: string) => apiClient.get<Vehicle>(URL_CONSTANTS.VEHICLES.BY_ID(id)),
  create: (data: Partial<SaveVehiclePayload>) =>
    apiClient.post(URL_CONSTANTS.VEHICLES.BASE, data),
  update: (id: string, data: Partial<SaveVehiclePayload & { locationId?: string | null }>) =>
    apiClient.patch(URL_CONSTANTS.VEHICLES.BY_ID(id), data),
  delete: (id: string) => apiClient.delete(URL_CONSTANTS.VEHICLES.BY_ID(id)),
};
