import { useQuery } from '@tanstack/react-query';
import { useInject } from '@/lib/di/hooks/useInject';
import { LEAGUE_STEWARDING_SERVICE_TOKEN } from '@/lib/di/tokens';
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
import { ApiError } from '@/lib/api/base/ApiError';

export function useProtestDetail(leagueId: string, protestId: string, enabled: boolean = true) {
  const leagueStewardingService = useInject(LEAGUE_STEWARDING_SERVICE_TOKEN);

  const queryResult = useQuery({
    queryKey: ['protestDetail', leagueId, protestId],
    queryFn: async () => {
      const result = await leagueStewardingService.getProtestDetailViewModel(leagueId, protestId);
      if (result.isErr()) {
        const error = result.getError();
        throw new ApiError(error.message, 'SERVER_ERROR', { timestamp: new Date().toISOString() });
      }
      return result.unwrap();
    },
    enabled: enabled && !!leagueId && !!protestId,
  });

  return enhanceQueryResult(queryResult);
}
