const baseUrl = import.meta.env.VITE_MOSQUITO_API_BASE_URL ?? ''
const apiKey = import.meta.env.VITE_MOSQUITO_API_KEY ?? ''
const userToken = import.meta.env.VITE_MOSQUITO_USER_TOKEN ?? ''

const requestJson = async (url: string) => {
  const response = await fetch(url, {
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': apiKey,
      ...(userToken ? { Authorization: `Bearer ${userToken}` } : {})
    }
  })
  const payload = await response.json().catch(() => ({}))
  if (!response.ok) {
    throw new Error(payload?.message || '请求失败')
  }
  return payload?.data ?? []
}

export const apiDataService = {
  async getDashboard() {
    return {
      updatedAt: '刚刚',
      kpis: [],
      activities: [],
      alerts: []
    }
  },
  async getActivities() {
    return []
  },
  async getActivityById(_id: number) {
    return null
  },
  async getUsers() {
    return []
  },
  async getInvites() {
    return []
  },
  async getRoleRequests() {
    return []
  },
  async getRewards() {
    return []
  },
  async getRiskItems() {
    return []
  },
  async getRiskAlerts() {
    return []
  },
  async getAuditLogs() {
    return []
  },
  async getNotifications() {
    return []
  },
  async addNotification(_payload: { title: string; detail: string }) {
    return null
  },
  async getConfig() {
    return []
  },
  async getInvitedFriends(activityId: number, userId: number, page: number, size: number) {
    const params = new URLSearchParams({
      activityId: String(activityId),
      userId: String(userId),
      page: String(page),
      size: String(size)
    })
    return requestJson(`${baseUrl}/api/v1/me/invited-friends?${params}`)
  }
}
