name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

permissions:
  contents: read

jobs:
  lint-and-unit:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Run lint
        run: pnpm lint

      - name: Build project
        run: pnpm build

      - name: Run unit tests
        run: pnpm test:unit:coverage

      - name: Code Coverage Comments
        if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
        uses: kcjpop/coverage-comments@v2.2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          coverage-file: './coverage/lcov.info'

      - name: Dry run the release
        if: github.event_name == 'pull_request'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npx semantic-release --dry-run

  integration:
    runs-on: ubuntu-latest
    needs: lint-and-unit
    strategy:
      matrix:
        next-test-app:
          - next-app-15-4-11
          - next-app-16-0-11
          - next-app-16-2-6
          - next-app-16-3-0
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Build project
        run: pnpm build

      - name: Start Redis
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: '7'
          redis-port: 6379

      - name: Install redis-cli
        run: sudo apt-get update && sudo apt-get install -y redis-tools

      - name: Configure Redis Keyspace Notifications
        run: redis-cli config set notify-keyspace-events Exe

      - name: Install test project
        run: cd test/nextjs-test-projects/${{ matrix.next-test-app }} && pnpm install

      - name: Build test project
        run: cd test/nextjs-test-projects/${{ matrix.next-test-app }} && pnpm build

      - name: Run integration tests
        run: pnpm test:integration
        env:
          SKIP_BUILD: true
          NEXT_TEST_APP: ${{ matrix.next-test-app }}

  integration-pages:
    runs-on: ubuntu-latest
    needs: lint-and-unit
    strategy:
      matrix:
        pages-test-app:
          - next-pages-16-2-6
          - next-pages-16-3-0
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Build project
        run: pnpm build

      - name: Start Redis
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: '7'
          redis-port: 6379

      - name: Install redis-cli
        run: sudo apt-get update && sudo apt-get install -y redis-tools

      - name: Configure Redis Keyspace Notifications
        run: redis-cli config set notify-keyspace-events Exe

      - name: Install test project
        run: cd test/nextjs-test-projects/${{ matrix.pages-test-app }} && pnpm install

      - name: Build test project
        run: cd test/nextjs-test-projects/${{ matrix.pages-test-app }} && pnpm build

      - name: Run Pages Router integration tests
        run: pnpm test:integration:pages
        env:
          SKIP_BUILD: true
          NEXT_PAGES_TEST_APP: ${{ matrix.pages-test-app }}

  integration-build-id-prefix:
    runs-on: ubuntu-latest
    needs: lint-and-unit
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Build project
        run: pnpm build

      - name: Start Redis
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: '7'
          redis-port: 6379

      - name: Install redis-cli
        run: sudo apt-get update && sudo apt-get install -y redis-tools

      - name: Configure Redis Keyspace Notifications
        run: redis-cli config set notify-keyspace-events Exe

      - name: Install test project
        run: cd test/nextjs-test-projects/next-app-15-4-11 && pnpm install

      - name: Build test project
        run: cd test/nextjs-test-projects/next-app-15-4-11 && pnpm build

      - name: Run BUILD_ID prefix integration test
        run: pnpm test:integration:build-id-prefix

  integration-cache-components:
    runs-on: ubuntu-latest
    needs: lint-and-unit
    strategy:
      matrix:
        cache-components-app:
          - next-app-16-0-11-cache-components
          - next-app-16-2-6-cache-components
          - next-app-16-3-0-cache-components
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Build project
        run: pnpm build

      - name: Start Redis
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: '7'
          redis-port: 6379

      - name: Install redis-cli
        run: sudo apt-get update && sudo apt-get install -y redis-tools

      - name: Configure Redis Keyspace Notifications
        run: redis-cli config set notify-keyspace-events Exe

      - name: Install test project
        run: cd test/nextjs-test-projects/${{ matrix.cache-components-app }} && pnpm install

      - name: Build test project
        run: cd test/nextjs-test-projects/${{ matrix.cache-components-app }} && pnpm build

      - name: Run cache-components integration tests
        run: pnpm test:integration:cache-components
        env:
          CACHE_COMPONENTS_APP: ${{ matrix.cache-components-app }}

  e2e:
    runs-on: ubuntu-latest
    needs: lint-and-unit
    strategy:
      matrix:
        cache-components-app:
          - next-app-16-0-11-cache-components
          - next-app-16-2-6-cache-components
          - next-app-16-3-0-cache-components
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        run: corepack enable

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Build project
        run: pnpm build

      - name: Start Redis
        uses: supercharge/redis-github-action@1.8.0
        with:
          redis-version: '7'
          redis-port: 6379

      - name: Install redis-cli
        run: sudo apt-get update && sudo apt-get install -y redis-tools

      - name: Configure Redis Keyspace Notifications
        run: redis-cli config set notify-keyspace-events Exe

      - name: Install test project
        run: cd test/nextjs-test-projects/${{ matrix.cache-components-app }} && pnpm install

      - name: Build test project
        run: cd test/nextjs-test-projects/${{ matrix.cache-components-app }} && pnpm build

      - name: Install Playwright browsers
        run: pnpm exec playwright install --with-deps

      - name: Run Playwright E2E tests
        run: pnpm test:e2e
        env:
          PLAYWRIGHT_TEST_APP: ${{ matrix.cache-components-app }}
