"""Perf-gate harness (M8): a dedicated 90k-row scratch DB on the sidecar
Postgres, built by the SAME seed_100x multiplier used for the full 100x
pass. 90k with an even two-year date spread is enough that a selective
date range and the default date-ordered LIMIT are index-served — so the
no-seq-scan gate reflects production behavior at scale, not a
small-table artifact (a tiny table always seq-scans, correctly).

This realizes the CI-testable half of the performance baseline
(04-architecture; T-RPT-006 re-tiered to "the M8 perf gate").
"""

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

from app.services.authz.context import resolve_actor
from scripts.seed_100x import _base_url, _with_db, build_perf_database
from tests.authz.conftest import fake_request

PERF_DB = "with_perf_test"
PERF_ENTRIES = 90_000


@pytest.fixture(scope="session")
def perf_engine():
    base = _base_url()
    target = _with_db(base, PERF_DB)
    report = build_perf_database(
        target_url=target, total_entries=PERF_ENTRIES, base_url=base
    )
    assert report["total"] >= PERF_ENTRIES, report
    engine = create_engine(target)
    yield engine
    engine.dispose()


@pytest.fixture()
def perf_session(perf_engine):
    with Session(perf_engine) as session:
        yield session


@pytest.fixture()
def perf_rian(perf_session):
    actor = resolve_actor(fake_request("rian"), perf_session)
    assert actor.role == "owner"
    return actor
