import pytest
from modules.marketplace.manifests_catalog import BOT_MANIFESTS, REUSABLE_MODULES


@pytest.mark.unit
def test_marketplace_bot_manifests_catalog_integrity():
    assert len(BOT_MANIFESTS) == 25, f"Expected 25 ready-made bot manifests, got {len(BOT_MANIFESTS)}"

    valid_categories = {
        "business",
        "community",
        "ai_specialist",
        "developer_tools",
        "content_media",
    }

    category_counts = {}
    seen_ids = set()

    for manifest in BOT_MANIFESTS:
        # Unique ID check
        bot_id = manifest["id"]
        assert bot_id not in seen_ids, f"Duplicate bot manifest ID found: {bot_id}"
        seen_ids.add(bot_id)
        assert bot_id.startswith("bot_"), f"ID should start with 'bot_': {bot_id}"

        # Category validity
        cat = manifest["category"]
        assert cat in valid_categories, f"Invalid category {cat} in {bot_id}"
        category_counts[cat] = category_counts.get(cat, 0) + 1

        # Required fields
        assert "name" in manifest and len(manifest["name"]) > 0
        assert "headline" in manifest and len(manifest["headline"]) > 0
        assert "description" in manifest and len(manifest["description"]) > 20
        assert "version" in manifest
        assert "config_schema" in manifest and isinstance(manifest["config_schema"], dict)
        assert "default_config" in manifest and isinstance(manifest["default_config"], dict)
        assert "supported_channels" in manifest and "telegram" in manifest["supported_channels"]
        assert "required_permissions" in manifest and isinstance(manifest["required_permissions"], list)
        assert "system_templates" in manifest and isinstance(manifest["system_templates"], list)

    # Category count validation
    assert category_counts["business"] == 6
    assert category_counts["community"] == 6
    assert category_counts["ai_specialist"] == 6
    assert category_counts["developer_tools"] == 4
    assert category_counts["content_media"] == 3


@pytest.mark.unit
def test_reusable_marketplace_modules_catalog():
    assert len(REUSABLE_MODULES) == 8, f"Expected 8 reusable modules, got {len(REUSABLE_MODULES)}"

    seen_ids = set()
    for mod in REUSABLE_MODULES:
        mod_id = mod["id"]
        assert mod_id not in seen_ids, f"Duplicate module ID found: {mod_id}"
        seen_ids.add(mod_id)
        assert mod_id.startswith("module_"), f"ID should start with 'module_': {mod_id}"
        assert "name" in mod and len(mod["name"]) > 0
        assert "description" in mod
        assert "category" in mod
        assert "config_schema" in mod
