import axios from 'axios'

const API_BASE_URL = 'https://ra3.z31.xyz/v1'

/**
 * 获取地图列表
 * @param {object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {string} params.search - 搜索关键字
 * @param {string} params.player_count - 玩家数量
 * @param {string} params.tags - 标签
 * @param {string} params.ordering - 排序方式
 * @returns {Promise<any>}
 */
export const getMaps = async (params = {}) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/maps/`, {
      params: {
        p: params.page || 1,
        search: params.search || '',
        player_count: params.player_count || '',
        tags: params.tags || '',
        ordering: params.ordering || ''
      }
    })
    return response.data
  } catch (error) {
    console.error('获取地图列表失败:', error)
    throw error
  }
}

/**
 * 获取每周推荐地图
 * @param {object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {string} params.ordering - 排序方式
 * @returns {Promise<any>}
 */
export const getWeeklyTopMaps = async (params = {}) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/maps/`, {
      params: {
        p: params.page || 1,
        format: 'json',
        ordering: params.ordering || ''
      }
    })
    return response.data.results
  } catch (error) {
    console.error('获取地图列表失败:', error)
    throw error
  }
}

/**
 * 获取地图详情
 * @param {number} id - 地图ID
 * @returns {Promise<any>}
 */
export const getMapDetail = async (id) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/maps/${id}/`)
    return response.data
  } catch (error) {
    console.error('获取地图详情失败:', error)
    throw error
  }
}

/**
 * 获取所有标签
 * @returns {Promise<string[]>}
 */
export const getAllTags = async () => {
  try {
    const response = await axios.get(`${API_BASE_URL}/maps/`)
    const tags = new Set()
    response.data.results.forEach(map => {
      map.tags.forEach(tag => tags.add(tag))
    })
    return Array.from(tags)
  } catch (error) {
    console.error('获取标签列表失败:', error)
    throw error
  }
} 