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

export const sportEventsApi = {
  // Public endpoints (no auth required)
  getPublicEvents: 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/sport-events/public${queryParams.toString() ? `?${queryParams}` : ''}`
    const response = await apiClient.get<SportEvent[]>(url)
    return response.data
  },

  getPublicEventWithGrid: async (eventId: number): Promise<SportEventWithBets> => {
    const response = await apiClient.get<SportEventWithBets>(`/api/v1/sport-events/public/${eventId}`)
    return response.data
  },

  // Authenticated endpoints
  getUpcomingEvents: 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/sport-events${queryParams.toString() ? `?${queryParams}` : ''}`
    const response = await apiClient.get<SportEvent[]>(url)
    return response.data
  },

  getEventWithGrid: async (eventId: number): Promise<SportEventWithBets> => {
    const response = await apiClient.get<SportEventWithBets>(`/api/v1/sport-events/${eventId}`)
    return response.data
  },

  // Comments
  getEventComments: async (eventId: number, params?: { skip?: number; limit?: number }): Promise<EventCommentsResponse> => {
    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/sport-events/${eventId}/comments${queryParams.toString() ? `?${queryParams}` : ''}`
    const response = await apiClient.get<EventCommentsResponse>(url)
    return response.data
  },

  addEventComment: async (eventId: number, content: string): Promise<EventComment> => {
    const response = await apiClient.post<EventComment>(`/api/v1/sport-events/${eventId}/comments`, { content })
    return response.data
  },
}
