// ================================================================
// frontend/src/api/services/distribution.ts
// ================================================================
/**
 * Distribution Service - Complete backend alignment
 *
 * Backend API structure:
 * - services/distribution/app/api/routes.py
 * - services/distribution/app/api/shipments.py
 *
 * Last Updated: 2025-12-03
 * Status: ✅ Complete - Backend alignment
 */

import { apiClient } from '../client';

export class DistributionService {
  private readonly baseUrl = '/tenants';

  // ===================================================================
  // SHIPMENTS
  // Backend: services/distribution/app/api/shipments.py
  // ===================================================================

  async getShipments(
    tenantId: string,
    date?: string
  ): Promise<any[]> {
    const params = new URLSearchParams();
    if (date) {
      params.append('date_from', date);
      params.append('date_to', date);
    }

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/distribution/shipments${queryString ? `?${queryString}` : ''}`;

    const response = await apiClient.get<any>(url);
    return response.shipments || response;
  }

  async getShipment(
    tenantId: string,
    shipmentId: string
  ): Promise<any> {
    return apiClient.get(`${this.baseUrl}/${tenantId}/distribution/shipments/${shipmentId}`);
  }

  async getRouteSequences(
    tenantId: string,
    date?: string
  ): Promise<any[]> {
    const params = new URLSearchParams();
    if (date) {
      params.append('date_from', date);
      params.append('date_to', date);
    }

    const queryString = params.toString();
    const url = `${this.baseUrl}/${tenantId}/distribution/routes${queryString ? `?${queryString}` : ''}`;

    const response = await apiClient.get<any>(url);
    return response.routes || response;
  }
}

export const distributionService = new DistributionService();
export default distributionService;
