@echo off
chcp 65001 >nul
title Jarvis 配置向导

echo ==========================================
echo   Jarvis 个人 AI 助理 - 配置向导
echo ==========================================
echo.

REM --- 选择 LLM 提供商 ---
echo 请选择 LLM 提供商:
echo   [1] OpenAI (GPT-4o)        - 默认推荐
echo   [2] DeepSeek (deepseek-chat) - 性价比高
echo   [3] Claude (Anthropic)       - 强大稳定
echo   [4] Ollama (本地模型)         - 完全免费
echo.
set /p PROVIDER_CHOICE=请选择 (1-4), 默认1:

if "%PROVIDER_CHOICE%"=="" set PROVIDER_CHOICE=1
if "%PROVIDER_CHOICE%"=="1" goto :openai
if "%PROVIDER_CHOICE%"=="2" goto :deepseek
if "%PROVIDER_CHOICE%"=="3" goto :claude
if "%PROVIDER_CHOICE%"=="4" goto :ollama
goto :openai

:openai
echo.
echo [选择] OpenAI (GPT-4o)
echo.
set /p OPENAI_API_KEY=请输入 OpenAI API Key (sk-...):
if "%OPENAI_API_KEY%"=="" (
    echo [错误] API Key 不能为空
    pause
    exit /b 1
)
goto :write_env

:deepseek
echo.
echo [选择] DeepSeek
echo.
set /p OPENAI_API_KEY=请输入 DeepSeek API Key:
if "%OPENAI_API_KEY%"=="" (
    echo [错误] API Key 不能为空
    pause
    exit /b 1
)
goto :write_env

:claude
echo.
echo [选择] Claude (Anthropic)
echo.
set /p ANTHROPIC_API_KEY=请输入 Anthropic API Key (sk-ant-...):
if "%ANTHROPIC_API_KEY%"=="" (
    echo [错误] API Key 不能为空
    pause
    exit /b 1
)
goto :write_env

:ollama
echo.
echo [选择] Ollama (本地模型)
echo 请确保 Ollama 已启动 (ollama serve)
echo.
set /p OLLAMA_MODEL_IN=请输入模型名 (留空默认 llama3):
if "%OLLAMA_MODEL_IN%"=="" set OLLAMA_MODEL=llama3
goto :write_env

:write_env
echo.
echo [配置] 写入 backend\.env...

(
echo # =============================================
echo # Jarvis 后端配置
echo # =============================================
echo.
echo APP_NAME=Jarvis
echo APP_VERSION=0.1.0
echo DEBUG=true
echo SECRET_KEY=jarvis-secret-key-change-in-production
echo.
echo CORS_ORIGINS=http://localhost:5173,http://localhost:3000
echo.
echo # LLM 配置
) > backend\.env

if "%PROVIDER_CHOICE%"=="1" (
    (
    echo LLM_PROVIDER=openai
    echo OPENAI_API_KEY=%OPENAI_API_KEY%
    echo OPENAI_MODEL=gpt-4o
    echo OPENAI_BASE_URL=https://api.openai.com/v1
    ) >> backend\.env
) else if "%PROVIDER_CHOICE%"=="2" (
    (
    echo LLM_PROVIDER=deepseek
    echo OPENAI_API_KEY=%OPENAI_API_KEY%
    echo OPENAI_BASE_URL=https://api.deepseek.com/v1
    ) >> backend\.env
) else if "%PROVIDER_CHOICE%"=="3" (
    (
    echo LLM_PROVIDER=claude
    echo ANTHROPIC_API_KEY=%ANTHROPIC_API_KEY%
    echo CLAUDE_MODEL=claude-sonnet-4-20250514
    ) >> backend\.env
) else (
    (
    echo LLM_PROVIDER=ollama
    echo OLLAMA_BASE_URL=http://localhost:11434
    echo OLLAMA_MODEL=%OLLAMA_MODEL%
    ) >> backend\.env
)

(
echo.
echo # 数据库
echo DATABASE_URL=sqlite+aiosqlite:///./data/jarvis.db
echo.
echo # ChromaDB
echo CHROMA_PERSIST_DIR=./data/chroma
echo.
echo # JWT
echo ACCESS_TOKEN_EXPIRE_MINUTES=1440
echo.
echo # 上传
echo UPLOAD_DIR=./data/uploads
echo MAX_UPLOAD_SIZE=52428800
echo.
echo # 定时任务
echo SCHEDULER_ENABLED=true
echo DAILY_PLAN_TIME=00:00
echo FORUM_SCAN_INTERVAL_MINUTES=30
echo.
echo DATA_DIR=./data
) >> backend\.env

echo [OK] 配置完成

REM --- 检查 uv ---
echo.
echo [检查] 验证环境...
where uv >nul 2>&1
if %errorlevel% neq 0 (
    echo [警告] 未找到 uv，请运行: pip install uv
    echo        或访问: https://github.com/astral-sh/uv
) else (
    echo [OK] uv 已安装
)

where npm >nul 2>&1
if %errorlevel% neq 0 (
    echo [警告] 未找到 npm，请安装 Node.js: https://nodejs.org
) else (
    echo [OK] npm 已安装
)

echo.
echo ==========================================
echo   配置完成!
echo.
echo   LLM: %PROVIDER_CHOICE%
echo.
echo   下一步: 双击 start.bat 启动服务
echo ==========================================
pause
