# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""DataSource and ExternalEntry models for external data integration.

DataSource links a UserConnection to an external path (SharePoint folder,
Google Drive folder, FTP directory, etc.) for agent-accessible data containers.
"""

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


class DataSource(PowerOnModel):
    """Configured external data source linked to a UserConnection."""
    id: str = Field(default_factory=lambda: str(uuid.uuid4()), description="Primary key")
    connectionId: str = Field(description="FK to UserConnection")
    sourceType: str = Field(description="sharepointFolder, googleDriveFolder, outlookFolder, ftpFolder")
    path: str = Field(description="External path (e.g. '/sites/MySite/Documents/Reports')")
    label: str = Field(description="User-visible label (often the last path segment)")
    displayPath: Optional[str] = Field(
        default=None,
        description="Human-readable full path for UI (connection-relative, slash-separated)",
    )
    featureInstanceId: Optional[str] = Field(default=None, description="Scoped to feature instance")
    mandateId: Optional[str] = Field(default=None, description="Mandate scope")
    userId: str = Field(default="", description="Owner user ID")
    autoSync: bool = Field(default=False, description="Automatically sync on schedule")
    lastSynced: Optional[float] = Field(default=None, description="Last sync timestamp")
    scope: str = Field(
        default="personal",
        description="Data visibility scope: personal, featureInstance, mandate, global",
        json_schema_extra={"frontend_type": "select", "frontend_readonly": False, "frontend_required": False, "frontend_options": [
            {"value": "personal", "label": {"en": "Personal", "de": "Persönlich"}},
            {"value": "featureInstance", "label": {"en": "Feature Instance", "de": "Feature-Instanz"}},
            {"value": "mandate", "label": {"en": "Mandate", "de": "Mandant"}},
            {"value": "global", "label": {"en": "Global", "de": "Global"}},
        ]}
    )
    neutralize: bool = Field(
        default=False,
        description="Whether this data source should be neutralized before AI processing",
        json_schema_extra={"frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False}
    )


registerModelLabels(
    "DataSource",
    {"en": "Data Source", "de": "Datenquelle", "fr": "Source de données"},
    {
        "id": {"en": "ID", "de": "ID", "fr": "ID"},
        "connectionId": {"en": "Connection ID", "de": "Verbindungs-ID", "fr": "ID de connexion"},
        "sourceType": {"en": "Source Type", "de": "Quellentyp", "fr": "Type de source"},
        "path": {"en": "Path", "de": "Pfad", "fr": "Chemin"},
        "label": {"en": "Label", "de": "Bezeichnung", "fr": "Libellé"},
        "displayPath": {"en": "Display path", "de": "Anzeigepfad", "fr": "Chemin affiché"},
        "featureInstanceId": {"en": "Feature Instance", "de": "Feature-Instanz", "fr": "Instance de fonctionnalité"},
        "mandateId": {"en": "Mandate ID", "de": "Mandanten-ID", "fr": "ID du mandat"},
        "userId": {"en": "User ID", "de": "Benutzer-ID", "fr": "ID utilisateur"},
        "autoSync": {"en": "Auto Sync", "de": "Auto-Sync", "fr": "Synchro auto"},
        "lastSynced": {"en": "Last Synced", "de": "Letzter Sync", "fr": "Dernier sync"},
        "scope": {"en": "Scope", "de": "Sichtbarkeit"},
        "neutralize": {"en": "Neutralize", "de": "Neutralisieren"},
    },
)


class ExternalEntry(BaseModel):
    """An item (file or folder) from an external data source."""
    name: str = Field(description="Item name")
    path: str = Field(description="Full path within the source")
    isFolder: bool = Field(default=False, description="True if directory/folder")
    size: Optional[int] = Field(default=None, description="File size in bytes")
    mimeType: Optional[str] = Field(default=None, description="MIME type (files only)")
    lastModified: Optional[float] = Field(default=None, description="Last modification timestamp")
    metadata: Dict[str, Any] = Field(default_factory=dict, description="Provider-specific metadata")
