"""
知识库模型 — SQLModel
对应 knowledge_items 和 knowledge_embeddings 两张表
embedding 字段使用 pgvector.Vector（对应 PG vector(1536)）
"""

from datetime import datetime, date
from typing import Optional, Any

from sqlalchemy import Column, DateTime as SADateTime, Text, Integer
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func as sa_func
from sqlmodel import Field, SQLModel

from pgvector.sqlalchemy import Vector


class KnowledgeItem(SQLModel, table=True):
    """知识库条目（knowledge_items）"""
    __tablename__ = "knowledge_items"

    id: Optional[int] = Field(default=None, primary_key=True)
    title: str = Field(max_length=300)
    content_md: Optional[str] = Field(default=None)
    source_type: str = Field(default="manual", max_length=30)
    source_file_name: Optional[str] = Field(default=None, max_length=300)
    source_url: Optional[str] = Field(default=None, max_length=1000)
    author: Optional[str] = Field(default=None, max_length=100)
    publish_date: Optional[date] = Field(default=None)
    tags: Any = Field(default=None, sa_column=Column(JSONB, default=[]))
    related_entities: Any = Field(default=None, sa_column=Column(JSONB, default=[]))
    related_concepts: Any = Field(default=None, sa_column=Column(JSONB, default=[]))
    created_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )
    updated_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )


class KnowledgeEmbedding(SQLModel, table=True):
    """知识库向量（knowledge_embeddings）"""
    __tablename__ = "knowledge_embeddings"

    id: Optional[int] = Field(default=None, primary_key=True)
    knowledge_id: int = Field(foreign_key="knowledge_items.id", index=True)
    chunk_index: int = Field(default=0)
    chunk_text: str = Field(sa_column=Column(Text, nullable=False))
    embedding: Any = Field(sa_column=Column(Vector(1536), nullable=False))
    created_at: datetime | None = Field(
        default=None,
        sa_column=Column(SADateTime(timezone=True), nullable=False, server_default=sa_func.now()),
    )