name: Run Maestro Tests

on:
  workflow_dispatch:
  schedule:
    - cron: "0 3 * * *"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build-android:
    if: github.repository == 'Jellify-Music/App'
    runs-on: ubuntu-latest
    outputs:
      cache-hit: ${{ steps.apk-cache.outputs.cache-hit }}
    steps:
      - name: 🛒 Checkout
        uses: actions/checkout@v6

      - name: 📦 Check APK Cache
        id: apk-cache
        uses: actions/cache@v5
        with:
          path: ./android/app/build/outputs/apk/release/app-universal-release.apk
          key: android-apk-${{ runner.os }}-${{ hashFiles('src/**', 'android/**', 'package.json', 'bun.lock') }}
          restore-keys: |
            android-apk-${{ runner.os }}-

      - name: 🖥 Setup Bun
        if: steps.apk-cache.outputs.cache-hit != 'true'
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.4

      - name: 📦 Cache node_modules
        if: steps.apk-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v5
        with:
          path: |
            node_modules
            ~/.cache/turbo
          key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-

      - name: ☕ Setup JDK 17
        if: steps.apk-cache.outputs.cache-hit != 'true'
        uses: actions/setup-java@v5
        with:
          distribution: 'zulu'
          java-version: '17'

      - name: 🐘 Setup Gradle with caching
        if: steps.apk-cache.outputs.cache-hit != 'true'
        uses: gradle/actions/setup-gradle@v5
        with:
          cache-read-only: false

      - name: 📦 Cache Gradle
        if: steps.apk-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v5
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: 📥 Install dependencies
        if: steps.apk-cache.outputs.cache-hit != 'true'
        run: bun i

      - name: 💬 Disable OTA Updates and Enable Maestro Build
        if: steps.apk-cache.outputs.cache-hit != 'true'
        run: bun scripts/updateEnv.js OTA_UPDATE_ENABLED=false IS_MAESTRO_BUILD=true

      - name: ✅ Validate Config Files
        if: steps.apk-cache.outputs.cache-hit != 'true'
        run: |
          bun -p "JSON.parse(require('fs').readFileSync('telemetrydeck.json'))"
          bun -p "JSON.parse(require('fs').readFileSync('glitchtip.json'))"

      - name: 🚀 Build Android APK
        if: steps.apk-cache.outputs.cache-hit != 'true'
        run: bun run android-build

      - name: 📤 Upload Android Artifacts
        uses: actions/upload-artifact@v6
        with:
          name: android-artifacts
          path: ./android/app/build/outputs/apk/release/app-universal-release.apk
          retention-days: 7

  run-maestro-tests:
    if: github.repository == 'Jellify-Music/App'
    runs-on: ubuntu-latest
    needs: build-android
    timeout-minutes: 45

    steps:
      - name: 🧹 Free Disk Space
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: true
          android: false
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true

      - name: 🛒 Checkout
        uses: actions/checkout@v6

      - name: 🖥 Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.4

      - name: 📦 Cache node_modules
        uses: actions/cache@v5
        with:
          path: |
            node_modules
            ~/.cache/turbo
          key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-

      - name: 📥 Install dependencies
        run: bun i

      - name: 🎭 Install Maestro
        shell: bash
        run: |
          export MAESTRO_VERSION=2.0.3
          curl -Ls "https://get.maestro.mobile.dev" | bash
          echo "$HOME/.maestro/bin" >> $GITHUB_PATH

      - name: ☕ Setup JDK 17
        uses: actions/setup-java@v5
        with:
          java-version: '17'
          distribution: 'zulu'

      - name: 📦 Cache AVD
        uses: actions/cache@v5
        id: avd-cache
        with:
          path: |
            ~/.android/avd/*
            ~/.android/adb*
          key: avd-api-34-x86_64-${{ runner.os }}

      - name: ⬇️ Download Android Artifacts
        uses: actions/download-artifact@v7
        with:
          name: android-artifacts
          path: artifacts/

      - name: 🔧 Enable KVM
        shell: bash
        run: |
          echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
          sudo udevadm control --reload-rules
          sudo udevadm trigger --name-match=kvm

      - name: 🧪 Run Maestro Tests
        id: run-tests
        uses: reactivecircus/android-emulator-runner@v2
        with:
          api-level: 34
          target: google_apis
          arch: x86_64
          profile: pixel_6
          ram-size: 4096M
          heap-size: 1024M
          disk-size: 6G
          cores: 4
          disable-animations: true
          disable-spellchecker: true
          disable-linux-hw-accel: false
          emulator-boot-timeout: 600
          avd-name: maestro_emulator
          force-avd-creation: false
          emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim
          script: bash scripts/maestro-android-retry.sh "https://jellyfin.jellify.app" "jerry"

      - name: 📸 Collect Screenshots
        if: always()
        run: |
          mkdir -p test-artifacts
          cp -r screenshots/* test-artifacts/ 2>/dev/null || true
          cp -r .maestro/screenshots/* test-artifacts/ 2>/dev/null || true
          cp *.mp4 test-artifacts/ 2>/dev/null || true

      - name: 🗣️ Notify Success on Discord
        if: success() && github.repository == 'Jellify-Music/App'
        run: |
          bun scripts/sendDiscordMessage.js "__**## ✅ Maestro Test Passed**__All checks completed successfully!"
        env:
          DISCORD_WEBHOOK_URL: ${{ secrets.MAESTRO_WEBHOOK_RESULTS }}

      - name: 🗣️ Notify Failure on Discord
        if: failure() && github.repository == 'Jellify-Music/App'
        run: |
          bun scripts/sendDiscordMessage.js "__**## ❌ Maestro Test Failed**__Some tests did not pass. Check artifacts for details."
        env:
          DISCORD_WEBHOOK_URL: ${{ secrets.MAESTRO_WEBHOOK_RESULTS }}

      - name: 📤 Upload Test Artifacts
        uses: actions/upload-artifact@v6
        if: always()
        with:
          name: maestro-test-results
          path: |
            test-artifacts/
          retention-days: 14
