name: CI

on:
  push:
    branches: ["**"]
  pull_request:
    branches: ["**"]

jobs:
  # ────────────────────────────────────────────────────────────────────────────
  # Format — checks that code conforms to rustfmt
  # ────────────────────────────────────────────────────────────────────────────
  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libfontconfig1-dev pkg-config

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

      - name: cargo fmt --check
        run: cargo fmt --check

  # ────────────────────────────────────────────────────────────────────────────
  # Clippy — zero warnings allowed
  # ────────────────────────────────────────────────────────────────────────────
  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    needs: fmt
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libfontconfig1-dev pkg-config

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

      - name: cargo clippy — default features
        run: cargo clippy -- -D warnings

      # v0.3.0+ — activate when optional features are declared in Cargo.toml
      # - name: cargo clippy — feature async
      #   run: cargo clippy --features async -- -D warnings

  # ────────────────────────────────────────────────────────────────────────────
  # Test — unit, integration and doc-test suite
  # ────────────────────────────────────────────────────────────────────────────
  test:
    name: Test
    runs-on: ubuntu-latest
    needs: fmt
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libfontconfig1-dev pkg-config

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

      - name: cargo test — default features
        run: cargo test

      - name: cargo test — doc examples
        run: cargo test --doc

      # v0.5.0+ — activate when async feature flag is declared in Cargo.toml
      # - name: cargo test — feature async
      #   run: cargo test --features async

  # ────────────────────────────────────────────────────────────────────────────
  # Doc — generation and deployment to GitHub Pages
  # Triggered on push to master only
  # ────────────────────────────────────────────────────────────────────────────
  doc:
    name: Documentation
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/master' && github.event_name == 'push'
    needs: [clippy, test]
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libfontconfig1-dev pkg-config

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

      - name: cargo doc
        run: cargo doc --no-deps --document-private-items

      - name: Add root redirect to dynamic_cli/index.html
        run: |
          echo '<meta http-equiv="refresh" content="0; url=chrom_rs/index.html">' \
            > target/doc/index.html

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./target/doc
          enable_jekyll: false
