# -*- coding: utf-8 -*-
"""
TSP助手预警管理Web应用
提供预警系统的Web界面和API接口
重构版本 - 使用蓝图架构
"""

import sys
import os
import logging
from datetime import datetime, timedelta
from typing import Dict, Any

from flask import Flask, render_template, request, jsonify, send_from_directory, make_response
from flask_cors import CORS

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# 导入核心模块
from src.core.database import db_manager
from src.core.models import Conversation, Alert, WorkOrder
from src.core.query_optimizer import query_optimizer
from src.web.service_manager import service_manager

# 导入蓝图
from src.web.blueprints.alerts import alerts_bp
from src.web.blueprints.workorders import workorders_bp
from src.web.blueprints.conversations import conversations_bp
from src.web.blueprints.knowledge import knowledge_bp
from src.web.blueprints.monitoring import monitoring_bp
from src.web.blueprints.system import system_bp
from src.web.blueprints.feishu_sync import feishu_sync_bp
from src.web.blueprints.core import core_bp

# 配置日志
logger = logging.getLogger(__name__)

# 抑制 /api/health 的访问日志
werkzeug_logger = logging.getLogger('werkzeug')

class HealthLogFilter(logging.Filter):
    def filter(self, record):
        try:
            msg = record.getMessage()
            return '/api/health' not in msg
        except Exception:
            return True

werkzeug_logger.addFilter(HealthLogFilter())

# 创建Flask应用
app = Flask(__name__)
CORS(app)

# 配置上传文件夹
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max file size

# 使用统一的服务管理器

# 注册蓝图
app.register_blueprint(alerts_bp)
app.register_blueprint(workorders_bp)
app.register_blueprint(conversations_bp)
app.register_blueprint(knowledge_bp)
app.register_blueprint(monitoring_bp)
app.register_blueprint(system_bp)
app.register_blueprint(feishu_sync_bp)
app.register_blueprint(core_bp)

# 页面路由
@app.route('/')
@app.route('/dashboard')
def index():
    """主页 - 综合管理平台"""
    response = make_response(render_template('dashboard.html'))
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    return response

@app.route('/alerts')
def alerts():
    """预警管理页面"""
    return render_template('index.html')

@app.route('/chat')
def chat():
    """实时对话页面 (WebSocket版本)"""
    return render_template('chat.html')

@app.route('/chat-http')
def chat_http():
    """实时对话页面 (HTTP版本)"""
    return render_template('chat_http.html')

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    """提供上传文件的下载服务"""
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

# ============================================================================
# 核心API路由 - 已迁移到蓝图
# ============================================================================
# 健康检查、预警规则、监控状态等核心功能已迁移到 core 蓝图
# 分析数据相关功能也已迁移到 core 蓝图

# ============================================================================
# 实时对话相关路由
# ============================================================================
@app.route('/api/chat/session', methods=['POST'])
def create_chat_session():
    """创建对话会话"""
    try:
        data = request.get_json()
        user_id = data.get('user_id', 'anonymous')
        work_order_id = data.get('work_order_id')

        session_id = service_manager.get_chat_manager().create_session(user_id, work_order_id)

        return jsonify({
            "success": True,
            "session_id": session_id,
            "message": "会话创建成功"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/message', methods=['POST'])
def send_chat_message():
    """发送聊天消息"""
    try:
        data = request.get_json()
        session_id = data.get('session_id')
        message = data.get('message')

        if not session_id or not message:
            return jsonify({"error": "缺少必要参数"}), 400

        result = service_manager.get_chat_manager().process_message(session_id, message)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/history/<session_id>')
def get_chat_history(session_id):
    """获取对话历史"""
    try:
        history = service_manager.get_chat_manager().get_session_history(session_id)
        return jsonify({
            "success": True,
            "history": history
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/work-order', methods=['POST'])
def create_work_order():
    """创建工单"""
    try:
        data = request.get_json()
        session_id = data.get('session_id')
        title = data.get('title')
        description = data.get('description')
        category = data.get('category', '技术问题')
        priority = data.get('priority', 'medium')

        if not session_id or not title or not description:
            return jsonify({"error": "缺少必要参数"}), 400

        result = service_manager.get_chat_manager().create_work_order(session_id, title, description, category, priority)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/work-order/<int:work_order_id>')
def get_work_order_status(work_order_id):
    """获取工单状态"""
    try:
        result = service_manager.get_chat_manager().get_work_order_status(work_order_id)
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/session/<session_id>', methods=['DELETE'])
def end_chat_session(session_id):
    """结束对话会话"""
    try:
        success = service_manager.get_chat_manager().end_session(session_id)
        return jsonify({
            "success": success,
            "message": "会话已结束" if success else "结束会话失败"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/chat/sessions')
def get_active_sessions():
    """获取活跃会话列表"""
    try:
        # 确保chat_manager已初始化
        manager = service_manager.get_chat_manager()
        sessions = manager.get_active_sessions()
        return jsonify({
            "success": True,
            "sessions": sessions
        })
    except Exception as e:
        logger.error(f"获取活跃会话失败: {e}")
        return jsonify({"error": str(e)}), 500

# ============================================================================
# Agent相关API
# ============================================================================
@app.route('/api/agent/status')
def get_agent_status():
    """获取Agent状态"""
    try:
        status = service_manager.get_agent_assistant().get_agent_status()
        return jsonify({"success": True, **status})
    except Exception as e:
        # 返回默认状态，避免500错误
        return jsonify({
            "success": False,
            "status": "inactive",
            "active_goals": 0,
            "available_tools": 0,
            "error": "Agent服务暂时不可用"
        })

@app.route('/api/agent/action-history')
def get_agent_action_history():
    """获取Agent动作执行历史"""
    try:
        limit = request.args.get('limit', 50, type=int)
        history = service_manager.get_agent_assistant().get_action_history(limit)
        return jsonify({
            "success": True,
            "history": history,
            "count": len(history)
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/trigger-sample', methods=['POST'])
def trigger_sample_action():
    """触发示例动作"""
    try:
        import asyncio
        result = asyncio.run(service_manager.get_agent_assistant().trigger_sample_actions())
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/clear-history', methods=['POST'])
def clear_agent_history():
    """清空Agent执行历史"""
    try:
        result = service_manager.get_agent_assistant().clear_execution_history()
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/llm-stats')
def get_llm_stats():
    """获取LLM使用统计"""
    try:
        stats = service_manager.get_agent_assistant().get_llm_usage_stats()
        return jsonify({
            "success": True,
            "stats": stats
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/toggle', methods=['POST'])
def toggle_agent_mode():
    """切换Agent模式"""
    try:
        data = request.get_json()
        enabled = data.get('enabled', True)
        success = service_manager.get_agent_assistant().toggle_agent_mode(enabled)
        return jsonify({
            "success": success,
            "message": f"Agent模式已{'启用' if enabled else '禁用'}"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/monitoring/start', methods=['POST'])
def start_agent_monitoring():
    """启动Agent监控"""
    try:
        success = service_manager.get_agent_assistant().start_proactive_monitoring()
        return jsonify({
            "success": success,
            "message": "Agent监控已启动" if success else "启动失败"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/monitoring/stop', methods=['POST'])
def stop_agent_monitoring():
    """停止Agent监控"""
    try:
        success = service_manager.get_agent_assistant().stop_proactive_monitoring()
        return jsonify({
            "success": success,
            "message": "Agent监控已停止" if success else "停止失败"
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/proactive-monitoring', methods=['POST'])
def proactive_monitoring():
    """主动监控检查"""
    try:
        result = service_manager.get_agent_assistant().run_proactive_monitoring()
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/intelligent-analysis', methods=['POST'])
def intelligent_analysis():
    """智能分析"""
    try:
        analysis = service_manager.get_agent_assistant().run_intelligent_analysis()
        return jsonify({"success": True, "analysis": analysis})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/chat', methods=['POST'])
def agent_chat():
    """Agent对话接口"""
    try:
        data = request.get_json()
        message = data.get('message', '')
        context = data.get('context', {})

        if not message:
            return jsonify({"error": "消息不能为空"}), 400

        # 使用Agent助手处理消息
        agent_assistant = service_manager.get_agent_assistant()

        # 模拟Agent处理（实际应该调用真正的Agent处理逻辑）
        import asyncio
        result = asyncio.run(agent_assistant.process_message_agent(
            message=message,
            user_id=context.get('user_id', 'admin'),
            work_order_id=None,
            enable_proactive=True
        ))

        return jsonify({
            "success": True,
            "response": result.get('response', 'Agent已处理您的请求'),
            "actions": result.get('actions', []),
            "status": result.get('status', 'completed')
        })
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# ============================================================================
# Agent 工具统计与自定义工具
# ============================================================================
@app.route('/api/agent/tools/stats')
def get_agent_tools_stats():
    try:
        agent_assistant = service_manager.get_agent_assistant()
        tools = agent_assistant.agent_core.tool_manager.get_available_tools()
        performance = agent_assistant.agent_core.tool_manager.get_tool_performance_report()
        return jsonify({
            "success": True,
            "tools": tools,
            "performance": performance
        })
    except Exception as e:
        # 返回默认工具列表，避免500错误
        return jsonify({
            "success": False,
            "tools": [],
            "performance": {},
            "error": "工具统计暂时不可用"
        })

@app.route('/api/agent/tools/execute', methods=['POST'])
def execute_agent_tool():
    """执行指定的Agent工具"""
    try:
        data = request.get_json() or {}
        tool_name = data.get('tool') or data.get('name')
        parameters = data.get('parameters') or {}
        if not tool_name:
            return jsonify({"error": "缺少工具名称tool"}), 400

        import asyncio
        result = asyncio.run(service_manager.get_agent_assistant().agent_core.tool_manager.execute_tool(tool_name, parameters))
        return jsonify(result)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/tools/register', methods=['POST'])
def register_custom_tool():
    """注册自定义工具（仅登记元数据，函数为占位符）"""
    try:
        data = request.get_json() or {}
        name = data.get('name')
        description = data.get('description', '')
        if not name:
            return jsonify({"error": "缺少工具名称"}), 400

        def _placeholder_tool(**kwargs):
            return {"message": f"自定义工具 {name} 已登记（占位），当前不可执行", "params": kwargs}

        service_manager.get_agent_assistant().agent_core.tool_manager.register_tool(
            name,
            _placeholder_tool,
            metadata={"description": description, "custom": True}
        )
        return jsonify({"success": True, "message": "工具已注册"})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/agent/tools/unregister/<name>', methods=['DELETE'])
def unregister_custom_tool(name):
    try:
        success = service_manager.get_agent_assistant().agent_core.tool_manager.unregister_tool(name)
        return jsonify({"success": success})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# ============================================================================
# 分析相关API - 已迁移到 core 蓝图
# ============================================================================

@app.route('/api/analytics/export')
def export_analytics():
    """导出分析报告"""
    try:
        # 生成Excel报告（使用数据库真实数据）
        analytics = query_optimizer.get_analytics_optimized(30)

        # 创建工作簿
        from openpyxl import Workbook
        from openpyxl.styles import Font
        wb = Workbook()
        ws = wb.active
        ws.title = "分析报告"

        # 添加标题
        ws['A1'] = 'TSP智能助手分析报告'
        ws['A1'].font = Font(size=16, bold=True)

        # 添加工单统计
        ws['A3'] = '工单统计'
        ws['A3'].font = Font(bold=True)
        ws['A4'] = '总工单数'
        ws['B4'] = analytics['workorders']['total']
        ws['A5'] = '待处理'
        ws['B5'] = analytics['workorders']['open']
        ws['A6'] = '已解决'
        ws['B6'] = analytics['workorders']['resolved']

        # 保存文件
        report_path = 'uploads/analytics_report.xlsx'
        os.makedirs('uploads', exist_ok=True)
        wb.save(report_path)

        from flask import send_file
        return send_file(report_path, as_attachment=True, download_name='analytics_report.xlsx')

    except Exception as e:
        return jsonify({"error": str(e)}), 500

# ============================================================================
# 车辆数据相关API
# ============================================================================
@app.route('/api/vehicle/data')
def get_vehicle_data():
    """获取车辆数据"""
    try:
        vehicle_id = request.args.get('vehicle_id')
        vehicle_vin = request.args.get('vehicle_vin')
        data_type = request.args.get('data_type')
        limit = request.args.get('limit', 10, type=int)

        vehicle_mgr = service_manager.get_vehicle_manager()
        if vehicle_vin:
            data = vehicle_mgr.get_vehicle_data_by_vin(vehicle_vin, data_type, limit)
        elif vehicle_id:
            data = vehicle_mgr.get_vehicle_data(vehicle_id, data_type, limit)
        else:
            data = vehicle_mgr.search_vehicle_data(limit=limit)

        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/vehicle/data/vin/<vehicle_vin>/latest')
def get_latest_vehicle_data_by_vin(vehicle_vin):
    """按VIN获取车辆最新数据"""
    try:
        data = service_manager.get_vehicle_manager().get_latest_vehicle_data_by_vin(vehicle_vin)
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/vehicle/data/<vehicle_id>/latest')
def get_latest_vehicle_data(vehicle_id):
    """获取车辆最新数据"""
    try:
        data = service_manager.get_vehicle_manager().get_latest_vehicle_data(vehicle_id)
        return jsonify(data)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/vehicle/data/<vehicle_id>/summary')
def get_vehicle_summary(vehicle_id):
    """获取车辆数据摘要"""
    try:
        summary = service_manager.get_vehicle_manager().get_vehicle_summary(vehicle_id)
        return jsonify(summary)
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/vehicle/data', methods=['POST'])
def add_vehicle_data():
    """添加车辆数据"""
    try:
        data = request.get_json()
        success = service_manager.get_vehicle_manager().add_vehicle_data(
            vehicle_id=data['vehicle_id'],
            data_type=data['data_type'],
            data_value=data['data_value'],
            vehicle_vin=data.get('vehicle_vin')
        )
        return jsonify({"success": success, "message": "数据添加成功" if success else "添加失败"})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/api/vehicle/init-sample-data', methods=['POST'])
def init_sample_vehicle_data():
    """初始化示例车辆数据"""
    try:
        success = service_manager.get_vehicle_manager().add_sample_vehicle_data()
        return jsonify({"success": success, "message": "示例数据初始化成功" if success else "初始化失败"})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

# ============================================================================
# API测试相关接口
# ============================================================================
@app.route('/api/test/connection', methods=['POST'])
def test_api_connection():
    """测试API连接"""
    try:
        data = request.get_json()
        api_provider = data.get('api_provider', 'openai')
        api_base_url = data.get('api_base_url', '')
        api_key = data.get('api_key', '')
        model_name = data.get('model_name', 'qwen-turbo')

        # 这里可以调用LLM客户端进行连接测试
        # 暂时返回模拟结果

        return jsonify({
            "success": True,
            "message": f"API连接测试成功 - {api_provider}",
            "response_time": "150ms",
            "model_status": "可用"
        })
    except Exception as e:
        return jsonify({"success": False, "error": str(e)}), 500

@app.route('/api/test/model', methods=['POST'])
def test_model_response():
    """测试模型回答"""
    try:
        data = request.get_json()
        test_message = data.get('test_message', '你好，请简单介绍一下你自己')

        # 这里可以调用LLM客户端进行回答测试
        # 暂时返回模拟结果
        return jsonify({
            "success": True,
            "test_message": test_message,
            "response": "你好！我是TSP智能助手，基于大语言模型构建的智能客服系统。我可以帮助您解决车辆相关问题，提供技术支持和服务。",
            "response_time": "1.2s",
            "tokens_used": 45
        })
    except Exception as e:
        return jsonify({"success": False, "error": str(e)}), 500

# ============================================================================
# 应用启动配置
# ============================================================================
# 飞书同步功能已合并到主页面，不再需要单独的路由

if __name__ == '__main__':
    import time
    app.config['START_TIME'] = time.time()
    app.config['SERVER_PORT'] = 5000
    app.config['WEBSOCKET_PORT'] = 8765
    app.run(debug=True, host='0.0.0.0', port=5000)
