import { useQuery } from '@tanstack/react-query';
import {
  RepDashboardSchema,
  SupervisorDashboardSchema,
  type RepDashboard,
  type SupervisorDashboard,
} from '@sar/api-interface';
import { apiFetch } from '../api-client';

export function useRepDashboard() {
  return useQuery<RepDashboard>({
    queryKey: ['dashboard', 'rep'],
    queryFn: async () => {
      const raw = await apiFetch('/dashboard/rep');
      return RepDashboardSchema.parse(raw);
    },
    staleTime: 5 * 60 * 1000,
  });
}

export function useSupervisorDashboard() {
  return useQuery<SupervisorDashboard>({
    queryKey: ['dashboard', 'supervisor'],
    queryFn: async () => {
      const raw = await apiFetch('/dashboard/supervisor');
      return SupervisorDashboardSchema.parse(raw);
    },
    staleTime: 30 * 1000, // 30s — simula near-real-time até C6 (SSE)
    refetchInterval: 30 * 1000,
  });
}
