import api from './api';
import type { AxiosResponse } from '../types/models';
import type {
  DailyGallonsResponse,
  WeeklyGallonsResponse,
  MonthlyGallonsResponse,
  TotalsComparisonResponse,
  DailyGallonsParams,
  WeeklyGallonsParams,
  MonthlyGallonsParams,
  WeeklyCustomerResponse
} from '../types/stats';

/**
 * Stats Service
 * API methods for the stats dashboard charts and comparison cards
 */
export const statsService = {
  /**
   * Get daily gallons delivered for time-series chart
   * @param params - start_date, end_date, years array
   */
  getDailyGallons: (params: DailyGallonsParams): Promise<AxiosResponse<DailyGallonsResponse>> => {
    const queryParams = new URLSearchParams({
      start_date: params.start_date,
      end_date: params.end_date,
      years: params.years.join(',')
    });
    return api.get(`/stats/gallons/daily?${queryParams.toString()}`);
  },

  /**
   * Get weekly aggregated gallons delivered
   * @param params - start_date, end_date, years array
   */
  getWeeklyGallons: (params: WeeklyGallonsParams): Promise<AxiosResponse<WeeklyGallonsResponse>> => {
    const queryParams = new URLSearchParams({
      start_date: params.start_date,
      end_date: params.end_date,
      years: params.years.join(',')
    });
    return api.get(`/stats/gallons/weekly?${queryParams.toString()}`);
  },

  /**
   * Get monthly aggregated gallons delivered
   * @param params - year, compare_years array
   */
  getMonthlyGallons: (params: MonthlyGallonsParams): Promise<AxiosResponse<MonthlyGallonsResponse>> => {
    const queryParams = new URLSearchParams({
      year: params.year.toString(),
      compare_years: params.compare_years.join(',')
    });
    return api.get(`/stats/gallons/monthly?${queryParams.toString()}`);
  },

  /**
   * Get totals comparison data (today, WTD, MTD, YTD vs previous year)
   */
  getTotalsComparison: (): Promise<AxiosResponse<TotalsComparisonResponse>> => {
    return api.get('/stats/totals/comparison');
  },

  /**
   * Get weekly new customer signups by year
   * @param years - array of years to compare
   */
  getWeeklyCustomers: (years: number[]): Promise<AxiosResponse<WeeklyCustomerResponse>> => {
    const queryParams = new URLSearchParams({ years: years.join(',') });
    return api.get(`/stats/customers/weekly?${queryParams.toString()}`);
  }
};

export default statsService;
