import { GamePrizeRequest } from '@/types/services/gamePrize'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'

export const useGamePrizesMutation = () => {
  const queryClient = useQueryClient()

  const createGamePrize = useMutation({
    mutationFn: async (newGamePrize: GamePrizeRequest) => {
      const response = await api.post('/marketing/game-prizes', newGamePrize)
      return response.data
    },
    onSuccess: () => {
      toast.success('GamePrize created successfully!')
      queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
    }
  })

  const updateGamePrize = useMutation({
    mutationFn: async ({ id, payload }: { id: string; payload: GamePrizeRequest }) => {
      const response = await api.put(`/marketing/game-prizes/${id}`, payload)
      return response.data
    },
    onSuccess: () => {
      toast.success('GamePrize updated successfully!')
      queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
    }
  })

  const deleteGamePrize = useMutation({
    mutationFn: async (id: string) => {
      const response = await api.delete(`/marketing/game-prizes/${id}`)
      return response.data
    },
    onSuccess: () => {
      toast.success('GamePrize deleted successfully!')
      queryClient.invalidateQueries({ queryKey: ['gamePrizes'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
    }
  })

  return { createGamePrize, updateGamePrize, deleteGamePrize }
}
