"""Newsletter subscriber model."""
from datetime import datetime
from typing import Optional
from sqlalchemy import String, Boolean, DateTime
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class NewsletterSubscriber(Base):
    """Newsletter subscriber model."""
    
    __tablename__ = "newsletter_subscribers"
    
    id: Mapped[int] = mapped_column(primary_key=True, index=True)
    email: Mapped[str] = mapped_column(String(255), unique=True, index=True, nullable=False)
    
    # Status
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    
    # Unsubscribe token for one-click unsubscribe
    unsubscribe_token: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
    
    # Timestamps
    subscribed_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    unsubscribed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
    
    # Optional tracking
    source: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)  # e.g., "homepage", "blog", "footer"
    
    def __repr__(self) -> str:
        status = "active" if self.is_active else "inactive"
        return f"<NewsletterSubscriber {self.email} ({status})>"

