name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Check (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

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

      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

      - name: Distribution manifest version gate
        run: scripts/check_distribution_versions.sh

      - name: Test
        run: cargo test

      - name: Theme contrast gate
        run: cargo test theme_contrast_guardrails -- --exact

      - name: Build release
        run: cargo build --release

  web-tests:
    name: Web Integration + Visual
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install JS deps
        run: npm ci

      - name: Install Playwright Chromium
        run: npx playwright install --with-deps chromium

      - name: Run Playwright suites
        run: npm run test:web

      - name: Upload visual artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: web-visual-snapshots
          path: artifacts/visual
          if-no-files-found: warn

      - name: Upload Playwright report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: playwright-report
          if-no-files-found: warn

  postgres-parity:
    name: Postgres Parity Suite
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: pftui_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres -d pftui_test"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      PFTUI_TEST_POSTGRES_URL: postgres://postgres:postgres@localhost:5432/pftui_test
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Run Postgres backend smoke test
        run: cargo test -q postgres_roundtrip_when_env_set

      - name: Run backend switch roundtrip tests
        run: |
          cargo test -q sqlite_export_import_into_postgres_when_env_set
          cargo test -q postgres_export_import_into_sqlite_when_env_set

      - name: Run setup backend selection tests
        run: cargo test -q commands::setup::tests::

      - name: Build pftui binary for parity script
        run: cargo build -q

      - name: Run sqlite vs postgres parity acceptance script
        env:
          PFTUI_BIN: target/debug/pftui
        run: scripts/parity_check.sh
