import axios from '@/utils/axios'

class PushService {
  // 注册设备
  async registerDevice(deviceInfo) {
    try {
      const response = await axios.post('/app-api/push/register', deviceInfo)
      return response.data
    } catch (error) {
      console.error('设备注册失败:', error)
      throw error
    }
  }

  // 注销设备
  async unregisterDevice(clientId) {
    try {
      const response = await axios.post('/app-api/push/unregister', { clientId })
      return response.data
    } catch (error) {
      console.error('设备注销失败:', error)
      throw error
    }
  }

  // 更新设备标签
  async updateDeviceTags(clientId, tags) {
    try {
      const response = await axios.post('/app-api/push/tags', {
        clientId,
        tags
      })
      return response.data
    } catch (error) {
      console.error('更新设备标签失败:', error)
      throw error
    }
  }

  // 发送测试推送
  async sendTestPush(clientId, message) {
    try {
      const response = await axios.post('/app-api/push/test', {
        clientId,
        title: message.title || '测试推送',
        content: message.content || '这是一条测试推送消息',
        payload: message.payload || {}
      })
      return response.data
    } catch (error) {
      console.error('发送测试推送失败:', error)
      throw error
    }
  }

  // 获取推送历史
  async getPushHistory(userId, page = 1, size = 20) {
    try {
      const response = await axios.get('/app-api/push/history', {
        params: { userId, page, size }
      })
      return response.data
    } catch (error) {
      console.error('获取推送历史失败:', error)
      throw error
    }
  }

  // 标记消息为已读
  async markAsRead(messageId) {
    try {
      const response = await axios.post('/app-api/push/mark-read', { messageId })
      return response.data
    } catch (error) {
      console.error('标记消息已读失败:', error)
      throw error
    }
  }

  // 获取未读消息数量
  async getUnreadCount(userId) {
    try {
      const response = await axios.get('/app-api/push/unread-count', {
        params: { userId }
      })
      return response.data
    } catch (error) {
      console.error('获取未读消息数量失败:', error)
      throw error
    }
  }
}

export default new PushService()