name: Nightly Quality

on:
  schedule:
    # Run at midnight UTC every day
    - cron: "0 0 * * *"
  workflow_dispatch:
    # Allow manual trigger for testing

env:
  UV_VERSION: "0.8.0"
  PYTHON_VERSION: "3.13"
  NOX_DEFAULT_VENV_BACKEND: "uv"

jobs:
  full-quality-suite:
    runs-on: docker
    container:
      image: python:3.13-slim
    steps:
      - name: Install system dependencies (git for merge tests)
        run: |
          apt-get update && apt-get install -y -qq git && rm -rf /var/lib/apt/lists/*

      - uses: actions/checkout@v4

      - name: Install uv
        run: |
          pip install -q uv==${{ env.UV_VERSION }}

      - name: Install nox with uv backend
        run: |
          uv pip install --system nox

      - name: Run lint checks via nox
        run: |
          nox -s lint
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run format check via nox
        run: |
          nox -s format -- --check
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run type checking via nox
        run: |
          nox -s typecheck
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run security scan via nox
        run: |
          nox -s security_scan
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run dead code detection via nox
        run: |
          nox -s dead_code
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run complexity analysis via nox
        run: |
          nox -s complexity
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run full Behave test suite with coverage
        run: |
          nox -s unit_tests-${{ env.PYTHON_VERSION }}
          nox -s coverage_report
        env:
          NOX_DEFAULT_VENV_BACKEND: uv

      - name: Run quality gates script
        run: |
          python scripts/check-quality-gates.py --coverage-min 96.5 --complexity-max F || echo "Quality gates script not found or failed, skipping..."

      - name: Generate quality trend data
        run: |
          python -c "
          import json, datetime
          from pathlib import Path

          report = {
              'timestamp': datetime.datetime.now(datetime.timezone.utc).isoformat(),
              'gates': {}
          }

          # Coverage (nox coverage_report writes to build/coverage.json)
          cov_path = Path('build/coverage.json')
          if cov_path.exists():
              cov_data = json.loads(cov_path.read_text())
              summary = cov_data.get('summary') or cov_data.get('totals') or {}
              report['gates']['coverage'] = summary.get('percent_covered', 0)

          # Security (nox security_scan writes to build/bandit-report.json)
          sec_path = Path('build/bandit-report.json')
          if sec_path.exists():
              sec_data = json.loads(sec_path.read_text())
              report['gates']['security_issues'] = len(sec_data.get('results', []))

          Path('build/reports').mkdir(parents=True, exist_ok=True)
          Path('build/reports/quality-trend.json').write_text(json.dumps(report, indent=2))
          print(json.dumps(report, indent=2))
          "

      - name: Upload quality reports
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: nightly-quality-reports
          path: build/reports/
          retention-days: 90
