# PCB AFM Simulation System - Windows Deployment Script
# Usage: .\deploy.ps1
# Prerequisites: Python 3.10+, Node.js 18+, Git

param(
    [switch]$SkipFrontendBuild,
    [switch]$SkipBackendDeps,
    [string]$BackendPort = "8000",
    [string]$FrontendPort = "5173"
)

$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$BackendDir = Join-Path $ProjectRoot "web\backend"
$FrontendDir = Join-Path $ProjectRoot "web\frontend"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "PCB AFM Simulation System - Deployment" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Step 1: Check prerequisites
Write-Host "[1/6] Checking prerequisites..." -ForegroundColor Yellow

try {
    $pythonVersion = python --version 2>&1
    Write-Host "  Python: $pythonVersion" -ForegroundColor Green
} catch {
    Write-Host "  ERROR: Python not found. Install Python 3.10+" -ForegroundColor Red
    exit 1
}

try {
    $nodeVersion = node --version 2>&1
    Write-Host "  Node.js: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "  ERROR: Node.js not found. Install Node.js 18+" -ForegroundColor Red
    exit 1
}

# Step 2: Backend dependencies
if (-not $SkipBackendDeps) {
    Write-Host ""
    Write-Host "[2/6] Installing backend dependencies..." -ForegroundColor Yellow
    Push-Location $BackendDir
    try {
        pip install -r requirements.txt
        Write-Host "  Backend dependencies installed." -ForegroundColor Green
    } finally {
        Pop-Location
    }
} else {
    Write-Host ""
    Write-Host "[2/6] Skipping backend dependencies (--SkipBackendDeps)." -ForegroundColor Yellow
}

# Step 3: Initialize database
Write-Host ""
Write-Host "[3/6] Initializing database..." -ForegroundColor Yellow
Push-Location $BackendDir
try {
    python -c "from app.database import init_db; init_db(); print('Database initialized.')"
    Write-Host "  Database initialized." -ForegroundColor Green
} catch {
    Write-Host "  WARNING: Database init failed: $_" -ForegroundColor Yellow
} finally {
    Pop-Location
}

# Step 4: Frontend build
if (-not $SkipFrontendBuild) {
    Write-Host ""
    Write-Host "[4/6] Building frontend..." -ForegroundColor Yellow
    Push-Location $FrontendDir
    try {
        if (-not (Test-Path "node_modules")) {
            Write-Host "  Installing npm packages..." -ForegroundColor Gray
            npm install
        }
        Write-Host "  Running build..." -ForegroundColor Gray
        npm run build
        Write-Host "  Frontend built successfully." -ForegroundColor Green
    } finally {
        Pop-Location
    }
} else {
    Write-Host ""
    Write-Host "[4/6] Skipping frontend build (--SkipFrontendBuild)." -ForegroundColor Yellow
}

# Step 5: Environment configuration
Write-Host ""
Write-Host "[5/6] Checking environment configuration..." -ForegroundColor Yellow
$envFile = Join-Path $BackendDir ".env"
if (-not (Test-Path $envFile)) {
    Write-Host "  Creating .env file from template..." -ForegroundColor Gray
    @"
DATABASE_URL=sqlite:///./afm_sim.db
KIMI_API_KEY=your_kimi_api_key_here
KIMI_MODEL=k3
KIMI_BASE_URL=https://api.kimi.com/coding/v1
MAX_PARALLEL_TASKS=2
"@ | Set-Content -Path $envFile -Encoding UTF8
    Write-Host "  .env created. Please edit with your Kimi API key." -ForegroundColor Yellow
} else {
    Write-Host "  .env file exists." -ForegroundColor Green
}

# Step 6: Start services
Write-Host ""
Write-Host "[6/6] Starting services..." -ForegroundColor Yellow
Write-Host ""
Write-Host "  Backend:  http://localhost:$BackendPort" -ForegroundColor Cyan
Write-Host "  Frontend: http://localhost:$FrontendPort" -ForegroundColor Cyan
Write-Host ""
Write-Host "  To start backend (in backend dir):" -ForegroundColor Gray
Write-Host "    python -m uvicorn app.main:app --host 0.0.0.0 --port $BackendPort" -ForegroundColor Gray
Write-Host ""
Write-Host "  To start frontend dev server (in frontend dir):" -ForegroundColor Gray
Write-Host "    npm run dev" -ForegroundColor Gray
Write-Host ""
Write-Host "  Or use Docker:" -ForegroundColor Gray
Write-Host "    docker-compose up -d" -ForegroundColor Gray
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deployment complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
