import api from "../axios";
import { API_ENDPOINTS } from "../endpoints";
import {
  ApiResponse,
  TDashboardClosingSoon,
  TDashboardCountries,
  TDashboardNoticeTypes,
  TDashboardStatistics,
  TDashboardSummary,
  TDashboardTrend,
} from "../types";

export const dashboardService = {
  summary: async (params?: {
    closing_window?: number;
  }): Promise<ApiResponse<TDashboardSummary>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.SUMMARY, { params })).data;
    } catch (error) {
      console.error("ERROR caught in Dashboard Service => summary:", error);
      throw error;
    }
  },

  trend: async (params?: {
    days?: number;
    metric?: "created" | "published" | "awarded";
  }): Promise<ApiResponse<TDashboardTrend>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.TREND, { params })).data;
    } catch (error) {
      console.error("ERROR caught in Dashboard Service => trend:", error);
      throw error;
    }
  },

  statistics: async (params?: {
    days?: number;
  }): Promise<ApiResponse<TDashboardStatistics>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.STATISTICS, { params }))
        .data;
    } catch (error) {
      console.error("ERROR caught in Dashboard Service => statistics:", error);
      throw error;
    }
  },

  countries: async (params?: {
    limit?: number;
  }): Promise<ApiResponse<TDashboardCountries>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.COUNTRIES, { params })).data;
    } catch (error) {
      console.error("ERROR caught in Dashboard Service => countries:", error);
      throw error;
    }
  },

  noticeTypes: async (): Promise<ApiResponse<TDashboardNoticeTypes>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.NOTICE_TYPES)).data;
    } catch (error) {
      console.error(
        "ERROR caught in Dashboard Service => notice-types:",
        error,
      );
      throw error;
    }
  },

  closingSoon: async (params?: {
    limit?: number;
    window?: number;
  }): Promise<ApiResponse<TDashboardClosingSoon>> => {
    try {
      return (await api.get(API_ENDPOINTS.DASHBOARD.CLOSING_SOON, { params }))
        .data;
    } catch (error) {
      console.error(
        "ERROR caught in Dashboard Service => closing-soon:",
        error,
      );
      throw error;
    }
  },
};
