"""
SQLAlchemy models for E-Mail-Templates — Benachrichtigungsvorlagen fuer DSGVO-Compliance.

Tables:
- compliance_email_templates: Template-Definitionen
- compliance_email_template_versions: Versionierte Inhalte mit Approval-Workflow
- compliance_email_template_approvals: Genehmigungen/Ablehnungen
- compliance_email_send_logs: Audit-Trail gesendeter E-Mails
- compliance_email_template_settings: Globale Branding-Einstellungen
"""

import uuid
from datetime import datetime

from sqlalchemy import (
    Column, Text, Boolean, Integer, DateTime, JSON, Index
)
from sqlalchemy.dialects.postgresql import UUID

from classroom_engine.database import Base


class EmailTemplateDB(Base):
    """E-Mail-Template Definition."""

    __tablename__ = 'compliance_email_templates'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    template_type = Column(Text, nullable=False)
    name = Column(Text, nullable=False)
    description = Column(Text)
    category = Column(Text, nullable=False, default='general')
    is_active = Column(Boolean, nullable=False, default=True)
    sort_order = Column(Integer, nullable=False, default=0)
    variables = Column(JSON, default=list)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    __table_args__ = (
        Index('idx_email_tpl_tenant', 'tenant_id'),
        Index('idx_email_tpl_type', 'template_type'),
        Index('idx_email_tpl_category', 'category'),
    )


class EmailTemplateVersionDB(Base):
    """Versionierte E-Mail-Template-Inhalte."""

    __tablename__ = 'compliance_email_template_versions'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    template_id = Column(UUID(as_uuid=True), nullable=False)
    version = Column(Text, nullable=False, default='1.0')
    language = Column(Text, nullable=False, default='de')
    subject = Column(Text, nullable=False)
    body_html = Column(Text, nullable=False)
    body_text = Column(Text)
    status = Column(Text, nullable=False, default='draft')
    submitted_at = Column(DateTime)
    submitted_by = Column(Text)
    published_at = Column(DateTime)
    published_by = Column(Text)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    created_by = Column(Text, default='system')

    __table_args__ = (
        Index('idx_email_tpl_ver_template', 'template_id'),
        Index('idx_email_tpl_ver_status', 'status'),
    )


class EmailTemplateApprovalDB(Base):
    """Approval-Workflow fuer Template-Versionen."""

    __tablename__ = 'compliance_email_template_approvals'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    version_id = Column(UUID(as_uuid=True), nullable=False)
    action = Column(Text, nullable=False, default='approve')
    comment = Column(Text)
    approved_by = Column(Text)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    __table_args__ = (
        Index('idx_email_tpl_appr_version', 'version_id'),
    )


class EmailSendLogDB(Base):
    """Audit-Trail gesendeter E-Mails."""

    __tablename__ = 'compliance_email_send_logs'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    template_type = Column(Text, nullable=False)
    version_id = Column(UUID(as_uuid=True))
    recipient = Column(Text, nullable=False)
    subject = Column(Text, nullable=False)
    status = Column(Text, nullable=False, default='sent')
    variables = Column(JSON, default=dict)
    error_message = Column(Text)
    sent_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)

    __table_args__ = (
        Index('idx_email_logs_tenant', 'tenant_id'),
        Index('idx_email_logs_type', 'template_type'),
        Index('idx_email_logs_sent', 'sent_at'),
    )


class EmailTemplateSettingsDB(Base):
    """Globale E-Mail-Einstellungen (Branding)."""

    __tablename__ = 'compliance_email_template_settings'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    sender_name = Column(Text, default='Datenschutzbeauftragter')
    sender_email = Column(Text, default='datenschutz@example.de')
    reply_to = Column(Text)
    logo_url = Column(Text)
    primary_color = Column(Text, default='#4F46E5')
    secondary_color = Column(Text, default='#7C3AED')
    footer_text = Column(Text, default='Datenschutzhinweis: Diese E-Mail enthaelt vertrauliche Informationen.')
    company_name = Column(Text)
    company_address = Column(Text)
    created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    __table_args__ = (
        Index('idx_email_settings_tenant', 'tenant_id', unique=True),
    )
