from __future__ import annotations

from enum import Enum
from typing import Literal

from pydantic import BaseModel, Field


class GenerationConfidenceValue(str, Enum):
    HIGH = "high"
    MEDIUM = "medium"
    LOW = "low"
    INFERRED = "inferred"
    UNKNOWN = "unknown"


class GenerationReadinessValue(str, Enum):
    READY = "ready"
    NEEDS_REVIEW = "needs_review"
    BLOCKED = "blocked"
    DRAFT = "draft"


class GenerationReviewWorkflowValue(str, Enum):
    PENDING = "pending"
    IN_REVIEW = "in_review"
    APPROVED = "approved"
    REJECTED = "rejected"


class GenerationConfidenceLevel(BaseModel):
    """Mức độ tin cậy của dữ liệu phục vụ sinh hồ sơ."""

    value: GenerationConfidenceValue = Field(
        ...,
        description="Mức độ tin cậy của dữ liệu dùng để sinh hồ sơ",
    )


class GenerationOrganizationInfo(BaseModel):
    """Thông tin tổ chức được làm giàu để phục vụ sinh hồ sơ."""

    name: str = Field(..., description="Tên tổ chức")
    short_name: str | None = Field(None, description="Tên viết tắt của tổ chức")
    department: str | None = Field(None, description="Phòng ban hoặc đơn vị trực thuộc")
    address: str | None = Field(None, description="Địa chỉ tổ chức")
    representative_name: str | None = Field(
        None,
        description="Tên người đại diện gắn với tổ chức",
    )
    representative_title: str | None = Field(
        None,
        description="Chức danh người đại diện gắn với tổ chức",
    )


class GenerationSignatoryInfo(BaseModel):
    """Thông tin người ký được làm giàu để phục vụ sinh hồ sơ."""

    full_name: str = Field(..., description="Họ tên người ký")
    title: str | None = Field(None, description="Chức danh người ký")
    organization_name: str | None = Field(None, description="Tên tổ chức của người ký")
    organization: GenerationOrganizationInfo | None = Field(
        None,
        description="Thông tin tổ chức mở rộng của người ký",
    )
    signing_place: str | None = Field(None, description="Địa điểm ký")
    signing_date_text: str | None = Field(
        None,
        description="Chuỗi ngày ký đã định dạng để đưa vào văn bản",
    )


class GenerationFieldStatus(BaseModel):
    """Trạng thái của một trường dữ liệu trong quá trình chuẩn bị sinh hồ sơ."""

    field_name: str = Field(..., description="Tên trường dữ liệu hoặc placeholder")
    label: str | None = Field(
        None, description="Nhãn hiển thị thân thiện cho người dùng"
    )
    value: str | int | float | bool | list[str] | None = Field(
        None,
        description="Giá trị hiện tại đã thu thập được cho trường dữ liệu",
    )
    confidence: GenerationConfidenceValue = Field(
        ...,
        description="Mức độ tin cậy của giá trị hiện tại",
    )
    is_required: bool = Field(
        True, description="Đánh dấu trường có bắt buộc để render hay không"
    )
    is_resolved: bool = Field(
        False,
        description="Đánh dấu trường đã đủ dữ liệu và không cần bổ sung thêm hay chưa",
    )
    source_paths: list[str] = Field(
        default_factory=list,
        description="Danh sách đường dẫn nguồn dữ liệu đã dùng để suy ra giá trị",
    )
    notes: str | None = Field(
        None,
        description="Ghi chú bổ sung cho reviewer hoặc người nhập liệu",
    )


class GenerationTemplateRegistryEntry(BaseModel):
    """Metadata template dùng trong kho template registry cho pilot."""

    template_key: str = Field(..., description="Khóa định danh duy nhất của template")
    template_name: str = Field(..., description="Tên hiển thị của template")
    document_type: Literal["bien_ban_ban_giao_mat_bang"] = Field(
        ...,
        description="Loại hồ sơ pilot mà template phục vụ",
    )
    knowledge_type: Literal["template_form"] = Field(
        "template_form",
        description="Loại tri thức của bản ghi template",
    )
    doc_stage: Literal["execution"] = Field(
        "execution",
        description="Giai đoạn hồ sơ mà template thuộc về",
    )
    doc_family: Literal["execution"] = Field(
        "execution",
        description="Nhóm hồ sơ nghiệp vụ mà template thuộc về",
    )
    placeholder_keys: list[str] = Field(
        default_factory=list,
        description="Danh sách placeholder được template hỗ trợ",
    )
    version: str | None = Field(None, description="Phiên bản template")
    is_active: bool = Field(True, description="Đánh dấu template đang hoạt động")


class GenerationReadinessStatus(BaseModel):
    """Trạng thái sẵn sàng sinh hồ sơ."""

    status: GenerationReadinessValue = Field(
        ...,
        description="Trạng thái sẵn sàng hiện tại",
    )
    missing_required_fields: list[str] = Field(
        default_factory=list,
        description="Danh sách trường bắt buộc còn thiếu",
    )
    unresolved_review_fields: list[str] = Field(
        default_factory=list,
        description="Danh sách trường còn cần reviewer xác nhận",
    )
    summary: str | None = Field(
        None,
        description="Tóm tắt ngắn gọn lý do của trạng thái sẵn sàng hiện tại",
    )


class GenerationReviewWorkflowStatus(BaseModel):
    """Trạng thái review của hồ sơ sinh tự động."""

    status: GenerationReviewWorkflowValue = Field(
        ...,
        description="Trạng thái review hiện tại",
    )
    reviewer_name: str | None = Field(None, description="Tên người review gần nhất")
    reviewed_at: str | None = Field(
        None,
        description="Thời điểm review gần nhất dưới dạng chuỗi ISO hoặc định dạng hiển thị",
    )
    review_notes: str | None = Field(None, description="Ghi chú review")
