###############################################################################
# pod-start.ps1 — Build image, create pod, start containers
#
# Usage:   .\pod-start.ps1
#          .\pod-start.ps1 -ServerIP 192.168.1.50
#          .\pod-start.ps1 -HlsSize 4g -DlSize 4g
#
# Requires: Podman installed, Podman Machine running
###############################################################################

param(
    [string]$ServerIP   = "127.0.0.1",
    [string]$HlsSize    = "2g",
    [string]$DlSize     = "2g",
    [string]$ImageName  = "o11v4",
    [string]$PodName    = "o11v4-pod"
)

$ErrorActionPreference = "Stop"

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  o11v4 Podman Pod Setup" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# ── Clean up any existing pod ────────────────────────────────────────────────
podman pod exists $PodName 2>$null
if ($LASTEXITCODE -eq 0) {
    Write-Host "Removing existing pod '$PodName'..." -ForegroundColor Yellow
    podman pod stop $PodName 2>$null
    podman pod rm -f $PodName 2>$null
}

# ── Build the image ─────────────────────────────────────────────────────────
Write-Host ""
Write-Host "[1/4] Building image '$ImageName'..." -ForegroundColor Green
podman build -t $ImageName .
Write-Host "      Image built successfully." -ForegroundColor Green

# ── Create the pod ──────────────────────────────────────────────────────────
# All containers in the pod share a network namespace (localhost).
# --add-host redirects licensing domains to the license server.
Write-Host ""
Write-Host "[2/4] Creating pod '$PodName'..." -ForegroundColor Green
podman pod create `
    --name $PodName `
    --add-host "lic.cryptolive.one:$ServerIP" `
    --add-host "lic.bitmaster.cc:$ServerIP" `
    -p 8484:8484 `
    -p 8080:80 `
    -p 8443:443 `
    -p 5454:5454
Write-Host "      Pod created." -ForegroundColor Green

# ── Start the license server container ──────────────────────────────────────
Write-Host ""
Write-Host "[3/4] Starting license server container..." -ForegroundColor Green
podman run -d `
    --pod $PodName `
    --name licserver `
    -e "SERVER_IP=$ServerIP" `
    $ImageName `
    /entrypoint-licserver.sh
Write-Host "      License server started." -ForegroundColor Green

# ── Start the o11v4 app container ───────────────────────────────────────────
Write-Host ""
Write-Host "[4/4] Starting o11v4 app container..." -ForegroundColor Green
podman run -d `
    --pod $PodName `
    --name o11v4-app `
    -e "SERVER_IP=$ServerIP" `
    --tmpfs "/home/o11/hls:size=$HlsSize,mode=1777" `
    --tmpfs "/home/o11/dl:size=$DlSize,mode=1777" `
    $ImageName `
    /entrypoint-app.sh
Write-Host "      o11v4 app started." -ForegroundColor Green

# ── Done ────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  All running!" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "  App:    http://localhost:8484" -ForegroundColor White
Write-Host "  Login:  admin / admin" -ForegroundColor White
Write-Host ""
Write-Host "  Useful commands:" -ForegroundColor Gray
Write-Host "    podman pod ps                          # pod status" -ForegroundColor Gray
Write-Host "    podman ps --pod                        # all containers" -ForegroundColor Gray
Write-Host "    podman logs -f licserver               # license server logs" -ForegroundColor Gray
Write-Host "    podman logs -f o11v4-app               # app logs" -ForegroundColor Gray
Write-Host "    podman exec -it o11v4-app bash          # shell into app" -ForegroundColor Gray
Write-Host "    .\pod-stop.ps1                         # stop everything" -ForegroundColor Gray
Write-Host ""
