# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Automation2 Feature - n8n-style flow automation.
Minimal bootstrap for feature instance creation. Build from here.
"""

import logging
from typing import Dict, List, Any, Optional

logger = logging.getLogger(__name__)

FEATURE_CODE = "automation2"

# Services required for automation2 (methodDiscovery, ActionExecutor, etc.)
REQUIRED_SERVICES = [
    {"serviceKey": "chat", "meta": {"usage": "Interfaces, RBAC"}},
    {"serviceKey": "utils", "meta": {"usage": "Timestamps, utilities"}},
    {"serviceKey": "ai", "meta": {"usage": "AI nodes"}},
    {"serviceKey": "extraction", "meta": {"usage": "Workflow method actions"}},
    {"serviceKey": "sharepoint", "meta": {"usage": "SharePoint actions"}},
]
FEATURE_LABEL = {"en": "Automation 2", "de": "Automatisierung 2", "fr": "Automatisation 2"}
FEATURE_ICON = "mdi-sitemap"

UI_OBJECTS = [
    {
        "objectKey": "ui.feature.automation2.editor",
        "label": {"en": "Editor", "de": "Editor", "fr": "Éditeur"},
        "meta": {"area": "editor"}
    },
    {
        "objectKey": "ui.feature.automation2.workflows",
        "label": {"en": "Workflows", "de": "Workflows", "fr": "Workflows"},
        "meta": {"area": "workflows"}
    },
    {
        "objectKey": "ui.feature.automation2.workflows-tasks",
        "label": {"en": "Tasks", "de": "Tasks", "fr": "Tâches"},
        "meta": {"area": "tasks"}
    },
]

RESOURCE_OBJECTS = [
    {
        "objectKey": "resource.feature.automation2.dashboard",
        "label": {"en": "Access Dashboard", "de": "Dashboard aufrufen", "fr": "Acceder au tableau de bord"},
        "meta": {"endpoint": "/api/automation2/{instanceId}/info", "method": "GET"}
    },
    {
        "objectKey": "resource.feature.automation2.node-types",
        "label": {"en": "Get Node Types", "de": "Node-Typen abrufen", "fr": "Obtenir types de nœuds"},
        "meta": {"endpoint": "/api/automation2/{instanceId}/node-types", "method": "GET"}
    },
    {
        "objectKey": "resource.feature.automation2.execute",
        "label": {"en": "Execute Workflow", "de": "Workflow ausführen", "fr": "Exécuter le workflow"},
        "meta": {"endpoint": "/api/automation2/{instanceId}/execute", "method": "POST"}
    },
]

TEMPLATE_ROLES = [
    {
        "roleLabel": "automation2-user",
        "description": {
            "en": "Automation2 User - Use automation2 flow builder",
            "de": "Automation2 Benutzer - Flow-Builder nutzen",
            "fr": "Utilisateur Automation2 - Utiliser le flow builder"
        },
        "accessRules": [
            {"context": "UI", "item": "ui.feature.automation2.editor", "view": True},
            {"context": "UI", "item": "ui.feature.automation2.workflows", "view": True},
            {"context": "UI", "item": "ui.feature.automation2.workflows-tasks", "view": True},
            {"context": "RESOURCE", "item": "resource.feature.automation2.dashboard", "view": True},
            {"context": "RESOURCE", "item": "resource.feature.automation2.node-types", "view": True},
            {"context": "RESOURCE", "item": "resource.feature.automation2.execute", "view": True},
            {"context": "DATA", "item": None, "view": True, "read": "m", "create": "m", "update": "m", "delete": "m"},
        ]
    },
    {
        "roleLabel": "automation2-admin",
        "description": {
            "en": "Automation2 Admin - Full UI and API for the instance; data remains user-scoped (MY)",
            "de": "Automation2 Admin - Volle UI und API für die Instanz; Daten weiterhin benutzerspezifisch (MY)",
            "fr": "Administrateur Automation2 - UI et API complets pour l'instance; donnees limitees a l'utilisateur (MY)",
        },
        "accessRules": [
            {"context": "UI", "item": None, "view": True},
            {"context": "RESOURCE", "item": None, "view": True},
            {"context": "DATA", "item": None, "view": True, "read": "m", "create": "m", "update": "m", "delete": "m"},
        ],
    },
]


def getRequiredServiceKeys() -> List[str]:
    """Return list of service keys this feature requires."""
    return [s["serviceKey"] for s in REQUIRED_SERVICES]


def getAutomation2Services(
    user,
    mandateId: Optional[str] = None,
    featureInstanceId: Optional[str] = None,
    workflow=None,
) -> "_Automation2ServiceHub":
    """
    Get a service hub for automation2 using the service center.
    Used for methodDiscovery (I/O nodes) and execution (ActionExecutor).
    """
    from modules.serviceCenter import getService
    from modules.serviceCenter.context import ServiceCenterContext

    _workflow = workflow
    if _workflow is None:
        _workflow = type(
            "_Placeholder",
            (),
            {"featureCode": FEATURE_CODE, "id": None, "workflowMode": None, "messages": []},
        )()

    ctx = ServiceCenterContext(
        user=user,
        mandate_id=mandateId,
        feature_instance_id=featureInstanceId,
        workflow=_workflow,
    )

    hub = _Automation2ServiceHub()
    hub.user = user
    hub.mandateId = mandateId
    hub.featureInstanceId = featureInstanceId
    hub._service_context = ctx
    hub.workflow = workflow
    hub.featureCode = FEATURE_CODE

    for spec in REQUIRED_SERVICES:
        key = spec["serviceKey"]
        try:
            svc = getService(key, ctx)
            setattr(hub, key, svc)
        except Exception as e:
            logger.warning(f"Could not resolve service '{key}' for automation2: {e}")
            setattr(hub, key, None)

    if hub.chat:
        hub.interfaceDbApp = getattr(hub.chat, "interfaceDbApp", None)
        hub.interfaceDbComponent = getattr(hub.chat, "interfaceDbComponent", None)
        hub.interfaceDbChat = getattr(hub.chat, "interfaceDbChat", None)
    hub.rbac = getattr(hub.interfaceDbApp, "rbac", None) if getattr(hub, "interfaceDbApp", None) else None

    return hub


class _Automation2ServiceHub:
    """Lightweight hub for automation2 (methodDiscovery, execution)."""

    user = None
    mandateId = None
    featureInstanceId = None
    _service_context = None
    workflow = None
    featureCode = FEATURE_CODE
    interfaceDbApp = None
    interfaceDbComponent = None
    interfaceDbChat = None
    rbac = None
    chat = None
    ai = None
    utils = None
    extraction = None
    sharepoint = None


async def onStart(eventUser) -> None:
    """Feature startup. Email poller is started on-demand when a run pauses for email.checkEmail."""


async def onStop(eventUser) -> None:
    """Feature shutdown - remove email poller if running."""
    from modules.features.automation2.emailPoller import stop as stopEmailPoller
    stopEmailPoller(eventUser)


def getFeatureDefinition() -> Dict[str, Any]:
    """Return the feature definition for registration."""
    return {
        "code": FEATURE_CODE,
        "label": FEATURE_LABEL,
        "icon": FEATURE_ICON,
        "autoCreateInstance": True,
    }


def getUiObjects() -> List[Dict[str, Any]]:
    """Return UI objects for RBAC catalog registration."""
    return UI_OBJECTS


def getResourceObjects() -> List[Dict[str, Any]]:
    """Return resource objects for RBAC catalog registration."""
    return RESOURCE_OBJECTS


def getTemplateRoles() -> List[Dict[str, Any]]:
    """Return template roles for this feature."""
    return TEMPLATE_ROLES


def registerFeature(catalogService) -> bool:
    """Register this feature's RBAC objects in the catalog."""
    try:
        for uiObj in UI_OBJECTS:
            catalogService.registerUiObject(
                featureCode=FEATURE_CODE,
                objectKey=uiObj["objectKey"],
                label=uiObj["label"],
                meta=uiObj.get("meta")
            )
        for resObj in RESOURCE_OBJECTS:
            catalogService.registerResourceObject(
                featureCode=FEATURE_CODE,
                objectKey=resObj["objectKey"],
                label=resObj["label"],
                meta=resObj.get("meta")
            )
        _syncTemplateRolesToDb()
        logger.info(f"Feature '{FEATURE_CODE}' registered {len(UI_OBJECTS)} UI objects and {len(RESOURCE_OBJECTS)} resource objects")
        return True
    except Exception as e:
        logger.error(f"Failed to register feature '{FEATURE_CODE}': {e}")
        return False


def _syncTemplateRolesToDb() -> int:
    """Sync template roles and their AccessRules to database.
    Also syncs rules to mandate-specific roles (same roleLabel) so new UI objects
    become visible after gateway restart without manual role update.
    """
    try:
        from modules.interfaces.interfaceDbApp import getRootInterface
        from modules.datamodels.datamodelRbac import Role

        rootInterface = getRootInterface()
        existingRoles = rootInterface.getRolesByFeatureCode(FEATURE_CODE)
        existingLabels = {r.roleLabel: str(r.id) for r in existingRoles if r.mandateId is None}
        created = 0

        for template in TEMPLATE_ROLES:
            roleLabel = template["roleLabel"]
            if roleLabel in existingLabels:
                roleId = existingLabels[roleLabel]
            else:
                newRole = Role(
                    roleLabel=roleLabel,
                    description=template.get("description", {}),
                    featureCode=FEATURE_CODE,
                    mandateId=None,
                    featureInstanceId=None,
                    isSystemRole=False
                )
                rec = rootInterface.db.recordCreate(Role, newRole.model_dump())
                roleId = rec.get("id")
                created += 1
                logger.info(f"Created template role '{roleLabel}' for {FEATURE_CODE}")

            _ensureAccessRulesForRole(rootInterface, roleId, template.get("accessRules", []))

            # Sync same rules to mandate-specific roles (so Workflows & Tasks etc. appear in sidebar)
            for r in existingRoles:
                if r.mandateId and r.roleLabel == roleLabel:
                    added = _ensureAccessRulesForRole(
                        rootInterface, str(r.id), template.get("accessRules", [])
                    )
                    if added:
                        logger.debug(f"Added {added} access rules to mandate role {r.id}")
        return created
    except Exception as e:
        logger.warning(f"Template role sync for {FEATURE_CODE}: {e}")
        return 0


def _ensureAccessRulesForRole(rootInterface, roleId: str, ruleTemplates: List[Dict[str, Any]]) -> int:
    """Ensure AccessRules exist for a role based on templates."""
    from modules.datamodels.datamodelRbac import AccessRule, AccessRuleContext

    existingRules = rootInterface.getAccessRulesByRole(roleId)
    existingSignatures = {
        (r.context.value if r.context else None, r.item)
        for r in existingRules
    }
    created = 0
    for t in ruleTemplates:
        context = t.get("context", "UI")
        item = t.get("item")
        sig = (context, item)
        if sig in existingSignatures:
            continue
        ctx_enum = (
            AccessRuleContext.UI if context == "UI" else
            AccessRuleContext.DATA if context == "DATA" else
            AccessRuleContext.RESOURCE if context == "RESOURCE" else context
        )
        newRule = AccessRule(
            roleId=roleId,
            context=ctx_enum,
            item=item,
            view=t.get("view", False),
            read=t.get("read"),
            create=t.get("create"),
            update=t.get("update"),
            delete=t.get("delete"),
        )
        rootInterface.db.recordCreate(AccessRule, newRule.model_dump())
        created += 1
    return created
