"""SQLAlchemy models for the reader service."""

from datetime import datetime

from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    ForeignKey,
    Index,
    Integer,
    String,
    Text,
    UniqueConstraint,
)
from sqlalchemy.orm import relationship

from app.database import Base


class Category(Base):
    __tablename__ = "reader_categories"

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(String(64), nullable=False)
    title = Column(String(255), nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)

    feeds = relationship("Feed", back_populates="category", lazy="selectin")


class Feed(Base):
    __tablename__ = "reader_feeds"

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(String(64), nullable=False)
    category_id = Column(Integer, ForeignKey("reader_categories.id", ondelete="SET NULL"), nullable=True)
    title = Column(String(500), nullable=False)
    feed_url = Column(Text, nullable=False, unique=True)
    site_url = Column(Text)
    etag = Column(String(255))
    last_modified = Column(String(255))
    last_fetched_at = Column(DateTime)
    created_at = Column(DateTime, default=datetime.utcnow)

    category = relationship("Category", back_populates="feeds", lazy="selectin")
    entries = relationship("Entry", back_populates="feed", lazy="noload", cascade="all, delete-orphan")


class Entry(Base):
    __tablename__ = "reader_entries"
    __table_args__ = (
        UniqueConstraint("feed_id", "url", name="uq_reader_entries_feed_url"),
        Index("idx_reader_entries_user_status", "user_id", "status"),
        Index("idx_reader_entries_user_starred", "user_id", "starred"),
        Index("idx_reader_entries_feed", "feed_id"),
        Index("idx_reader_entries_published", "published_at"),
    )

    id = Column(Integer, primary_key=True, autoincrement=True)
    feed_id = Column(Integer, ForeignKey("reader_feeds.id", ondelete="CASCADE"), nullable=False)
    user_id = Column(String(64), nullable=False)
    title = Column(String(1000))
    url = Column(Text)
    content = Column(Text)
    full_content = Column(Text)
    author = Column(String(500))
    published_at = Column(DateTime)
    status = Column(String(10), default="unread")
    starred = Column(Boolean, default=False)
    thumbnail = Column(Text)
    reading_time = Column(Integer, default=1)
    created_at = Column(DateTime, default=datetime.utcnow)

    feed = relationship("Feed", back_populates="entries", lazy="selectin")
