from datetime import datetime

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

from backend.database.connection import Base


class EchoTikCache(Base):
    """Cache for EchoTik API responses stored as JSON."""
    __tablename__ = "echotik_cache"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    cache_type: Mapped[str] = mapped_column(String(50), index=True)
    cache_key: Mapped[str] = mapped_column(String(200), unique=True, index=True)
    data_json: Mapped[str] = mapped_column(Text, default="[]")
    item_count: Mapped[int] = mapped_column(Integer, default=0)
    fetched_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=True)
