name: CI

on:
  pull_request:
    branches: [main, dev]
    types: [labeled]
  pull_request_review:
    types: [submitted]

jobs:
  check-ready:
    name: Check if Ready for CI
    runs-on: ubuntu-latest
    outputs:
      should-run: ${{ steps.check.outputs.run }}
    steps:
      - name: Check trigger conditions
        id: check
        run: |
          if [[ "${{ github.event_name }}" == "pull_request_review" && "${{ github.event.review.state }}" == "approved" ]]; then
            echo "Triggered by PR approval"
            echo "run=true" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.label.name }}" == "ready-for-ci" ]]; then
            echo "Triggered by ready-for-ci label"
            echo "run=true" >> $GITHUB_OUTPUT
          else
            echo "Conditions not met, skipping CI"
            echo "run=false" >> $GITHUB_OUTPUT
          fi

  lint:
    name: Lint
    needs: check-ready
    if: needs.check-ready.outputs.should-run == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4
        with:
          version: 10.28.1

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "pnpm"

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

      - name: Run linter
        run: pnpm lint

  typecheck:
    name: Type Check
    needs: check-ready
    if: needs.check-ready.outputs.should-run == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4
        with:
          version: 10.28.1

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "pnpm"

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

      - name: Run type check
        run: pnpm typecheck

  test:
    name: Test
    needs: check-ready
    if: needs.check-ready.outputs.should-run == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - uses: pnpm/action-setup@v4
        with:
          version: 10.28.1

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "pnpm"

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

      - name: Run tests
        run: pnpm test
