@echo off
chcp 65001 >nul
title Jarvis 一键启动

echo ==========================================
echo   Jarvis 个人 AI 助理 - 一键启动
echo ==========================================
echo.

REM --- 检查 uv ---
where uv >nul 2>&1
if %errorlevel% neq 0 (
    echo [错误] 未找到 uv，请先安装: https://github.com/astral-sh/uv
    echo.
    echo 安装命令:
    echo   pip install uv
    echo.
    pause
    exit /b 1
)

REM --- 检查 npm ---
where npm >nul 2>&1
if %errorlevel% neq 0 (
    echo [错误] 未找到 npm，请先安装 Node.js: https://nodejs.org
    pause
    exit /b 1
)

REM --- 检查 .env ---
if not exist backend\.env (
    echo [提示] 首次运行，需要配置 API Key
    echo [提示] 请编辑 backend\.env 文件，填入:
    echo   ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxx
    echo.
)

REM --- 安装后端依赖 ---
echo [1/4] 安装后端依赖...
cd /d "%~dp0backend"
uv sync --quiet
if %errorlevel% neq 0 (
    echo [错误] 后端依赖安装失败
    pause
    exit /b 1
)
echo [OK] 后端依赖安装完成

REM --- 安装前端依赖 ---
echo.
echo [2/4] 安装前端依赖...
cd /d "%~dp0frontend"
call npm install >nul 2>&1
if %errorlevel% neq 0 (
    echo [错误] 前端依赖安装失败
    pause
    exit /b 1
)
echo [OK] 前端依赖安装完成

REM --- 启动后端 ---
echo.
echo [3/4] 启动后端服务 (端口 8000)...
cd /d "%~dp0backend"
start "Jarvis-Backend" cmd /c "uv run uvicorn app.main:app --reload --port 8000"

REM --- 等待后端启动 ---
echo       等待后端启动...
timeout /t 5 /nobreak >nul

REM --- 启动前端 ---
echo.
echo [4/4] 启动前端服务 (端口 5173)...
cd /d "%~dp0frontend"
start "Jarvis-Frontend" cmd /c "npm run dev"

REM --- 完成 ---
echo.
echo ==========================================
echo   启动完成!
echo.
echo   后端: http://localhost:8000
echo   前端: http://localhost:5173
echo   API文档: http://localhost:8000/docs
echo ==========================================
echo.
echo 提示:
echo   - 首次使用请先注册账号
echo   - 对话前请在 backend\.env 填入 API Key
echo   - 关闭时请关闭两个终端窗口
echo.
pause
