name: Pre-release CI

on:
  push:
    branches: ["main"]
    paths-ignore:
      - "README.md"
      - "CHANGELOG.md"
      - "docs/**"
  pull_request:
    branches: ["main"]
    paths-ignore:
      - "README.md"
      - "CHANGELOG.md"
      - "docs/**"

permissions:
  contents: read

jobs:
  preflight:
    timeout-minutes: 45
    concurrency:
      group: pre-release-${{ github.workflow }}-${{ github.ref }}
      cancel-in-progress: true
    runs-on: macos-15
    steps:
      - name: Checkout repository
        env:
          REPO: ${{ github.repository }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REF: ${{ github.sha }}
          SERVER_URL: ${{ github.server_url }}
        run: |
          set -euo pipefail
          SERVER_HOST="${SERVER_URL#https://}"
          git init
          git remote add origin "https://x-access-token:${GH_TOKEN}@${SERVER_HOST}/${REPO}.git"
          git fetch --depth=1 origin "$REF"
          git checkout FETCH_HEAD

      - name: Select/verify Xcode (17 preferred, 16+ fallback)
        id: xcode_select
        run: |
          set -euo pipefail
          if scripts/ci/select_xcode17.sh; then
            echo "Using Xcode 17+ for pre-release CI."
            echo "legacy_icon_mode=false" >> "$GITHUB_OUTPUT"
          else
            echo "Xcode 17+ unavailable on hosted runner; falling back to Xcode 16+."
            xcodebuild -version
            XCODE_MAJOR="$(xcodebuild -version | awk '/Xcode/ {split($2, v, "."); print v[1]}')"
            if [[ "${XCODE_MAJOR:-0}" -lt 16 ]]; then
              echo "Xcode 16+ is required for pre-release CI fallback." >&2
              exit 1
            fi
            echo "legacy_icon_mode=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Prepare legacy app icon for Xcode 16 fallback
        if: ${{ steps.xcode_select.outputs.legacy_icon_mode == 'true' }}
        run: |
          set -euo pipefail
          ICON_DIR="Neon Vision Editor/Resources"
          if [[ -d "${ICON_DIR}/AppIcon.icon" ]]; then
            mv "${ICON_DIR}/AppIcon.icon" "${ICON_DIR}/AppIcon.icon.disabled"
          fi
          rm -rf "${ICON_DIR}/Assets.xcassets/AppIcon.appiconset"
          cp -R "${ICON_DIR}/Assets.xcassets/AppIcon-iOS.appiconset" "${ICON_DIR}/Assets.xcassets/AppIcon.appiconset"
          echo "Prepared AppIcon.appiconset fallback for Xcode 16 hosted runner."

      - name: Validate basic release docs format
        run: |
          set -euo pipefail
          if grep -nE "^- TODO$" CHANGELOG.md >/dev/null; then
            echo "CHANGELOG contains TODO entries. Fill release notes before merging." >&2
            exit 1
          fi
          grep -nE "^> Latest release: \\*\\*v[0-9]+\\.[0-9]+\\.[0-9]+(-[A-Za-z0-9.]+)?\\*\\*$" README.md >/dev/null
          grep -nE "^- Latest release: \\*\\*v[0-9]+\\.[0-9]+\\.[0-9]+(-[A-Za-z0-9.]+)?\\*\\*$" README.md >/dev/null

      - name: Run critical runtime tests
        env:
          DERIVED_DATA: ${{ runner.temp }}/DerivedData
        run: |
          set -euo pipefail
          xcodebuild \
            -project "Neon Vision Editor.xcodeproj" \
            -scheme "Neon Vision Editor" \
            -destination "platform=macOS" \
            -derivedDataPath "$DERIVED_DATA" \
            MACOSX_DEPLOYMENT_TARGET=15.5 \
            CODE_SIGNING_ALLOWED=NO \
            CODE_SIGNING_REQUIRED=NO \
            CODE_SIGN_IDENTITY="" \
            -only-testing:"Neon Vision EditorTests/ReleaseRuntimePolicyTests" \
            test

      - name: Verify icon payload in built app
        env:
          DERIVED_DATA: ${{ runner.temp }}/DerivedData
        run: |
          set -euo pipefail
          APP="$DERIVED_DATA/Build/Products/Debug/Neon Vision Editor.app"
          scripts/ci/verify_icon_payload.sh "$APP"
