labels:
  role: builder
  backend: local

skip_clone: true

steps:
  checkout:
    image: powershell
    commands:
      - if (-not $env:CI_REPO_CLONE_URL) { throw 'CI_REPO_CLONE_URL missing; set the Forgejo-backed clone URL in Woodpecker.' }
      - $repoRoot = Join-Path (Get-Location).Path 'repo'
      - if (Test-Path $repoRoot) { Remove-Item -Recurse -Force $repoRoot }
      - git clone --branch main --single-branch $env:CI_REPO_CLONE_URL $repoRoot
      - Set-Location $repoRoot
      - if ($env:CI_COMMIT_SHA) { git checkout --detach $env:CI_COMMIT_SHA }

  repo_sanity:
    image: powershell
    depends_on:
      - checkout
    commands:
      - Set-Location repo
      - git rev-parse --is-inside-work-tree
      - if (-not (Test-Path 'UnrealHyperTwist/UnrealHyperTwist.uproject')) { throw 'Missing UnrealHyperTwist/UnrealHyperTwist.uproject' }
      - if (-not (Test-Path 'UnrealHyperTwist/Source/UnrealHyperTwist')) { throw 'Missing UnrealHyperTwist source module root' }
      - if (-not (Test-Path 'docs')) { throw 'Missing docs directory' }
      - Get-ChildItem 'UnrealHyperTwist/Source/UnrealHyperTwist/Public' -Recurse | Select-Object -First 20 FullName
    when:
      - event: [push, pull_request, manual, tag]

  website_frontend_validation:
    image: powershell
    depends_on:
      - repo_sanity
    commands:
      - Set-Location repo/website
      - node --version
      - npm --version
      - npm ci
      - npm run type-check
      - npm test
      - npm run build
    when:
      - event: [push, pull_request, manual, tag]

  website_auth_validation:
    image: powershell
    depends_on:
      - repo_sanity
    commands:
      - Set-Location repo/website/server
      - node --version
      - npm --version
      - npm ci
      - npm run type-check
      - npm test
    when:
      - event: [push, pull_request, manual, tag]

  website_live_deploy:
    image: powershell
    depends_on:
      - website_frontend_validation
      - website_auth_validation
    commands:
      - Set-Location repo/website
      - $identityFile = if ($env:HYPERTWIST_VPS_SSH_KEY_PATH) { $env:HYPERTWIST_VPS_SSH_KEY_PATH } else { 'C:\ScriptoriumAI\ops\ssh\scriptorium_vps_ed25519' }
      - $manifestPath = if ($env:HYPERTWIST_SAME_ORIGIN_MANIFEST) { $env:HYPERTWIST_SAME_ORIGIN_MANIFEST } else { 'deploy/hypertwist.same-origin.preview.bundle.example.json' }
      - if (-not (Test-Path -LiteralPath $identityFile)) { throw "HyperTwist live deploy SSH key not found at '$identityFile'. Set HYPERTWIST_VPS_SSH_KEY_PATH on the Woodpecker builder." }
      - if (-not (Test-Path -LiteralPath $manifestPath)) { throw "HyperTwist same-origin manifest not found at '$manifestPath'. Set HYPERTWIST_SAME_ORIGIN_MANIFEST or commit the intended manifest." }
      - npm run run:vps-same-origin-live-deploy -- --manifest $manifestPath --identity-file $identityFile --wait-seconds 60
    when:
      - event: push
        branch: main
