"""
책갈피 모델
"""
from sqlalchemy import Column, String, DateTime, Text, Integer, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
import uuid

from ..core.database import Base


class Bookmark(Base):
    """책갈피 테이블"""
    __tablename__ = "bookmarks"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    
    # 연결 정보
    user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
    document_id = Column(UUID(as_uuid=True), ForeignKey("documents.id"), nullable=False)
    
    # 책갈피 정보
    title = Column(String(200), nullable=False)  # 책갈피 제목
    description = Column(Text, nullable=True)    # 설명
    
    # 위치 정보
    page_number = Column(Integer, nullable=True)  # 페이지 번호 (추정)
    scroll_position = Column(Integer, default=0)  # 스크롤 위치 (픽셀)
    element_id = Column(String(100), nullable=True)  # 특정 요소 ID
    element_selector = Column(Text, nullable=True)   # CSS 선택자
    
    # 메타데이터
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    # 관계
    user = relationship("User", backref="bookmarks")
    document = relationship("Document", back_populates="bookmarks")

    def __repr__(self):
        return f"<Bookmark(title='{self.title}', document='{self.document_id}')>"
