name: Build App
on:
  push:
    branches:
      - main

jobs:
  build:
    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 pnpm
      uses: pnpm/action-setup@v4
      with:
        version: latest

    - 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: 🏗 Setup Expo and EAS
      uses: expo/expo-github-action@v8
      with:
        token: ${{ secrets.EXPO_TOKEN }}
        eas-version: latest
        packager: pnpm

    - name: 📦 Install dependencies
      run: pnpm install
      working-directory: mobile

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

    # Neuer Schritt: Rename das Binary, damit es wie eine echte App aussieht
    - name: 📝 Rename build to APK
      run: mv mobile/app-build mobile/app-release.apk

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

    - name: 🏷 Create tag
      run: |
        TAG="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: mobile/app-release.apk
        generate_release_notes: true
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}