# app/modules/tenancy/definition.py
"""
Tenancy Management module definition.

Platform, merchant, store, and admin user management.
Required for multi-tenant operation - cannot be disabled.
"""

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


def _get_metrics_provider():
    """Lazy import of metrics provider to avoid circular imports."""
    from app.modules.tenancy.services.tenancy_metrics import tenancy_metrics_provider

    return tenancy_metrics_provider


def _get_widget_provider():
    """Lazy import of widget provider to avoid circular imports."""
    from app.modules.tenancy.services.tenancy_widgets import tenancy_widget_provider

    return tenancy_widget_provider


def _get_feature_provider():
    """Lazy import of feature provider to avoid circular imports."""
    from app.modules.tenancy.services.tenancy_features import tenancy_feature_provider

    return tenancy_feature_provider


def _get_onboarding_provider():
    """Lazy import of onboarding provider to avoid circular imports."""
    from app.modules.tenancy.services.tenancy_onboarding_service import (
        tenancy_onboarding_provider,
    )

    return tenancy_onboarding_provider


tenancy_module = ModuleDefinition(
    code="tenancy",
    name="Tenancy Management",
    description="Platform, merchant, store, and admin user management. Required for multi-tenant operation.",
    version="1.0.0",
    is_core=True,
    is_self_contained=True,
    # Module-driven permissions
    permissions=[
        PermissionDefinition(
            id="team.view",
            label_key="tenancy.permissions.team_view",
            description_key="tenancy.permissions.team_view_desc",
            category="team",
        ),
        PermissionDefinition(
            id="team.invite",
            label_key="tenancy.permissions.team_invite",
            description_key="tenancy.permissions.team_invite_desc",
            category="team",
            is_owner_only=True,
        ),
        PermissionDefinition(
            id="team.edit",
            label_key="tenancy.permissions.team_edit",
            description_key="tenancy.permissions.team_edit_desc",
            category="team",
            is_owner_only=True,
        ),
        PermissionDefinition(
            id="team.remove",
            label_key="tenancy.permissions.team_remove",
            description_key="tenancy.permissions.team_remove_desc",
            category="team",
            is_owner_only=True,
        ),
    ],
    features=[
        "platform_management",
        "merchant_management",
        "store_management",
        "admin_user_management",
    ],
    # Legacy menu_items
    menu_items={
        FrontendType.ADMIN: [
            "platforms",
            "merchants",
            "stores",
            "admin-users",
            "merchant-users",
            "store-roles",
        ],
        FrontendType.STORE: [
            "team",
            "roles",
        ],
        FrontendType.MERCHANT: [
            "my_account",
            "stores",
            "profile",
        ],
    },
    # New module-driven menu definitions
    menus={
        FrontendType.ADMIN: [
            MenuSectionDefinition(
                id="userManagement",
                label_key="tenancy.menu.user_management",
                icon="users",
                order=10,
                items=[
                    MenuItemDefinition(
                        id="admin-users",
                        label_key="tenancy.menu.admin_users",
                        icon="shield",
                        route="/admin/admin-users",
                        order=10,
                        is_mandatory=True,
                        is_super_admin_only=True,
                    ),
                    MenuItemDefinition(
                        id="merchant-users",
                        label_key="tenancy.menu.merchant_users",
                        icon="user-group",
                        route="/admin/merchant-users",
                        order=20,
                        is_mandatory=True,
                    ),
                    MenuItemDefinition(
                        id="store-roles",
                        label_key="tenancy.menu.store_roles",
                        icon="shield-check",
                        route="/admin/store-roles",
                        order=30,
                    ),
                ],
            ),
            MenuSectionDefinition(
                id="platformAdmin",
                label_key="tenancy.menu.platform_admin",
                icon="office-building",
                order=20,
                items=[
                    MenuItemDefinition(
                        id="merchants",
                        label_key="tenancy.menu.merchants",
                        icon="office-building",
                        route="/admin/merchants",
                        order=10,
                        is_mandatory=True,
                    ),
                    MenuItemDefinition(
                        id="stores",
                        label_key="tenancy.menu.stores",
                        icon="shopping-bag",
                        route="/admin/stores",
                        order=20,
                        is_mandatory=True,
                    ),
                    MenuItemDefinition(
                        id="platforms",
                        label_key="tenancy.menu.platforms",
                        icon="globe-alt",
                        route="/admin/platforms",
                        order=5,
                    ),
                ],
            ),
        ],
        FrontendType.MERCHANT: [
            MenuSectionDefinition(
                id="account",
                label_key="tenancy.menu.account",
                icon="cog",
                order=900,
                items=[
                    MenuItemDefinition(
                        id="my_account",
                        label_key="tenancy.menu.my_account",
                        icon="user-circle",
                        route="/merchants/account/my-account",
                        order=5,
                    ),
                    MenuItemDefinition(
                        id="stores",
                        label_key="tenancy.menu.stores",
                        icon="shopping-bag",
                        route="/merchants/account/stores",
                        order=10,
                    ),
                    MenuItemDefinition(
                        id="team",
                        label_key="tenancy.menu.team",
                        icon="user-group",
                        route="/merchants/account/team",
                        order=15,
                    ),
                    MenuItemDefinition(
                        id="profile",
                        label_key="tenancy.menu.business_profile",
                        icon="user",
                        route="/merchants/account/profile",
                        order=20,
                    ),
                ],
            ),
        ],
        FrontendType.STORE: [
            MenuSectionDefinition(
                id="account",
                label_key="tenancy.menu.account_settings",
                icon="user-group",
                order=900,
                items=[
                    MenuItemDefinition(
                        id="team",
                        label_key="tenancy.menu.team",
                        icon="user-group",
                        route="/store/{store_code}/team",
                        order=5,
                        requires_permission="team.view",
                    ),
                    MenuItemDefinition(
                        id="roles",
                        label_key="tenancy.menu.roles",
                        icon="shield-check",
                        route="/store/{store_code}/team/roles",
                        order=10,
                        requires_permission="team.view",
                    ),
                ],
            ),
        ],
    },
    migrations_path="migrations",
    services_path="app.modules.tenancy.services",
    models_path="app.modules.tenancy.models",
    schemas_path="app.modules.tenancy.schemas",
    exceptions_path="app.modules.tenancy.exceptions",
    # Metrics provider for dashboard statistics
    metrics_provider=_get_metrics_provider,
    # Widget provider for dashboard widgets
    widget_provider=_get_widget_provider,
    feature_provider=_get_feature_provider,
    onboarding_provider=_get_onboarding_provider,
)

__all__ = ["tenancy_module"]
