/**
 * Dashboard API
 *
 * Endpoints for dashboard analytics and KPIs
 */

import { get } from './client';

/**
 * Dashboard KPIs Response
 */
export interface DashboardKPIs {
  bookingsThisMonth: number;
  bookingsThisMonthChange: number;
  totalTEUs: number;
  totalTEUsChange: number;
  estimatedRevenue: number;
  estimatedRevenueChange: number;
  pendingConfirmations: number;
  pendingConfirmationsChange: number;
}

/**
 * Bookings Chart Data
 */
export interface BookingsChartData {
  labels: string[];
  data: number[];
}

/**
 * Trade Lane Data
 */
export interface TradeLane {
  route: string;
  bookingCount: number;
  origin: string;
  destination: string;
}

/**
 * Dashboard Alert
 */
export interface DashboardAlert {
  id: string;
  severity: 'critical' | 'high' | 'medium' | 'low';
  title: string;
  message: string;
  bookingId?: string;
  bookingNumber?: string;
  createdAt: string;
}

/**
 * Get dashboard KPIs
 * GET /api/v1/dashboard/kpis
 */
export async function getKPIs(): Promise<DashboardKPIs> {
  return get<DashboardKPIs>('/api/v1/dashboard/kpis');
}

/**
 * Get bookings chart data (6 months)
 * GET /api/v1/dashboard/bookings-chart
 */
export async function getBookingsChart(): Promise<BookingsChartData> {
  return get<BookingsChartData>('/api/v1/dashboard/bookings-chart');
}

/**
 * Get top 5 trade lanes
 * GET /api/v1/dashboard/top-trade-lanes
 */
export async function getTopTradeLanes(): Promise<TradeLane[]> {
  return get<TradeLane[]>('/api/v1/dashboard/top-trade-lanes');
}

/**
 * Get dashboard alerts
 * GET /api/v1/dashboard/alerts
 */
export async function getAlerts(): Promise<DashboardAlert[]> {
  return get<DashboardAlert[]>('/api/v1/dashboard/alerts');
}

/**
 * CSV Booking KPIs Response
 */
export interface CsvBookingKPIs {
  totalAccepted: number;
  totalRejected: number;
  totalPending: number;
  totalWeightAcceptedKG: number;
  totalVolumeAcceptedCBM: number;
  acceptanceRate: number;
  acceptedThisMonth: number;
  rejectedThisMonth: number;
}

/**
 * Top Carrier Data
 */
export interface TopCarrier {
  carrierName: string;
  totalBookings: number;
  acceptedBookings: number;
  rejectedBookings: number;
  acceptanceRate: number;
  totalWeightKG: number;
  totalVolumeCBM: number;
  avgPriceUSD: number;
}

/**
 * Get CSV booking KPIs
 * GET /api/v1/dashboard/csv-booking-kpis
 */
export async function getCsvBookingKPIs(): Promise<CsvBookingKPIs> {
  return get<CsvBookingKPIs>('/api/v1/dashboard/csv-booking-kpis');
}

/**
 * Get top carriers (top 5 by booking count)
 * GET /api/v1/dashboard/top-carriers
 */
export async function getTopCarriers(): Promise<TopCarrier[]> {
  return get<TopCarrier[]>('/api/v1/dashboard/top-carriers');
}

/**
 * Export all dashboard APIs
 */
export const dashboardApi = {
  getKPIs,
  getBookingsChart,
  getTopTradeLanes,
  getAlerts,
  getCsvBookingKPIs,
  getTopCarriers,
};
