"""document_chunks 테이블 ORM — chunk 단위 검색 (Phase 0.1)"""

from datetime import datetime

from pgvector.sqlalchemy import Vector
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Integer, SmallInteger, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

from core.database import Base


class DocumentChunk(Base):
    __tablename__ = "document_chunks"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    doc_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("documents.id", ondelete="CASCADE"), nullable=False
    )
    chunk_index: Mapped[int] = mapped_column(Integer, nullable=False)

    # chunking 전략 메타
    chunk_type: Mapped[str] = mapped_column(String(30), nullable=False)
    section_title: Mapped[str | None] = mapped_column(Text)
    heading_path: Mapped[str | None] = mapped_column(Text)
    page: Mapped[int | None] = mapped_column(Integer)

    # 다국어/domain 메타
    language: Mapped[str | None] = mapped_column(String(10))
    country: Mapped[str | None] = mapped_column(String(10))
    source: Mapped[str | None] = mapped_column(String(100))
    domain_category: Mapped[str] = mapped_column(String(20), nullable=False)

    # 본문 + 임베딩
    text: Mapped[str] = mapped_column(Text, nullable=False)
    embedding = mapped_column(Vector(1024), nullable=True)

    # Hier-Decomp-1: 계층 분해 트리 (migration 282). 기존 chunk_worker INSERT 는 미설정 →
    # server_default 로 legacy 행 = in_corpus=true / is_leaf=false 보장.
    parent_id: Mapped[int | None] = mapped_column(BigInteger)  # 트리 부모. DB FK 미설정(app-level).
    level: Mapped[int | None] = mapped_column(SmallInteger)    # authoritative depth.
    node_type: Mapped[str | None] = mapped_column(Text)        # nullable hint, retrieval/replace 활성 조건 미사용.
    is_leaf: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="false")   # authoritative leaf 마커.
    in_corpus: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="true")  # 검색 코퍼스 편입 여부.

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now, onupdate=datetime.now
    )

    __table_args__ = (
        UniqueConstraint("doc_id", "chunk_index", name="uq_chunks_doc_index"),
    )
