import { Request, Response } from 'express';
import { logger } from '../../utils/logger';
import { AIService } from '../../services/AIService';

/**
 * [UX_FE_CLEANUP_02] AI 控制台控制器
 */
export class AIController {
  /**
   * 处理 AI 对话/命令
   */
  static async chat(req: Request, res: Response) {
    const { message } = req.body;
    const { tenantId, userId, traceId } = (req as any).traceContext;

    if (!message) {
      return res.status(400).json({ success: false, error: 'Message is required' });
    }

    try {
      logger.info(`[AIConsole] Received message from user ${userId}: ${message}`);

      // 调用 AGI 服务处理指令
      // 这里可以根据指令类型分发，比如 "分析订单", "调价建议", "生成图片"
      const aiResponse = await AIService.processCommand(tenantId, userId, message);

      res.json({
        success: true,
        data: {
          reply: aiResponse.text,
          actions: aiResponse.suggestedActions, // 可执行的建议按钮
          causalChain: aiResponse.causalChain, // 因果链展示
          traceId
        }
      });
    } catch (err: any) {
      logger.error(`[AIConsole] Chat failed: ${err.message}`);
      res.status(500).json({ success: false, error: 'AI processing failed' });
    }
  }
}
