"""The meeting module must work for any integrating product, not just HelixtaHR."""

import re
from pathlib import Path

import pytest

from app.integrations.installed import build_registry
from tests.tokens import meeting_token

APP = Path(__file__).resolve().parent.parent / "app"
INTEGRATIONS = APP / "integrations"

# Product-specific names that belong only in app/integrations/<product>/.
PRODUCT_WORDS = re.compile(r"helixta|company_id|company_name|emp_no|role_name", re.IGNORECASE)


def test_meeting_module_never_mentions_a_product():
    offenders = [
        f"{path.relative_to(APP)}:{number}: {line.strip()}"
        for path in APP.rglob("*.py")
        if INTEGRATIONS not in path.parents
        for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), start=1)
        if PRODUCT_WORDS.search(line)
    ]
    assert offenders == [], "Product details belong in app/integrations/:\n" + "\n".join(offenders)


def test_enabled_product_is_built_from_its_own_settings(monkeypatch):
    monkeypatch.setenv("HELIXTAHR_ISSUER", "https://own-settings.test/login")
    monkeypatch.setenv("HELIXTAHR_JWT_SECRET", "x" * 64)
    monkeypatch.setenv("HELIXTAHR_BASE_URL", "https://own-settings.test")

    registry = build_registry(["helixtahr"])
    token = meeting_token(iss="https://own-settings.test/login", secret="x" * 64)

    assert registry.verify_token(token).product == "helixtahr"


def test_unknown_product_is_a_startup_error():
    with pytest.raises(ValueError, match="Unknown product"):
        build_registry(["eazeesign"])
