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

const router = Router();

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

// 单文本处理路由（需要权限）
router.post('/process', 
  requireTraceContext,
  requirePermission('nlp:process'),
  async (req, res) => {
    const controller = new NaturalLanguageProcessingController();
    const result = await controller.processText(req.body);
    res.status(result.status === 'error' ? 500 : 200).json(result);
  }
);

// 批量文本处理路由
router.post('/batch-process', 
  requireTraceContext,
  requirePermission('nlp:batch_process'),
  async (req, res) => {
    const controller = new NaturalLanguageProcessingController();
    const result = await controller.batchProcessTexts(req.body);
    res.status(200).json(result);
  }
);

// 语言检测路由
router.post('/detect-language', 
  requireTraceContext,
  requirePermission('nlp:detect_language'),
  async (req, res) => {
    const controller = new NaturalLanguageProcessingController();
    const result = await controller.detectLanguage(req.body);
    res.json(result);
  }
);

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

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

// 获取支持语言路由
router.get('/supported-languages', 
  requireTraceContext,
  requirePermission('nlp:read_languages'),
  async (req, res) => {
    const controller = new NaturalLanguageProcessingController();
    const result = await controller.getSupportedLanguages();
    res.json(result);
  }
);

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

export default router;