/**
 * Forecasting Service
 * API calls for forecasting service endpoints
 */

import { apiClient } from '../client/apiClient';
import {
  ForecastRequest,
  ForecastResponse,
  BatchForecastRequest,
  BatchForecastResponse,
  ForecastListResponse,
  ForecastByIdResponse,
  ForecastStatistics,
  DeleteForecastResponse,
  GetForecastsParams,
  ForecastingHealthResponse,
  MultiDayForecastResponse,
} from '../types/forecasting';

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

  /**
   * Generate a single product forecast
   * POST /tenants/{tenant_id}/forecasts/single
   */
  async createSingleForecast(
    tenantId: string,
    request: ForecastRequest
  ): Promise<ForecastResponse> {
    return apiClient.post<ForecastResponse, ForecastRequest>(
      `${this.baseUrl}/${tenantId}/forecasts/single`,
      request
    );
  }

  /**
   * Generate batch forecasts for multiple products
   * POST /tenants/{tenant_id}/forecasts/batch
   */
  async createBatchForecast(
    tenantId: string,
    request: BatchForecastRequest
  ): Promise<BatchForecastResponse> {
    return apiClient.post<BatchForecastResponse, BatchForecastRequest>(
      `${this.baseUrl}/${tenantId}/forecasts/batch`,
      request
    );
  }

  /**
   * Get tenant forecasts with filtering and pagination
   * GET /tenants/{tenant_id}/forecasts
   */
  async getTenantForecasts(
    tenantId: string,
    params?: GetForecastsParams
  ): Promise<ForecastListResponse> {
    const searchParams = new URLSearchParams();
    
    if (params?.inventory_product_id) {
      searchParams.append('inventory_product_id', params.inventory_product_id);
    }
    if (params?.start_date) {
      searchParams.append('start_date', params.start_date);
    }
    if (params?.end_date) {
      searchParams.append('end_date', params.end_date);
    }
    if (params?.skip !== undefined) {
      searchParams.append('skip', params.skip.toString());
    }
    if (params?.limit !== undefined) {
      searchParams.append('limit', params.limit.toString());
    }

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

    return apiClient.get<ForecastListResponse>(url);
  }

  /**
   * Get specific forecast by ID
   * GET /tenants/{tenant_id}/forecasts/{forecast_id}
   */
  async getForecastById(
    tenantId: string,
    forecastId: string
  ): Promise<ForecastByIdResponse> {
    return apiClient.get<ForecastByIdResponse>(
      `${this.baseUrl}/${tenantId}/forecasts/${forecastId}`
    );
  }

  /**
   * Delete a forecast
   * DELETE /tenants/{tenant_id}/forecasts/{forecast_id}
   */
  async deleteForecast(
    tenantId: string,
    forecastId: string
  ): Promise<DeleteForecastResponse> {
    return apiClient.delete<DeleteForecastResponse>(
      `${this.baseUrl}/${tenantId}/forecasts/${forecastId}`
    );
  }

  /**
   * Get comprehensive forecast statistics
   * GET /tenants/{tenant_id}/forecasts/statistics
   */
  async getForecastStatistics(
    tenantId: string
  ): Promise<ForecastStatistics> {
    return apiClient.get<ForecastStatistics>(
      `${this.baseUrl}/${tenantId}/forecasts/statistics`
    );
  }

  /**
   * Generate multi-day forecasts for a single product
   * POST /tenants/{tenant_id}/forecasts/multi-day
   */
  async createMultiDayForecast(
    tenantId: string,
    request: ForecastRequest
  ): Promise<MultiDayForecastResponse> {
    return apiClient.post<MultiDayForecastResponse, ForecastRequest>(
      `${this.baseUrl}/${tenantId}/forecasts/multi-day`,
      request
    );
  }

  /**
   * Health check for forecasting service
   * GET /health
   */
  async getHealthCheck(): Promise<ForecastingHealthResponse> {
    return apiClient.get<ForecastingHealthResponse>('/health');
  }
}

// Export singleton instance
export const forecastingService = new ForecastingService();
export default forecastingService;