# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Messaging models: MessagingSubscription, MessagingSubscriptionRegistration, MessagingDelivery."""

import uuid
from typing import Optional
from enum import Enum
from pydantic import BaseModel, Field, ConfigDict
from modules.shared.attributeUtils import registerModelLabels
from modules.shared.timeUtils import getUtcTimestamp


class MessagingChannel(str, Enum):
    """Messaging channel types"""
    EMAIL = "email"
    SMS = "sms"
    WHATSAPP = "whatsapp"
    TEAMS_CHAT = "teams_chat"
    # Weitere Kanäle können hier hinzugefügt werden


class DeliveryStatus(str, Enum):
    """Individual delivery status"""
    PENDING = "pending"
    SENT = "sent"
    FAILED = "failed"


class MessagingSubscription(BaseModel):
    """Data model for messaging subscriptions"""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the subscription",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    subscriptionId: str = Field(
        description="Unique subscription identifier (e.g., 'system_errors', 'audit_login')",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": False, "frontend_required": True}
    )
    subscriptionLabel: str = Field(
        description="Display name of the subscription",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": False, "frontend_required": True}
    )
    mandateId: str = Field(
        description="ID of the mandate this subscription belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    featureInstanceId: str = Field(
        description="ID of the feature instance this subscription belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    description: Optional[str] = Field(
        default=None,
        description="Description of the subscription",
        json_schema_extra={"frontend_type": "textarea", "frontend_readonly": False, "frontend_required": False}
    )
    isSystemSubscription: bool = Field(
        default=False,
        description="Whether this is a system subscription (only admin can create)",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": True, "frontend_required": False}
    )
    enabled: bool = Field(
        default=True,
        description="Whether the subscription is enabled",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False}
    )
    creationDate: float = Field(
        default_factory=getUtcTimestamp,
        description="When the subscription was created (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )
    lastModified: float = Field(
        default_factory=getUtcTimestamp,
        description="When the subscription was last modified (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )
    createdBy: Optional[str] = Field(
        default=None,
        description="User ID who created the subscription",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    modifiedBy: Optional[str] = Field(
        default=None,
        description="User ID who last modified the subscription",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )

    model_config = ConfigDict(use_enum_values=True)


registerModelLabels(
    "MessagingSubscription",
    {"en": "Messaging Subscription", "fr": "Abonnement de messagerie"},
    {
        "id": {"en": "ID", "fr": "ID"},
        "subscriptionId": {"en": "Subscription ID", "fr": "ID d'abonnement"},
        "subscriptionLabel": {"en": "Subscription Label", "fr": "Label d'abonnement"},
        "mandateId": {"en": "Mandate ID", "fr": "ID du mandat"},
        "featureInstanceId": {"en": "Feature Instance ID", "fr": "ID de l'instance de fonctionnalité"},
        "description": {"en": "Description", "fr": "Description"},
        "isSystemSubscription": {"en": "System Subscription", "fr": "Abonnement système"},
        "enabled": {"en": "Enabled", "fr": "Activé"},
        "creationDate": {"en": "Creation Date", "fr": "Date de création"},
        "lastModified": {"en": "Last Modified", "fr": "Dernière modification"},
        "createdBy": {"en": "Created By", "fr": "Créé par"},
        "modifiedBy": {"en": "Modified By", "fr": "Modifié par"},
    },
)


class MessagingSubscriptionRegistration(BaseModel):
    """Data model for user registrations to messaging subscriptions"""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the registration",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    mandateId: str = Field(
        description="ID of the mandate this registration belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    featureInstanceId: str = Field(
        description="ID of the feature instance this registration belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    subscriptionId: str = Field(
        description="ID of the subscription this registration belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": False, "frontend_required": True}
    )
    userId: str = Field(
        description="ID of the user registered to this subscription",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    channel: MessagingChannel = Field(
        description="Channel type for this registration",
        json_schema_extra={
            "frontend_type": "select",
            "frontend_readonly": False,
            "frontend_required": True,
            "frontend_options": [
                {"value": "email", "label": {"en": "Email", "fr": "Email"}},
                {"value": "sms", "label": {"en": "SMS", "fr": "SMS"}},
                {"value": "whatsapp", "label": {"en": "WhatsApp", "fr": "WhatsApp"}},
                {"value": "teams_chat", "label": {"en": "Teams Chat", "fr": "Chat Teams"}}
            ]
        }
    )
    channelConfig: str = Field(
        default="",
        description="Channel-specific configuration (e.g., email address, phone number, Teams user ID)",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": False, "frontend_required": False}
    )
    enabled: bool = Field(
        default=True,
        description="Whether this registration is enabled",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False}
    )
    creationDate: float = Field(
        default_factory=getUtcTimestamp,
        description="When the registration was created (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )
    lastModified: float = Field(
        default_factory=getUtcTimestamp,
        description="When the registration was last modified (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )

    model_config = ConfigDict(use_enum_values=True)


registerModelLabels(
    "MessagingSubscriptionRegistration",
    {"en": "Messaging Registration", "fr": "Inscription à la messagerie"},
    {
        "id": {"en": "ID", "fr": "ID"},
        "mandateId": {"en": "Mandate ID", "fr": "ID du mandat"},
        "featureInstanceId": {"en": "Feature Instance ID", "fr": "ID de l'instance de fonctionnalité"},
        "subscriptionId": {"en": "Subscription ID", "fr": "ID d'abonnement"},
        "userId": {"en": "User ID", "fr": "ID utilisateur"},
        "channel": {"en": "Channel", "fr": "Canal"},
        "channelConfig": {"en": "Channel Config", "fr": "Configuration du canal"},
        "enabled": {"en": "Enabled", "fr": "Activé"},
        "creationDate": {"en": "Creation Date", "fr": "Date de création"},
        "lastModified": {"en": "Last Modified", "fr": "Dernière modification"},
    },
)


class MessagingDelivery(BaseModel):
    """Data model for individual message deliveries"""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the delivery",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    mandateId: str = Field(
        description="ID of the mandate this delivery belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    featureInstanceId: str = Field(
        description="ID of the feature instance this delivery belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    subscriptionId: str = Field(
        description="ID of the subscription this delivery belongs to",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    userId: str = Field(
        description="ID of the user receiving this delivery",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    channel: MessagingChannel = Field(
        description="Channel used for this delivery",
        json_schema_extra={
            "frontend_type": "select",
            "frontend_readonly": True,
            "frontend_required": False,
            "frontend_options": [
                {"value": "email", "label": {"en": "Email", "fr": "Email"}},
                {"value": "sms", "label": {"en": "SMS", "fr": "SMS"}},
                {"value": "whatsapp", "label": {"en": "WhatsApp", "fr": "WhatsApp"}},
                {"value": "teams_chat", "label": {"en": "Teams Chat", "fr": "Chat Teams"}}
            ]
        }
    )
    status: DeliveryStatus = Field(
        default=DeliveryStatus.PENDING,
        description="Status of the delivery",
        json_schema_extra={
            "frontend_type": "select",
            "frontend_readonly": True,
            "frontend_required": False,
            "frontend_options": [
                {"value": "pending", "label": {"en": "Pending", "fr": "En attente"}},
                {"value": "sent", "label": {"en": "Sent", "fr": "Envoyé"}},
                {"value": "failed", "label": {"en": "Failed", "fr": "Échoué"}}
            ]
        }
    )
    errorMessage: Optional[str] = Field(
        default=None,
        description="Error message if delivery failed",
        json_schema_extra={"frontend_type": "textarea", "frontend_readonly": True, "frontend_required": False}
    )
    sentAt: Optional[float] = Field(
        default=None,
        description="When the delivery was sent (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )
    creationDate: float = Field(
        default_factory=getUtcTimestamp,
        description="When the delivery record was created (UTC timestamp in seconds)",
        json_schema_extra={"frontend_type": "datetime", "frontend_readonly": True, "frontend_required": False}
    )

    model_config = ConfigDict(use_enum_values=True)


registerModelLabels(
    "MessagingDelivery",
    {"en": "Messaging Delivery", "fr": "Livraison de messagerie"},
    {
        "id": {"en": "ID", "fr": "ID"},
        "mandateId": {"en": "Mandate ID", "fr": "ID du mandat"},
        "featureInstanceId": {"en": "Feature Instance ID", "fr": "ID de l'instance de fonctionnalité"},
        "subscriptionId": {"en": "Subscription ID", "fr": "ID d'abonnement"},
        "userId": {"en": "User ID", "fr": "ID utilisateur"},
        "channel": {"en": "Channel", "fr": "Canal"},
        "status": {"en": "Status", "fr": "Statut"},
        "errorMessage": {"en": "Error Message", "fr": "Message d'erreur"},
        "sentAt": {"en": "Sent At", "fr": "Envoyé le"},
        "creationDate": {"en": "Creation Date", "fr": "Date de création"},
    },
)


class MessagingEventParameters(BaseModel):
    """Data model for event parameters passed to subscription functions"""
    triggerData: dict = Field(
        default_factory=dict,
        description="Event data from trigger as dictionary/JSON",
        json_schema_extra={"frontend_type": "json", "frontend_readonly": False, "frontend_required": False}
    )


registerModelLabels(
    "MessagingEventParameters",
    {"en": "Messaging Event Parameters", "fr": "Paramètres d'événement de messagerie"},
    {
        "triggerData": {"en": "Trigger Data", "fr": "Données de déclenchement"},
    },
)


registerModelLabels(
    "MessagingSendResult",
    {"en": "Messaging Send Result", "fr": "Résultat d'envoi de messagerie"},
    {
        "success": {"en": "Success", "fr": "Succès"},
        "deliveryId": {"en": "Delivery ID", "fr": "ID de livraison"},
        "errorMessage": {"en": "Error Message", "fr": "Message d'erreur"},
    },
)


registerModelLabels(
    "MessagingSubscriptionExecutionResult",
    {"en": "Messaging Subscription Execution Result", "fr": "Résultat d'exécution d'abonnement"},
    {
        "success": {"en": "Success", "fr": "Succès"},
        "messagesSent": {"en": "Messages Sent", "fr": "Messages envoyés"},
        "errorMessage": {"en": "Error Message", "fr": "Message d'erreur"},
    },
)


class MessagingSendResult(BaseModel):
    """Data model for sendMessage result"""
    success: bool = Field(
        description="Whether the message was sent successfully",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": True, "frontend_required": True}
    )
    deliveryId: Optional[str] = Field(
        default=None,
        description="ID of the created MessagingDelivery record",
        json_schema_extra={"frontend_type": "text", "frontend_readonly": True, "frontend_required": False}
    )
    errorMessage: Optional[str] = Field(
        default=None,
        description="Error message if sending failed",
        json_schema_extra={"frontend_type": "textarea", "frontend_readonly": True, "frontend_required": False}
    )


class MessagingSubscriptionExecutionResult(BaseModel):
    """Data model for subscription function execution result"""
    success: bool = Field(
        description="Whether the subscription execution was successful",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": True, "frontend_required": True}
    )
    messagesSent: int = Field(
        default=0,
        description="Number of messages sent",
        json_schema_extra={"frontend_type": "number", "frontend_readonly": True, "frontend_required": False}
    )
    errorMessage: Optional[str] = Field(
        default=None,
        description="Error message if execution failed",
        json_schema_extra={"frontend_type": "textarea", "frontend_readonly": True, "frontend_required": False}
    )
    model_config = ConfigDict(extra="allow")  # Allow additional fields for custom results
