name: CodexBar Mobile Build

on:
  push:
    branches:
      - main
    paths:
      - "**/*.ts"
      - "**/*.tsx"
      - "**/*.js"
      - "**/*.json"
      - ".gitea/workflows/**"

jobs:
  build-android:
    runs-on: ubuntu-latest
    steps:
      - name: 🏗 Setup repo
        uses: actions/checkout@v2

      - name: 🏗 Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 22

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

      - name: Set up Android SDK
        run: |
          export ANDROID_SDK_ROOT="$HOME/android-sdk"
          mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools"
          curl -fsSL -o /tmp/cmdline-tools.zip https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip
          unzip -q /tmp/cmdline-tools.zip -d /tmp/cmdline-tools
          mv /tmp/cmdline-tools/cmdline-tools "$ANDROID_SDK_ROOT/cmdline-tools/latest"
          export PATH="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools:$PATH"
          yes | sdkmanager --sdk_root="$ANDROID_SDK_ROOT" --licenses >/dev/null || true
          sdkmanager --sdk_root="$ANDROID_SDK_ROOT" 'platform-tools' 'platforms;android-34' 'build-tools;34.0.0'
          echo "ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT" >> "$GITHUB_ENV"
          echo "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin" >> "$GITHUB_PATH"
          echo "$ANDROID_SDK_ROOT/platform-tools" >> "$GITHUB_PATH"

      - name: Set up Gradle cache
        uses: gradle/actions/setup-gradle@v3

      - name: 🏗 Setup Expo and EAS
        uses: expo/expo-github-action@v8
        with:
          token: ${{ secrets.EXPO_TOKEN }}
          eas-version: latest
          packager: npm

      - name: 📦 Install dependencies
        run: npm ci --legacy-peer-deps

      - name: 👷 Build app
        run: |
          eas build --local \
            --non-interactive \
            --output=./app-build \
            --platform=android \
            --profile=preview

      - name: 📝 Rename build to APK
        run: mv app-build codexbar-release.apk

      - name: 🗜 Zip APK
        run: zip codexbar-release.zip codexbar-release.apk

      - name: 📤 Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: codexbar-android-preview-build
          path: codexbar-release.zip
          if-no-files-found: error

  release:
    runs-on: ubuntu-latest
    needs: [build-android]
    steps:
      - name: 🏗 Setup repo
        uses: actions/checkout@v2

      - name: 📥 Download Android artifact
        uses: actions/download-artifact@v3
        with:
          name: codexbar-android-preview-build

      - name: 🏷 Create tag
        run: |
          TAG="codexbar-build-$(git rev-parse --short HEAD)"
          git tag "$TAG"
          git push origin "$TAG"
          echo "RELEASE_TAG=$TAG" >> $GITHUB_ENV

      - name: 🚀 Create release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ env.RELEASE_TAG }}
          name: ${{ env.RELEASE_TAG }}
          files: codexbar-release.zip
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
