name: Unity Build

on:
  push:
    branches: [ "main" ]
    paths:
      - ".github/workflows/unity_build.yml"
      - "src/main/csharp/net/eagle0/clients/unity/**"
      - "src/main/proto/**"
      - "scripts/build_protos.sh"
      - "scripts/build_plugins.sh"
      - "scripts/build_windows_plugin.sh"
      - "ci/github_actions/build_unity.sh"
      - "ci/github_actions/restore_library.sh"
      - "ci/github_actions/persist_library.sh"
      - "MODULE.bazel"
      - "WORKSPACE"
      - "src/main/proto/net/eagle0/eagle/**/BUILD.bazel"
  pull_request:
    paths:
      - ".github/workflows/unity_build.yml"
      - "src/main/csharp/net/eagle0/clients/unity/**"
      - "src/main/proto/**"
      - "scripts/build_protos.sh"
      - "scripts/build_plugins.sh"
      - "scripts/build_windows_plugin.sh"
      - "ci/github_actions/build_unity.sh"
      - "ci/github_actions/restore_library.sh"
      - "ci/github_actions/persist_library.sh"
      - "MODULE.bazel"
      - "WORKSPACE"
      - "src/main/proto/**/BUILD.bazel"

permissions:
  contents: read

jobs:
  windows-unity:
    runs-on: self-hosted

    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true
          clean: false
      - name: Pull lfs files
        run: git lfs pull
      - name: Restore Library/
        run: ./ci/github_actions/restore_library.sh
      - name: Build Windows unity
        run: ./ci/github_actions/build_unity.sh "/tmp/eagle0/eagle0WIN"
      - name: Persist Library/
        run: ./ci/github_actions/persist_library.sh
      - name: Deploy Windows unity
        if: success() #&& github.ref == 'refs/heads/main' && github.event_name == 'push'
        env:
          ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
        run: bazel run //src/main/go/net/eagle0/build/unity3d_windows_build_handler:unity3d_windows_build_handler -- "/tmp/eagle0/eagle0WIN" "/tmp/unity_manifest.txt"

      - name: Update unified manifest
        if: success() #&& github.ref == 'refs/heads/main' && github.event_name == 'push'
        env:
          ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
        run: bazel run //src/main/go/net/eagle0/build/manifest_manager:manifest_manager -- unity3d /tmp/unity_manifest.txt
      - name: Archive build log
        if: success() || failure()
        uses: actions/upload-artifact@v4
        with:
          name: editor_win.log
          path: /tmp/eagle0/editor_win.log