from __future__ import annotations

from pathlib import Path

# Hằng số dùng chung cho ingest tài liệu mẫu vàng.
PROJECT_DEFAULTS_PATH = Path("docs/kenh-xay-gold-reference-metadata-schema.json")
MANIFEST_PATH = Path("docs/kenh-xay-document-manifest.json")
DEFAULT_ROOT_DIR = Path(".")
DEFAULT_INGEST_BATCH = "kenh_xay_gold_reference_v1"

CHUNK_SIZES = {
    "pdf": {"chunk_size": 1200, "overlap": 160},
    "doc": {"chunk_size": 1400, "overlap": 180},
    "docx": {"chunk_size": 1400, "overlap": 180},
    "xls": {"chunk_size": 1000, "overlap": 120},
    "xlsx": {"chunk_size": 1000, "overlap": 120},
    "pages": {"chunk_size": 1200, "overlap": 150},
    "txt": {"chunk_size": 1400, "overlap": 180},
}

PACKAGE_NAME_MAP = {
    "package_project_wide": "Hồ sơ dùng chung toàn công trình",
    "package_01_tcxd": "Gói 01 - Thi công xây dựng",
    "package_02_tvgs": "Gói 02 - Tư vấn giám sát",
    "package_03_tvqlda": "Gói 03 - Tư vấn quản lý dự án",
    "package_04_kiem_toan_doc_lap": "Gói 04 - Kiểm toán độc lập",
    "package_design": "Gói/nhóm thiết kế - lập BCKTKT",
    "package_tham_tra": "Gói/nhóm thẩm tra",
    "package_unknown": "Chưa xác định gói",
}

TASK_TYPE_BY_DOCUMENT_GROUP = {
    "project_overview": "project_planning",
    "technical_design_estimate": "technical_delivery",
    "legal_approvals": "legal_compliance",
    "construction_package": "contract_management",
    "consulting_packages": "payment_settlement",
    "execution_quality_payment": "payment_settlement",
    "system_noise": "audit_inspection",
}

TASK_TYPE_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": "project_planning",
    "danh_sach_nha_thau": "procurement_tender",
    "quyet_dinh": "legal_compliance",
    "ke_hoach_lua_chon_nha_thau": "procurement_tender",
    "phe_duyet_ket_qua_lcnt": "procurement_tender",
    "don_nhan_thau": "procurement_tender",
    "bien_ban_thuong_thao": "contract_management",
    "hop_dong": "contract_management",
    "phap_ly": "legal_compliance",
    "ban_ve": "technical_delivery",
    "khoi_luong": "technical_delivery",
    "du_toan": "technical_delivery",
    "vat_tu_dinh_muc": "technical_delivery",
    "nghiem_thu": "payment_settlement",
    "de_nghi_thanh_toan": "payment_settlement",
    "phu_luc_thanh_toan": "payment_settlement",
    "nhat_ky": "technical_delivery",
    "ho_so_chat_luong": "technical_delivery",
    "quyet_toan": "payment_settlement",
    "mau_bieu_02a": "payment_settlement",
}

STAGE_NORMALIZATION_MAP = {
    "project_setup": "overview",
    "approval": "legal_setup",
    "procurement_approval": "procurement",
    "consulting_assignment": "legal_setup",
    "contract_award": "procurement",
    "pre_contract": "contracting",
    "contract_execution": "execution",
    "execution_quality": "execution",
    "completion": "acceptance",
}

DOC_FAMILY_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": "inventory",
    "danh_sach_nha_thau": "procurement",
    "quyet_dinh": "decision",
    "ke_hoach_lua_chon_nha_thau": "decision",
    "phe_duyet_ket_qua_lcnt": "decision",
    "don_nhan_thau": "procurement",
    "bien_ban_thuong_thao": "contract",
    "hop_dong": "contract",
    "phap_ly": "legal",
    "ban_ve": "drawing",
    "khoi_luong": "estimate",
    "du_toan": "estimate",
    "vat_tu_dinh_muc": "estimate",
    "nghiem_thu": "acceptance",
    "de_nghi_thanh_toan": "payment",
    "phu_luc_thanh_toan": "payment",
    "nhat_ky": "execution",
    "ho_so_chat_luong": "quality",
    "quyet_toan": "settlement",
    "mau_bieu_02a": "template",
    "bia_ho_so": "cover",
    "noise": "noise",
}

GOLD_REFERENCE_ROLE_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": "required",
    "ban_ve": "required",
    "khoi_luong": "required",
    "du_toan": "required",
    "vat_tu_dinh_muc": "example",
    "danh_sach_nha_thau": "optional",
    "quyet_dinh": "required",
    "ke_hoach_lua_chon_nha_thau": "required",
    "phe_duyet_ket_qua_lcnt": "required",
    "don_nhan_thau": "required",
    "bien_ban_thuong_thao": "required",
    "hop_dong": "required",
    "phap_ly": "optional",
    "mau_bieu_02a": "example",
    "nghiem_thu": "required",
    "de_nghi_thanh_toan": "required",
    "phu_luc_thanh_toan": "required",
    "nhat_ky": "optional",
    "ho_so_chat_luong": "optional",
    "quyet_toan": "required",
    "bia_ho_so": "exclude",
    "noise": "exclude",
}

REQUIRED_FOR_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": ["overview", "missing_doc_detection"],
    "ban_ve": ["design_estimate", "execution", "acceptance"],
    "khoi_luong": ["design_estimate", "payment", "settlement"],
    "du_toan": ["design_estimate", "payment", "settlement"],
    "vat_tu_dinh_muc": ["design_estimate", "execution"],
    "danh_sach_nha_thau": ["procurement"],
    "quyet_dinh": ["legal_setup", "procurement", "contracting"],
    "ke_hoach_lua_chon_nha_thau": ["procurement"],
    "phe_duyet_ket_qua_lcnt": ["procurement", "contracting"],
    "don_nhan_thau": ["procurement", "contracting"],
    "bien_ban_thuong_thao": ["contracting"],
    "hop_dong": ["contracting", "execution", "payment", "settlement"],
    "phap_ly": ["contracting", "execution"],
    "mau_bieu_02a": ["payment"],
    "nghiem_thu": ["acceptance", "payment"],
    "de_nghi_thanh_toan": ["payment"],
    "phu_luc_thanh_toan": ["payment"],
    "nhat_ky": ["execution", "acceptance"],
    "ho_so_chat_luong": ["execution", "acceptance", "payment"],
    "quyet_toan": ["settlement", "audit_preparation"],
}

USED_FOR_TASKS_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": [
        "template_training",
        "inventory_mapping",
        "missing_doc_detection",
        "retrieval_grounding",
    ],
    "ban_ve": ["template_training", "execution_guidance", "retrieval_grounding"],
    "khoi_luong": [
        "template_training",
        "payment_check",
        "settlement_check",
        "retrieval_grounding",
    ],
    "du_toan": [
        "template_training",
        "payment_check",
        "settlement_check",
        "retrieval_grounding",
    ],
    "vat_tu_dinh_muc": [
        "template_training",
        "execution_guidance",
        "retrieval_grounding",
    ],
    "danh_sach_nha_thau": [
        "inventory_mapping",
        "package_classification",
        "retrieval_grounding",
    ],
    "quyet_dinh": [
        "legal_timeline_check",
        "package_classification",
        "retrieval_grounding",
    ],
    "ke_hoach_lua_chon_nha_thau": [
        "legal_timeline_check",
        "package_classification",
        "retrieval_grounding",
    ],
    "phe_duyet_ket_qua_lcnt": [
        "legal_timeline_check",
        "contract_review",
        "retrieval_grounding",
    ],
    "don_nhan_thau": ["contract_review", "retrieval_grounding"],
    "bien_ban_thuong_thao": [
        "template_training",
        "contract_review",
        "retrieval_grounding",
    ],
    "hop_dong": [
        "template_training",
        "contract_review",
        "payment_check",
        "settlement_check",
        "retrieval_grounding",
    ],
    "phap_ly": ["contract_review", "legal_timeline_check", "retrieval_grounding"],
    "mau_bieu_02a": ["template_training", "payment_check", "retrieval_grounding"],
    "nghiem_thu": [
        "template_training",
        "quality_check",
        "payment_check",
        "retrieval_grounding",
    ],
    "de_nghi_thanh_toan": [
        "template_training",
        "payment_check",
        "audit_preparation",
        "retrieval_grounding",
    ],
    "phu_luc_thanh_toan": ["payment_check", "audit_preparation", "retrieval_grounding"],
    "nhat_ky": ["execution_guidance", "quality_check", "retrieval_grounding"],
    "ho_so_chat_luong": ["quality_check", "payment_check", "retrieval_grounding"],
    "quyet_toan": ["settlement_check", "audit_preparation", "retrieval_grounding"],
}

EVIDENCE_ROLE_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": "inventory_anchor",
    "ban_ve": "technical_basis",
    "khoi_luong": "quantity_basis",
    "du_toan": "pricing_basis",
    "vat_tu_dinh_muc": "norms_reference",
    "danh_sach_nha_thau": "procurement_context",
    "quyet_dinh": "legal_basis",
    "ke_hoach_lua_chon_nha_thau": "procurement_basis",
    "phe_duyet_ket_qua_lcnt": "award_basis",
    "don_nhan_thau": "bid_submission",
    "bien_ban_thuong_thao": "negotiation_basis",
    "hop_dong": "payment_basis",
    "phap_ly": "legal_context",
    "mau_bieu_02a": "template_example",
    "nghiem_thu": "acceptance_basis",
    "de_nghi_thanh_toan": "payment_request",
    "phu_luc_thanh_toan": "payment_schedule",
    "nhat_ky": "execution_trace",
    "ho_so_chat_luong": "quality_evidence",
    "quyet_toan": "settlement_basis",
}

RETRIEVAL_PRIORITY_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": 100,
    "hop_dong": 100,
    "nghiem_thu": 98,
    "khoi_luong": 97,
    "du_toan": 97,
    "ke_hoach_lua_chon_nha_thau": 96,
    "phe_duyet_ket_qua_lcnt": 96,
    "quyet_dinh": 94,
    "de_nghi_thanh_toan": 93,
    "phu_luc_thanh_toan": 92,
    "ban_ve": 91,
    "quyet_toan": 91,
    "bien_ban_thuong_thao": 90,
    "phap_ly": 84,
    "nhat_ky": 82,
    "ho_so_chat_luong": 80,
    "danh_sach_nha_thau": 76,
    "mau_bieu_02a": 72,
    "vat_tu_dinh_muc": 70,
    "bia_ho_so": 0,
    "noise": 0,
}

TRAINING_VALUE_BY_DOCUMENT_TYPE = {
    "danh_muc_ho_so": "high",
    "ban_ve": "high",
    "khoi_luong": "high",
    "du_toan": "high",
    "quyet_dinh": "high",
    "ke_hoach_lua_chon_nha_thau": "high",
    "phe_duyet_ket_qua_lcnt": "high",
    "bien_ban_thuong_thao": "high",
    "hop_dong": "high",
    "nghiem_thu": "high",
    "de_nghi_thanh_toan": "high",
    "phu_luc_thanh_toan": "medium",
    "quyet_toan": "high",
    "phap_ly": "medium",
    "nhat_ky": "medium",
    "ho_so_chat_luong": "medium",
    "danh_sach_nha_thau": "medium",
    "mau_bieu_02a": "medium",
    "vat_tu_dinh_muc": "medium",
    "bia_ho_so": "low",
    "noise": "low",
}
