/**
 * 活动管理服务
 */
import type { Activity } from '../types/activity'

export interface ApiResponse<T> {
  code: number
  data: T
  message?: string
}

export interface ActivityListQuery {
  page?: number
  size?: number
  status?: string
  keyword?: string
  startDate?: string
  endDate?: string
}

class ActivityService {
  private baseUrl = '/api'

  /**
   * 获取活动列表
   */
  async getActivities(params?: ActivityListQuery): Promise<Activity[]> {
    const searchParams = new URLSearchParams()
    if (params?.page) searchParams.set('page', String(params.page))
    if (params?.size) searchParams.set('size', String(params.size))
    if (params?.status) searchParams.set('status', params.status)
    if (params?.keyword) searchParams.set('keyword', params.keyword)

    const response = await fetch(`${this.baseUrl}/activities?${searchParams}`, {
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<Activity[]>
    if (result.code !== 200) {
      throw new Error(result.message || '获取活动列表失败')
    }
    return result.data
  }

  /**
   * 获取单个活动详情
   */
  async getActivityById(id: number): Promise<Activity | null> {
    const response = await fetch(`${this.baseUrl}/activities/${id}`, {
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<Activity>
    if (result.code !== 200) {
      return null
    }
    return result.data
  }

  /**
   * 创建活动
   */
  async createActivity(data: Partial<Activity>): Promise<number> {
    const response = await fetch(`${this.baseUrl}/activities`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      credentials: 'include',
      body: JSON.stringify(data)
    })
    const result = await response.json() as ApiResponse<number>
    if (result.code !== 200) {
      throw new Error(result.message || '创建活动失败')
    }
    return result.data
  }

  /**
   * 更新活动
   */
  async updateActivity(id: number, data: Partial<Activity>): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      credentials: 'include',
      body: JSON.stringify(data)
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '更新活动失败')
    }
  }

  /**
   * 删除活动
   */
  async deleteActivity(id: number): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}`, {
      method: 'DELETE',
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '删除活动失败')
    }
  }

  /**
   * 发布活动
   */
  async publishActivity(id: number): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/publish`, {
      method: 'POST',
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '发布活动失败')
    }
  }

  /**
   * 暂停活动
   */
  async pauseActivity(id: number): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/pause`, {
      method: 'POST',
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '暂停活动失败')
    }
  }

  /**
   * 恢复活动
   */
  async resumeActivity(id: number): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/resume`, {
      method: 'POST',
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '恢复活动失败')
    }
  }

  /**
   * 结束活动
   */
  async endActivity(id: number): Promise<void> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/end`, {
      method: 'POST',
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<void>
    if (result.code !== 200) {
      throw new Error(result.message || '结束活动失败')
    }
  }

  /**
   * 获取活动统计
   */
  async getActivityStats(id: number): Promise<any> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/stats`, {
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<any>
    if (result.code !== 200) {
      throw new Error(result.message || '获取活动统计失败')
    }
    return result.data
  }

  /**
   * 获取活动图表数据
   */
  async getActivityGraph(id: number): Promise<any> {
    const response = await fetch(`${this.baseUrl}/activities/${id}/graph`, {
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<any>
    if (result.code !== 200) {
      throw new Error(result.message || '获取活动图表失败')
    }
    return result.data
  }

  /**
   * 获取活动排行榜
   */
  async getActivityLeaderboard(id: number, limit?: number): Promise<any[]> {
    const params = limit ? `?limit=${limit}` : ''
    const response = await fetch(`${this.baseUrl}/activities/${id}/leaderboard${params}`, {
      credentials: 'include'
    })
    const result = await response.json() as ApiResponse<any[]>
    if (result.code !== 200) {
      throw new Error(result.message || '获取排行榜失败')
    }
    return result.data
  }
}

export const activityService = new ActivityService()
export default activityService
