name: Release APK

on:
  push:
    branches:
      - master

jobs:
  release:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    permissions:
      contents: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node 20
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Bump version
        id: bump
        run: |
          NEW_VERSION=$(node scripts/bump-version.js)
          echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT

      - name: Commit version bump
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add app.json package.json
          git commit -m "chore: bump version to v${{ steps.bump.outputs.version }} [skip ci]"
          git push

      - name: Setup Java 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

      - name: Expo Prebuild
        run: npx expo prebuild --platform android --no-install

      - name: Grant Gradle execute permission
        run: chmod +x android/gradlew

      - name: Build Release APK
        env:
          SENTRY_DISABLE_AUTO_UPLOAD: 'true'
        run: |
          cd android
          ./gradlew assembleRelease --no-daemon

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "v${{ steps.bump.outputs.version }}" \
            --title "JKVideo v${{ steps.bump.outputs.version }}" \
            --generate-notes \
            android/app/build/outputs/apk/release/app-release.apk
