name: iOS Build

on:
  push:
    branches: [ main ]
    paths:
      - 'apps/web/**'
      - 'packages/**'
      - 'apps/api/**'
      - 'package.json'
      - 'pnpm-workspace.yaml'
      - 'pnpm-lock.yaml'
      - '.github/workflows/ios-build.yml'
  pull_request:
    branches: [ main ]
    paths:
      - 'apps/web/**'
      - 'packages/**'
      - 'apps/api/**'
      - 'package.json'
      - 'pnpm-workspace.yaml'
      - 'pnpm-lock.yaml'
      - '.github/workflows/ios-build.yml'
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: ios-build-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-ios:
    name: Build iOS (simulator .app)
    runs-on: macos-latest
    env:
      CAPACITOR_SKIP_UPDATE_CHECK: 'true'
      NODE_ENV: production

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Cache turbo
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules/.cache/turbo
          key: ${{ runner.os }}-turbo-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-turbo-

      - name: Build web assets
        run: pnpm --filter=@my-roadtrip/web build

      - name: Validate build outputs
        run: |
          set -euo pipefail
          test -d apps/web/dist || (echo "apps/web/dist is missing" && exit 1)
          test -f apps/web/dist/manifest.webmanifest || (echo "manifest.webmanifest is missing" && exit 1)

      - name: Add iOS platform (Capacitor)
        run: pnpm cap add ios

      - name: Sync web assets to iOS
        run: pnpm cap sync ios

      - name: Build iOS app (simulator)
        run: |
          cd ios/App
          xcodebuild \
            -workspace App.xcworkspace \
            -scheme App \
            -configuration Debug \
            -sdk iphonesimulator \
            -derivedDataPath ../DerivedData \
            CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO \
            build

      - name: Collect iOS artifact
        id: collect_ios
        run: |
          set -euo pipefail
          SHORT_SHA=${GITHUB_SHA::7}
          APP_PATH="${GITHUB_WORKSPACE}/ios/DerivedData/Build/Products/Debug-iphonesimulator/App.app"
          ARTIFACT_DIR="${GITHUB_WORKSPACE}/build-artifacts/ios"
          mkdir -p "$ARTIFACT_DIR"
          ARTIFACT_BASENAME="roadtripper-ios-${SHORT_SHA}"
          TARGET="$ARTIFACT_DIR/${ARTIFACT_BASENAME}.zip"
          (cd "${APP_PATH}/.." && zip -r "$TARGET" "$(basename "$APP_PATH")")
          shasum -a 256 "$TARGET" > "$ARTIFACT_DIR/${ARTIFACT_BASENAME}.sha256"
          echo "ios_path=$TARGET" >> "$GITHUB_OUTPUT"
          echo "checksum_path=$ARTIFACT_DIR/${ARTIFACT_BASENAME}.sha256" >> "$GITHUB_OUTPUT"
          echo "artifact_name=$ARTIFACT_BASENAME" >> "$GITHUB_OUTPUT"

      - name: Upload iOS artifact
        uses: actions/upload-artifact@v4
        with:
          name: ios-simulator-app
          path: |
            ${{ steps.collect_ios.outputs.ios_path }}
            ${{ steps.collect_ios.outputs.checksum_path }}

      - name: Publish summary
        if: always()
        run: |
          TARGET="${{ steps.collect_ios.outputs.ios_path }}"
          CHECKSUM_FILE="${{ steps.collect_ios.outputs.checksum_path }}"
          SIZE=$(stat -f%z "$TARGET")
          echo "## ✅ iOS Simulator build" >> $GITHUB_STEP_SUMMARY
          echo "- Artifact: \`${{ steps.collect_ios.outputs.artifact_name }}.zip\`" >> $GITHUB_STEP_SUMMARY
          echo "- Size: ${SIZE} bytes" >> $GITHUB_STEP_SUMMARY
          echo "- Checksum: $(cat "$CHECKSUM_FILE" | cut -d' ' -f1)" >> $GITHUB_STEP_SUMMARY
          echo "- Commit: ${GITHUB_SHA}" >> $GITHUB_STEP_SUMMARY
