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

Platform, company, vendor, 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


tenancy_module = ModuleDefinition(
    code="tenancy",
    name="Tenancy Management",
    description="Platform, company, vendor, 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",
        "company_management",
        "vendor_management",
        "admin_user_management",
    ],
    # Legacy menu_items
    menu_items={
        FrontendType.ADMIN: [
            "platforms",
            "companies",
            "vendors",
            "admin-users",
        ],
        FrontendType.VENDOR: [
            "team",
        ],
    },
    # New module-driven menu definitions
    menus={
        FrontendType.ADMIN: [
            MenuSectionDefinition(
                id="superAdmin",
                label_key="tenancy.menu.super_admin",
                icon="shield",
                order=10,
                is_super_admin_only=True,
                items=[
                    MenuItemDefinition(
                        id="admin-users",
                        label_key="tenancy.menu.admin_users",
                        icon="shield",
                        route="/admin/admin-users",
                        order=10,
                        is_mandatory=True,
                    ),
                ],
            ),
            MenuSectionDefinition(
                id="platformAdmin",
                label_key="tenancy.menu.platform_admin",
                icon="office-building",
                order=20,
                items=[
                    MenuItemDefinition(
                        id="companies",
                        label_key="tenancy.menu.companies",
                        icon="office-building",
                        route="/admin/companies",
                        order=10,
                        is_mandatory=True,
                    ),
                    MenuItemDefinition(
                        id="vendors",
                        label_key="tenancy.menu.vendors",
                        icon="shopping-bag",
                        route="/admin/vendors",
                        order=20,
                        is_mandatory=True,
                    ),
                ],
            ),
            MenuSectionDefinition(
                id="contentMgmt",
                label_key="tenancy.menu.content_management",
                icon="globe-alt",
                order=70,
                items=[
                    MenuItemDefinition(
                        id="platforms",
                        label_key="tenancy.menu.platforms",
                        icon="globe-alt",
                        route="/admin/platforms",
                        order=10,
                    ),
                ],
            ),
        ],
        FrontendType.VENDOR: [
            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="/vendor/{vendor_code}/team",
                        order=5,
                    ),
                ],
            ),
        ],
    },
    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,
)

__all__ = ["tenancy_module"]
