# app/modules/inventory/definition.py
"""
Inventory module definition.

Defines the inventory 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_admin_router():
    """Lazy import of admin router to avoid circular imports."""
    from app.modules.inventory.routes.admin import router

    return router


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

    return router


def _get_metrics_provider():
    """Lazy import of metrics provider to avoid circular imports."""
    from app.modules.inventory.services.inventory_metrics import (
        inventory_metrics_provider,
    )

    return inventory_metrics_provider


def _get_feature_provider():
    """Lazy import of feature provider to avoid circular imports."""
    from app.modules.inventory.services.inventory_features import (
        inventory_feature_provider,
    )

    return inventory_feature_provider


# Inventory module definition
inventory_module = ModuleDefinition(
    code="inventory",
    name="Inventory Management",
    description=(
        "Stock level tracking, inventory locations, low stock alerts, "
        "transaction history, and bulk imports."
    ),
    version="1.0.0",
    requires=["catalog"],  # Depends on catalog module for Product model
    # Module-driven permissions
    permissions=[
        PermissionDefinition(
            id="stock.view",
            label_key="inventory.permissions.stock_view",
            description_key="inventory.permissions.stock_view_desc",
            category="stock",
        ),
        PermissionDefinition(
            id="stock.edit",
            label_key="inventory.permissions.stock_edit",
            description_key="inventory.permissions.stock_edit_desc",
            category="stock",
        ),
        PermissionDefinition(
            id="stock.transfer",
            label_key="inventory.permissions.stock_transfer",
            description_key="inventory.permissions.stock_transfer_desc",
            category="stock",
        ),
    ],
    features=[
        "inventory_basic",  # Basic stock tracking
        "inventory_locations",  # Multiple warehouse locations
        "low_stock_alerts",  # Automated low stock notifications
        "inventory_purchase_orders",  # Purchase order management
        "product_management",  # Product catalog CRUD
        "inventory_transactions",  # Stock movement history
        "inventory_import",  # Bulk inventory import
    ],
    menu_items={
        FrontendType.ADMIN: [
            "inventory",  # Platform-wide inventory view
            "store-products",  # Product catalog management
        ],
        FrontendType.STORE: [
            "products",  # Store product catalog
            "inventory",  # Store inventory management
        ],
    },
    # New module-driven menu definitions
    menus={
        FrontendType.ADMIN: [
            MenuSectionDefinition(
                id="storeOps",
                label_key="inventory.menu.store_operations",
                icon="cube",
                order=40,
                items=[
                    MenuItemDefinition(
                        id="store-products",
                        label_key="inventory.menu.products",
                        icon="cube",
                        route="/admin/store-products",
                        order=10,
                    ),
                    MenuItemDefinition(
                        id="inventory",
                        label_key="inventory.menu.inventory",
                        icon="archive",
                        route="/admin/inventory",
                        order=30,
                    ),
                ],
            ),
        ],
        FrontendType.STORE: [
            MenuSectionDefinition(
                id="products",
                label_key="inventory.menu.products_inventory",
                icon="package",
                order=10,
                items=[
                    MenuItemDefinition(
                        id="inventory",
                        label_key="inventory.menu.inventory",
                        icon="clipboard-list",
                        route="/store/{store_code}/inventory",
                        order=20,
                        requires_permission="stock.view",
                    ),
                ],
            ),
        ],
    },
    is_core=False,
    # =========================================================================
    # Self-Contained Module Configuration
    # =========================================================================
    is_self_contained=True,
    services_path="app.modules.inventory.services",
    models_path="app.modules.inventory.models",
    schemas_path="app.modules.inventory.schemas",
    exceptions_path="app.modules.inventory.exceptions",
    migrations_path="migrations",
    # Metrics provider for dashboard statistics
    metrics_provider=_get_metrics_provider,
    # Feature provider for feature flags
    feature_provider=_get_feature_provider,
)


def get_inventory_module_with_routers() -> ModuleDefinition:
    """
    Get inventory module with routers attached.

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


__all__ = ["inventory_module", "get_inventory_module_with_routers"]
