# app/modules/marketplace/definition.py
"""
Marketplace module definition.

Defines the marketplace module including its features, menu items,
dependencies, route configurations, and scheduled tasks.

Note: This module requires the inventory module to be enabled.
"""

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


def _get_admin_router():
    """Lazy import of admin router to avoid circular imports."""
    from app.modules.marketplace.routes.api.admin import router

    return router


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

    return router


def _get_metrics_provider():
    """Lazy import of metrics provider to avoid circular imports."""
    from app.modules.marketplace.services.marketplace_metrics import (
        marketplace_metrics_provider,
    )

    return marketplace_metrics_provider


def _get_widget_provider():
    """Lazy import of widget provider to avoid circular imports."""
    from app.modules.marketplace.services.marketplace_widgets import (
        marketplace_widget_provider,
    )

    return marketplace_widget_provider


def _get_feature_provider():
    """Lazy import of feature provider to avoid circular imports."""
    from app.modules.marketplace.services.marketplace_features import (
        marketplace_feature_provider,
    )

    return marketplace_feature_provider


def _get_onboarding_provider():
    """Lazy import of onboarding provider to avoid circular imports."""
    from app.modules.marketplace.services.marketplace_onboarding_service import (
        marketplace_onboarding_provider,
    )

    return marketplace_onboarding_provider


# Marketplace module definition
marketplace_module = ModuleDefinition(
    code="marketplace",
    name="Marketplace (Letzshop)",
    description=(
        "Letzshop marketplace integration for product sync, order import, "
        "and catalog synchronization."
    ),
    version="1.0.0",
    requires=["inventory", "catalog", "orders"],  # Depends on inventory, catalog, and orders modules
    features=[
        "letzshop_sync",  # Sync products with Letzshop
        "marketplace_import",  # Import products from marketplace
        "product_sync",  # Bidirectional product sync
        "order_import",  # Import orders from marketplace
        "marketplace_analytics",  # Marketplace performance metrics
    ],
    # Module-driven permissions
    permissions=[
        PermissionDefinition(
            id="marketplace.view_integration",
            label_key="marketplace.permissions.view_integration",
            description_key="marketplace.permissions.view_integration_desc",
            category="marketplace",
        ),
        PermissionDefinition(
            id="marketplace.manage_integration",
            label_key="marketplace.permissions.manage_integration",
            description_key="marketplace.permissions.manage_integration_desc",
            category="marketplace",
        ),
        PermissionDefinition(
            id="marketplace.sync_products",
            label_key="marketplace.permissions.sync_products",
            description_key="marketplace.permissions.sync_products_desc",
            category="marketplace",
        ),
    ],
    menu_items={
        FrontendType.ADMIN: [
            "marketplace-letzshop",  # Marketplace monitoring
        ],
        FrontendType.STORE: [
            "marketplace",  # Store marketplace settings
            "letzshop",  # Letzshop integration
        ],
    },
    # New module-driven menu definitions
    menus={
        FrontendType.ADMIN: [
            MenuSectionDefinition(
                id="marketplace",
                label_key="marketplace.menu.marketplace",
                icon="shopping-cart",
                order=60,
                items=[
                    MenuItemDefinition(
                        id="marketplace-letzshop",
                        label_key="marketplace.menu.letzshop",
                        icon="shopping-cart",
                        route="/admin/marketplace/letzshop",
                        order=10,
                    ),
                ],
            ),
        ],
        FrontendType.STORE: [
            MenuSectionDefinition(
                id="products",
                label_key="marketplace.menu.products_inventory",
                icon="download",
                order=10,
                items=[
                    MenuItemDefinition(
                        id="marketplace",
                        label_key="marketplace.menu.marketplace_import",
                        icon="download",
                        route="/store/{store_code}/marketplace",
                        order=30,
                        requires_permission="marketplace.view_integration",
                    ),
                ],
            ),
            MenuSectionDefinition(
                id="sales",
                label_key="marketplace.menu.sales_orders",
                icon="external-link",
                order=20,
                items=[
                    MenuItemDefinition(
                        id="letzshop",
                        label_key="marketplace.menu.letzshop_orders",
                        icon="external-link",
                        route="/store/{store_code}/letzshop",
                        order=20,
                        requires_permission="marketplace.view_integration",
                    ),
                ],
            ),
        ],
    },
    is_core=False,
    # =========================================================================
    # Self-Contained Module Configuration
    # =========================================================================
    is_self_contained=True,
    services_path="app.modules.marketplace.services",
    models_path="app.modules.marketplace.models",
    schemas_path="app.modules.marketplace.schemas",
    exceptions_path="app.modules.marketplace.exceptions",
    tasks_path="app.modules.marketplace.tasks",
    migrations_path="migrations",
    # =========================================================================
    # Scheduled Tasks
    # =========================================================================
    scheduled_tasks=[
        ScheduledTask(
            name="marketplace.sync_store_directory",
            task="app.modules.marketplace.tasks.sync_tasks.sync_store_directory",
            schedule="0 2 * * *",  # Daily at 02:00
            options={"queue": "scheduled"},
        ),
    ],
    # Metrics provider for dashboard statistics
    metrics_provider=_get_metrics_provider,
    # Widget provider for dashboard widgets
    widget_provider=_get_widget_provider,
    # Feature provider for feature flags
    feature_provider=_get_feature_provider,
    # Onboarding provider for post-signup checklist
    onboarding_provider=_get_onboarding_provider,
)


def get_marketplace_module_with_routers() -> ModuleDefinition:
    """
    Get marketplace module with routers attached.

    This function attaches the routers lazily to avoid circular imports
    during module initialization.
    """
    marketplace_module.admin_router = _get_admin_router()
    marketplace_module.store_router = _get_store_router()
    return marketplace_module


__all__ = ["marketplace_module", "get_marketplace_module_with_routers"]
