import { useMutation, useQueryClient } from '@tanstack/react-query';
import type { CreateRepoParams } from '@/client/models/CreateRepoParams';
import { ReposService } from '@/client/services/ReposService';

export function useCreateRepo(workspaceName: string | null) {
  const qc = useQueryClient();

  return useMutation({
    mutationFn: (params: CreateRepoParams) => {
      if (!workspaceName) throw new Error('No workspace name');
      return ReposService.repoCreate({ workspaceName, requestBody: params });
    },
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['workspace', workspaceName, 'repos'] });
    },
  });
}
