import { useQuery } from '@tanstack/react-query';
import {
  ReportMetaResponseSchema,
  ReportCarteiraResponseSchema,
  ReportAbcResponseSchema,
  type ReportMetaQuery,
  type ReportAbcQuery,
  type ReportMetaResponse,
  type ReportCarteiraResponse,
  type ReportAbcResponse,
} from '@sar/api-interface';
import { apiFetch } from '../api-client';

export function useReportMeta(params: ReportMetaQuery) {
  const qs = new URLSearchParams({ mes: String(params.mes), ano: String(params.ano) });
  return useQuery<ReportMetaResponse>({
    queryKey: ['reports', 'meta', params],
    queryFn: async () => ReportMetaResponseSchema.parse(await apiFetch(`/reports/meta?${qs}`)),
  });
}

export function useReportCarteira() {
  return useQuery<ReportCarteiraResponse>({
    queryKey: ['reports', 'carteira'],
    queryFn: async () => ReportCarteiraResponseSchema.parse(await apiFetch('/reports/carteira')),
  });
}

export function useReportAbc(params: ReportAbcQuery) {
  const qs = new URLSearchParams();
  if (params.mes != null) qs.set('mes', String(params.mes));
  if (params.ano != null) qs.set('ano', String(params.ano));
  return useQuery<ReportAbcResponse>({
    queryKey: ['reports', 'abc', params],
    queryFn: async () => ReportAbcResponseSchema.parse(await apiFetch(`/reports/abc?${qs}`)),
  });
}
