# DashCaddy Deployment Script
# Deploys changes from Dev (E:) to Prod (C:)

$DevRoot = "E:\CaddyCerts\sites"
$ProdRoot = "C:\Caddy"
$ErrorActionPreference = "Stop"

Write-Host "Deploying DashCaddy Changes..." -ForegroundColor Cyan

# 1. Pre-deploy validation - syntax check all JS files
Write-Host "Validating JavaScript syntax..." -ForegroundColor Yellow
$syntaxErrors = 0
Get-ChildItem "$DevRoot\dashcaddy-api" -Filter "*.js" | ForEach-Object {
    $result = & node -c $_.FullName 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Syntax error in $($_.Name): $result"
        $syntaxErrors++
    }
}
Get-ChildItem "$DevRoot\dashcaddy-api\routes" -Filter "*.js" | ForEach-Object {
    $result = & node -c $_.FullName 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Syntax error in routes/$($_.Name): $result"
        $syntaxErrors++
    }
}
if ($syntaxErrors -gt 0) {
    Write-Error "Aborting deploy: $syntaxErrors syntax error(s) found."
    exit 1
}
Write-Host "  All files pass syntax check." -ForegroundColor Green

# 2. Update Frontend
Write-Host "Updating Dashboard UI..." -ForegroundColor Yellow
if (Test-Path "$ProdRoot\sites\status") {
    # Build frontend bundles
    Write-Host "Building frontend JavaScript..." -ForegroundColor Yellow
    Set-Location "$DevRoot\status"
    & npm install
    & node build.js
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Frontend build failed!"
        exit 1
    }
    Write-Host "  Frontend build complete." -ForegroundColor Green
    
    # Copy all necessary files
    Copy-Item "$DevRoot\status\index.html" "$ProdRoot\sites\status\index.html" -Force
    Copy-Item "$DevRoot\status\dist\*" "$ProdRoot\sites\status\dist\" -Force
    Set-Location $ProdRoot
} else {
    Write-Warning "Target status folder not found. Skipping UI update."
}

# 3. Update Backend API
Write-Host "Updating API Server..." -ForegroundColor Yellow
if (Test-Path "$ProdRoot\sites\dashcaddy-api") {
    # Copy all JS files, package files, API spec
    Get-ChildItem "$DevRoot\dashcaddy-api" -Filter "*.js" | Copy-Item -Destination "$ProdRoot\sites\dashcaddy-api\" -Force
    Copy-Item "$DevRoot\dashcaddy-api\package.json" "$ProdRoot\sites\dashcaddy-api\" -Force
    Copy-Item "$DevRoot\dashcaddy-api\package-lock.json" "$ProdRoot\sites\dashcaddy-api\" -Force -ErrorAction SilentlyContinue
    Copy-Item "$DevRoot\dashcaddy-api\openapi.yaml" "$ProdRoot\sites\dashcaddy-api\" -Force -ErrorAction SilentlyContinue

    # Copy route modules
    if (!(Test-Path "$ProdRoot\sites\dashcaddy-api\routes")) {
        New-Item -ItemType Directory -Path "$ProdRoot\sites\dashcaddy-api\routes" | Out-Null
    }
    Copy-Item "$DevRoot\dashcaddy-api\routes\*" "$ProdRoot\sites\dashcaddy-api\routes\" -Force

    # 4. Rebuild and Restart
    Write-Host "Rebuilding API Container..." -ForegroundColor Yellow
    Set-Location $ProdRoot
    docker-compose up -d --build dashcaddy-api

    # 5. Post-deploy health check
    Write-Host "Waiting for container startup..." -ForegroundColor Yellow
    Start-Sleep -Seconds 5
    try {
        $health = Invoke-RestMethod -Uri "http://localhost:3001/health" -TimeoutSec 10 -ErrorAction Stop
        if ($health.status -eq 'ok') {
            Write-Host "  Health check passed." -ForegroundColor Green
        } else {
            Write-Warning "Health check returned unexpected status: $($health.status)"
        }
    } catch {
        Write-Warning "Health check failed: $_"
        Write-Warning "Check logs with: docker logs --tail 30 dashcaddy-api"
    }
} else {
    Write-Warning "Target API folder not found. Skipping API update."
}

Write-Host "Deployment Complete! Refresh your dashboard." -ForegroundColor Green
