# 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

from .actions.create import create

logger = logging.getLogger(__name__)


class MethodFile(MethodBase):
    """File creation methods - create documents from context (e.g. from AI nodes)."""

    def __init__(self, services):
        super().__init__(services)
        self.name = "file"
        self.description = "File creation from context"

        self._actions = {
            "create": WorkflowActionDefinition(
                actionId="file.create",
                description="Create a file from context (text/markdown from AI). Configurable format and style preset.",
                dynamicMode=True,
                parameters={
                    "contentSources": WorkflowActionParameter(
                        name="contentSources",
                        type="list",
                        frontendType=FrontendType.HIDDEN,
                        required=False,
                        description="Array of context refs. Resolved and concatenated. Empty = from connected node.",
                    ),
                    "context": WorkflowActionParameter(
                        name="context",
                        type="str",
                        frontendType=FrontendType.HIDDEN,
                        required=False,
                        description="Injected from contentSource or upstream connection",
                    ),
                    "outputFormat": WorkflowActionParameter(
                        name="outputFormat",
                        type="str",
                        frontendType=FrontendType.SELECT,
                        frontendOptions=["docx", "pdf", "txt", "md", "html", "xlsx", "csv", "json"],
                        required=True,
                        default="docx",
                        description="Output file format",
                    ),
                    "title": WorkflowActionParameter(
                        name="title",
                        type="str",
                        frontendType=FrontendType.TEXT,
                        required=False,
                        description="Document title",
                    ),
                    "templateName": WorkflowActionParameter(
                        name="templateName",
                        type="str",
                        frontendType=FrontendType.SELECT,
                        frontendOptions=["default", "corporate", "minimal"],
                        required=False,
                        description="Style preset",
                    ),
                    "language": WorkflowActionParameter(
                        name="language",
                        type="str",
                        frontendType=FrontendType.SELECT,
                        frontendOptions=["de", "en", "fr", "it", "es"],
                        required=False,
                        default="de",
                        description="Language code",
                    ),
                },
                execute=create.__get__(self, self.__class__),
            ),
        }

        self._validateActions()
        self.create = create.__get__(self, self.__class__)
