@echo off
REM 情报数据处理系统 - 简化启动脚本
REM 功能: Python环境检测 + 系统启动
REM 作者: AI Assistant
REM 版本: 1.0
REM 日期: 2025-10-29

setlocal enabledelayedexpansion

REM 设置颜色
for /f %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
set "GREEN=%ESC%[32m"
set "RED=%ESC%[31m"
set "YELLOW=%ESC%[33m"
set "CYAN=%ESC%[36m"
set "RESET=%ESC%[0m"

REM 配置变量
set "CONDA_ENV_NAME=intelligence_env"
set "PROJECT_PATH=%~dp0.."
set "PYTHON_VERSION=3.11"

echo %CYAN%===============================================%RESET%
echo %CYAN%    情报数据处理系统启动器%RESET%
echo %CYAN%===============================================%RESET%
echo.

REM 检查项目路径
if not exist "%PROJECT_PATH%\main.py" (
    echo %RED%错误: 项目路径不存在或main.py文件未找到%RESET%
    echo %YELLOW%当前路径: %PROJECT_PATH%%RESET%
    pause
    exit /b 1
)

echo %GREEN%✓ 项目路径检查通过%RESET%

REM 检查Python是否安装
echo %CYAN%检查Python环境...%RESET%
python --version >nul 2>&1
if %errorLevel% neq 0 (
    echo %RED%Python未安装或未添加到PATH%RESET%
    echo %YELLOW%正在尝试检测Anaconda...%RESET%
    
    REM 检查Anaconda
    where conda >nul 2>&1
    if %errorLevel% neq 0 (
        echo %RED%Anaconda未安装%RESET%
        echo %YELLOW%请安装Python或Anaconda后重试%RESET%
        echo %CYAN%下载地址: https://www.python.org/downloads/%RESET%
        echo %CYAN%或: https://www.anaconda.com/products/distribution%RESET%
        pause
        exit /b 1
    ) else (
        echo %GREEN%✓ 检测到Anaconda%RESET%
        conda --version
    )
) else (
    echo %GREEN%✓ Python已安装%RESET%
    python --version
)

REM 检查Conda环境
echo %CYAN%检查Conda环境: %CONDA_ENV_NAME%%RESET%
conda env list | findstr /i "%CONDA_ENV_NAME%" >nul 2>&1
if %errorLevel% neq 0 (
    echo %YELLOW%环境不存在，正在创建...%RESET%
    conda create -n %CONDA_ENV_NAME% python=%PYTHON_VERSION% -y
    if %errorLevel% neq 0 (
        echo %RED%环境创建失败%RESET%
        pause
        exit /b 1
    )
    echo %GREEN%✓ 环境创建成功%RESET%
) else (
    echo %GREEN%✓ 环境已存在%RESET%
)

REM 激活环境
echo %CYAN%激活Conda环境...%RESET%
call conda activate %CONDA_ENV_NAME%
if %errorLevel% neq 0 (
    echo %RED%环境激活失败%RESET%
    pause
    exit /b 1
)
echo %GREEN%✓ 环境激活成功%RESET%

REM 检查依赖
echo %CYAN%检查Python依赖...%RESET%
if exist "%PROJECT_PATH%\requirements.txt" (
    echo %YELLOW%安装/更新依赖包...%RESET%
    pip install -r "%PROJECT_PATH%\requirements.txt" --quiet
    if %errorLevel% neq 0 (
        echo %YELLOW%依赖安装失败，尝试继续运行...%RESET%
    ) else (
        echo %GREEN%✓ 依赖安装完成%RESET%
    )
) else (
    echo %YELLOW%未找到requirements.txt，跳过依赖安装%RESET%
)

REM 切换到项目目录
echo %CYAN%切换到项目目录: %PROJECT_PATH%%RESET%
cd /d "%PROJECT_PATH%"

REM 检查配置文件
if not exist "config.py" (
    echo %YELLOW%警告: 未找到config.py配置文件%RESET%
    echo %CYAN%将使用默认配置运行%RESET%
)

REM 显示启动信息
echo.
echo %GREEN%===============================================%RESET%
echo %GREEN%    启动情报数据处理系统%RESET%
echo %GREEN%===============================================%RESET%
echo.
echo %CYAN%环境信息:%RESET%
echo   Conda环境: %CONDA_ENV_NAME%
echo   项目路径: %PROJECT_PATH%
echo   Python版本: 
python --version
echo.
echo %YELLOW%按 Ctrl+C 停止系统%RESET%
echo.

REM 启动系统
echo %CYAN%启动情报数据处理系统主程序...%RESET%
python main.py

echo.
echo %CYAN%情报数据处理系统已停止%RESET%
pause
