name: CI/CD with Staging Preset

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  install-validate:
    name: Validate Installation with Staging Preset
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
      fail-fast: false

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

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - name: Setup NuShell
        run: |
          if [[ "${{ runner.os }}" == "Linux" ]]; then
            cargo install nu
          else
            brew install nushell
          fi

      - name: Install development dependencies
        run: |
          if [[ "${{ runner.os }}" == "Linux" ]]; then
            sudo apt-get update
            sudo apt-get install -y cmake pkg-config libssl-dev
          fi

      - name: Validate presets configuration
        run: |
          nu tests/provisioning/test-presets.nu

      - name: Validate provctl detection
        run: |
          nu tests/provisioning/test-detect-provctl.nu --verbose

      - name: Generate staging preset config
        run: |
          nu scripts/provisioning/install-with-presets.nu \
            --preset staging \
            --generate-config > /tmp/staging-install.toml
          echo "Generated staging configuration:"
          cat /tmp/staging-install.toml

  build:
    name: Build and Test with Staging Preset
    runs-on: ubuntu-latest
    needs: install-validate
    services:
      docker:
        image: docker:dind
        options: --privileged

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

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

      - name: Setup NuShell
        run: cargo install nu

      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Run tests
        run: |
          # Run provisioning tests first
          nu tests/provisioning/test-all.nu

          # Run standard tests
          cargo test --all --lib

      - name: Run linting
        run: |
          cargo fmt --all -- --check
          cargo clippy --all --all-targets -- -D warnings

      - name: Run security audit
        run: cargo audit

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

  staging-integration:
    name: Integration Test with Docker Compose
    runs-on: ubuntu-latest
    needs: build

    services:
      docker:
        image: docker:dind
        options: --privileged

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

      - name: Install dependencies
        run: |
          apt-get update
          apt-get install -y curl docker-compose

      - name: Install NuShell
        run: curl -o nu https://releases.nushell.dev/latest && chmod +x nu && mv nu /usr/local/bin/

      - name: Start Docker daemon
        run: |
          dockerd -D &
          sleep 5

      - name: Validate staging preset services
        run: |
          nu scripts/provisioning/provctl-services.nu list staging

      - name: Check service definitions
        run: |
          nu scripts/provisioning/provctl-services.nu validate

  docs-check:
    name: Validate Documentation
    runs-on: ubuntu-latest

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

      - name: Check documentation exists
        run: |
          test -f docs/INSTALLATION_CONTEXTS.md || exit 1
          test -f CLAUDE.md || exit 1
          test -f configs/installation.toml || exit 1

      - name: Validate documentation format
        run: |
          # Basic markdown validation
          grep -q "# Installation Contexts" docs/INSTALLATION_CONTEXTS.md || exit 1
          grep -q "Installation with Presets" CLAUDE.md || exit 1

  summary:
    name: Test Summary
    runs-on: ubuntu-latest
    needs: [install-validate, build, docs-check]
    if: always()

    steps:
      - name: Check results
        run: |
          if [[ "${{ needs.install-validate.result }}" == "failure" ]] || \
             [[ "${{ needs.build.result }}" == "failure" ]] || \
             [[ "${{ needs.docs-check.result }}" == "failure" ]]; then
            echo "❌ Some jobs failed"
            exit 1
          else
            echo "✅ All checks passed!"
          fi
