import express from 'express';
import { fetchWatchlist, addToWatchlist, removeFromWatchlist, checkWatchlistStatus } from '../services/watchlistService';

const router = express.Router();

// 获取自选列表
router.get('/', async (req, res) => {
  try {
    const data = await fetchWatchlist();
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '获取自选列表失败' });
  }
});

// 添加自选
router.post('/', async (req, res) => {
  try {
    const { symbol, name } = req.body;
    const data = await addToWatchlist(symbol, name);
    res.status(200).json({ success: true, message: '添加成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '添加自选失败' });
  }
});

// 删除自选
router.delete('/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await removeFromWatchlist(symbol);
    res.status(200).json({ success: true, message: '删除成功' });
  } catch (error) {
    res.status(500).json({ success: false, message: '删除自选失败' });
  }
});

// 检查自选状态
router.get('/status/:symbol', async (req, res) => {
  try {
    const { symbol } = req.params;
    const data = await checkWatchlistStatus(symbol);
    res.status(200).json({ success: true, data });
  } catch (error) {
    res.status(500).json({ success: false, message: '检查自选状态失败' });
  }
});

export default router;