name: Mobile SDK CI

on:
  pull_request:
    paths:
      - "packages/mobile-sdk-alpha/**"
      - ".github/workflows/mobile-sdk-ci.yml"
      - ".github/actions/**"

jobs:
  # Build dependencies once and cache for other jobs
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Build dependencies
        shell: bash
        run: |
          yarn workspace @selfxyz/common build
          yarn workspace @selfxyz/mobile-sdk-alpha build
      - name: Cache build artifacts
        uses: actions/cache/save@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
            node_modules
            packages/mobile-sdk-alpha/node_modules
            common/node_modules
          key: mobile-sdk-alpha-build-${{ github.sha }}

  lint:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
            node_modules
            packages/mobile-sdk-alpha/node_modules
            common/node_modules
          key: mobile-sdk-alpha-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Run linter
        run: yarn workspace @selfxyz/mobile-sdk-alpha lint

  format:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
            node_modules
            packages/mobile-sdk-alpha/node_modules
            common/node_modules
          key: mobile-sdk-alpha-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Check Prettier formatting
        run: yarn workspace @selfxyz/mobile-sdk-alpha prettier --check .

  types:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
            node_modules
            packages/mobile-sdk-alpha/node_modules
            common/node_modules
          key: mobile-sdk-alpha-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Type checking
        run: yarn workspace @selfxyz/mobile-sdk-alpha types

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
            node_modules
            packages/mobile-sdk-alpha/node_modules
            common/node_modules
          key: mobile-sdk-alpha-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Run tests
        run: yarn workspace @selfxyz/mobile-sdk-alpha test
