from datetime import datetime, timezone

from sqlalchemy import DateTime, Integer, Text
from sqlalchemy.orm import Mapped, mapped_column

from fabledassistant.models import Base


class ImageCache(Base):
    """Metadata for an image fetched from the web and stored locally on disk."""

    __tablename__ = "image_cache"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    url_hash: Mapped[str] = mapped_column(Text, unique=True, nullable=False, index=True)
    original_url: Mapped[str] = mapped_column(Text, nullable=False)
    source_domain: Mapped[str | None] = mapped_column(Text, nullable=True)
    title: Mapped[str | None] = mapped_column(Text, nullable=True)
    content_type: Mapped[str] = mapped_column(Text, nullable=False, default="image/jpeg")
    file_size: Mapped[int | None] = mapped_column(Integer, nullable=True)
    file_ext: Mapped[str] = mapped_column(Text, nullable=False, default="jpg")
    fetched_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=lambda: datetime.now(timezone.utc),
    )
