# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Membership models: UserMandate, FeatureAccess, and Junction Tables.

Diese Models definieren die m:n Beziehungen zwischen User, Mandate und FeatureInstance.
Rollen werden über Junction Tables verknüpft für saubere CASCADE DELETE.
"""

import uuid
from pydantic import BaseModel, Field
from modules.datamodels.datamodelBase import PowerOnModel
from modules.shared.i18nRegistry import i18nModel


@i18nModel("Benutzer-Mandant")
class UserMandate(PowerOnModel):
    """
    User-Mitgliedschaft in einem Mandanten.
    Kein User gehört direkt zu einem Mandanten - Zugehörigkeit wird über dieses Model gesteuert.
    """
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the user-mandate membership",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_visible": False, "frontend_required": False}
    )
    userId: str = Field(
        description="FK → User.id (CASCADE DELETE)",
        json_schema_extra={
            "label": "Benutzer",
            "frontend_type": "select",
            "frontend_readonly": False,
            "frontend_required": True,
            "frontend_fk_source": "/api/users/",
            "frontend_fk_display_field": "username",
            "fk_model": "User",
        },
    )
    mandateId: str = Field(
        description="FK → Mandate.id (CASCADE DELETE)",
        json_schema_extra={
            "label": "Mandant",
            "frontend_type": "select",
            "frontend_readonly": False,
            "frontend_required": True,
            "frontend_fk_source": "/api/mandates/",
            "frontend_fk_display_field": "label",
            "fk_model": "Mandate",
        },
    )
    enabled: bool = Field(
        default=True,
        description="Whether this membership is enabled",
        json_schema_extra={"label": "Aktiviert", "frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False}
    )


@i18nModel("Feature-Zugang")
class FeatureAccess(PowerOnModel):
    """
    User-Zugriff auf eine Feature-Instanz.
    Definiert welche User auf welche Feature-Instanzen zugreifen können.
    """
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the feature access",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_visible": False, "frontend_required": False}
    )
    userId: str = Field(
        description="FK → User.id (CASCADE DELETE)",
        json_schema_extra={"label": "Benutzer", "frontend_type": "select", "frontend_readonly": False, "frontend_required": True, "frontend_fk_source": "/api/users/", "frontend_fk_display_field": "username"}
    )
    featureInstanceId: str = Field(
        description="FK → FeatureInstance.id (CASCADE DELETE)",
        json_schema_extra={"label": "Feature-Instanz", "frontend_type": "select", "frontend_readonly": False, "frontend_required": True, "frontend_fk_source": "/api/features/instances", "frontend_fk_display_field": "label"}
    )
    enabled: bool = Field(
        default=True,
        description="Whether this feature access is enabled",
        json_schema_extra={"label": "Aktiviert", "frontend_type": "checkbox", "frontend_readonly": False, "frontend_required": False}
    )


@i18nModel("Benutzer-Mandant-Rolle")
class UserMandateRole(PowerOnModel):
    """
    Junction Table: UserMandate zu Role.
    Ermöglicht CASCADE DELETE auf Datenbankebene.
    """
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the junction record",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_visible": False, "frontend_required": False}
    )
    userMandateId: str = Field(
        description="FK → UserMandate.id (CASCADE DELETE)",
        json_schema_extra={"label": "Benutzer-Mandant", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True}
    )
    roleId: str = Field(
        description="FK → Role.id (CASCADE DELETE)",
        json_schema_extra={"label": "Rolle", "frontend_type": "select", "frontend_readonly": False, "frontend_required": True, "frontend_fk_source": "/api/rbac/roles", "frontend_fk_display_field": "roleLabel"}
    )


@i18nModel("Feature-Zugang-Rolle")
class FeatureAccessRole(PowerOnModel):
    """
    Junction Table: FeatureAccess zu Role.
    Ermöglicht CASCADE DELETE auf Datenbankebene.
    """
    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        description="Unique ID of the junction record",
        json_schema_extra={"label": "ID", "frontend_type": "text", "frontend_readonly": True, "frontend_visible": False, "frontend_required": False}
    )
    featureAccessId: str = Field(
        description="FK → FeatureAccess.id (CASCADE DELETE)",
        json_schema_extra={"label": "Feature-Zugang", "frontend_type": "text", "frontend_readonly": True, "frontend_required": True}
    )
    roleId: str = Field(
        description="FK → Role.id (CASCADE DELETE)",
        json_schema_extra={"label": "Rolle", "frontend_type": "select", "frontend_readonly": False, "frontend_required": True, "frontend_fk_source": "/api/rbac/roles", "frontend_fk_display_field": "roleLabel"}
    )
