name: Release Mobile Artifacts

on:
  release:
    types: [published]
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: release-mobile-${{ github.ref }}
  cancel-in-progress: false

jobs:
  android:
    name: Build Android package
    runs-on: ubuntu-latest

    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: Build web assets
        run: pnpm --filter=@my-roadtrip/web build

      - name: Prepare Android artifact
        id: android_artifact
        shell: bash
        run: |
          set -euo pipefail
          ARTIFACT_DIR="${GITHUB_WORKSPACE}/build-artifacts"
          mkdir -p "$ARTIFACT_DIR"

          TARGET="$ARTIFACT_DIR/RoadTripper-${GITHUB_REF_NAME:-build}-android-pwa.zip"
          (cd apps/web/dist && zip -r "$TARGET" .)
          echo "apk_path=$TARGET" >> "$GITHUB_OUTPUT"
          echo "build_type=pwa" >> "$GITHUB_OUTPUT"

      - name: Upload Android artifact
        uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: ${{ steps.android_artifact.outputs.apk_path }}

      - name: Attach Android package to release
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v1
        with:
          files: ${{ steps.android_artifact.outputs.apk_path }}

  ios:
    name: Build iOS archive
    runs-on: macos-latest

    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: Build web assets
        run: pnpm --filter=@my-roadtrip/web build

      - name: Package iOS simulator build
        id: ios_artifact
        shell: bash
        run: |
          set -euo pipefail
          ARTIFACT_DIR="${GITHUB_WORKSPACE}/build-artifacts"
          mkdir -p "$ARTIFACT_DIR"
          TARGET="$ARTIFACT_DIR/RoadTripper-${GITHUB_REF_NAME:-build}-ios-pwa.zip"
          (cd apps/web/dist && zip -r "$TARGET" .)
          echo "ios_path=$TARGET" >> "$GITHUB_OUTPUT"

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

      - name: Attach iOS archive to release
        if: github.event_name == 'release'
        uses: softprops/action-gh-release@v1
        with:
          files: ${{ steps.ios_artifact.outputs.ios_path }}
