name: Publish on Testflight (Internal)
on:
  push:
    tags:
      - '*'
jobs:
  publish:
    # runs-on: macos-15  # GitHub-hosted runner
    runs-on: [self-hosted, macOS, ARM64]  # Self-hosted Mac Mini
    steps:
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.PRV_REPO_PAT }}
          submodules: recursive
          fetch-depth: 0
      # - name: Set up JDK  # Not needed on self-hosted runner (Java 17 pre-installed)
      #   uses: actions/setup-java@v3
      #   with:
      #     distribution: 'temurin'
      #     java-version: 17
      - name: Set up Xcode
        run: |
          ls /Applications/Xcode*
          swift --version
          sudo xcode-select --switch /Applications/Xcode_26.2.app/Contents/Developer
          # gem install xcpretty  # Already installed on self-hosted runner
      # - name: Set up Swift  # Not needed on self-hosted runner (comes with Xcode)
      #   uses: swift-actions/setup-swift@v2.2.0
      - name: Configure local.properties for CocoaPods
        run: |
          echo "kotlin.apple.cocoapods.bin=/opt/homebrew/lib/ruby/gems/4.0.0/bin/pod" > local.properties
      - name: Clear Clang Module Cache
        run: sudo rm -rf "$(getconf DARWIN_USER_CACHE_DIR)clang"
      - name: Kotlin/Native Pod Setup
        env:
          GRADLE_OPTS: -Dorg.gradle.daemon=false -Dkotlin.native.disableCompilerDaemon=true
          GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
          GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}
        run: ./gradlew podspec podInstall -Pkotlin.native.parallel.threads=2
      - name: Import Apple Certs
        uses: apple-actions/import-codesign-certs@v6
        with:
          p12-file-base64: ${{ secrets.APPLE_CERTS_P12 }}
          p12-password: ${{ secrets.APPLE_CERTS_P12_PW }}
      - name: Download Apple Provisioning Profiles
        uses: apple-actions/download-provisioning-profiles@v4
        with:
          bundle-id: 'coredevices.coreapp'
          issuer-id: ${{ vars.APPSTORE_API_ISSUER_ID }}
          api-key-id: ${{ secrets.APPSTORE_API_KEY_ID }}
          api-private-key: ${{ secrets.APPSTORE_API_KEY }}
      - name: Build
        env:
          GRADLE_OPTS: -Dorg.gradle.daemon=false -Dkotlin.native.disableCompilerDaemon=true
          GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
          GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}
          CI_RELEASE: true
        run: |
          export PATH="/Applications/Xcode_26.2.app/Contents/Developer/usr/bin:/opt/homebrew/lib/ruby/gems/4.0.0/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin"
          cd public/iosApp
          xcodebuild -workspace iosApp.xcworkspace -list
          echo "Archiving to ${RUNNER_TEMP}/CoreApp.xcarchive"
          xcodebuild -workspace iosApp.xcworkspace \
            -scheme iosApp \
            -configuration Release \
            -archivePath "${RUNNER_TEMP}/CoreApp.xcarchive" \
            -destination generic/platform=iOS \
            DEVELOPMENT_TEAM="${{ vars.APPSTORE_TEAM_ID }}" \
            CODE_SIGN_STYLE="Manual" \
            PROVISIONING_PROFILE_SPECIFIER="${{ vars.APPLE_DEV_PROVISIONING_ID }}" \
            archive | tee xcodebuild.log | xcpretty && exit ${PIPESTATUS[0]}
      - name: "Generate ExportOptions.plist"
        run: |
          cat <<EOF > ${RUNNER_TEMP}/ExportOptions.plist
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
            <dict>
              <key>destination</key>
              <string>export</string>
              <key>method</key>
              <string>app-store-connect</string>
              <key>signingStyle</key>
              <string>manual</string>
              <key>generateAppStoreInformation</key>
              <true/>
              <key>stripSwiftSymbols</key>
              <true/>
              <key>teamID</key>
              <string>${{ vars.APPSTORE_TEAM_ID }}</string>
              <key>uploadSymbols</key>
              <true/>
              <key>provisioningProfiles</key>
              <dict>
                <key>coredevices.coreapp</key>
                <string>${{ vars.APPLE_DIST_PROVISIONING_ID }}</string>
              </dict>
            </dict>
          </plist>
          EOF
      - id: export_archive
        name: Export Archive
        run: |
          echo "Exporting to ${RUNNER_TEMP}/Pebble.ipa from ${RUNNER_TEMP}/CoreApp.xcarchive"
          xcodebuild -exportArchive \
            -exportOptionsPlist "${RUNNER_TEMP}/ExportOptions.plist" \
            -archivePath "${RUNNER_TEMP}/CoreApp.xcarchive" \
            -exportPath "${RUNNER_TEMP}" \
            PRODUCT_BUNDLE_IDENTIFIER="coredevices.coreapp"
          echo "ipa_path=${RUNNER_TEMP}/Pebble.ipa" >> $GITHUB_ENV
      - uses: Apple-Actions/upload-testflight-build@v3
        with:
          app-path: ${{ env.ipa_path }}
          issuer-id: ${{ vars.APPSTORE_API_ISSUER_ID }}
          api-key-id: ${{ secrets.APPSTORE_API_KEY_ID }}
          api-private-key: ${{ secrets.APPSTORE_API_KEY }}
      - name: Upload Log
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: xcodebuild.log
          path: public/iosApp/xcodebuild.log
      - name: Upload IPA
        uses: actions/upload-artifact@v4
        with:
          name: Pebble.ipa
          path: ${{ env.ipa_path }}

# ============================================================================
# BACKUP: Original workflow from commit fedcf75 (before self-hosted runner)
# This was the last working version for GitHub-hosted runners
# ============================================================================
# name: Publish on Testflight (Internal)
# on:
#   push:
#     tags:
#       - '*'
# jobs:
#   publish:
#     runs-on: macos-15
#     steps:
#       - uses: actions/checkout@v3
#         with:
#           token: ${{ secrets.PRV_REPO_PAT }}
#           submodules: recursive
#           fetch-depth: 0
#       - name: Cache Gradle
#         uses: actions/cache@v4
#         with:
#           path: |
#             ~/.gradle/caches
#             ~/.gradle/wrapper
#             .gradle
#             ~/.konan/dependencies/cache
#           key: ${{ runner.os }}-${{ runner.arch }}-gradle-${{ hashFiles('gradle.properties', 'gradlew', '**/*.gradle.kts', 'gradle/**') }}
#       - name: Restore DerivedData cache
#         uses: actions/cache/restore@v4
#         with:
#           path: ~/Library/Developer/Xcode/DerivedData
#           key: ${{ runner.os }}-${{ runner.arch }}-deriveddata-${{ github.sha }}
#       - name: Set up JDK
#         uses: actions/setup-java@v3
#         with:
#           distribution: 'temurin'
#           java-version: 17
#       - name: Set up Xcode
#         run: |
#           ls /Applications/Xcode*
#           swift --version
#           sudo xcode-select --switch /Applications/Xcode_26.2.app/Contents/Developer
#           gem install xcpretty
#       - name: Set up Swift
#         uses: swift-actions/setup-swift@v2.2.0
#       - name: Kotlin/Native Pod Setup
#         env:
#           GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
#           GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}
#         run: |
#           ./gradlew podspec
#           ./gradlew podInstall
#       - name: Import Apple Certs
#         uses: apple-actions/import-codesign-certs@v6
#         with:
#           p12-file-base64: ${{ secrets.APPLE_CERTS_P12 }}
#           p12-password: ${{ secrets.APPLE_CERTS_P12_PW }}
#       - name: Download Apple Provisioning Profiles
#         uses: apple-actions/download-provisioning-profiles@v4
#         with:
#           bundle-id: 'coredevices.coreapp'
#           issuer-id: ${{ vars.APPSTORE_API_ISSUER_ID }}
#           api-key-id: ${{ secrets.APPSTORE_API_KEY_ID }}
#           api-private-key: ${{ secrets.APPSTORE_API_KEY }}
#       - name: Build
#         env:
#           GITHUB_ACTOR: ${{ secrets.PRV_REPO_USER }}
#           GITHUB_TOKEN: ${{ secrets.PRV_REPO_PAT }}
#           CI_RELEASE: true
#         run: |
#           cd public/iosApp
#           xcodebuild -workspace iosApp.xcworkspace -list
#           echo "Archiving to ${RUNNER_TEMP}/CoreApp.xcarchive"
#           xcodebuild -workspace iosApp.xcworkspace \
#             -scheme iosApp \
#             -configuration Release \
#             -archivePath "${RUNNER_TEMP}/CoreApp.xcarchive" \
#             -destination generic/platform=iOS \
#             DEVELOPMENT_TEAM="${{ vars.APPSTORE_TEAM_ID }}" \
#             CODE_SIGN_STYLE="Manual" \
#             PROVISIONING_PROFILE_SPECIFIER="${{ vars.APPLE_DEV_PROVISIONING_ID }}" \
#             archive | tee xcodebuild.log | xcpretty && exit ${PIPESTATUS[0]}
#       - name: "Generate ExportOptions.plist"
#         run: |
#           cat <<EOF > ${RUNNER_TEMP}/ExportOptions.plist
#           <?xml version="1.0" encoding="UTF-8"?>
#           <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
#           <plist version="1.0">
#             <dict>
#               <key>destination</key>
#               <string>export</string>
#               <key>method</key>
#               <string>app-store-connect</string>
#               <key>signingStyle</key>
#               <string>manual</string>
#               <key>generateAppStoreInformation</key>
#               <true/>
#               <key>stripSwiftSymbols</key>
#               <true/>
#               <key>teamID</key>
#               <string>${{ vars.APPSTORE_TEAM_ID }}</string>
#               <key>uploadSymbols</key>
#               <true/>
#               <key>provisioningProfiles</key>
#               <dict>
#                 <key>coredevices.coreapp</key>
#                 <string>${{ vars.APPLE_DIST_PROVISIONING_ID }}</string>
#               </dict>
#             </dict>
#           </plist>
#           EOF
#       - id: export_archive
#         name: Export Archive
#         run: |
#           echo "Exporting to ${RUNNER_TEMP}/Pebble.ipa from ${RUNNER_TEMP}/CoreApp.xcarchive"
#           xcodebuild -exportArchive \
#             -exportOptionsPlist "${RUNNER_TEMP}/ExportOptions.plist" \
#             -archivePath "${RUNNER_TEMP}/CoreApp.xcarchive" \
#             -exportPath "${RUNNER_TEMP}" \
#             PRODUCT_BUNDLE_IDENTIFIER="coredevices.coreapp"
#           echo "ipa_path=${RUNNER_TEMP}/Pebble.ipa" >> $GITHUB_ENV
#       - uses: Apple-Actions/upload-testflight-build@v3
#         with:
#           app-path: ${{ env.ipa_path }}
#           issuer-id: ${{ vars.APPSTORE_API_ISSUER_ID }}
#           api-key-id: ${{ secrets.APPSTORE_API_KEY_ID }}
#           api-private-key: ${{ secrets.APPSTORE_API_KEY }}
#       - name: Upload Log
#         if: always()
#         uses: actions/upload-artifact@v4
#         with:
#           name: xcodebuild.log
#           path: public/iosApp/xcodebuild.log
#       - name: Upload IPA
#         uses: actions/upload-artifact@v4
#         with:
#           name: Pebble.ipa
#           path: ${{ env.ipa_path }}
