"""市場情報平台種子資料規劃。

本模組只產生可審核的 seed plan，不執行資料庫寫入。
"""

from services.market_intel.adapters import get_adapter, get_adapter_registry


def _adapter_to_seed(adapter):
    policy = adapter.safety_policy.to_dict()
    sources = [source.to_dict() for source in adapter.campaign_sources()]
    return {
        "code": adapter.platform_code,
        "name": adapter.platform_name,
        "base_url": adapter.base_url,
        "enabled": False,
        "crawl_policy_json": {
            "request_interval_sec": policy["request_interval_sec"],
            "timeout_sec": policy["timeout_sec"],
            "max_pages_per_run": policy["max_pages_per_run"],
            "allow_login": policy["allow_login"],
            "allow_database_write": policy["allow_database_write"],
            "allow_scheduler_attach": policy["allow_scheduler_attach"],
            "seed_source_keys": [source["source_key"] for source in sources],
        },
        "source_count": len(sources),
        "sources": sources,
        "write_action": "upsert_market_platform_after_gate_approval",
    }


def build_platform_seed_rows(platform_code="all"):
    """根據已註冊 adapter 建立平台 seed rows，不碰 DB。"""
    if platform_code and platform_code != "all":
        adapter = get_adapter(platform_code)
        if not adapter:
            return []
        return [_adapter_to_seed(adapter)]

    return [
        _adapter_to_seed(adapter)
        for adapter in get_adapter_registry().values()
    ]
