"""Views & Visualization — cross-module service contracts.

Other modules use these contracts to consume the visualization engine
without coupling to its internal implementation.
"""

from __future__ import annotations

from typing import Any

from simorgh.apps.visualization.registry import (
    list_chart_types,
    list_view_types,
    list_widget_types,
    register_chart_type,
    register_view_type,
    register_widget_type,
)


def get_available_view_types() -> list[dict[str, Any]]:
    """Return all registered view types as serialisable dicts.

    Every module can call this to discover which view types are available
    for rendering entity data.
    """
    types = list_view_types()
    return [
        {
            "kind": t.kind,
            "label_key": t.label_key,
            "description": t.description,
            "icon": t.icon,
            "supports_custom_config": t.supports_custom_config,
            "supports_saved_views": t.supports_saved_views,
            "supports_filters": t.supports_filters,
            "supports_export": t.supports_export,
            "supports_grouping": t.supports_grouping,
            "config_schema": t.config_schema,
        }
        for t in types
    ]


def get_available_chart_types() -> list[dict[str, Any]]:
    """Return all registered chart types as serialisable dicts."""
    types = list_chart_types()
    return [
        {"kind": t.kind, "label_key": t.label_key, "description": t.description, "icon": t.icon}
        for t in types
    ]


def get_available_widget_types(kind: str | None = None) -> list[dict[str, Any]]:
    """Return all registered widget types, optionally filtered by kind."""
    types = list_widget_types()
    if kind:
        types = [w for w in types if w.kind == kind]
    return [
        {
            "name": t.name,
            "kind": t.kind,
            "label_key": t.label_key,
            "description": t.description,
            "icon": t.icon,
            "default_cols": t.default_cols,
            "config_schema": t.config_schema,
            "module": t.module,
        }
        for t in types
    ]


def list_views_for_entity(
    *,
    tenant_id: int,
    entity_name: str,
    view_type: str | None = None,
) -> list[dict[str, Any]]:
    """Return all view configurations for a given entity.

    This is the primary contract that other modules use to discover
    and load view configurations for their entities.
    """
    from simorgh.apps.visualization.selectors import get_view_configurations

    qs = get_view_configurations(
        tenant_id=tenant_id,
        entity_name=entity_name,
        view_type=view_type,
    )
    return [
        {
            "id": v.pk,
            "public_id": str(v.public_id),
            "name": v.name,
            "entity_name": v.entity_name,
            "view_type": v.view_type,
            "config": v.config,
            "is_default": v.is_default,
            "is_system": v.is_system,
            "visibility": v.visibility,
            "sort_order": v.sort_order,
        }
        for v in qs
    ]


def list_dashboards_for_tenant(
    *,
    tenant_id: int,
    visibility: str | None = None,
) -> list[dict[str, Any]]:
    """Return all dashboards for a tenant with their widgets."""
    from simorgh.apps.visualization.selectors import get_dashboards

    qs = get_dashboards(tenant_id=tenant_id, visibility=visibility)
    return [
        {
            "id": d.pk,
            "public_id": str(d.public_id),
            "name": d.name,
            "label_key": d.label_key,
            "icon": d.icon,
            "is_default": d.is_default,
            "is_system": d.is_system,
            "visibility": d.visibility,
            "config": d.config,
            "sort_order": d.sort_order,
            "widgets": [
                {
                    "id": w.pk,
                    "widget_type": w.widget_type,
                    "title_key": w.title_key,
                    "row_index": w.row_index,
                    "col_span": w.col_span,
                    "sort_order": w.sort_order,
                    "config": w.config,
                }
                for w in d.widgets.all()
            ],
        }
        for d in qs
    ]


def list_charts_for_entity(
    *,
    tenant_id: int,
    entity_name: str,
) -> list[dict[str, Any]]:
    """Return all chart configurations for a given entity."""
    from simorgh.apps.visualization.selectors import get_chart_configurations

    qs = get_chart_configurations(tenant_id=tenant_id, entity_name=entity_name)
    return [
        {
            "id": c.pk,
            "public_id": str(c.public_id),
            "name": c.name,
            "entity_name": c.entity_name,
            "chart_type": c.chart_type,
            "config": c.config,
            "is_default": c.is_default,
            "visibility": c.visibility,
        }
        for c in qs
    ]


__all__ = [
    "get_available_chart_types",
    "get_available_view_types",
    "get_available_widget_types",
    "list_charts_for_entity",
    "list_dashboards_for_tenant",
    "list_views_for_entity",
    "register_chart_type",
    "register_view_type",
    "register_widget_type",
]
