import pytest
from app.services.business_docs import DOC_REGISTRY, get_tier_doc_types, calculate_maximization_score


class TestDocRegistry:
    def test_registry_not_empty(self):
        assert len(DOC_REGISTRY) > 0

    def test_all_entries_have_required_fields(self):
        required = {"label", "tier", "temperature", "max_tokens", "icon"}
        for key, entry in DOC_REGISTRY.items():
            missing = required - set(entry.keys())
            assert not missing, f"{key} missing: {missing}"

    def test_temperatures_in_range(self):
        for key, entry in DOC_REGISTRY.items():
            assert 0 <= entry["temperature"] <= 2, f"{key} temperature out of range: {entry['temperature']}"


class TestGetTierDocTypes:
    @pytest.mark.parametrize("tier", ["business_docs", "marketing", "legal", "operations", "contracts", "calculators"])
    def test_known_tiers(self, tier):
        docs = get_tier_doc_types(tier)
        assert len(docs) > 0
        for d in docs:
            assert d["key"] in DOC_REGISTRY

    def test_unknown_tier_returns_empty(self):
        docs = get_tier_doc_types("nonexistent_tier_xyz")
        assert docs == []

    def test_all_tiers_covered(self):
        tiers = set(m["tier"] for m in DOC_REGISTRY.values())
        for tier in tiers:
            docs = get_tier_doc_types(tier)
            assert len(docs) > 0, f"Tier {tier} returned no docs"


class TestMaximizationScore:
    def test_empty_box_scores_zero(self):
        result = calculate_maximization_score({})
        assert result["total_score"] == 0
        assert result["total_possible"] > 0

    def test_with_some_docs(self):
        box = {"documents": {"business_plan": {"content": "A real plan", "generated_at": "2026-01-01"}}}
        result = calculate_maximization_score(box)
        assert result["total_generated"] == 1

    def test_next_actions_present(self):
        result = calculate_maximization_score({})
        assert "next_actions" in result
