import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { adminFetch } from './admin-api';

// ── Types ──────────────────────────────────────────────────────────────

export interface DatabaseThresholds {
  connectionPoolWarning: number;
  connectionPoolCritical: number;
  queryDurationWarning: number;
  queryDurationCritical: number;
}

export interface ThresholdConfig {
  database: DatabaseThresholds;
}

// ── Query Hooks ────────────────────────────────────────────────────────

export function useThresholds() {
  return useQuery({
    queryKey: ['admin', 'thresholds'],
    queryFn: () => adminFetch<ThresholdConfig>('/thresholds'),
  });
}

// ── Mutation Hooks ─────────────────────────────────────────────────────

export function useUpdateThresholds() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (config: ThresholdConfig) =>
      adminFetch<ThresholdConfig>('/thresholds', {
        method: 'PUT',
        body: JSON.stringify(config),
      }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['admin', 'thresholds'] });
    },
  });
}
