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

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

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

  return useMutation({
    mutationFn: (body: {
      name: string;
      description: string;
      visibility: string;
      default_branch: string;
    }) => ReposService.repoUpdate({ workspaceName, repoName, requestBody: body }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName] });
    },
  });
}
