"""Shared test fixtures for the control pipeline test suite."""

import os
import sys
import pytest

# Ensure control-pipeline is in path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))


@pytest.fixture(scope="session")
def db_session():
    """DB session for integration tests — skip if no DATABASE_URL."""
    url = os.getenv("DATABASE_URL")
    if not url:
        pytest.skip("DATABASE_URL not set — skipping DB tests")
    from db.session import SessionLocal
    db = SessionLocal()
    yield db
    db.close()


@pytest.fixture
def sample_controls(db_session):
    """Load 100 random draft controls for regression testing."""
    from sqlalchemy import text
    rows = db_session.execute(text("""
        SELECT control_id, title, category, severity,
               generation_metadata->>'assertion' as assertion,
               generation_metadata->>'check_type' as check_type,
               generation_metadata->>'merge_group_hint' as merge_key
        FROM compliance.canonical_controls
        WHERE release_state = 'draft' AND decomposition_method = 'pass0b'
        ORDER BY random() LIMIT 100
    """)).fetchall()
    return [dict(r._mapping) for r in rows]
