import { apiClient } from './client'
import type { Bet, BetDetail, CreateBetData, BetCategory } from '@/types'

export const betsApi = {
  getBets: async (params?: { skip?: number; limit?: number; category?: BetCategory }): Promise<Bet[]> => {
    const response = await apiClient.get<Bet[]>('/api/v1/bets', { params })
    return response.data
  },

  getBet: async (id: number): Promise<BetDetail> => {
    const response = await apiClient.get<BetDetail>(`/api/v1/bets/${id}`)
    return response.data
  },

  createBet: async (data: CreateBetData): Promise<Bet> => {
    const response = await apiClient.post<Bet>('/api/v1/bets', data)
    return response.data
  },

  acceptBet: async (id: number): Promise<Bet> => {
    const response = await apiClient.post<Bet>(`/api/v1/bets/${id}/accept`)
    return response.data
  },

  settleBet: async (id: number, winnerId: number): Promise<BetDetail> => {
    const response = await apiClient.post<BetDetail>(`/api/v1/bets/${id}/settle`, { winner_id: winnerId })
    return response.data
  },

  cancelBet: async (id: number): Promise<void> => {
    await apiClient.delete(`/api/v1/bets/${id}`)
  },

  getMyCreatedBets: async (): Promise<Bet[]> => {
    const response = await apiClient.get<Bet[]>('/api/v1/bets/my/created')
    return response.data
  },

  getMyAcceptedBets: async (): Promise<Bet[]> => {
    const response = await apiClient.get<Bet[]>('/api/v1/bets/my/accepted')
    return response.data
  },

  getMyActiveBets: async (): Promise<Bet[]> => {
    const response = await apiClient.get<Bet[]>('/api/v1/bets/my/active')
    return response.data
  },

  getMyHistory: async (): Promise<BetDetail[]> => {
    const response = await apiClient.get<BetDetail[]>('/api/v1/bets/my/history')
    return response.data
  },
}
