name: Common CI

on:
  pull_request:
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Cache Yarn dependencies
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            common/node_modules
          cache-version: v1
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Build dependencies
        run: yarn workspace @selfxyz/mobile-app run build:deps
      - name: Cache build artifacts
        uses: actions/cache/save@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
          key: build-deps-${{ runner.os }}-${{ hashFiles('common/**/*', 'packages/mobile-sdk-alpha/**/*', '!common/dist/**', '!packages/mobile-sdk-alpha/dist/**') }}

  lint:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Cache Yarn dependencies
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            common/node_modules
          cache-version: v1
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Run linter
        run: yarn workspace @selfxyz/common lint

  type-check:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Cache Yarn dependencies
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            common/node_modules
          cache-version: v1
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        id: restore-build-deps
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
          key: build-deps-${{ runner.os }}-${{ hashFiles('common/**/*', 'packages/mobile-sdk-alpha/**/*', '!common/dist/**', '!packages/mobile-sdk-alpha/dist/**') }}
          fail-on-cache-miss: false
      - name: Build @selfxyz/common (fallback on cache miss)
        if: steps.restore-build-deps.outputs.cache-hit != 'true'
        run: yarn workspace @selfxyz/common build
      - name: Build @selfxyz/mobile-sdk-alpha
        run: yarn workspace @selfxyz/mobile-sdk-alpha build
      - name: Build @selfxyz/qrcode
        run: yarn workspace @selfxyz/qrcode build:deps
      - name: Yarn types
        run: yarn types

  test-common:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Cache Yarn dependencies
        uses: ./.github/actions/cache-yarn
        with:
          path: |
            .yarn/cache
            node_modules
            common/node_modules
          cache-version: v1
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Restore build artifacts
        id: restore-build-deps
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            packages/mobile-sdk-alpha/dist
          key: build-deps-${{ runner.os }}-${{ hashFiles('common/**/*', 'packages/mobile-sdk-alpha/**/*', '!common/dist/**', '!packages/mobile-sdk-alpha/dist/**') }}
          fail-on-cache-miss: false
      - name: Build @selfxyz/common (fallback on cache miss)
        if: steps.restore-build-deps.outputs.cache-hit != 'true'
        run: yarn workspace @selfxyz/common build
      - name: Build @selfxyz/mobile-sdk-alpha
        run: yarn workspace @selfxyz/mobile-sdk-alpha build
      - name: Run @selfxyz/common tests
        run: yarn workspace @selfxyz/common test
