"""Dữ liệu placeholder dictionary và template registry cho generation pilot."""

from __future__ import annotations

from typing import Final, TypedDict

from app.models.dossier_schemas import GenerationTemplateRegistryEntry


class GenerationPlaceholderDefinition(TypedDict):
    """Định nghĩa metadata chuẩn của một placeholder."""

    name: str
    label: str
    required: bool
    source_hint: str
    example_value: str
    minimum_confidence: str


BIEN_BAN_BAN_GIAO_MAT_BANG_PLACEHOLDERS: Final[
    list[GenerationPlaceholderDefinition]
] = [
    {
        "name": "document_title",
        "label": "Tiêu đề biên bản",
        "required": True,
        "source_hint": "Giá trị mặc định theo mẫu văn bản đã chuẩn hóa",
        "example_value": "BIÊN BẢN BÀN GIAO MẶT BẰNG",
        "minimum_confidence": "verified_from_source",
    },
    {
        "name": "document_number",
        "label": "Số hiệu biên bản",
        "required": False,
        "source_hint": "Số văn bản nội bộ hoặc số phát hành do người dùng nhập",
        "example_value": "12/BBBGMB-2026",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "project_name",
        "label": "Tên dự án",
        "required": True,
        "source_hint": "Metadata dự án hoặc hồ sơ công trình đã xác minh",
        "example_value": "Dự án cải tạo tuyến đường nội thị khu A",
        "minimum_confidence": "verified_from_source",
    },
    {
        "name": "site_name",
        "label": "Tên khu vực hoặc hạng mục bàn giao",
        "required": True,
        "source_hint": "Thông tin công trình, vị trí mặt bằng hoặc mô tả hiện trường",
        "example_value": "Khu vực thi công gói thầu số 03",
        "minimum_confidence": "suggested_from_similar_template",
    },
    {
        "name": "handover_date",
        "label": "Ngày bàn giao",
        "required": True,
        "source_hint": "Ngày ghi trong biên bản, lịch thực hiện hoặc quyết định liên quan",
        "example_value": "Ngày 15 tháng 03 năm 2026",
        "minimum_confidence": "verified_from_source",
    },
    {
        "name": "handover_location",
        "label": "Địa điểm bàn giao",
        "required": True,
        "source_hint": "Địa điểm công trình, hiện trường hoặc nơi lập biên bản",
        "example_value": "Tại hiện trường dự án phường Tân Lập, thành phố Buôn Ma Thuột",
        "minimum_confidence": "suggested_from_similar_template",
    },
    {
        "name": "transferor_org_name",
        "label": "Tên đơn vị bàn giao",
        "required": True,
        "source_hint": "Thông tin tổ chức bên giao từ hồ sơ pháp lý hoặc dữ liệu đã xác minh",
        "example_value": "Ban Quản lý dự án đầu tư xây dựng thành phố",
        "minimum_confidence": "verified_from_source",
    },
    {
        "name": "transferor_org_address",
        "label": "Địa chỉ đơn vị bàn giao",
        "required": False,
        "source_hint": "Hồ sơ tổ chức bên giao hoặc thông tin hành chính đã lưu",
        "example_value": "Số 12 Lê Duẩn, thành phố Buôn Ma Thuột, Đắk Lắk",
        "minimum_confidence": "suggested_from_similar_template",
    },
    {
        "name": "transferor_signatory_full_name",
        "label": "Họ tên người đại diện bên giao",
        "required": True,
        "source_hint": "Thông tin người ký hoặc người đại diện được phân công phía bàn giao",
        "example_value": "Nguyễn Văn A",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "transferor_signatory_title",
        "label": "Chức danh người đại diện bên giao",
        "required": True,
        "source_hint": "Chức danh người ký hoặc người đại diện phía bàn giao",
        "example_value": "Giám đốc Ban Quản lý dự án",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "recipient_org_name",
        "label": "Tên đơn vị nhận bàn giao",
        "required": True,
        "source_hint": "Thông tin tổ chức bên nhận từ hợp đồng hoặc hồ sơ đã xác minh",
        "example_value": "Công ty Cổ phần Xây dựng Minh Phát",
        "minimum_confidence": "verified_from_source",
    },
    {
        "name": "recipient_org_address",
        "label": "Địa chỉ đơn vị nhận bàn giao",
        "required": False,
        "source_hint": "Hồ sơ tổ chức bên nhận hoặc dữ liệu pháp lý doanh nghiệp",
        "example_value": "Số 45 Nguyễn Tất Thành, thành phố Buôn Ma Thuột, Đắk Lắk",
        "minimum_confidence": "suggested_from_similar_template",
    },
    {
        "name": "recipient_signatory_full_name",
        "label": "Họ tên người đại diện bên nhận",
        "required": True,
        "source_hint": "Thông tin người ký hoặc người đại diện phía nhận bàn giao",
        "example_value": "Trần Văn B",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "recipient_signatory_title",
        "label": "Chức danh người đại diện bên nhận",
        "required": True,
        "source_hint": "Chức danh người ký hoặc người đại diện phía nhận bàn giao",
        "example_value": "Chỉ huy trưởng công trình",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "handover_scope_summary",
        "label": "Tóm tắt phạm vi bàn giao",
        "required": True,
        "source_hint": "Mô tả mặt bằng, phạm vi thi công hoặc nội dung bàn giao được chốt",
        "example_value": "Bàn giao toàn bộ mặt bằng thi công đoạn Km0+000 đến Km0+500 đã hoàn tất công tác giải phóng mặt bằng",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "handover_notes",
        "label": "Ghi chú bàn giao",
        "required": False,
        "source_hint": "Ghi chú bổ sung do người lập biên bản hoặc reviewer nhập",
        "example_value": "Nhà thầu có trách nhiệm tiếp nhận và tổ chức rào chắn an toàn trước khi thi công",
        "minimum_confidence": "manual_input_required",
    },
    {
        "name": "legal_basis_summary",
        "label": "Căn cứ pháp lý hoặc căn cứ bàn giao",
        "required": False,
        "source_hint": "Tóm tắt quyết định, hợp đồng hoặc căn cứ pháp lý liên quan",
        "example_value": "Căn cứ Hợp đồng thi công số 05/HĐTC ngày 10/03/2026 và kế hoạch triển khai đã được phê duyệt",
        "minimum_confidence": "suggested_from_similar_template",
    },
]

BIEN_BAN_BAN_GIAO_MAT_BANG_TEMPLATE_REGISTRY: Final[
    list[GenerationTemplateRegistryEntry]
] = [
    GenerationTemplateRegistryEntry(
        template_key="bien_ban_ban_giao_mat_bang_v1",
        template_name="Biên bản bàn giao mặt bằng - mẫu chuẩn v1",
        document_type="bien_ban_ban_giao_mat_bang",
        knowledge_type="template_form",
        doc_stage="execution",
        doc_family="execution",
        placeholder_keys=[
            placeholder["name"]
            for placeholder in BIEN_BAN_BAN_GIAO_MAT_BANG_PLACEHOLDERS
        ],
        version="v1",
        is_active=True,
    )
]

GENERATION_PLACEHOLDER_DICTIONARY: Final[
    dict[str, list[GenerationPlaceholderDefinition]]
] = {
    "bien_ban_ban_giao_mat_bang": BIEN_BAN_BAN_GIAO_MAT_BANG_PLACEHOLDERS,
}

GENERATION_TEMPLATE_REGISTRY: Final[
    dict[str, list[GenerationTemplateRegistryEntry]]
] = {
    "bien_ban_ban_giao_mat_bang": BIEN_BAN_BAN_GIAO_MAT_BANG_TEMPLATE_REGISTRY,
}


def get_placeholder_definitions(
    document_type: str,
) -> list[GenerationPlaceholderDefinition]:
    """Trả về danh sách placeholder chuẩn theo loại tài liệu."""

    return GENERATION_PLACEHOLDER_DICTIONARY.get(document_type, [])


def get_template_registry_entries(
    document_type: str,
) -> list[GenerationTemplateRegistryEntry]:
    """Trả về danh sách template registry theo loại tài liệu."""

    return GENERATION_TEMPLATE_REGISTRY.get(document_type, [])


def get_active_template_registry_entry(
    document_type: str,
) -> GenerationTemplateRegistryEntry | None:
    """Trả về template active đầu tiên của loại tài liệu."""

    entries = get_template_registry_entries(document_type)
    for entry in entries:
        if entry.is_active:
            return entry
    return None
