import { useMutation } from '@tanstack/react-query';

import { useApiClient } from '@/lib/apiClient';

interface TrackActionParams {
  entityType: 'video' | 'image';
  entityId: string;
  action: 'download'; // Restricted to 'download' for now, can add more actions in the future
  batchId?: string | null;
  clipId?: string | null;
}

export function useTrackGenerationAction() {
  const apiClient = useApiClient();

  const mutation = useMutation({
    mutationFn: async (params: TrackActionParams): Promise<void> => {
      await apiClient
        .POST('/api/video_gen/action', {
          body: {
            entity_type: params.entityType,
            entity_id: params.entityId,
            action: params.action,
            batch_id: params.batchId ?? null,
            clip_id: params.clipId ?? null,
          },
        })
        .catch(() => {
          // Silently catch errors - don't want to disrupt user experience
        });
    },
  });

  return {
    trackGenerationAction: mutation.mutate,
    mutation,
  };
}
