name: Build APK

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
        working-directory: ./apk

    steps:
      - name: 📥 Checkout
        uses: actions/checkout@v4

      - name: 🟢 Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: 📦 Install dependencies
        run: npm install

      - name: ☕ Setup Java
        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 --clean --non-interactive

      - name: 🔐 Decode Keystore
        run: |
          echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 --decode > android/release.keystore

      - name: 🏗️ Build APK (Release)
        working-directory: ./apk/android
        run: |
          ./gradlew assembleRelease
      - name: 📤 Upload APK Artifact
        uses: actions/upload-artifact@v3
        with:
          name: app-release-apk
          path: apk/android/app/build/outputs/apk/release/app-release.apk
  release:
    runs-on: docker
    needs: [build]
    steps:
      - name: Download Web Artifact
        uses: actions/download-artifact@v3
        with:
          name: app-release-apk
          path: dist

      - name: Create Release
        uses: https://gitea.com/actions/gitea-release-action@v1
        with:
          tag_name: latest
          name: Latest Build
          overwrite_files: true
          files: |
            dist/app-release.apk
        env:
          GITEA_TOKEN: ${{ secrets.GITEA }}