name: Build Mobile Apps (APK & IPA)

on:
  push:
    branches:
      - "AppPhoneAPK&IPA"
  workflow_dispatch:

jobs:
  build-android:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout du code
        uses: actions/checkout@v4

      - name: Configuration de Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Configuration de Java (Requis pour Android)
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '21'

      - name: Installation des dépendances
        run: npm install

      - name: Génération propre du dossier Android
        run: |
          rm -rf android
          npx cap add android
          npx cap sync android

      - name: Build de l'APK Android
        run: |
          cd android
          chmod +x gradlew
          ./gradlew assembleDebug

      - name: Upload de l'APK
        uses: actions/upload-artifact@v4
        with:
          name: OpenPlanetsMaps-Android-APK
          path: android/app/build/outputs/apk/debug/app-debug.apk

  build-ios:
    runs-on: macos-latest
    steps:
      - name: Checkout du code
        uses: actions/checkout@v4

      - name: Configuration de Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Installation des dépendances
        run: npm install

      - name: Génération propre du dossier iOS
        run: |
          rm -rf ios
          npx cap add ios
          npx cap sync ios

      - name: Build iOS (Non signé)
        run: |
          cd ios/App
          pod install
          xcodebuild -workspace App.xcworkspace -scheme App -configuration Debug -sdk iphoneos build CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO CODE_SIGNING_IDENTITY="" SYMROOT=$GITHUB_WORKSPACE/ios/App/build
          
          # Création forcée de l'IPA (Non signé)
          cd $GITHUB_WORKSPACE/ios/App/build/Debug-iphoneos
          mkdir -p Payload
          cp -R App.app Payload/
          zip -r $GITHUB_WORKSPACE/OpenPlanetsMaps-iOS-Unsigned.ipa Payload

      - name: Upload de l'IPA (Non signé)
        uses: actions/upload-artifact@v4
        with:
          name: OpenPlanetsMaps-iOS-IPA
          path: OpenPlanetsMaps-iOS-Unsigned.ipa