# app/modules/analytics/definition.py
"""
Analytics module definition.

Defines the analytics module including its features, menu items,
route configurations, and self-contained module settings.
"""

from app.modules.base import (
    MenuItemDefinition,
    MenuSectionDefinition,
    ModuleDefinition,
    PermissionDefinition,
)
from app.modules.enums import FrontendType


def _get_store_api_router():
    """Lazy import of store API router to avoid circular imports."""
    from app.modules.analytics.routes.api.store import router

    return router


def _get_store_page_router():
    """Lazy import of store page router to avoid circular imports."""
    from app.modules.analytics.routes.pages.store import router

    return router


def _get_feature_provider():
    """Lazy import of feature provider to avoid circular imports."""
    from app.modules.analytics.services.analytics_features import (
        analytics_feature_provider,
    )

    return analytics_feature_provider


# Analytics module definition
analytics_module = ModuleDefinition(
    code="analytics",
    name="Analytics & Reporting",
    description="Dashboard analytics, custom reports, and data exports.",
    version="1.0.0",
    features=[
        "basic_reports",  # Basic reporting
        "analytics_dashboard",  # Analytics dashboard
        "custom_reports",  # Custom report builder
        "export_reports",  # Export to CSV/Excel
        "usage_metrics",  # Usage and performance metrics
    ],
    # Module-driven permissions
    permissions=[
        PermissionDefinition(
            id="analytics.view",
            label_key="analytics.permissions.view",
            description_key="analytics.permissions.view_desc",
            category="analytics",
        ),
        PermissionDefinition(
            id="analytics.export",
            label_key="analytics.permissions.export",
            description_key="analytics.permissions.export_desc",
            category="analytics",
        ),
        PermissionDefinition(
            id="analytics.manage_dashboards",
            label_key="analytics.permissions.manage_dashboards",
            description_key="analytics.permissions.manage_dashboards_desc",
            category="analytics",
        ),
    ],
    menu_items={
        FrontendType.ADMIN: [
            # Analytics appears in dashboard for admin
        ],
        FrontendType.STORE: [
            "analytics",  # Store analytics page
        ],
    },
    # New module-driven menu definitions
    menus={
        FrontendType.STORE: [
            MenuSectionDefinition(
                id="main",
                label_key=None,
                icon=None,
                order=0,
                is_collapsible=False,
                items=[
                    MenuItemDefinition(
                        id="analytics",
                        label_key="analytics.menu.analytics",
                        icon="chart-bar",
                        route="/store/{store_code}/analytics",
                        order=20,
                        requires_permission="analytics.view",
                    ),
                ],
            ),
        ],
    },
    requires=["catalog", "inventory", "marketplace", "orders"],  # Imports from these modules
    is_core=False,
    # =========================================================================
    # Self-Contained Module Configuration
    # =========================================================================
    is_self_contained=True,
    services_path="app.modules.analytics.services",
    models_path="app.modules.analytics.models",
    schemas_path="app.modules.analytics.schemas",
    exceptions_path="app.modules.analytics.exceptions",
    # Module templates (namespaced as analytics/admin/*.html and analytics/store/*.html)
    templates_path="templates",
    # Module-specific translations (accessible via analytics.* keys)
    locales_path="locales",
    feature_provider=_get_feature_provider,
)


def get_analytics_module_with_routers() -> ModuleDefinition:
    """
    Get analytics module with routers attached.

    This function attaches the routers lazily to avoid circular imports
    during module initialization.

    Routers:
    - store_api_router: API endpoints for store analytics
    - store_page_router: Page routes for store analytics dashboard
    """
    analytics_module.store_api_router = _get_store_api_router()
    analytics_module.store_page_router = _get_store_page_router()
    return analytics_module


__all__ = ["analytics_module", "get_analytics_module_with_routers"]
