<#
.SYNOPSIS
  Docker Compose helper for the Airport Cyber Resilience Simulator V2.
.EXAMPLE
  .\sim.ps1 up | down | logs | rebuild | restart | status | shell | open | clean
#>
[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-v2'
$Url = 'http://localhost:8000'
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 "Simulator V2 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; $h = docker inspect --format '{{.State.Health.Status}}' $Service 2>$null; if ($h) { Write-Host "Health: $h" -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 V2 - Docker helper
Usage: .\sim.ps1 <command>
  up  down  logs  rebuild  restart  status  shell  open  clean
"@ -ForegroundColor Gray }
}
