"""Search registration for Reporting Engine entities.

Registers searchable specs with the global search engine (Cmd+K).
"""

from __future__ import annotations

from simorgh.core.search import SearchableSpec, register_searchable


def _search_reports(ctx, query: str, limit: int) -> list:
    """Find report definitions matching *query* within the tenant scope."""
    from simorgh.apps.reporting.models import ReportDefinition
    from simorgh.core.search import SearchHit

    qs = ReportDefinition.objects.filter(
        tenant=ctx.tenant,
        is_active=True,
        name__icontains=query,
    )[:limit]

    return [
        SearchHit(
            label=obj.name,
            description=obj.description or obj.code,
            entity=obj.code,
            url=f"/reporting/reports/{obj.pk}",
        )
        for obj in qs
    ]


register_searchable(
    SearchableSpec(
        entity="reporting.report",
        label_key="reporting.search.report",
        search=_search_reports,
        permission="reporting.reports.view",
    )
)
