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

const router = Router();

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

// 图像识别路由（需要权限）
router.post('/recognize', 
  requireTraceContext,
  requirePermission('image:recognize'),
  async (req, res) => {
    const controller = new ImageRecognitionController();
    const result = await controller.recognizeImage(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 批量图像识别路由
router.post('/batch-recognize', 
  requireTraceContext,
  requirePermission('image:batch_recognize'),
  async (req, res) => {
    const controller = new ImageRecognitionController();
    const result = await controller.batchRecognizeImages(req.body);
    res.status(200).json(result);
  }
);

// 获取识别历史路由
router.get('/history', 
  requireTraceContext,
  requirePermission('image:read_history'),
  async (req, res) => {
    const controller = new ImageRecognitionController();
    const { tenantId, limit } = req.query;
    const result = await controller.getRecognitionHistory(
      tenantId as string, 
      limit ? parseInt(limit as string) : 50
    );
    res.json(result);
  }
);

// 获取模型统计路由
router.get('/stats', 
  requireTraceContext,
  requirePermission('image:read_stats'),
  async (req, res) => {
    const controller = new ImageRecognitionController();
    const { tenantId } = req.query;
    const result = await controller.getModelStats(tenantId as string);
    res.json(result);
  }
);

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

export default router;