# -*- coding: utf-8 -*-
"""
TSP智能助手综合管理平台
"""

import sys
import os
import logging
import threading
import asyncio
from datetime import datetime

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

def setup_logging():
    """设置日志"""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler('logs/dashboard.log', encoding='utf-8'),
            logging.StreamHandler()
        ]
    )

def start_websocket_server():
    """启动WebSocket服务器"""
    try:
        from src.web.websocket_server import WebSocketServer
        server = WebSocketServer(host="localhost", port=8765)
        server.run()
    except Exception as e:
        print(f"WebSocket服务器启动失败: {e}")

def main():
    """主函数"""
    print("=" * 60)
    print("TSP智能助手 - 综合管理平台")
    print("=" * 60)
    print(f"启动时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print()
    
    # 设置日志
    setup_logging()
    logger = logging.getLogger(__name__)
    
    try:
        # 检查必要目录
        os.makedirs('logs', exist_ok=True)
        os.makedirs('data', exist_ok=True)
        
        logger.info("正在启动TSP智能助手综合管理平台...")
        
        # 跳过系统检查，直接启动（避免重复初始化）
        logger.info("跳过系统检查，直接启动服务...")
        
        # 导入并启动Flask应用
        from src.web.app import app
        
        print()
        print("访问地址:")
        print("  主页: http://localhost:5000")
        print("  预警管理: http://localhost:5000/alerts")
        print("  实时对话: http://localhost:5000/chat")
        print("  WebSocket: ws://localhost:8765")
        print()
        print("按 Ctrl+C 停止服务")
        print("=" * 60)
        
        # 在单独线程中启动WebSocket服务器
        websocket_thread = threading.Thread(target=start_websocket_server, daemon=True)
        websocket_thread.start()
        
        # 启动Flask应用
        app.run(
            debug=False,
            host='0.0.0.0',
            port=5000,
            threaded=True
        )
        
    except KeyboardInterrupt:
        print("\n正在停止服务...")
        logger.info("用户手动停止服务")
    except Exception as e:
        print(f"启动失败: {e}")
        logger.error(f"启动失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()
