import json
from pathlib import Path

from scripts.build_marketing_capability_matrix import build_matrix
from src.domain.app_blocks.canonical import CANONICAL_BLOCK_IDS
from src.domain.app_blocks.catalogs import DEFAULT_KNOWN_PACKS
from src.domain.feature_packs.contract import RESOLVABLE_PRIMITIVES


def test_marketing_matrix_covers_every_page_with_canonical_requirements():
    payload = build_matrix()
    assert payload["page_count"] == 205
    assert payload["bundle_count"] > 1
    pages = [slug for bundle in payload["bundles"] for slug in bundle["pages"]]
    assert len(pages) == 205
    assert len(pages) == len(set(pages))
    for bundle in payload["bundles"]:
        assert set(bundle["app_blocks"]) <= CANONICAL_BLOCK_IDS
        assert set(bundle["feature_packs"]) <= DEFAULT_KNOWN_PACKS
        assert set(bundle["primitive_ids"]) <= RESOLVABLE_PRIMITIVES
        assert bundle["job_feature_packs"]


def test_job_backend_packs_participate_in_bundle_identity():
    payload = build_matrix()
    clinical = next(bundle for bundle in payload["bundles"] if "personal-injury-case-management" in bundle["pages"])
    generic = next(bundle for bundle in payload["bundles"] if "bug-tracking-issue-triage" in bundle["pages"])
    assert clinical["bundle_id"] != generic["bundle_id"]
    assert clinical["job_feature_packs"] == ["clinical_case_operations"]
    assert generic["job_feature_packs"] == ["case_operations"]
    assert "activity-timeline" in clinical["feature_packs"]


def test_committed_matrix_is_regenerated_and_current():
    path = Path(__file__).parents[1] / "docs/coverage/MARKETING_CAPABILITY_MATRIX.json"
    assert json.loads(path.read_text(encoding="utf-8")) == build_matrix()
