import client from './client'
import type { AdminUser, AdminMarathon, UserRole, PlatformStats } from '@/types'

export const adminApi = {
  // Users
  listUsers: async (skip = 0, limit = 50, search?: string): Promise<AdminUser[]> => {
    const params: Record<string, unknown> = { skip, limit }
    if (search) params.search = search
    const response = await client.get<AdminUser[]>('/admin/users', { params })
    return response.data
  },

  getUser: async (id: number): Promise<AdminUser> => {
    const response = await client.get<AdminUser>(`/admin/users/${id}`)
    return response.data
  },

  setUserRole: async (id: number, role: UserRole): Promise<AdminUser> => {
    const response = await client.patch<AdminUser>(`/admin/users/${id}/role`, { role })
    return response.data
  },

  deleteUser: async (id: number): Promise<void> => {
    await client.delete(`/admin/users/${id}`)
  },

  // Marathons
  listMarathons: async (skip = 0, limit = 50, search?: string): Promise<AdminMarathon[]> => {
    const params: Record<string, unknown> = { skip, limit }
    if (search) params.search = search
    const response = await client.get<AdminMarathon[]>('/admin/marathons', { params })
    return response.data
  },

  deleteMarathon: async (id: number): Promise<void> => {
    await client.delete(`/admin/marathons/${id}`)
  },

  // Stats
  getStats: async (): Promise<PlatformStats> => {
    const response = await client.get<PlatformStats>('/admin/stats')
    return response.data
  },
}
