<#
.SYNOPSIS
  Convenience wrapper around 'docker compose' for the Airport Cyber Resilience Simulator.

.DESCRIPTION
  Run from the project root. Requires Docker Desktop to be installed and running.

.EXAMPLE
  .\sim.ps1 up          # build (if needed) and start in the background
  .\sim.ps1 down        # stop and remove containers
  .\sim.ps1 logs        # tail service logs
  .\sim.ps1 rebuild     # clean no-cache rebuild, then start
  .\sim.ps1 status      # show container + health state
  .\sim.ps1 open        # open the kiosk in the default browser
#>

[CmdletBinding()]
param(
    [Parameter(Position = 0)]
    [ValidateSet('up', 'down', 'logs', 'rebuild', 'restart', 'status', 'shell', 'open', 'clean', 'help')]
    [string]$Command = 'help'
)

$ErrorActionPreference = 'Stop'
$Service = 'airport-cyber-sim'
$Url = 'http://localhost:8000'

# Always operate from the directory this script lives in (the project root).
Set-Location -Path $PSScriptRoot

function Assert-Docker {
    if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
        Write-Host "Docker is not on PATH. Install Docker Desktop for Windows and ensure it is running." -ForegroundColor Red
        exit 1
    }
    docker info *> $null
    if (-not $?) {
        Write-Host "Docker Desktop does not appear to be running. Start it and try again." -ForegroundColor Red
        exit 1
    }
}

switch ($Command) {
    'up' {
        Assert-Docker
        docker compose up --build -d
        Write-Host "Airport Cyber Resilience Simulator running at $Url" -ForegroundColor Green
    }
    'down' {
        Assert-Docker
        docker compose down
    }
    'logs' {
        Assert-Docker
        docker compose logs -f $Service
    }
    'rebuild' {
        Assert-Docker
        docker compose build --no-cache
        docker compose up -d
        Write-Host "Rebuilt and running at $Url" -ForegroundColor Green
    }
    'restart' {
        Assert-Docker
        docker compose restart $Service
    }
    'status' {
        Assert-Docker
        docker compose ps
        $health = docker inspect --format '{{.State.Health.Status}}' $Service 2>$null
        if ($health) { Write-Host "Health: $health" -ForegroundColor Cyan }
    }
    'shell' {
        Assert-Docker
        docker compose exec $Service /bin/bash
    }
    'open' {
        Start-Process $Url
    }
    'clean' {
        Assert-Docker
        docker compose down --rmi local --remove-orphans
    }
    default {
        Write-Host @"
Airport Cyber Resilience Simulator - Docker helper

Usage: .\sim.ps1 <command>

  up        Build (if needed) and start in the background
  down      Stop and remove containers
  logs      Tail service logs
  rebuild   Clean no-cache rebuild, then start
  restart   Restart the service
  status    Show container status and health
  shell     Open a shell inside the running container
  open      Open the kiosk ($Url) in the default browser
  clean     Stop and remove containers + local image
"@ -ForegroundColor Gray
    }
}
