import { Router } from 'express';
import { requirePermission } from '../../core/guards/rbac.guard';
import { requireTraceContext } from '../../core/guards/trace-context.guard';
import { RecommendationController } from '../controllers/RecommendationController';

const router = Router();

// 健康检查路由
router.get('/health', async (req, res) => {
  const controller = new RecommendationController();
  const result = await controller.healthCheck();
  res.json(result);
});

// 记录用户行为路由（需要权限）
router.post('/record-behavior', 
  requireTraceContext,
  requirePermission('recommendation:record_behavior'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.recordUserBehavior(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 更新商品属性路由
router.post('/update-item', 
  requireTraceContext,
  requirePermission('recommendation:update_item'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.updateItemAttributes(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 获取推荐结果路由
router.post('/recommendations', 
  requireTraceContext,
  requirePermission('recommendation:get_recommendations'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.getRecommendations(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 获取推荐统计路由
router.get('/stats', 
  requireTraceContext,
  requirePermission('recommendation:read_stats'),
  async (req, res) => {
    const controller = new RecommendationController();
    const { tenantId } = req.query;
    const result = await controller.getRecommendationStats(tenantId as string);
    res.json(result);
  }
);

// 清理缓存路由（仅管理员）
router.post('/cleanup-cache', 
  requireTraceContext,
  requirePermission('recommendation:admin'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.cleanupExpiredCache(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 测试算法路由
router.post('/test-algorithm', 
  requireTraceContext,
  requirePermission('recommendation:test_algorithm'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.testAlgorithm(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 获取算法配置路由
router.get('/algorithm-config', 
  requireTraceContext,
  requirePermission('recommendation:read_config'),
  async (req, res) => {
    const controller = new RecommendationController();
    const { tenantId } = req.query;
    const result = await controller.getAlgorithmConfig(tenantId as string);
    res.json(result);
  }
);

// 初始化服务路由（仅管理员）
router.post('/init', 
  requireTraceContext,
  requirePermission('recommendation:admin'),
  async (req, res) => {
    const controller = new RecommendationController();
    const result = await controller.initRecommendation();
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

export default router;