# Copyright (c) 2025 Patrick Motsch
# All rights reserved.

import logging
from modules.workflows.methods.methodBase import MethodBase
from modules.datamodels.datamodelWorkflowActions import WorkflowActionDefinition, WorkflowActionParameter
from modules.shared.frontendTypes import FrontendType

# Import actions
from .actions.queryDatabase import queryDatabase

logger = logging.getLogger(__name__)

class MethodChatbot(MethodBase):
    """Chatbot operations methods."""
    
    def __init__(self, services):
        super().__init__(services)
        self.name = "chatbot"
        self.description = "Chatbot operations"
        
        # RBAC-Integration: Action-Definitionen mit actionId
        self._actions = {
            "queryDatabase": WorkflowActionDefinition(
                actionId="chatbot.queryDatabase",
                description="Execute a SQL SELECT query via the preprocessor connector. Returns formatted query results.",
                dynamicMode=False,
                parameters={
                    "sqlQuery": WorkflowActionParameter(
                        name="sqlQuery",
                        type="str",
                        frontendType=FrontendType.TEXTAREA,
                        required=False,
                        description="SQL SELECT query to execute. If not provided, will attempt to extract from analysis_result document in documentList."
                    ),
                    "documentList": WorkflowActionParameter(
                        name="documentList",
                        type="List[str]",
                        frontendType=FrontendType.DOCUMENT_REFERENCE,
                        required=False,
                        description="Document reference(s) containing analysis_result with sqlQuery field. Used if sqlQuery parameter is not provided."
                    )
                },
                execute=queryDatabase.__get__(self, self.__class__)
            )
        }
        
        # Validate actions after definition
        self._validateActions()
        
        # Register actions as methods (optional, für direkten Zugriff)
        self.queryDatabase = queryDatabase.__get__(self, self.__class__)

