name: CI

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

concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Format
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --check

  clippy:
    name: Clippy
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"
          components: clippy

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Run clippy
        run: cargo clippy --locked --all-targets -- -D warnings

  build:
    name: Build
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Cargo build
        run: cargo build --locked

  test:
    name: Test (${{ matrix.target }})
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            runner: macos-latest
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Run tests
        run: cargo test --locked --target ${{ matrix.target }}

  deny:
    name: Deny
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: EmbarkStudios/cargo-deny-action@v2

  msrv:
    name: MSRV (1.86)
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@1.86

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Check MSRV
        run: cargo check --locked

  doc:
    name: Doc
    runs-on: ubuntu-22.04
    env:
      RUSTDOCFLAGS: "-D warnings"
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Cargo doc
        run: cargo doc --no-deps --locked

  site-sync:
    name: Site sync
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - name: Verify embedded copies match sources
        run: sh scripts/site/check-sync.sh

  design-checks:
    name: Design / messages / keybindings
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - name: Design system enforcement
        run: ./scripts/check-design-system.sh

      - name: Messages centralization
        run: ./scripts/check-messages.sh

      - name: Keybinding invariants
        run: ./scripts/check-keybindings.sh

  smoke-tui:
    name: TUI smoke test
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev tmux

      - name: Build binary
        run: cargo build --locked

      - name: Run smoke test
        run: ./tests/smoke_tui.sh ./target/debug/purple

  visual-regression:
    name: Visual regression
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.95"

      - uses: Swatinem/rust-cache@v2

      - name: Install Linux dependencies
        run: sudo apt-get update && sudo apt-get install -y pkg-config libssl-dev

      - name: Run visual regression suite
        run: cargo test --locked --bin purple visual_regression

  nix-flake:
    name: Nix flake check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6

      - uses: DeterminateSystems/nix-installer-action@ef8a148080ab6020fd15196c2084a2eea5ff2d25  # v22

      - uses: DeterminateSystems/magic-nix-cache-action@565684385bcd71bad329742eefe8d12f2e765b39  # v13
        with:
          diagnostic-endpoint: ""

      - name: Validate flake and build package
        run: nix flake check
