"""Email delivery log model.""" import uuid import enum from datetime import datetime, timezone from sqlalchemy import String, DateTime, Enum as SAEnum, ForeignKey, Text from sqlalchemy.orm import Mapped, mapped_column from app.database import Base class EmailStatus(str, enum.Enum): sent = "sent" failed = "failed" class EmailType(str, enum.Enum): invoice = "invoice" low_credit = "low_credit" license_expiry = "license_expiry" overdue_warning = "overdue_warning" suspension = "suspension" monthly_report = "monthly_report" welcome = "welcome" test = "test" other = "other" class EmailLog(Base): __tablename__ = "email_logs" id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid.uuid4())) school_id: Mapped[str | None] = mapped_column( String(36), ForeignKey("schools.id", ondelete="SET NULL"), nullable=True, index=True ) to_email: Mapped[str] = mapped_column(String(255), nullable=False, index=True) subject: Mapped[str] = mapped_column(String(500), nullable=False) email_type: Mapped[EmailType] = mapped_column(SAEnum(EmailType), default=EmailType.other, nullable=False, index=True) status: Mapped[EmailStatus] = mapped_column(SAEnum(EmailStatus), nullable=False, index=True) error_message: Mapped[str | None] = mapped_column(Text, nullable=True) sent_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc) )