name: Commercial Build

on:
  push:
    branches:
      - 'commercial/**'
  workflow_dispatch:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  commercial-build:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev \
            libgtk-3-dev \
            libayatana-appindicator3-dev \
            librsvg2-dev \
            libssl-dev \
            build-essential \
            pkg-config

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-pro-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-pro-

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - name: Install npm dependencies
        run: npm ci

      - name: Cargo check (pro features)
        run: cargo check --features pro

      - name: Cargo test (pro features)
        run: cargo test --features pro

      - name: Vitest (frontend)
        run: npm run test

      - name: Commercial tests
        run: |
          if [ -d "tests/commercial/" ] && ls tests/commercial/*.test.* 2>/dev/null; then
            npx vitest run tests/commercial/
          else
            echo "No commercial tests found, skipping."
          fi
