"""pytest setup for the tide-table pipeline tests.

Puts the tide-table dir on sys.path so the pipeline modules import as top-level
(`import tidal_score`, `import build_catalog_view`) exactly as they do in prod.
"""
import sys
from pathlib import Path

import pytest

HERE = Path(__file__).resolve().parent
TIDE_TABLE = HERE.parent
REPO = TIDE_TABLE.parent.parent          # …/Sound/Tidal
sys.path.insert(0, str(TIDE_TABLE))


@pytest.fixture(scope="session")
def repo():
    return REPO


@pytest.fixture(scope="session")
def fixtures():
    return HERE / "fixtures"


@pytest.fixture(scope="session")
def view():
    """The real pipeline output, built once (IT-on-real-data). Skips cleanly when the
    site content (corner C, in the www repo) is unavailable — e.g. www checked out to a
    branch without next/content/lives — since these IT tests depend on that external repo."""
    import build_catalog_view as bcv
    try:
        v = bcv.build()
    except RuntimeError as e:
        pytest.skip(f"site content unavailable: {e}")
    if not (v["tracks"] if isinstance(v, dict) else v.tracks):
        pytest.skip("site content unavailable (0 tracks) — www on a contentless branch?")
    return v


@pytest.fixture(scope="session")
def registry():
    """The real pattern registry, built once (IT-on-real-data). Skips when the site
    content is unavailable (same external www dependency as `view`)."""
    import pattern_ngrams as pn
    try:
        r = pn.build()
    except RuntimeError as e:
        pytest.skip(f"site content unavailable: {e}")
    if not r.get("tracks"):
        pytest.skip("site content unavailable (0 tracks) — www on a contentless branch?")
    return r
