import {
  SGW_ROUTE_PORTS,
  SGW_ROUTE_SHIP_GROUPS,
  SGW_ROUTE_SHIP_TYPES,
  SGW_ROUTE_SHIPS,
} from '@/constants/slave/sgw/routes';
import { request } from '@umijs/max';

// ========== Ship CRUD Operations ==========

/**
 * Add a new ship
 */
export async function apiAddShip(
  params: SgwModel.ShipCreateParams,
): Promise<SgwModel.ShipDetail> {
  return request<SgwModel.ShipDetail>(SGW_ROUTE_SHIPS, {
    method: 'POST',
    data: params,
  });
}

/**
 * Update an existing ship
 */
export async function apiUpdateShip(
  id: string,
  update: SgwModel.ShipUpdateParams,
): Promise<SgwModel.ShipDetail> {
  return request<SgwModel.ShipDetail>(`${SGW_ROUTE_SHIPS}/${id}`, {
    method: 'PUT',
    data: update,
  });
}

/**
 * Delete a ship by ID
 */
export async function apiDeleteShip(id: string): Promise<void> {
  return request<void>(`${SGW_ROUTE_SHIPS}/${id}`, {
    method: 'DELETE',
  });
}

/**
 * Query ships with filters
 */
export async function apiQueryShips(
  params?: SgwModel.ShipQueryParams,
): Promise<SgwModel.ShipQueryResponse> {
  return request<SgwModel.ShipQueryResponse>(SGW_ROUTE_SHIPS, {
    params: params,
  });
}

/**
 * Get ship detail by thing ID
 */
export async function apiViewShip(
  thingId: string,
): Promise<SgwModel.ShipDetail> {
  return request<SgwModel.ShipDetail>(`${SGW_ROUTE_SHIPS}/${thingId}`);
}

/**
 * Get all ship types
 */
export async function apiGetShipTypes(): Promise<SgwModel.ShipType[]> {
  return request<SgwModel.ShipType[]>(SGW_ROUTE_SHIP_TYPES);
}

// ========== Ship Group Operations ==========

/**
 * Add a new ship group
 */
export async function apiAddShipGroup(
  params: SgwModel.ShipGroupCreateParams,
): Promise<SgwModel.ShipGroup> {
  return request<SgwModel.ShipGroup>(SGW_ROUTE_SHIP_GROUPS, {
    method: 'POST',
    data: params,
  });
}

/**
 * Get all ship groups
 */
export async function apiGetShipGroups(): Promise<
  SgwModel.GroupShipResponse[]
> {
  return request<SgwModel.GroupShipResponse[]>(SGW_ROUTE_SHIP_GROUPS);
}

/**
 * List all ship groups (alternative method)
 */
export async function apiListGroupShips(): Promise<SgwModel.ShipGroup[]> {
  return request<SgwModel.ShipGroup[]>(SGW_ROUTE_SHIP_GROUPS);
}

/**
 * Update a ship group
 */
export async function apiUpdateShipGroup(
  id: string,
  update: SgwModel.ShipGroupUpdateParams,
): Promise<SgwModel.ShipGroup> {
  return request<SgwModel.ShipGroup>(`${SGW_ROUTE_SHIP_GROUPS}/${id}`, {
    method: 'PUT',
    data: update,
  });
}

/**
 * Delete a ship group
 */
export async function apiDeleteShipGroup(id: string): Promise<void> {
  return request<void>(`${SGW_ROUTE_SHIP_GROUPS}/${id}`, {
    method: 'DELETE',
  });
}

// ========== Port Operations ==========

/**
 * Query ports with filters
 */
export async function apiQueryPorts(
  params?: SgwModel.PortQueryParams,
): Promise<SgwModel.PortQueryResponse> {
  return request<SgwModel.PortQueryResponse>(SGW_ROUTE_PORTS, {
    method: 'POST',
    data: params,
  });
}
