import express from 'express';
import { fetchPushSettings, savePushSettings, testPush, fetchPushHistory } from '../services/pushService';

const router = express.Router();

// 获取推送设置
router.get('/settings/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await fetchPushSettings(symbol);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取推送设置失败' });
  }
});

// 保存推送设置
router.post('/settings/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await savePushSettings(symbol, req.body);
    res.status(200).json({ success: true, message: '设置保存成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '保存推送设置失败' });
  }
});

// 测试推送
router.post('/test', async (req, res) => {
  try {
    const { method, content } = req.body;
    const data = await testPush(method, content);
    res.status(200).json({ success: true, message: '测试推送成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '测试推送失败' });
  }
});

// 获取推送历史
router.get('/history', async (req, res) => {
  try {
    const data = await fetchPushHistory();
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取推送历史失败' });
  }
});

export default router;