import { useMutation, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
import { User } from '../../types/services/user'

type CreateUserPayload = {
  name: string
  email: string
}

const useUsersMutation = () => {
  const queryClient = useQueryClient()
  
  const createUser = useMutation<User, Error, CreateUserPayload>({
    mutationFn: async newUser => {
      const response = await api.post('/users', newUser)
      return response.data
    },
    onSuccess: () => {
      // Optional: refetch 'users' list after success
      queryClient.invalidateQueries({ queryKey: ['users'] })
    }
  })

  return { createUser }
}

export default useUsersMutation
