"""
SQLAlchemy models for Legal Documents Extension.

Tables:
- compliance_user_consents: End-User Consent-Records
- compliance_consent_audit_log: Immutable Audit-Trail
- compliance_cookie_categories: Cookie-Kategorien fuer Banner
"""

import uuid
from datetime import datetime

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

from classroom_engine.database import Base


class UserConsentDB(Base):
    """End-User Consent-Record fuer rechtliche Dokumente."""

    __tablename__ = 'compliance_user_consents'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    user_id = Column(Text, nullable=False)
    document_id = Column(UUID(as_uuid=True), nullable=False)
    document_version_id = Column(UUID(as_uuid=True))
    document_type = Column(Text, nullable=False)
    consented = Column(Boolean, nullable=False, default=True)
    ip_address = Column(Text)
    user_agent = Column(Text)
    consented_at = Column(DateTime, nullable=False, default=datetime.utcnow)
    withdrawn_at = Column(DateTime)
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow)

    __table_args__ = (
        Index('idx_user_consents_tenant', 'tenant_id'),
        Index('idx_user_consents_user', 'user_id'),
        Index('idx_user_consents_doc', 'document_id'),
        Index('idx_user_consents_type', 'document_type'),
    )


class ConsentAuditLogDB(Base):
    """Immutable Audit-Trail fuer Consent-Aktionen."""

    __tablename__ = 'compliance_consent_audit_log'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    action = Column(Text, nullable=False)
    entity_type = Column(Text, nullable=False)
    entity_id = Column(UUID(as_uuid=True))
    user_id = Column(Text)
    details = Column(JSON, default=dict)
    ip_address = Column(Text)
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow)

    __table_args__ = (
        Index('idx_consent_audit_tenant', 'tenant_id'),
        Index('idx_consent_audit_action', 'action'),
        Index('idx_consent_audit_created', 'created_at'),
    )


class CookieCategoryDB(Base):
    """Cookie-Kategorien fuer Consent-Banner."""

    __tablename__ = 'compliance_cookie_categories'

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    tenant_id = Column(UUID(as_uuid=True), nullable=False)
    name_de = Column(Text, nullable=False)
    name_en = Column(Text)
    description_de = Column(Text)
    description_en = Column(Text)
    is_required = Column(Boolean, nullable=False, default=False)
    sort_order = Column(Integer, nullable=False, default=0)
    is_active = Column(Boolean, nullable=False, default=True)
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    __table_args__ = (
        Index('idx_cookie_cats_tenant', 'tenant_id'),
    )
