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

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

export function useUpdateWorkspaceSettings() {
  const { workspaceName = '' } = useParams();
  const qc = useQueryClient();

  return useMutation({
    mutationFn: (body: {
      default_branch_name: string;
      default_repo_visibility: string;
      allow_member_invites: boolean;
      allow_public_repos: boolean;
      require_two_factor: boolean;
      issue_tracking_enabled: boolean;
      pull_requests_enabled: boolean;
      wiki_enabled: boolean;
    }) => WorkspacesService.workspaceUpdateSettings({ workspaceName, requestBody: body }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['workspace', workspaceName, 'settings'] });
    },
  });
}
