# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Automation2 models: Automation2Workflow, Automation2WorkflowRun, Automation2HumanTask."""

from typing import Dict, Any, List, Optional
from pydantic import BaseModel, Field
from modules.datamodels.datamodelBase import PowerOnModel
from modules.shared.attributeUtils import registerModelLabels
import uuid


class Automation2Workflow(BaseModel):
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Primary key",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    mandateId: str = Field(
        description="Mandate ID",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    featureInstanceId: str = Field(
        description="Feature instance ID",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    label: str = Field(
        description="User-friendly workflow name",
        json_schema_extra={"frontend_type": "text", "frontend_required": True},
    )
    graph: Dict[str, Any] = Field(
        default_factory=dict,
        description="Graph with nodes and connections (incl. node parameters)",
        json_schema_extra={"frontend_type": "textarea", "frontend_required": True},
    )
    active: bool = Field(
        default=True,
        description="Whether workflow is active",
        json_schema_extra={"frontend_type": "checkbox", "frontend_required": False},
    )


registerModelLabels(
    "Automation2Workflow",
    {"en": "Automation2 Workflow", "de": "Automation2 Workflow", "fr": "Workflow Automation2"},
    {
        "id": {"en": "ID", "de": "ID", "fr": "ID"},
        "mandateId": {"en": "Mandate ID", "de": "Mandanten-ID", "fr": "ID du mandat"},
        "featureInstanceId": {"en": "Feature Instance ID", "de": "Feature-Instanz-ID", "fr": "ID instance"},
        "label": {"en": "Label", "de": "Bezeichnung", "fr": "Libellé"},
        "graph": {"en": "Graph", "de": "Graph", "fr": "Graphe"},
        "active": {"en": "Active", "de": "Aktiv", "fr": "Actif"},
    },
)


class Automation2WorkflowRun(PowerOnModel):
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Primary key",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    workflowId: str = Field(
        description="Workflow ID",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    status: str = Field(
        default="running",
        description="Status: running|paused|completed|failed",
        json_schema_extra={"frontend_type": "text", "frontend_required": False},
    )
    nodeOutputs: Dict[str, Any] = Field(
        default_factory=dict,
        description="Outputs from executed nodes",
        json_schema_extra={"frontend_type": "textarea", "frontend_required": False},
    )
    currentNodeId: Optional[str] = Field(
        default=None,
        description="Node ID when paused (human task)",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    context: Dict[str, Any] = Field(
        default_factory=dict,
        description="Context for resume (connectionMap, inputSources, etc.)",
        json_schema_extra={"frontend_type": "textarea", "frontend_required": False},
    )


registerModelLabels(
    "Automation2WorkflowRun",
    {"en": "Automation2 Workflow Run", "de": "Automation2 Workflow-Ausführung", "fr": "Exécution workflow"},
    {
        "id": {"en": "ID", "de": "ID", "fr": "ID"},
        "workflowId": {"en": "Workflow ID", "de": "Workflow-ID", "fr": "ID workflow"},
        "status": {"en": "Status", "de": "Status", "fr": "Statut"},
        "nodeOutputs": {"en": "Node Outputs", "de": "Node-Ausgaben", "fr": "Sorties nœuds"},
        "currentNodeId": {"en": "Current Node", "de": "Aktueller Knoten", "fr": "Nœud actuel"},
        "context": {"en": "Context", "de": "Kontext", "fr": "Contexte"},
    },
)


class Automation2HumanTask(PowerOnModel):
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Primary key",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    runId: str = Field(
        description="Workflow run ID",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    workflowId: str = Field(
        description="Workflow ID",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    nodeId: str = Field(
        description="Node ID in the graph",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    nodeType: str = Field(
        description="Node type: form|approval|upload|comment|review|selection|confirmation",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    config: Dict[str, Any] = Field(
        default_factory=dict,
        description="Node config (form schema, approval text, etc.)",
        json_schema_extra={"frontend_type": "textarea", "frontend_required": False},
    )
    assigneeId: Optional[str] = Field(
        default=None,
        description="User ID assigned to complete the task",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": False, "frontend_required": False},
    )
    status: str = Field(
        default="pending",
        description="Status: pending|completed|rejected",
        json_schema_extra={"frontend_type": "text", "frontend_required": False},
    )
    result: Optional[Dict[str, Any]] = Field(
        default=None,
        description="Task result (form data, approval decision, etc.)",
        json_schema_extra={"frontend_type": "textarea", "frontend_required": False},
    )


registerModelLabels(
    "Automation2HumanTask",
    {"en": "Automation2 Human Task", "de": "Automation2 Benutzer-Aufgabe", "fr": "Tâche utilisateur"},
    {
        "id": {"en": "ID", "de": "ID", "fr": "ID"},
        "runId": {"en": "Run ID", "de": "Lauf-ID", "fr": "ID exécution"},
        "workflowId": {"en": "Workflow ID", "de": "Workflow-ID", "fr": "ID workflow"},
        "nodeId": {"en": "Node ID", "de": "Knoten-ID", "fr": "ID nœud"},
        "nodeType": {"en": "Node Type", "de": "Knotentyp", "fr": "Type nœud"},
        "config": {"en": "Config", "de": "Konfiguration", "fr": "Configuration"},
        "assigneeId": {"en": "Assignee", "de": "Zugewiesen an", "fr": "Assigné à"},
        "status": {"en": "Status", "de": "Status", "fr": "Statut"},
        "result": {"en": "Result", "de": "Ergebnis", "fr": "Résultat"},
    },
)
