param(
  [string]$Registry = $env:DOCKER_REGISTRY,
  [string]$Namespace = $env:REG_NAMESPACE,
  [string]$ImageName = $(if($env:IMAGE_NAME){$env:IMAGE_NAME}else{"nexus-material"}),
  [string]$LocalImageName = $(if($env:LOCAL_IMAGE_NAME){$env:LOCAL_IMAGE_NAME}else{"nexus-material-share-app"}),
  [string]$Tag = $(if($env:IMAGE_TAG){$env:IMAGE_TAG}else{"latest"}),
  [string]$Username = $env:REG_USERNAME,
  [string]$Password = $env:REG_PASSWORD
)

$ErrorActionPreference = "Stop"

if (-not $Registry) { throw "DOCKER_REGISTRY not set" }
if (-not $Username -or -not $Password) { throw "REG_USERNAME/REG_PASSWORD not set" }
if (-not $Namespace) { throw "REG_NAMESPACE not set" }

docker compose build app

$localRef = "$LocalImageName:$Tag"
$remoteRef = "$Registry/$Namespace/$ImageName:$Tag"

# Secure login without echoing password
$Password | docker login $Registry -u $Username --password-stdin
docker tag $localRef $remoteRef
docker push $remoteRef

Write-Host "Pushed $remoteRef"
