name: Nightly E2E Tests

on:
  schedule:
    - cron: '0 3 * * *'  # 3 AM UTC daily
  workflow_dispatch:       # Allow manual trigger

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-D warnings"
  RUSTC_WRAPPER: /usr/local/bin/sccache
  SCCACHE_DIR: /tmp/sccache
  TEST_MONGODB_URI: "mongodb://root:example@mongo:27017/?authSource=admin"

concurrency:
  group: nightly-e2e
  cancel-in-progress: true

jobs:
  e2e:
    name: E2E Tests
    runs-on: docker
    container:
      image: rust:1.94-bookworm
    services:
      mongo:
        image: mongo:7
        env:
          MONGO_INITDB_ROOT_USERNAME: root
          MONGO_INITDB_ROOT_PASSWORD: example
    steps:
      - name: Checkout
        run: |
          git init
          git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          git fetch --depth=1 origin "${GITHUB_SHA:-refs/heads/main}"
          git checkout FETCH_HEAD

      - name: Install sccache
        run: |
          curl -fsSL https://github.com/mozilla/sccache/releases/download/v0.9.1/sccache-v0.9.1-x86_64-unknown-linux-musl.tar.gz \
            | tar xz --strip-components=1 -C /usr/local/bin/ sccache-v0.9.1-x86_64-unknown-linux-musl/sccache
          chmod +x /usr/local/bin/sccache
        env:
          RUSTC_WRAPPER: ""

      - name: Run E2E tests
        run: cargo test -p compliance-agent --test e2e -- --test-threads=4

      - name: Show sccache stats
        run: sccache --show-stats
        if: always()
