from typing import Literal, Optional

from pydantic import BaseModel, Field, ConfigDict
from pydantic.alias_generators import to_camel

from persona_models.common import create_uuid

KnowledgeBaseQuality = Literal["extremely_high", "very_high", "high", "medium", "low"]
KnowledgeBaseDocumentStatus = Literal["pending", "processing", "completed", "failed"]
KnowledgeBaseChunkSize = Literal["small", "medium", "large"]


class KnowledgeBase(BaseModel):
    id: str = Field(default_factory=create_uuid)
    project_id: str | None = None
    name: str
    description: str | None = None
    quality: KnowledgeBaseQuality = "medium"
    chunk_size: KnowledgeBaseChunkSize = "medium"
    embedding_model: str = "text-multilingual-embedding-002"

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class KnowledgeBaseDocument(BaseModel):
    id: str = Field(default_factory=create_uuid)
    knowledge_base_id: str
    path: str
    name: str
    size: int | None = None
    total_pages: int | None = None
    content_type: str | None = None
    status: KnowledgeBaseDocumentStatus = "pending"
    progress: float = 0.0
    extractor: str = "fitz"
    error: str | None = None
    metadata: dict = Field(default_factory=dict)
    callback_url: str | None = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class SearchChunksRequest(BaseModel):
    query: str
    top_k: int = 5
    threshold: Optional[float] = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)
