---
name: CI/CD Pipeline

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        rust: [stable, 1.75]
        features: [default, development, dev-full]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libostree-1-dev \
            libapt-pkg-dev \
            libpolkit-gobject-1-dev \
            bubblewrap \
            binutils \
            pkg-config \
            build-essential

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

      - name: Run Clippy
        run: cargo clippy --features ${{ matrix.features }} -- -D warnings

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

      - name: Build release
        run: cargo build --release --features ${{ matrix.features }}

      - name: Test development commands
        if: matrix.features != 'default'
        run: |
          cargo run --features ${{ matrix.features }} -- testutils --help
          cargo run --features ${{ matrix.features }} -- shlib-backend --help
          cargo run --features ${{ matrix.features }} -- internals --help

  build-debian:
    name: Build Debian Package
    runs-on: "ubuntu-latest"
    needs: test
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Install build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libostree-1-dev \
            libapt-pkg-dev \
            libpolkit-gobject-1-dev \
            bubblewrap \
            binutils \
            pkg-config \
            build-essential \
            devscripts \
            debhelper \
            dh-cargo

      - name: Build Debian package
        run: |
          ./build-debian-trixie.sh

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: debian-package
          path: |
            *.deb
            *.dsc
            *.tar.*

  security-audit:
    name: Security Audit
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        rust: [stable]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}

      - name: Install cargo-audit
        run: cargo install cargo-audit

      - name: Run security audit
        run: cargo audit

  dependency-audit:
    name: Dependency Audit
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        rust: [stable]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust }}

      - name: Install cargo-outdated
        run: cargo install cargo-outdated

      - name: Check for outdated dependencies
        run: cargo outdated

  documentation:
    name: Build Documentation
    runs-on: "ubuntu-latest"
    strategy:
      matrix:
        features: [default, development]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libostree-1-dev \
            libapt-pkg-dev \
            libpolkit-gobject-1-dev \
            pkg-config

      - name: Build documentation
        run: cargo doc --features ${{ matrix.features }} --no-deps

      - name: Upload documentation
        uses: actions/upload-artifact@v4
        with:
          name: docs-${{ matrix.features }}
          path: target/doc/
