import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';

import { ReposService } from '@/client/services/ReposService';

export function useWatchRepo() {
  const { workspaceName = '', repoName = '' } = useParams();
  const qc = useQueryClient();

  return useMutation({
    mutationFn: () =>
      ReposService.repoWatch({ workspaceName, repoName, requestBody: {} }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'watchers'] });
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'stats'] });
    },
  });
}

export function useUnwatchRepo() {
  const { workspaceName = '', repoName = '' } = useParams();
  const qc = useQueryClient();

  return useMutation({
    mutationFn: () => ReposService.repoUnwatch({ workspaceName, repoName }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'watchers'] });
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'stats'] });
    },
  });
}
