import {
  SGW_ROUTE_CREW,
  SGW_ROUTE_GET_FISH,
  SGW_ROUTE_HAUL_HANDLE,
  SGW_ROUTE_TRIP_CREW,
  SGW_ROUTE_TRIPS,
  SGW_ROUTE_TRIPS_BY_ID,
  SGW_ROUTE_TRIPS_CREWS,
  SGW_ROUTE_TRIPS_LAST,
  SGW_ROUTE_TRIPS_LIST,
  SGW_ROUTE_UPDATE_FISHING_LOGS,
  SGW_ROUTE_UPDATE_TRIP_STATUS,
} from '@/constants/slave/sgw/routes';
import { request } from '@umijs/max';

/**
 * Update trip state/status
 * @param body Trip state update request
 */
export async function apiUpdateTripState(
  body: SgwModel.TripUpdateStateRequest,
) {
  return request(SGW_ROUTE_UPDATE_TRIP_STATUS, {
    method: 'PUT',
    data: body,
  });
}

/**
 * Start a new haul (fishing log)
 * @param body New fishing log request
 */
export async function apiStartNewHaul(body: SgwModel.NewFishingLogRequest) {
  return request(SGW_ROUTE_HAUL_HANDLE, {
    method: 'PUT',
    data: body,
  });
}

/**
 * Get list of fish species
 */
export async function apiGetFishSpecies() {
  return request<SgwModel.FishSpeciesResponse[]>(SGW_ROUTE_GET_FISH);
}

/**
 * Update fishing logs information
 * @param body Fishing log data
 */
export async function apiUpdateFishingLogs(body: SgwModel.FishingLog) {
  return request(SGW_ROUTE_UPDATE_FISHING_LOGS, {
    method: 'PUT',
    data: body,
  });
}

/**
 * Create a new trip for a thing (device)
 * @param thing_id Thing/Device ID
 * @param params Trip creation parameters
 */
export async function apiCreateTrip(
  thing_id: string,
  params: SgwModel.TripCreateParams,
): Promise<SgwModel.Trip> {
  return request<SgwModel.Trip>(`${SGW_ROUTE_TRIPS}/${thing_id}`, {
    method: 'POST',
    data: params,
  });
}

/**
 * Query trips list with filters
 * @param params Query parameters
 */
export async function apiQueryTrips(
  params: SgwModel.TripQueryParams,
): Promise<SgwModel.TripQueryResponse> {
  return request<SgwModel.TripQueryResponse>(SGW_ROUTE_TRIPS_LIST, {
    method: 'POST',
    data: params,
  });
}

/**
 * Update a trip by ID
 * @param trip_id Trip ID
 * @param update Trip update parameters
 */
export async function apiUpdateTrip(
  trip_id: string,
  update: SgwModel.TripUpdateParams,
): Promise<SgwModel.Trip> {
  return request<SgwModel.Trip>(`${SGW_ROUTE_TRIPS}/${trip_id}`, {
    method: 'PUT',
    data: update,
  });
}

/**
 * Delete trip(s)
 * @param data Trip deletion parameters (array of trip IDs)
 */
export async function apiDeleteTrip(
  data: SgwModel.TripDeleteParams,
): Promise<void> {
  return request(SGW_ROUTE_TRIPS, {
    method: 'DELETE',
    data: data,
  });
}

/**
 * Query last trip for a thing (device)
 * @param thing_id Thing/Device ID
 */
export async function apiQueryLastTrips(
  thing_id: string,
): Promise<SgwModel.Trip> {
  return request<SgwModel.Trip>(`${SGW_ROUTE_TRIPS_LAST}/${thing_id}`);
}

/**
 * Get trip by ID
 * @param trip_id Trip ID
 */
export async function apiGetTripById(trip_id: string): Promise<SgwModel.Trip> {
  return request<SgwModel.Trip>(`${SGW_ROUTE_TRIPS_BY_ID}/${trip_id}`);
}

/* ===========================
   Crew Management APIs
=========================== */

/**
 * Create a new crew member
 * @param data Crew creation parameters
 */
export async function apiCreateCrew(
  data: SgwModel.CrewCreateParams,
): Promise<SgwModel.TripCrewPerson> {
  return request<SgwModel.TripCrewPerson>(SGW_ROUTE_CREW, {
    method: 'POST',
    data: data,
  });
}

/**
 * Get crew member by ID
 * @param crew_id Crew member ID
 */
export async function apiGetCrew(
  crew_id: string,
): Promise<SgwModel.TripCrewPerson> {
  return request<SgwModel.TripCrewPerson>(`${SGW_ROUTE_TRIPS}/crew/${crew_id}`);
}

/**
 * Update crew member information
 * @param crew_id Crew member ID
 * @param update Crew update parameters
 */
export async function apiUpdateCrew(
  crew_id: string,
  update: SgwModel.CrewUpdateParams,
): Promise<SgwModel.TripCrewPerson> {
  return request<SgwModel.TripCrewPerson>(`${SGW_ROUTE_CREW}/${crew_id}`, {
    method: 'PUT',
    data: update,
  });
}

/**
 * Add crew member to a trip
 * @param data Trip crew creation parameters
 */
export async function apiAddTripCrew(
  data: SgwModel.TripCrewCreateParams,
): Promise<void> {
  return request(SGW_ROUTE_TRIP_CREW, {
    method: 'POST',
    data: data,
  });
}

/**
 * Get all crew members for a trip
 * @param trip_id Trip ID
 */
export async function apiGetTripCrew(
  trip_id: string,
): Promise<SgwModel.TripCrewQueryResponse> {
  return request<SgwModel.TripCrewQueryResponse>(
    `${SGW_ROUTE_TRIPS_CREWS}/${trip_id}`,
  );
}

/**
 * Update trip crew information
 * @param update Trip crew update parameters
 */
export async function apiUpdateTripCrew(
  update: SgwModel.TripCrewUpdateParams,
): Promise<void> {
  return request(SGW_ROUTE_TRIP_CREW, {
    method: 'PUT',
    data: update,
  });
}

/**
 * Remove crew member from a trip
 * @param trip_id Trip ID
 * @param crew_id Crew member ID
 */
export async function apiDeleteTripCrew(
  trip_id: string,
  crew_id: string,
): Promise<void> {
  return request(`${SGW_ROUTE_TRIP_CREW}/${trip_id}/${crew_id}`, {
    method: 'DELETE',
  });
}
