"""SQLAlchemy model for the audit log table."""

# Import uuid
import uuid

# Import Column, DateTime, ForeignKey, Index, String, func from sqlalchemy
from sqlalchemy import Column, DateTime, ForeignKey, Index, String, func

# Import JSONB, UUID from sqlalchemy.dialects.postgresql
from sqlalchemy.dialects.postgresql import JSONB, UUID

# Import relationship from sqlalchemy.orm
from sqlalchemy.orm import relationship

# Import Base from app.database
from app.database import Base


# Define class AuditLog
class AuditLog(Base):
    """Audit log model for tracking all system actions.

    Records user actions, entity changes, and system events
    for security auditing and compliance purposes.
    """
    # Assign __tablename__ = "audit_logs"
    __tablename__ = "audit_logs"

    # Assign id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    # Assign user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True)
    user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True)
    # Assign action = Column(String, nullable=False)
    action = Column(String, nullable=False)
    # Assign entity_type = Column(String, nullable=True)
    entity_type = Column(String, nullable=True)
    # Assign entity_id = Column(String, nullable=True)
    entity_id = Column(String, nullable=True)
    # Assign timestamp = Column(DateTime(timezone=True), server_default=func.now())
    timestamp = Column(DateTime(timezone=True), server_default=func.now())
    # Assign details = Column(JSONB, nullable=True)
    details = Column(JSONB, nullable=True)
    # Assign ip_address = Column(String(45), nullable=True)
    ip_address = Column(String(45), nullable=True)
    # Assign user_agent = Column(String(500), nullable=True)
    user_agent = Column(String(500), nullable=True)
    # Assign integrity_hash = Column(String(64), nullable=True)
    integrity_hash = Column(String(64), nullable=True)
    # Assign session_id = Column(String(100), nullable=True)
    session_id = Column(String(100), nullable=True)

    # Relationships
    user = relationship("User")

    # Assign __table_args__ = (
    __table_args__ = (
        Index("ix_audit_logs_entity", "entity_type", "entity_id"),
        Index("ix_audit_logs_timestamp", "timestamp"),
        Index("ix_audit_logs_entity_type_entity_id_action", "entity_type", "entity_id", "action"),
    )
