name: CCW Pre-Release Gate

on:
  workflow_dispatch:
  push:
    branches:
      - main
  pull_request:

concurrency:
  group: ccw-pre-release-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  gate:
    timeout-minutes: 45
    runs-on:
      - self-hosted
      - windows
      - ccw

    defaults:
      run:
        shell: powershell

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Run pre-release gate
        env:
          CCW_BASE_URL: ${{ vars.CCW_BASE_URL || 'http://127.0.0.1/CCW' }}
          CCW_SMOKE_USERNAME: ${{ secrets.CCW_SMOKE_USERNAME || 'admin' }}
          CCW_SMOKE_PASSWORD: ${{ secrets.CCW_SMOKE_PASSWORD || 'admin123' }}
          CCW_SKIP_PHP_LINT: ${{ vars.CCW_SKIP_PHP_LINT || '0' }}
          CCW_KEEP_TEST_DATA: ${{ vars.CCW_KEEP_TEST_DATA || '0' }}
          CCW_CHECK_DUPLICATE_SUBMIT: ${{ vars.CCW_CHECK_DUPLICATE_SUBMIT || '1' }}
          CCW_CI_ARTIFACT_DIR: ${{ runner.temp }}\ccw-pre-release-gate
        run: .\scripts\ci_pre_release_gate.ps1

      - name: Append gate summary
        if: always()
        run: |
          $summaryPath = Join-Path $env:RUNNER_TEMP 'ccw-pre-release-gate\ci-pre-release-gate-summary.txt'
          if (Test-Path -LiteralPath $summaryPath) {
            Add-Content -Path $env:GITHUB_STEP_SUMMARY -Value '## CCW Pre-Release Gate'
            Get-Content -LiteralPath $summaryPath | Add-Content -Path $env:GITHUB_STEP_SUMMARY
          }

      - name: Upload gate logs
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: ccw-pre-release-gate-logs
          path: ${{ runner.temp }}\ccw-pre-release-gate
          if-no-files-found: warn
          retention-days: 14
