import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
import { GamePrize, GamePrizes } from '@/types/services/gamePrize'

interface GamePrizeQueryParams {
  page?: number
  limit?: number
  search?: string
}

export function useGamePrizes(params: GamePrizeQueryParams = {}) {
  const { page = 1, limit = 10, search = '', ...filters } = params

  return useQuery<GamePrizes>({
    queryKey: ['gamePrizes', { page, limit, search, ...filters }],
    queryFn: async () => {
      const queryParams = new URLSearchParams()

      queryParams.append('page', page.toString())
      queryParams.append('limit', limit.toString())

      if (search) {
        queryParams.append('search', search)
      }

      Object.entries(filters).forEach(([key, value]) => {
        if (value !== undefined && value !== null && value !== '') {
          queryParams.append(key, value.toString())
        }
      })

      const res = await api.get(`/marketing/game-prizes?${queryParams.toString()}`)
      return res.data.data
    }
  })
}

export function useGamePrizeById(id: string) {
  return useQuery<GamePrize>({
    queryKey: ['gamePrizes', id],
    queryFn: async () => {
      const res = await api.get(`/marketing/game-prizes/${id}`)
      return res.data.data
    }
  })
}
