name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches-ignore:
      - 'rfc/**'

jobs:
  install:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

      - name: Install dependencies (cache warm-up)
        run: pnpm install --frozen-lockfile

  lint:
    needs: install
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Lint
        run: pnpm lint

  typecheck:
    needs: install
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Build packages for type resolution
        run: pnpm build:packages

      - name: Typecheck
        run: pnpm typecheck

  test:
    needs: install
    name: Test (${{ matrix.package }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package:
          - '@videojs/cli'
          - '@videojs/compiler'
          - '@videojs/core'
          - '@videojs/icons'
          - '@videojs/jsx'
          - '@videojs/media'
          - '@videojs/skins'
          - '@videojs/store'
          - '@videojs/utils'
          - '@videojs/element'
          - '@videojs/html'
          - '@videojs/react'

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Cache turbo test setup
        uses: actions/cache@v5
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-test-${{ github.sha }}-${{ matrix.package }}
          restore-keys: |
            ${{ runner.os }}-turbo-test-
            ${{ runner.os }}-turbo-

      - name: Test package
        run: pnpm turbo run test --filter="${{ matrix.package }}"

  test-spf:
    name: Test (@videojs/spf)
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/playwright:v1.59.1-noble
    env:
      PLAYWRIGHT_BROWSERS_PATH: /ms-playwright

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'

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

      - name: Test SPF
        run: pnpm turbo run test --filter="@videojs/spf" --env-mode=loose

  workspace:
    needs: install
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Check workspace consistency
        run: pnpm check:workspace

  build:
    needs: install
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js
        uses: actions/setup-node@v5
        with:
          node-version-file: '.nvmrc'
          cache: pnpm

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

      - name: Cache turbo build setup
        uses: actions/cache@v5
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-

      - name: Build
        run: pnpm build:packages

      - name: Build CDN bundles
        run: pnpm build:cdn
