name: iOS Addressables Build

on:
  push:
    branches: [ "main" ]
    paths:
      - ".github/workflows/ios_addressables_build.yml"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/AddressableAssetsData/**"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/Music/**"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/Editor/BuildScript.cs"
      - "ci/github_actions/build_ios_addressables.sh"
      - "ci/github_actions/upload_addressables.sh"
  pull_request:
    paths:
      - ".github/workflows/ios_addressables_build.yml"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/AddressableAssetsData/**"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/Music/**"
      - "src/main/csharp/net/eagle0/clients/unity/eagle0/Assets/Editor/BuildScript.cs"
      - "ci/github_actions/build_ios_addressables.sh"
      - "ci/github_actions/upload_addressables.sh"
  workflow_dispatch:

permissions:
  contents: read

env:
  # Runner-specific build directory to allow parallel builds on multiple runners
  EAGLE0_BUILD_DIR: /tmp/eagle0-${{ github.run_id }}

jobs:
  build-ios-addressables:
    runs-on: [self-hosted, macOS, unity-mac]

    steps:
      - name: Prune stale PR refs
        run: |
          # Self-hosted runners persist .git between runs. When a PR is updated,
          # old local refs (refs/remotes/pull/*/merge) may point to commits whose
          # objects were never fetched or have been pruned. Remove these stale refs
          # before checkout to prevent "missing object" errors.
          if [ -d ".git" ]; then
            echo "Pruning stale PR refs..."
            git for-each-ref --format='%(refname)' refs/remotes/pull/ 2>/dev/null | \
              xargs -r git update-ref -d 2>/dev/null || true
          fi
      - uses: actions/checkout@v4
        with:
          lfs: false  # Fetch LFS after checkout to avoid stale ref issues
          clean: true

      - name: Fetch LFS files
        run: |
          git lfs install
          git lfs pull

      - name: Ensure Unity version installed
        run: ./ci/github_actions/ensure_unity_installed.sh ios

      - name: Restore Library/
        env:
          UNITY_CACHE_PLATFORM: ios
        run: ./ci/github_actions/restore_library.sh

      - name: Build iOS Addressables
        run: ./ci/github_actions/build_ios_addressables.sh "$EAGLE0_BUILD_DIR/editor_ios_addressables.log"

      - name: Persist Library/
        if: success()
        env:
          UNITY_CACHE_PLATFORM: ios
        run: ./ci/github_actions/persist_library.sh

      - name: Upload Addressables to CDN
        if: success() && github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
        env:
          ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
        run: ./ci/github_actions/upload_addressables.sh iOS

      - name: Archive Build Log
        if: success() || failure()
        uses: actions/upload-artifact@v4
        with:
          name: editor_ios_addressables.log
          path: ${{ env.EAGLE0_BUILD_DIR }}/editor_ios_addressables.log
          retention-days: 5
      - name: Cleanup build directory
        if: always()
        run: rm -rf "${{ env.EAGLE0_BUILD_DIR }}"
