import { useQuery, useMutation, useQueryClient, keepPreviousData } from '@tanstack/react-query';
import { apiClient } from '../lib/api';

/**
 * Hook to fetch accessible video edit state for QC review
 */
export function useAccessibleVideoEditState(jobId: string, language: string) {
  return useQuery({
    queryKey: ['jobs', jobId, 'accessible-video', language, 'edit-state'],
    queryFn: () => apiClient.getAccessibleVideoEditState(jobId, language),
    enabled: !!jobId && !!language,
    staleTime: 0,
    refetchOnWindowFocus: false,
    retry: false,
    placeholderData: keepPreviousData,
  });
}

/**
 * Hook to update a pause point's adjusted timing
 */
export function useUpdatePausePoint() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      jobId,
      language,
      cueIndex,
      adjustedMs,
    }: {
      jobId: string;
      language: string;
      cueIndex: number;
      adjustedMs: number;
    }) => apiClient.updatePausePoint(jobId, language, cueIndex, adjustedMs),
    onSuccess: (_, { jobId, language }) => {
      queryClient.invalidateQueries({
        queryKey: ['jobs', jobId, 'accessible-video', language, 'edit-state'],
      });
    },
  });
}

/**
 * Hook to queue TTS regeneration for specific cues
 */
export function useQueueTTSRegeneration() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      jobId,
      language,
      cueIndices,
    }: {
      jobId: string;
      language: string;
      cueIndices: number[];
    }) => apiClient.queueTTSRegeneration(jobId, language, cueIndices),
    onSuccess: (_, { jobId, language }) => {
      queryClient.invalidateQueries({
        queryKey: ['jobs', jobId, 'accessible-video', language, 'edit-state'],
      });
    },
  });
}

/**
 * Hook to remove a cue from the TTS regeneration queue
 */
export function useRemoveTTSRegeneration() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      jobId,
      language,
      cueIndex,
    }: {
      jobId: string;
      language: string;
      cueIndex: number;
    }) => apiClient.removeTTSRegeneration(jobId, language, cueIndex),
    onSuccess: (_, { jobId, language }) => {
      queryClient.invalidateQueries({
        queryKey: ['jobs', jobId, 'accessible-video', language, 'edit-state'],
      });
    },
  });
}

/**
 * Hook to trigger accessible video re-render with QC changes
 */
export function useRerenderAccessibleVideo() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({
      jobId,
      language,
      whisperRefine = false,
    }: {
      jobId: string;
      language: string;
      whisperRefine?: boolean;
    }) => apiClient.rerenderAccessibleVideo(jobId, language, whisperRefine),
    onSuccess: (_, { jobId, language }) => {
      // Invalidate job and edit state queries
      queryClient.invalidateQueries({ queryKey: ['jobs', jobId] });
      queryClient.invalidateQueries({
        queryKey: ['jobs', jobId, 'accessible-video', language, 'edit-state'],
      });
      queryClient.invalidateQueries({ queryKey: ['jobs'] });
    },
  });
}
