import { cargoService, type Cargo } from "@/services/cargoService";
import { containerService, type Container } from "@/services/containerService";
import {
  locomotivesService,
  type Locomotive,
  type LocomotiveListFilters,
} from "@/services/locomotives.service";
import { trainService, type Train } from "@/services/trains.service";
import { wagonService, type Wagon, type WagonListFilters } from "@/services/wagon.service";
import { vehiclesService, type Vehicle, type VehicleListFilters } from "@/services/vehicles.service";
import { driversService, type Driver, type DriverListFilters } from "@/services/drivers.service";
import type { FleetResourceSlug } from "@/pages/fleet/config/resources";

export type FleetRecord = Locomotive | Train | Wagon | Container | Cargo | Vehicle | Driver;

export type FleetListFilters = WagonListFilters & LocomotiveListFilters & VehicleListFilters & DriverListFilters;

const listHandlers: Record<
  FleetResourceSlug,
  (filters?: FleetListFilters) => Promise<FleetRecord[]>
> = {
  locomotives: (filters) => locomotivesService.getAll(filters ?? {}).then((r) => r.data),
  trains: () => trainService.getAll().then((r) => r.data),
  wagons: (filters) => wagonService.getAll(filters ?? {}).then((r) => r.data),
  containers: () => containerService.getAll().then((r) => r.data),
  cargoes: () => cargoService.getAll().then((r) => r.data),
  vehicles: (filters) => vehiclesService.getAll(filters ?? {}).then((r) => r.data),
  drivers: (filters) => driversService.getAll(filters ?? {}).then((r) => r.data),
};

const createHandlers: Record<FleetResourceSlug, (data: Record<string, unknown>) => Promise<unknown>> = {
  locomotives: (data) => locomotivesService.create(data),
  trains: (data) => trainService.create(data),
  wagons: (data) => wagonService.create(data),
  containers: (data) => containerService.create(data),
  cargoes: (data) => cargoService.create(data),
  vehicles: (data) => vehiclesService.create(data),
  drivers: (data) => driversService.create(data),
};

const updateHandlers: Record<
  FleetResourceSlug,
  (id: string, data: Record<string, unknown>) => Promise<unknown>
> = {
  locomotives: (id, data) => locomotivesService.update(id, data),
  trains: (id, data) => trainService.update(id, data),
  wagons: (id, data) => wagonService.update(id, data),
  containers: (id, data) => containerService.update(id, data),
  cargoes: (id, data) => cargoService.update(id, data),
  vehicles: (id, data) => vehiclesService.update(id, data),
  drivers: (id, data) => driversService.update(id, data),
};

const removeHandlers: Record<FleetResourceSlug, (id: string) => Promise<unknown>> = {
  locomotives: (id) => locomotivesService.decommission(id),
  trains: (id) => trainService.delete(id),
  wagons: (id) => wagonService.delete(id),
  containers: (id) => containerService.delete(id),
  cargoes: (id) => cargoService.delete(id),
  vehicles: (id) => vehiclesService.delete(id),
  drivers: (id) => driversService.delete(id),
};

export const fleetService = {
  list: (slug: FleetResourceSlug, filters?: FleetListFilters) => listHandlers[slug](filters),
  create: (slug: FleetResourceSlug, data: Record<string, unknown>) => createHandlers[slug](data),
  update: (slug: FleetResourceSlug, id: string, data: Record<string, unknown>) =>
    updateHandlers[slug](id, data),
  remove: (slug: FleetResourceSlug, id: string) => removeHandlers[slug](id),
};
