name: CI Pipeline
on:
  pull_request:
    types: [opened, synchronize, reopened]

concurrency:
  group: ci-pipeline-${{ github.head_ref }}
  cancel-in-progress: true

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Cache node_modules
        id: cache-deps
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Verify pnpm installation
        run: pnpm --version
      - name: Install dependencies
        run: pnpm install --frozen-lockfile

  eslint:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Run Eslint check
        run: pnpm exec turbo run lint

  prettier:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Run Prettier check
        run: pnpm exec turbo run format

  cspell:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Run cspell check
        run: pnpm exec turbo run spell

  vitest:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Run Vitest tests
        run: pnpm exec turbo test

  build:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Build project
        run: pnpm exec turbo run build
      - name: Upload build output
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: |
            dist

  playwright:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - uses: pnpm/action-setup@v4
        with:
          version: 10.11.0
      - name: Restore node_modules from cache
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-
      - name: Install Playwright Browsers
        run: pnpm exec playwright install --with-deps
      - name: Download build output
        uses: actions/download-artifact@v4
        with:
          name: build
          path: dist
      - name: Run Playwright tests
        run: pnpm exec turbo run test:playwright
