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

import { URL_CONSTANTS } from '@/constants/URLS';

export type LocomotiveType = 'DIESEL' | 'ELECTRIC';
export type LocomotiveStatus =
  | 'AVAILABLE'
  | 'MAINTENANCE'
  | 'ASSIGNED'
  | 'OUT_OF_SERVICE';

export interface Locomotive {
  id: string;
  code: string;
  name?: string | null;
  locomotiveType: LocomotiveType;
  status: LocomotiveStatus;
  maxPullWeightTons: number;
  maxTrainLengthMeters: number;
  powerKw?: number | null;
  tractionForceKn?: number | null;
  maxSpeedKmh?: number | null;
  createdAt: string;
  updatedAt: string;
}

export type SaveLocomotivePayload = Omit<
  Locomotive,
  'id' | 'createdAt' | 'updatedAt'
>;

export const locomotivesService = {
  getAll: () => apiClient.get<Locomotive[]>(URL_CONSTANTS.LOCOMOTIVES.BASE),
  getById: (id: string) => apiClient.get<Locomotive>(URL_CONSTANTS.LOCOMOTIVES.BY_ID(id)),
  create: (data: Partial<SaveLocomotivePayload>) =>
    apiClient.post(URL_CONSTANTS.LOCOMOTIVES.BASE, data),
  update: (id: string, data: Partial<SaveLocomotivePayload>) =>
    apiClient.patch(URL_CONSTANTS.LOCOMOTIVES.BY_ID(id), data),
  decommission: (id: string) => apiClient.post(URL_CONSTANTS.LOCOMOTIVES.DECOMMISSION(id), {}),
};
