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

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

export interface YardRef {
  id: string;
  code: string;
  label: string;
  country?: string;
}

export interface RouteMilestone {
  id: string;
  routeId: string;
  yardId: string;
  sequenceNo: number;
  yard?: YardRef | null;
}

export interface RouteRecord {
  id: string;
  name: string;
  originYardId: string;
  destinationYardId: string;
  isActive: boolean;
  originYard?: YardRef | null;
  destinationYard?: YardRef | null;
  milestones?: RouteMilestone[];
}

export interface SaveRoutePayload {
  name: string;
  milestones: Array<{ yardId: string }>;
  isActive?: boolean;
}

interface YardListResponse {
  data: YardRef[];
}

export const routesService = {
  getAll: () => apiClient.get<RouteRecord[]>(URL_CONSTANTS.ROUTES.BASE),
  getById: (id: string) => apiClient.get<RouteRecord>(URL_CONSTANTS.ROUTES.BY_ID(id)),
  create: (data: SaveRoutePayload) => apiClient.post(URL_CONSTANTS.ROUTES.BASE, data),
  update: (id: string, data: Partial<SaveRoutePayload>) =>
    apiClient.patch(URL_CONSTANTS.ROUTES.BY_ID(id), data),
  deactivate: (id: string) => apiClient.delete(URL_CONSTANTS.ROUTES.BY_ID(id)),
  getYards: () =>
    apiClient.get<YardListResponse>(URL_CONSTANTS.RULE_ENGINE.YARDS, {
      params: { isActive: true, pageSize: 200 },
    }),
};
