// src/hooks/queries/useSuggestedCorrectionsQuery.ts
import { useQuery } from '@tanstack/react-query';
import { getSuggestedCorrections } from '../../services/apiClient';
import { queryKeys } from '../../config/queryKeys';
import type { SuggestedCorrection } from '../../types';

/**
 * Query hook for fetching user-submitted corrections (admin feature).
 *
 * Returns a list of pending corrections that need admin review/approval.
 *
 * Uses TanStack Query for automatic caching and refetching (ADR-0005 Phase 5).
 *
 * @returns TanStack Query result with SuggestedCorrection[] data
 */
export const useSuggestedCorrectionsQuery = () => {
  return useQuery({
    queryKey: queryKeys.suggestedCorrections(),
    queryFn: async (): Promise<SuggestedCorrection[]> => {
      const response = await getSuggestedCorrections();

      if (!response.ok) {
        const error = await response.json().catch(() => ({
          message: `Request failed with status ${response.status}`,
        }));
        throw new Error(error.message || 'Failed to fetch suggested corrections');
      }

      return response.json();
    },
    staleTime: 1000 * 60, // 1 minute - corrections change moderately
  });
};
