name: Build for iOS

on:
  workflow_call:
    inputs:
      tag:
        description: "tag"
        required: false
        default: ""
        type: string
  workflow_dispatch:

jobs:
  build-macos-app:
    name: Release IOS
    runs-on: macos-26
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
          flutter-version-file: pubspec.yaml

      - name: Set and Extract version
        shell: pwsh
        run: lib/scripts/build.ps1

      - name: Apply Patch
        shell: pwsh
        run: lib/scripts/patch.ps1 iOS
        continue-on-error: true

      - name: Build iOS
        run: |
          flutter build ios --release --no-codesign --dart-define-from-file=pili_release.json
          ln -sf ./build/ios/iphoneos Payload
          # make AltSign happy...
          find Payload/Runner.app/Frameworks -type d -name "*.framework" -exec codesign --force --sign - --preserve-metadata=identifier,entitlements {} \;
          zip -r9 PiliPlus_ios_${{env.version}}.ipa Payload/Runner.app

      - name: Release
        if: ${{ github.event.inputs.tag != '' }}
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.event.inputs.tag }}
          name: ${{ github.event.inputs.tag }}
          files: |
            PiliPlus_ios_*.ipa

      - name: Upload ios release
        uses: actions/upload-artifact@v7
        with:
          archive: false
          name: iOS-release
          path: PiliPlus_ios_*.ipa
