import { API_ENDPOINTS, dashboardService } from "@/lib/api";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";

export const useDashboardSummaryQuery = (params?: {
  closing_window?: number;
}) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.DASHBOARD.SUMMARY, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => dashboardService.summary(params),
  });
};

export const useDashboardTrendQuery = (params?: {
  days?: number;
  metric?: "created" | "published" | "awarded";
}) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.DASHBOARD.TREND, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => dashboardService.trend(params),
  });
};

export const useDashboardStatisticsQuery = (params?: { days?: number }) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.DASHBOARD.STATISTICS, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => dashboardService.statistics(params),
    refetchInterval: 60_000,
  });
};

export const useDashboardCountriesQuery = (params?: { limit?: number }) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.DASHBOARD.COUNTRIES, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => dashboardService.countries(params),
  });
};

export const useDashboardNoticeTypesQuery = () => {
  return useQuery({
    queryKey: [API_ENDPOINTS.DASHBOARD.NOTICE_TYPES],
    queryFn: () => dashboardService.noticeTypes(),
  });
};

export const useDashboardClosingSoonQuery = (params?: {
  limit?: number;
  window?: number;
}) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.DASHBOARD.CLOSING_SOON, params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => dashboardService.closingSoon(params),
  });
};
