import { apiClient } from './client'
import type { SportEvent } from '@/types/sport-event'

export interface AdminSettings {
  id: number
  default_house_commission_percent: number
  default_min_bet_amount: number
  default_max_bet_amount: number
  default_min_spread: number
  default_max_spread: number
  spread_increment: number
  platform_name: string
  maintenance_mode: boolean
}

export interface CreateEventData {
  sport: string
  home_team: string
  away_team: string
  official_spread: number
  game_time: string
  venue?: string
  league?: string
  min_spread?: number
  max_spread?: number
  min_bet_amount?: number
  max_bet_amount?: number
}

export const adminApi = {
  getSettings: async (): Promise<AdminSettings> => {
    const response = await apiClient.get<AdminSettings>('/api/v1/admin/settings')
    return response.data
  },

  updateSettings: async (settings: Partial<AdminSettings>): Promise<AdminSettings> => {
    const response = await apiClient.patch<AdminSettings>('/api/v1/admin/settings', settings)
    return response.data
  },

  createEvent: async (eventData: CreateEventData): Promise<SportEvent> => {
    const response = await apiClient.post<SportEvent>('/api/v1/admin/events', eventData)
    return response.data
  },

  getEvents: async (params?: { skip?: number; limit?: number }): Promise<SportEvent[]> => {
    const queryParams = new URLSearchParams()
    if (params?.skip !== undefined) queryParams.append('skip', params.skip.toString())
    if (params?.limit !== undefined) queryParams.append('limit', params.limit.toString())

    const url = `/api/v1/admin/events${queryParams.toString() ? `?${queryParams}` : ''}`
    const response = await apiClient.get<SportEvent[]>(url)
    return response.data
  },

  deleteEvent: async (eventId: number): Promise<{ message: string }> => {
    const response = await apiClient.delete<{ message: string }>(`/api/v1/admin/events/${eventId}`)
    return response.data
  },
}
