/**
 * Rates API
 *
 * Endpoints for searching shipping rates (both API and CSV-based)
 */

import { get, post } from './client';
import type {
  RateSearchRequest,
  RateSearchResponse,
  CsvRateSearchRequest,
  CsvRateSearchResponse,
  AvailableCompaniesResponse,
  FilterOptionsResponse,
} from '@/types/api';

/**
 * Route Port Info - port details with coordinates
 */
export interface RoutePortInfo {
  code: string;
  name: string;
  city: string;
  country: string;
  countryName: string;
  displayName: string;
  latitude?: number;
  longitude?: number;
}

/**
 * Available Origins Response
 */
export interface AvailableOriginsResponse {
  origins: RoutePortInfo[];
  total: number;
}

/**
 * Available Destinations Response
 */
export interface AvailableDestinationsResponse {
  origin: string;
  destinations: RoutePortInfo[];
  total: number;
}

/**
 * Search shipping rates (API-based)
 * POST /api/v1/rates/search
 */
export async function searchRates(data: RateSearchRequest): Promise<RateSearchResponse> {
  return post<RateSearchResponse>('/api/v1/rates/search', data);
}

/**
 * Search CSV-based rates with detailed pricing
 * POST /api/v1/rates/search-csv
 */
export async function searchCsvRates(data: CsvRateSearchRequest): Promise<CsvRateSearchResponse> {
  return post<CsvRateSearchResponse>('/api/v1/rates/search-csv', data);
}

/**
 * Search CSV-based rates with service level offers (RAPID, STANDARD, ECONOMIC)
 * POST /api/v1/rates/search-csv-offers
 *
 * Generates 3 offers per matching rate:
 * - RAPID: +20% price, -30% transit (most expensive, fastest)
 * - STANDARD: base price and transit
 * - ECONOMIC: -15% price, +50% transit (cheapest, slowest)
 */
export async function searchCsvRatesWithOffers(
  data: CsvRateSearchRequest
): Promise<CsvRateSearchResponse> {
  return post<CsvRateSearchResponse>('/api/v1/rates/search-csv-offers', data);
}

/**
 * Get available companies for filtering
 * GET /api/v1/rates/companies
 */
export async function getAvailableCompanies(): Promise<AvailableCompaniesResponse> {
  return post<AvailableCompaniesResponse>('/api/v1/rates/companies');
}

/**
 * Get filter options (companies, container types, currencies)
 * GET /api/v1/rates/filters/options
 */
export async function getFilterOptions(): Promise<FilterOptionsResponse> {
  return post<FilterOptionsResponse>('/api/v1/rates/filters/options');
}

/**
 * Get available origin ports from CSV rates
 * GET /api/v1/rates/available-routes/origins
 *
 * Returns only ports that have shipping routes defined in CSV rate files.
 * Use this to populate origin port selection dropdown.
 */
export async function getAvailableOrigins(): Promise<AvailableOriginsResponse> {
  return get<AvailableOriginsResponse>('/api/v1/rates/available-routes/origins');
}

/**
 * Get available destination ports for a given origin
 * GET /api/v1/rates/available-routes/destinations?origin=XXXX
 *
 * Returns only ports that have shipping routes from the specified origin port.
 * Use this to populate destination port selection after origin is selected.
 */
export async function getAvailableDestinations(
  origin: string
): Promise<AvailableDestinationsResponse> {
  return get<AvailableDestinationsResponse>(
    `/api/v1/rates/available-routes/destinations?origin=${encodeURIComponent(origin)}`
  );
}
