name: Core SDK CI

on:
  pull_request:
    branches:
      - dev
      - staging
      - main

jobs:
  check_changes:
    runs-on: ubuntu-slim
    outputs:
      should_run: ${{ steps.filter.outputs.should_run }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Check if should run
        id: filter
        run: |
          set -e
          if [[ "${{ github.base_ref }}" == "main" ]] || [[ "${{ github.base_ref }}" == "staging" ]]; then
            echo "should_run=true" >> $GITHUB_OUTPUT
            echo "Running for ${{ github.base_ref }} - no path filter"
          else
            # For dev branch, check if relevant files changed
            # Fetch the base branch to ensure it's available for comparison
            git fetch origin ${{ github.base_ref }} --depth=1
            CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD) || {
              echo "Error: Failed to diff against base branch"
              exit 1
            }
            if echo "$CHANGED_FILES" | grep -qE "^(sdk/core/|common/|\.github/workflows/core-sdk-ci\.yml|\.github/actions/)"; then
              echo "should_run=true" >> $GITHUB_OUTPUT
              echo "Running for dev - relevant files changed"
            else
              echo "should_run=false" >> $GITHUB_OUTPUT
              echo "Skipping for dev - no relevant files changed"
            fi
          fi

  build:
    needs: check_changes
    if: github.event.pull_request.draft == false && needs.check_changes.outputs.should_run == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Build dependencies
        run: |
          yarn workspace @selfxyz/common build
          yarn workspace @selfxyz/core build
      - name: Cache build artifacts
        uses: actions/cache/save@v4
        with:
          path: |
            common/dist
            sdk/core/dist
            node_modules
            sdk/core/node_modules
            common/node_modules
          key: core-sdk-build-${{ github.sha }}

  lint:
    runs-on: ubuntu-latest
    needs: [check_changes, build]
    if: github.event.pull_request.draft == false && needs.check_changes.outputs.should_run == 'true'
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Setup Corepack
        run: |
          corepack enable
          corepack prepare yarn@4.12.0 --activate
      - name: Restore build artifacts
        id: build-cache
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            sdk/core/dist
            node_modules
            sdk/core/node_modules
            common/node_modules
          key: core-sdk-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Install Dependencies
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: ./.github/actions/yarn-install
      - name: Run linter
        run: yarn workspace @selfxyz/core lint

  types:
    runs-on: ubuntu-latest
    needs: [check_changes, build]
    if: github.event.pull_request.draft == false && needs.check_changes.outputs.should_run == 'true'
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Setup Corepack
        run: |
          corepack enable
          corepack prepare yarn@4.12.0 --activate
      - name: Restore build artifacts
        id: build-cache
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            sdk/core/dist
            node_modules
            sdk/core/node_modules
            common/node_modules
          key: core-sdk-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Install Dependencies
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: ./.github/actions/yarn-install
      - name: Type checking
        run: yarn workspace @selfxyz/core types

  test:
    runs-on: ubuntu-latest
    needs: [check_changes, build]
    if: github.event.pull_request.draft == false && needs.check_changes.outputs.should_run == 'true'
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - name: Setup Corepack
        run: |
          corepack enable
          corepack prepare yarn@4.12.0 --activate
      - name: Restore build artifacts
        id: build-cache
        uses: actions/cache/restore@v4
        with:
          path: |
            common/dist
            sdk/core/dist
            node_modules
            sdk/core/node_modules
            common/node_modules
          key: core-sdk-build-${{ github.sha }}
          fail-on-cache-miss: true
      - name: Install Dependencies
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: ./.github/actions/yarn-install
      - name: Run tests
        run: yarn workspace @selfxyz/core test
