name: Publish Android APK and TestFlight Betas
on:
  push:
    branches:
      - 'main'
jobs:
  publish-beta:
    runs-on: macos-15
    steps:
      - name: 🛒 Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.SIGNING_REPO_PAT }}

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

      - name: 🍎 Run yarn init-ios:new-arch
        run: yarn init-ios:new-arch
      
      - name: ➕ Version Up
        run: yarn react-native bump-version --type patch  
          
      - name: 💬 Echo package.json version to Github ENV
        run: echo VERSION_NUMBER=$(node -p -e "require('./package.json').version") >> $GITHUB_ENV
          
      - name: 🤫 Output App Store Connect API Key JSON to Fastlane
        run: echo -e '${{ secrets.APPSTORE_CONNECT_API_KEY_JSON }}' > appstore_connect_api_key.json
        working-directory: ./ios/fastlane

      - name: 🤫 Output TelemetryDeck Secrets to TelemetryDeck.json          
        run: |
          echo "{" > telemetrydeck.json
          echo "\"appID\": \"${{ secrets.TELEMETRYDECK_APPID }}\"," >> telemetrydeck.json
          echo "\"clientUser\": \"anonymous\"," >> telemetrydeck.json
          echo "\"app\": \"Jellify\"" >> telemetrydeck.json
          echo "}" >> telemetrydeck.json

          
      - name: 🤫 Output Glitchtip Secrets to Glitchtip.json
        run: |
          echo "{" > glitchtip.json
          echo "\"dsn\": \"${{ secrets.GLITCHTIP_DSN }}\"" >> glitchtip.json
          echo "}" >> glitchtip.json
                
      - name: ✅ Validate TelemetryDeck.json
        run: |
          node -e "JSON.parse(require('fs').readFileSync('telemetrydeck.json'))"

      - name: ✅ Validate Glitchtip.json
        run: |
          node -e "JSON.parse(require('fs').readFileSync('glitchtip.json'))"
          
      - name: 🚀 Run Android fastlane build
        run: yarn fastlane:android:build
          
      - name: 🚀 Run iOS fastlane build and publish to TestFlight
        run: yarn fastlane:ios:beta
        env:
          APPSTORE_CONNECT_API_KEY_JSON: ${{ secrets.APPSTORE_CONNECT_API_KEY_JSON }}
          FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD: ${{ secrets.FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD }}
          MATCH_PASSWORD: ${{ secrets.MATCH_PASSWORD }}
          MATCH_REPO_PAT: "anultravioletaurora:${{ secrets.SIGNING_REPO_PAT }}"

      - name: 👩‍💻 Configure Git
        run: |
          git config --global user.email "violet@cosmonautical.cloud"
          git config --global user.name "anultravioletaurora"

      - name: 🧹 Clean up Glitchtip and TelemetryDeck files
        run: |
          git restore telemetrydeck.json
          git restore glitchtip.json

      # Commit Fastlane Xcode build number increment
      - name: 🔢 Commit changes for version increment
        run: |
          git add package.json
          git add ios/Jellify.xcodeproj/project.pbxproj
          git add android/app/build.gradle
          git commit -m "[skip actions]"
          git push origin main

      - name: 🔢 Set artifact version numbers
        run: |
          # Create artifacts folder for uploading to release
          mkdir artifacts

          # Move the iOS IPA
          mv ./ios/Jellify.ipa ./artifacts/Jellify-${{ env.VERSION_NUMBER }}.ipa

          # Move and rename all android release APKs
          for apk in ./android/app/build/outputs/apk/release/*.apk; do
            filename=$(basename "$apk")
            newname="Jellify-${{ env.VERSION_NUMBER }}-${filename}"
            cp "$apk" "./artifacts/$newname"
          done

      - name: 🎉 Create Github release 
        uses: ncipollo/release-action@v1
        id: githubRelease
        with:
          artifacts: "./artifacts/*"
          name: ${{ env.VERSION_NUMBER }}
          generateReleaseNotes: true
          prerelease: true
          tag: ${{ env.VERSION_NUMBER }}
          token: ${{ secrets.SIGNING_REPO_PAT }}

      - name: 🗣️ Notify on Discord
        run: |
          cd ios
          bundle exec fastlane notifyOnDiscord
          cd ..

        env: 
          DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
          APP_VERSION: ${{ env.VERSION_NUMBER }}
          release_url: ${{ steps.githubRelease.outputs.html_url }}
