from datetime import datetime

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

from backend.database.connection import Base


class SavedVideo(Base):
    """User's saved/favorite videos - personal library of winning videos."""
    __tablename__ = "saved_videos"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    video_id: Mapped[int] = mapped_column(ForeignKey("videos.id"), index=True)
    notes: Mapped[str] = mapped_column(Text, default="")
    tags: Mapped[str] = mapped_column(Text, default="")  # comma-separated
    folder: Mapped[str] = mapped_column(String(100), default="geral")
    saved_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    video = relationship("Video")

    def to_dict(self) -> dict:
        return {
            "id": self.id,
            "video_id": self.video_id,
            "notes": self.notes,
            "tags": self.tags,
            "folder": self.folder,
            "saved_at": self.saved_at.isoformat() if self.saved_at else None,
        }
