// frontend/src/api/services/external.ts
/**
 * External Data API Service
 * Handles weather and traffic data operations
 */

import { apiClient } from '../client';
import type {
  CityInfoResponse,
  DataAvailabilityResponse,
  WeatherDataResponse,
  TrafficDataResponse,
  HistoricalWeatherRequest,
  HistoricalTrafficRequest,
} from '../types/external';

class ExternalDataService {
  /**
   * List all supported cities
   */
  async listCities(): Promise<CityInfoResponse[]> {
    return await apiClient.get<CityInfoResponse[]>(
      '/api/v1/external/cities'
    );
  }

  /**
   * Get data availability for a specific city
   */
  async getCityAvailability(cityId: string): Promise<DataAvailabilityResponse> {
    return await apiClient.get<DataAvailabilityResponse>(
      `/api/v1/external/operations/cities/${cityId}/availability`
    );
  }

  /**
   * Get historical weather data (optimized city-based endpoint)
   */
  async getHistoricalWeatherOptimized(
    tenantId: string,
    params: {
      latitude: number;
      longitude: number;
      start_date: string;
      end_date: string;
    }
  ): Promise<WeatherDataResponse[]> {
    return await apiClient.get<WeatherDataResponse[]>(
      `/api/v1/tenants/${tenantId}/external/operations/historical-weather-optimized`,
      { params }
    );
  }

  /**
   * Get historical traffic data (optimized city-based endpoint)
   */
  async getHistoricalTrafficOptimized(
    tenantId: string,
    params: {
      latitude: number;
      longitude: number;
      start_date: string;
      end_date: string;
    }
  ): Promise<TrafficDataResponse[]> {
    return await apiClient.get<TrafficDataResponse[]>(
      `/api/v1/tenants/${tenantId}/external/operations/historical-traffic-optimized`,
      { params }
    );
  }

  /**
   * Get current weather for a location (real-time)
   */
  async getCurrentWeather(
    tenantId: string,
    params: {
      latitude: number;
      longitude: number;
    }
  ): Promise<WeatherDataResponse> {
    return await apiClient.get<WeatherDataResponse>(
      `/api/v1/tenants/${tenantId}/external/operations/weather/current`,
      { params }
    );
  }

  /**
   * Get weather forecast
   */
  async getWeatherForecast(
    tenantId: string,
    params: {
      latitude: number;
      longitude: number;
      days?: number;
    }
  ): Promise<WeatherDataResponse[]> {
    return await apiClient.get<WeatherDataResponse[]>(
      `/api/v1/tenants/${tenantId}/external/operations/weather/forecast`,
      { params }
    );
  }

  /**
   * Get current traffic conditions (real-time)
   */
  async getCurrentTraffic(
    tenantId: string,
    params: {
      latitude: number;
      longitude: number;
    }
  ): Promise<TrafficDataResponse> {
    return await apiClient.get<TrafficDataResponse>(
      `/api/v1/tenants/${tenantId}/external/operations/traffic/current`,
      { params }
    );
  }
}

export const externalDataService = new ExternalDataService();
export default externalDataService;
