import express from 'express';
import { fetchStopLossAdvice, fetchPositionAdvice, fetchRolloverAlerts, fetchRiskAssessment } from '../services/riskService';

const router = express.Router();

// 获取止损建议
router.get('/stoploss/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await fetchStopLossAdvice(symbol);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取止损建议失败' });
  }
});

// 获取仓位建议
router.post('/position', async (req, res) => {
  try {
    const { capital, risk_tolerance, symbols } = req.body;
    const data = await fetchPositionAdvice(capital, risk_tolerance, symbols);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取仓位建议失败' });
  }
});

// 获取换月预警
router.get('/rollover', async (req, res) => {
  try {
    const data = await fetchRolloverAlerts();
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取换月预警失败' });
  }
});

// 获取风险评估
router.get('/assessment/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await fetchRiskAssessment(symbol);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取风险评估失败' });
  }
});

// 保存风控设置
router.post('/settings', async (req, res) => {
  try {
    const { stop_loss_strategy, risk_tolerance, max_position } = req.body;
    // 模拟保存操作
    await new Promise(resolve => setTimeout(resolve, 200));
    res.status(200).json({ success: true, message: '设置保存成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '保存风控设置失败' });
  }
});

export default router;