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

type ListResponse<T> = T[] | { data: T[] };

export interface WagonType {
  id: string;
  code: string;
  name: string;
  capacityTons: number;
  lengthMeters: number;
  maxWagonsPerTrain?: number | null;
  supportedLoadTypes: string[];
  isActive: boolean;
}

const asList = <T>(payload: ListResponse<T>): T[] =>
  Array.isArray(payload) ? payload : payload.data;

export const wagonTypesService = {
  async getWagonTypes() {
    const response = await api.get<ListResponse<WagonType>>('/wagon-types', {
      params: { isActive: 'all', pageSize: 500 },
    });
    return asList(response.data);
  },
  create: (data: Partial<WagonType>) => api.post('/wagon-types', data),
  update: (id: string, data: Partial<WagonType>) => api.patch(`/wagon-types/${id}`, data),
  delete: (id: string) => api.delete(`/wagon-types/${id}`),
};
