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

export interface Wagon {
  id: string;
  wagonNumber: string;
  wagonTypeId: string;
  trainId: string | null;
  sequenceNumber: number | null;
  tareWeight: number;
  maxPayloadWeight: number;
  status: string;
  notes?: string;
}

export const wagonService = {
  getAll: () => apiClient.get<Wagon[]>('/wagons'),
  getById: (id: string) => apiClient.get<Wagon>(`/wagons/${id}`),
  getByTrain: (trainId: string) => apiClient.get<Wagon[]>(`/wagons?trainId=${trainId}`),
  assignToTrain: (wagonId: string, trainId: string, sequenceNumber?: number) =>
    apiClient.post(`/wagons/${wagonId}/assign-train`, { trainId, sequenceNumber }),
  unassign: (wagonId: string) => apiClient.post(`/wagons/${wagonId}/unassign-train`),
  reorder: (trainId: string, wagonIds: string[]) =>
    apiClient.post(`/trains/${trainId}/reorder-wagons`, { wagonIds }),
  create: (data: Partial<Wagon>) => apiClient.post('/wagons', data),
  update: (id: string, data: Partial<Wagon>) => apiClient.patch(`/wagons/${id}`, data),
  delete: (id: string) => apiClient.delete(`/wagons/${id}`),
};
