# .github/workflows/ci.yml — slice-1 CI (spec §6.2).
# Gates: fmt --check, clippy -D warnings, test --all, cargo deny check.
name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  # Manual trigger for the twilio-live job (pre-release validation against
  # real Twilio credentials). Never runs per-PR — the routine clippy/test
  # jobs use default features (MockCallControlClient); the live
  # TwilioCallControlClient is feature-gated behind `twilio-live` + only the
  # maintainer exercises it before tagging a release (spec §1.2 + plan T6/T10).
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --check

  clippy:
    runs-on: ubuntu-latest
    services:
      valkey:
        image: valkey/valkey:latest
        ports:
          - 6379:6379
        options: >-
          --health-cmd "valkey-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 3
    env:
      VALKEY_URL: redis://127.0.0.1:6379/
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Install libopus (media crate FFI dep)
        run: apt-get update && apt-get install -y libopus-dev
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all -- -D warnings

  test:
    runs-on: ubuntu-latest
    services:
      valkey:
        image: valkey/valkey:latest
        ports:
          - 6379:6379
        options: >-
          --health-cmd "valkey-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 3
    strategy:
      matrix:
        toolchain: [stable, "1.85"]
    env:
      VALKEY_URL: redis://127.0.0.1:6379/
    steps:
      - uses: actions/checkout@v4

      # Seam gate (slice-4 §7 #3; re-pinned slice-5):
      #
      # `loop_driver.rs` stays byte-identical to slice-3 — the hot-path
      # poll loop is untouched. `rtc_session.rs` was re-pinned in slice-5
      # for MediaAddressConfig (2026-07-04 scalability review, B1:
      # bind/advertised address split + port range). If a legitimate
      # change is needed, update the pinned blob hashes below in the same
      # PR — loud and reviewable by design.
      - name: Seam gate — loop_driver frozen (slice-3) + rtc_session pinned (slice-5)
        run: |
          EXPECTED_LOOP_DRIVER='744bf314edf7f4925c8bb3bd0f5176dbc88f8113'
          EXPECTED_RTC_SESSION='f47d63b9a2883d37066a93c9daa0e2cf8816bec4'
          GOT_LOOP_DRIVER=$(git hash-object crates/rutster-media/src/loop_driver.rs)
          GOT_RTC_SESSION=$(git hash-object crates/rutster-media/src/rtc_session.rs)
          if [ "$GOT_LOOP_DRIVER" != "$EXPECTED_LOOP_DRIVER" ]; then
            echo "::error::loop_driver.rs blob mismatch: $GOT_LOOP_DRIVER != $EXPECTED_LOOP_DRIVER"
            exit 1
          fi
          if [ "$GOT_RTC_SESSION" != "$EXPECTED_RTC_SESSION" ]; then
            echo "::error::rtc_session.rs blob mismatch: $GOT_RTC_SESSION != $EXPECTED_RTC_SESSION"
            exit 1
          fi

      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.toolchain }}
      - name: Install libopus (media crate FFI dep)
        run: apt-get update && apt-get install -y libopus-dev
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --all

  deny:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: EmbarkStudios/cargo-deny-action@v2
        with:
          command: check

  # slice-4½ (ADR-0010): the CI-regressed threshold sweep. Default-off
  # `sim-bench` feature; runs `cargo test --all --features=sim-bench`
  # in a SEPARATE job per PR + nightly. A latency regression fails the
  # build the same way a broken test does. `--test-threads=1` is
  # load-bearing: concurrent sim-bench tests would contaminate each
  # other's shared gauge (the TickLagStats reads the SHARED tokio
  # runtime; concurrent sweeps across tests would all pollute the same
  # gauge). See crates/rutster-sim/src/thresholds.rs's
  # `bench_assertions` module docs + spec §5.4 + §6.5.
  sim-bench:
    name: sim-bench (stable)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Install libopus (media crate FFI dep)
        run: apt-get update && apt-get install -y libopus-dev
      - uses: Swatinem/rust-cache@v2
      - name: cargo fmt + clippy on sim-bench feature paths
        run: |
          cargo fmt --all --check
          cargo clippy --all --all-targets --features=sim-bench -- -D warnings
      - name: Run sim-bench threshold sweep
        run: cargo test --all --features=sim-bench -- --test-threads=1

  # The live TwilioCallControlClient is feature-gated behind `twilio-live`
  # (reqwest + rustls-tls + tracing + serde_json pulled in only when the
  # feature is on). This job exercises it against REAL Twilio credentials.
  # It runs ONLY on manual `workflow_dispatch` (never per-PR) — the routine
  # clippy/test jobs above use default features (MockCallControlClient).
  # The maintainer triggers this before tagging a release, after setting the
  # TWILIO_* secrets in the repo settings (spec §1.2 + plan T10 Step 3).
  twilio-live:
    name: twilio-live (manual only)
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch'
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Install libopus (media crate FFI dep)
        run: apt-get update && apt-get install -y libopus-dev
      - uses: Swatinem/rust-cache@v2
      - name: Clippy with twilio-live feature
        run: cargo clippy --all --all-targets --features=twilio-live -- -D warnings
      - name: Twilio-live tests (includes ignored live e2e; needs TWILIO_* secrets)
        env:
          RUTSTER_TWILIO_ACCOUNT_SID: ${{ secrets.TWILIO_ACCOUNT_SID }}
          RUTSTER_TWILIO_AUTH_TOKEN: ${{ secrets.TWILIO_AUTH_TOKEN }}
          RUTSTER_TWILIO_MEDIA_BIND: 0.0.0.0:8081
          RUTSTER_TWILIO_WEBHOOK_BASE: ${{ secrets.TWILIO_WEBHOOK_BASE }}
          RUTSTER_TWILIO_TEST_TO: ${{ secrets.TWILIO_TEST_TO }}
          RUTSTER_TWILIO_TEST_FROM: ${{ secrets.TWILIO_TEST_FROM }}
        run: cargo test --all --features=twilio-live -- --include-ignored

  # deploy-B §6.1 image-build — builds all four first-party images via
  # `docker buildx build --target` against deploy/Dockerfile. Runs AFTER the
  # fmt/clippy/test/deny/sim-bench matrix gates: the smoke job downstream
  # depends on these built images.
  #
  # Single builder invocation that builds all four --targets would be ideal
  # (shared cache), but buildx's `--target` is one-per-invocation — so four
  # build steps share the GHA cache via `cache-from/cache-to: type=gha`.
  # Warm caches take image-build from ~6 min to ~90s.
  image-build:
    name: image-build (4 images)
    runs-on: ubuntu-latest
    needs: [fmt, clippy, test, deny, sim-bench]
    steps:
      - uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      # libopus-dev — the headers the `opus` crate's build.rs expects.
      # (The builder stage of deploy/Dockerfile already installs this
      # inside the build image; this is a belt-and-braces step so a local
      # buildx cache root has the headers if the cache misses.)
      - name: Build-time host deps (belt-and-braces)
        run: sudo apt-get update && sudo apt-get install -y libopus-dev

      - name: Build rutster-engine image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: deploy/Dockerfile
          target: rutster-engine
          tags: rutster-engine:ci-${{ github.sha }}
          push: false
          load: true
          cache-from: type=gha,scope=rutster-engine
          cache-to: type=gha,mode=max,scope=rutster-engine

      - name: Build rutster-brain image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: deploy/Dockerfile
          target: rutster-brain
          tags: rutster-brain:ci-${{ github.sha }}
          push: false
          load: true
          cache-from: type=gha,scope=rutster-brain
          cache-to: type=gha,mode=max,scope=rutster-brain

      - name: Build rutster-edge image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: deploy/Dockerfile
          target: rutster-edge
          tags: rutster-edge:ci-${{ github.sha }}
          push: false
          load: true
          cache-from: type=gha,scope=rutster-edge
          cache-to: type=gha,mode=max,scope=rutster-edge

      - name: Build rutster-allinone image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: deploy/Dockerfile
          target: rutster-allinone
          tags: rutster-allinone:ci-${{ github.sha }}
          push: false
          load: true
          cache-from: type=gha,scope=rutster-allinone
          cache-to: type=gha,mode=max,scope=rutster-allinone

      - name: Smoke-tag the images for the downstream smoke job
        # The smoke job references images as `:smoke`
        # (deploy/smoke/compose_smoke.sh tags images with the
        # RUTSTER_IMAGES_TAG env). Tag the freshly-built :ci-<sha> images as
        # :smoke too, so the smoke job picks them up.
        run: |
          for img in rutster-engine rutster-brain rutster-edge rutster-allinone; do
            docker tag "${img}:ci-${{ github.sha }}" "${img}:smoke"
          done
          docker images | grep rutster

      - name: caddy validate
        # Validate the Caddyfile inside the freshly-built rutster-edge image.
        # Cheap regression against malformed Caddyfile (the local `caddy
        # validate` in Task 3 is optional; this one is mandatory in CI).
        run: |
          docker run --rm \
            -e RUTSTER_DOMAIN=pbx.example.com \
            -e RUTSTER_ACME_EMAIL=test@example.com \
            -v "$PWD/deploy/Caddyfile:/etc/caddy/Caddyfile:ro" \
            rutster-edge:smoke \
            caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile
