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

import type { CreateReleaseParams } from '@/client/models/CreateReleaseParams';
import { ReposService } from '@/client/services/ReposService';

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

  return useMutation({
    mutationFn: (params: CreateReleaseParams) =>
      ReposService.repoCreateRelease({ workspaceName, repoName, requestBody: params }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['repo', workspaceName, repoName, 'releases'] });
    },
  });
}
