from __future__ import annotations

from .config import load_runtime_config
from .contracts import DisplayPolicy, ModuleId, ModuleRegistryEntry, RuntimeConfig, Sensitivity, SourceType

CONTRACT_VERSION = "jarvis.module_snapshot.v1"


def get_module_registry(config: RuntimeConfig | None = None) -> list[ModuleRegistryEntry]:
    current = config or load_runtime_config()
    finance_source_type = SourceType.HTTP_API if current.finance_adapter_mode == "live_readonly" else SourceType.STATIC_CONFIG if current.finance_adapter_mode == "disabled" else SourceType.MOCK
    finance_notes = [f"adapter_mode={current.finance_adapter_mode}", "exact values blocked", "actions disabled"]
    health_source_type = SourceType.SAFE_MARKER if current.health_adapter_mode == "safe_marker" else SourceType.LOCAL_PROBE if current.health_adapter_mode == "local_probe" else SourceType.STATIC_CONFIG if current.health_adapter_mode == "disabled" else SourceType.MOCK
    health_notes = [f"adapter_mode={current.health_adapter_mode}", "metadata only", "detail links blocked", "raw health data blocked", "actions disabled"]
    autoshorts_source_type = SourceType.HTTP_API if current.autoshorts_adapter_mode == "live_readonly" else SourceType.LOCAL_PROBE if current.autoshorts_adapter_mode == "local_probe" else SourceType.STATIC_CONFIG if current.autoshorts_adapter_mode == "disabled" else SourceType.MOCK
    autoshorts_notes = [f"adapter_mode={current.autoshorts_adapter_mode}", "read-only", "media access blocked", "actions disabled"]
    family_notes = ["adapter_mode=link_only", "read-only handoff", "actions disabled"]
    return [
        ModuleRegistryEntry(
            module_id=ModuleId.FINANCE,
            title="FinanceManager",
            enabled=True,
            sensitivity=Sensitivity.SENSITIVE,
            source_type=finance_source_type,
            base_url_env="FINANCE_API_BASE_URL",
            legacy_url_env="FINANCE_LEGACY_DASHBOARD_URL",
            runtime_base_env=None,
            contract_version=CONTRACT_VERSION,
            default_display_policy=DisplayPolicy.SUMMARY,
            actions_enabled=False,
            notes=finance_notes,
        ),
        ModuleRegistryEntry(
            module_id=ModuleId.HEALTH,
            title="HealthManager",
            enabled=True,
            sensitivity=Sensitivity.CRITICAL,
            source_type=health_source_type,
            base_url_env=None,
            legacy_url_env="HEALTH_LEGACY_DASHBOARD_URL",
            runtime_base_env="HEALTH_RUNTIME_BASE" if current.health_adapter_mode == "local_probe" else "HEALTH_SAFE_STATUS_MARKER_PATH" if current.health_adapter_mode == "safe_marker" else None,
            contract_version=CONTRACT_VERSION,
            default_display_policy=DisplayPolicy.SUMMARY,
            actions_enabled=False,
            notes=health_notes,
        ),
        ModuleRegistryEntry(
            module_id=ModuleId.AUTOSHORTS,
            title="AutoShorts",
            enabled=True,
            sensitivity=Sensitivity.INTERNAL,
            source_type=autoshorts_source_type,
            base_url_env="AUTOSHORTS_API_BASE_URL",
            legacy_url_env="AUTOSHORTS_LEGACY_DASHBOARD_URL",
            runtime_base_env="AUTOSHORTS_RUNTIME_BASE" if current.autoshorts_adapter_mode == "local_probe" else None,
            contract_version=CONTRACT_VERSION,
            default_display_policy=DisplayPolicy.SUMMARY,
            actions_enabled=False,
            notes=autoshorts_notes,
        ),
        ModuleRegistryEntry(
            module_id=ModuleId.FAMILY,
            title="FamilyDashboard",
            enabled=True,
            sensitivity=Sensitivity.INTERNAL,
            source_type=SourceType.STATIC_CONFIG,
            base_url_env=None,
            legacy_url_env="FAMILY_LEGACY_DASHBOARD_URL",
            runtime_base_env=None,
            contract_version=CONTRACT_VERSION,
            default_display_policy=DisplayPolicy.SUMMARY,
            actions_enabled=False,
            notes=family_notes,
        ),
        ModuleRegistryEntry(
            module_id=ModuleId.SYSTEM,
            title="JARVIS System",
            enabled=True,
            sensitivity=Sensitivity.INTERNAL,
            source_type=SourceType.STATIC_CONFIG,
            base_url_env=None,
            legacy_url_env=None,
            runtime_base_env=None,
            contract_version=CONTRACT_VERSION,
            default_display_policy=DisplayPolicy.SUMMARY,
            actions_enabled=False,
            notes=["static mock status", "actions disabled"],
        ),
    ]
