import { Router } from 'express';
import { query } from 'express-validator';
import { authenticate } from '../middleware/auth.js';
import { asyncHandler } from '../middleware/error.js';
import { validateRequest } from '../middleware/validate.js';
import { WorkDayStatisticsService } from '../services/WorkDayStatisticsService.js';

const router = Router();
const workDayStatisticsService = new WorkDayStatisticsService();

router.use(authenticate);

/**
 * GET /api/dashboard/trend
 * 获取数据趋势（直接调用 Node 统计服务，不再经 Python 转发）
 * 按平台分组返回，每个平台一条曲线
 *
 * 查询参数:
 *   - days: 天数（默认30天）
 *   - account_id: 账号ID（可选，不填则查询所有平台）
 */
router.get(
  '/trend',
  [
    query('days').optional().isInt({ min: 1, max: 30 }).withMessage('days 必须是 1-30 之间的整数'),
    query('account_id').optional().isInt().withMessage('account_id 必须是整数'),
    validateRequest,
  ],
  asyncHandler(async (req, res) => {
    const days = req.query.days ? parseInt(req.query.days as string) : 30;
    const accountId = req.query.account_id ? parseInt(req.query.account_id as string) : undefined;

    const data = await workDayStatisticsService.getTrend(req.user!.userId, {
      days,
      accountId,
    });

    return res.json({ success: true, data });
  })
);

export default router;
