$ErrorActionPreference = "Stop"

$root = $PSScriptRoot
$backendDir = Join-Path $root "yaoai-comic-studio"
$frontendDir = Join-Path $root "doc\html"
$adminDir = Join-Path $root "yaoai-admin-web"
$mavenCmd = Join-Path $root ".tools\apache-maven-3.9.6\bin\mvn.cmd"

Push-Location $root
try {
    Write-Host "[dev] starting infrastructure from yaoai-comic-studio/docker-compose.yml ..."
    docker compose -f (Join-Path $backendDir "docker-compose.yml") up -d

    Write-Host ""
    Write-Host "[dev] stable local development environment"
    Write-Host "  mysql      : localhost:13306"
    Write-Host "  redis      : localhost:16379"
    Write-Host "  rabbitmq   : localhost:15672"
    Write-Host "  prometheus : localhost:19090"
    Write-Host "  grafana    : localhost:13000"
    Write-Host "  backend    : http://localhost:8081"
    Write-Host "  frontend   : http://localhost:5173"
    Write-Host "  admin-web  : http://localhost:5174"
    Write-Host ""
    Write-Host "[dev] next commands"
    Write-Host "  1. build backend"
    Write-Host "     & `"$mavenCmd`" -f `"$backendDir\pom.xml`" package -pl yaoai-bootstrap -am -DskipTests"
    Write-Host "  2. run backend"
    Write-Host "     java -jar `"$backendDir\yaoai-bootstrap\target\yaoai-bootstrap-0.1.0-SNAPSHOT.jar`""
    Write-Host "  3. run frontend"
    Write-Host "     Set-Location `"$frontendDir`"; npm install; npm run dev"
    Write-Host "  4. run admin-web"
    Write-Host "     Set-Location `"$adminDir`"; npm install; npm run dev"
    Write-Host ""
    Write-Host "[dev] do not run 'docker compose down -v' unless you really want to delete dev data."
}
finally {
    Pop-Location
}
