name: CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    container:
      image: ubuntu:25.10
    steps:
      - name: Install git
        run: |
          apt-get update
          apt-get install -y git curl

      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: ./scripts/shepherd deps install build

      - name: Add Rust to PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Cache cargo registry and 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: Build
        run: |
          . "$HOME/.cargo/env"
          ./scripts/shepherd build

  test:
    name: Test
    runs-on: ubuntu-latest
    container:
      image: ubuntu:25.10
    steps:
      - name: Install git
        run: |
          apt-get update
          apt-get install -y git curl

      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: ./scripts/shepherd deps install build

      - name: Add Rust to PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Cache cargo registry and 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 tests
        run: |
          . "$HOME/.cargo/env"
          cargo test --all-targets

  lint:
    name: Clippy
    runs-on: ubuntu-latest
    container:
      image: ubuntu:25.10
    steps:
      - name: Install git
        run: |
          apt-get update
          apt-get install -y git curl

      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: ./scripts/shepherd deps install build

      - name: Add clippy component
        run: |
          . "$HOME/.cargo/env"
          rustup component add clippy

      - name: Add Rust to PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Cache cargo registry and 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 Clippy
        run: |
          . "$HOME/.cargo/env"
          cargo clippy --all-targets -- -D warnings

  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    container:
      image: ubuntu:25.10
    steps:
      - name: Install git
        run: |
          apt-get update
          apt-get install -y git curl

      - uses: actions/checkout@v4

      - name: Install build dependencies
        run: ./scripts/shepherd deps install build

      - name: Add rustfmt component
        run: |
          . "$HOME/.cargo/env"
          rustup component add rustfmt

      - name: Add Rust to PATH
        run: echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Cache cargo registry and 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: Check formatting
        run: |
          . "$HOME/.cargo/env"
          cargo fmt --all -- --check

  shellcheck:
    name: ShellCheck
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install ShellCheck
        run: |
          sudo apt-get update
          sudo apt-get install -y shellcheck

      - name: Run ShellCheck
        run: |
          # SC1091: Not following sourced files (info only, safe to ignore)
          shellcheck -e SC1091 scripts/shepherd scripts/dev scripts/admin
          shellcheck -e SC1091 scripts/lib/*.sh
          shellcheck -e SC1091 run-dev
