name: Build iOS IPA
on:
  workflow_dispatch:
  pull_request:
    paths:
      - '.github/workflows/build-ios.yml'
      - 'ios/**'
      - 'package.json'


concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true
jobs:
  build-ios:
    if: github.repository == 'Jellify-Music/App'
    runs-on: macos-15
    steps:
      - name: 🛒 Checkout
        uses: actions/checkout@v6

      - name: 🖥 Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.4

      - name: 💬 Echo package.json version to Github ENV
        run: echo VERSION_NUMBER=$(bun -p "require('./package.json').version") >> $GITHUB_ENV

      - name: 🍎 Setup Xcode
        uses: ./.github/actions/setup-xcode

      - name: 🍎 Run bun init-ios:new-arch
        run: bun run init-android && cd ios && bundle install &&  bundle exec pod install


      - name: 🚀 Run fastlane build
        run: |
          cd ios
          set -o pipefail
          xcodebuild \
            CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ \
            -derivedDataPath build -UseModernBuildSystem=YES \
            -workspace Jellify.xcworkspace \
            -scheme Jellify \
            -sdk iphonesimulator \
            -configuration Release \
            -destination 'generic/platform=iOS Simulator' \
            build \
            CODE_SIGNING_ALLOWED=NO
      - name: Package .app for Simulator
        run: |
          cd ios/build/Build/Products/Release-iphonesimulator
          zip -r Jellify-Release-Simulator.zip Jellify.app

      - name: 📦 Upload IPA for testing
        uses: actions/upload-artifact@v6
        if: always()
        with:
          name: jellify-ios-pr-${{ github.event.number }}-${{ env.VERSION_NUMBER }}
          path: |
            ios/build/*.ipa
            ios/*.ipa
            Jellify.app
            *.zip
            ios/build/Build/Products/Release-iphonesimulator/Jellify-Release-Simulator.zip
          retention-days: 7
          if-no-files-found: warn
