import express from 'express';
import { fetchMarketOverview, fetchMarketDetail, fetchKlineData, fetchMarketHotspots, fetchRiskAlerts } from '../services/marketService';

const router = express.Router();

// 获取市场概览
router.get('/overview', async (req, res) => {
  try {
    const data = await fetchMarketOverview();
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取市场概览失败' });
  }
});

// 获取品种详情
router.get('/detail/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await fetchMarketDetail(symbol);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取品种详情失败' });
  }
});

// 获取K线数据
router.get('/klines/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const { period = '1H' } = req.query;
    const data = await fetchKlineData(symbol, period as string);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取K线数据失败' });
  }
});

// 获取市场热点
router.get('/hotspots', async (req, res) => {
  try {
    const data = await fetchMarketHotspots();
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取市场热点失败' });
  }
});

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

export default router;