# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Neutralizer models: DataNeutraliserConfig and DataNeutralizerAttributes."""

import uuid
from enum import Enum
from typing import Optional
from pydantic import BaseModel, Field
from modules.datamodels.datamodelBase import PowerOnModel
from modules.shared.i18nRegistry import i18nModel


class DataScope(str, Enum):
    PERSONAL = "personal"
    FEATURE_INSTANCE = "featureInstance"
    MANDATE = "mandate"
    GLOBAL = "global"


@i18nModel("Daten-Neutralisierung Konfiguration")
class DataNeutraliserConfig(PowerOnModel):
    """Konfiguration fuer die Daten-Neutralisierung."""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the configuration",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    mandateId: str = Field(
        description="ID of the mandate this configuration belongs to",
        json_schema_extra={"label": "Mandanten-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    featureInstanceId: str = Field(
        description="ID of the feature instance this configuration belongs to",
        json_schema_extra={"label": "Feature-Instanz-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    userId: str = Field(
        description="ID of the user who created this configuration",
        json_schema_extra={"label": "Benutzer-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    enabled: bool = Field(
        default=True,
        description="Whether data neutralization is enabled",
        json_schema_extra={"label": "Aktiviert", "frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False},
    )
    scope: str = Field(
        default="personal",
        description="Data visibility scope: personal, featureInstance, mandate, global",
        json_schema_extra={"label": "Sichtbarkeit", "frontend_type": "select", "frontend_readonly": False, "frontend_required": False, "frontend_options": [
            {"value": "personal", "label": "Persönlich"},
            {"value": "featureInstance", "label": "Feature-Instanz"},
            {"value": "mandate", "label": "Mandant"},
            {"value": "global", "label": "Global"},
        ]},
    )
    neutralizationStatus: str = Field(
        default="not_required",
        description="Status of neutralization: pending, completed, failed, not_required",
        json_schema_extra={"label": "Neutralisierungsstatus", "frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    namesToParse: str = Field(
        default="",
        description="Multiline list of names to parse for neutralization",
        json_schema_extra={"label": "Zu parsende Namen", "frontend_type": "textarea", "frontend_readonly": False, "frontend_required": False},
    )
    sharepointSourcePath: str = Field(
        default="",
        description="SharePoint path to read files for neutralization",
        json_schema_extra={"label": "SharePoint Quellpfad", "frontend_type": "text", "frontend_readonly": False, "frontend_required": False},
    )
    sharepointTargetPath: str = Field(
        default="",
        description="SharePoint path to store neutralized files",
        json_schema_extra={"label": "SharePoint Zielpfad", "frontend_type": "text", "frontend_readonly": False, "frontend_required": False},
    )


@i18nModel("Neutralisiertes Datenattribut")
class DataNeutralizerAttributes(BaseModel):
    """Zuordnung Originaltext zu Platzhalter fuer neutralisierte Daten."""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the attribute mapping (used as UID in neutralized files)",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    mandateId: str = Field(
        description="ID of the mandate this attribute belongs to",
        json_schema_extra={"label": "Mandanten-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    featureInstanceId: str = Field(
        description="ID of the feature instance this attribute belongs to",
        json_schema_extra={"label": "Feature-Instanz-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    userId: str = Field(
        description="ID of the user who created this attribute",
        json_schema_extra={"label": "Benutzer-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    originalText: str = Field(
        description="Original text that was neutralized",
        json_schema_extra={"label": "Originaltext", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )
    fileId: Optional[str] = Field(
        default=None,
        description="ID of the file this attribute belongs to",
        json_schema_extra={"label": "Datei-ID", "frontend_type": "text", "frontend_readonly": True, "frontend_required": False},
    )
    patternType: str = Field(
        description="Type of pattern that matched (email, phone, name, etc.)",
        json_schema_extra={"label": "Mustertyp", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True},
    )


@i18nModel("Neutralisierungs-Snapshot")
class DataNeutralizationSnapshot(BaseModel):
    """Speichert den vollstaendigen neutralisierten Text (mit Platzhaltern) pro Quelle."""
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        json_schema_extra={"label": "ID"},
    )
    mandateId: str = Field(
        description="Mandate scope",
        json_schema_extra={"label": "Mandanten-ID"},
    )
    featureInstanceId: str = Field(
        default="",
        description="Feature instance scope",
        json_schema_extra={"label": "Feature-Instanz-ID"},
    )
    userId: str = Field(
        description="User who triggered neutralization",
        json_schema_extra={"label": "Benutzer-ID"},
    )
    sourceLabel: str = Field(
        description="Human label, e.g. 'Prompt', 'Kontext', 'Nachricht 3'",
        json_schema_extra={"label": "Quelle"},
    )
    neutralizedText: str = Field(
        description="Full text with [type.uuid] placeholders embedded",
        json_schema_extra={"label": "Neutralisierter Text"},
    )
    placeholderCount: int = Field(
        default=0,
        description="Number of placeholders in the text",
        json_schema_extra={"label": "Platzhalter"},
    )
