import { useMutation, useQueryClient } from '@tanstack/react-query'
import { updateRole } from '../api/updateRole'
import type { UpdateRolRequest } from '../api/types'
import { rolesQueryKey } from './useRoles'

interface UpdateRoleVars {
  codigo: string
  payload: UpdateRolRequest
}

export function useUpdateRole() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: ({ codigo, payload }: UpdateRoleVars) => updateRole(codigo, payload),
    onSuccess: (_data, vars) => {
      void qc.invalidateQueries({ queryKey: rolesQueryKey })
      void qc.invalidateQueries({ queryKey: ['roles', vars.codigo] })
    },
  })
}
