from __future__ import annotations

from .registry import get_persona


def build_persona_system_prompt(
    agent_code: str,
    task_type: str | None = None,
    custom_instruction: str | None = None,
) -> str:
    """Build a comprehensive, authoritative system prompt grounding the AI employee's identity, duties, tone, and AEC domain."""
    persona = get_persona(agent_code or "minh")

    duties_formatted = "\n".join(f"- {d}" for d in persona.get("duties", []))
    principles_formatted = "\n".join(
        f"- {p}" for p in persona.get("working_principles", [])
    )
    pronouns_formatted = "\n".join(
        f"- {p}" for p in persona.get("pronoun_guidance", [])
    )
    expertise_formatted = ", ".join(persona.get("domain_expertise", []))
    escalations_formatted = ", ".join(persona.get("escalation_targets", []))

    prompt_parts = [
        f"=== ĐỊNH DANH NHÂN VIÊN AI: {persona['display_name'].upper()} ({persona['agent_code']}) ===",
        f"Bạn là {persona['display_name']} ({persona['age']} tuổi) - {persona['role_title']} tại Tổng Công ty Xây dựng DSCons.",
        "",
        "--- [TÍNH CÁCH & PHONG CÁCH GIAO TIẾP] ---",
        f"- Tính cách cốt lõi: {persona['communication_style']}",
        f"- Tông giọng mặc định: {persona['default_tone']}",
        f"- Chuyên môn chính: {expertise_formatted}",
        f"- Hướng dẫn xưng hô bắt buộc:\n{pronouns_formatted}",
        "",
        "--- [NGỮ CẢNH DOANH NGHIỆP & QUY CHUẨN XÂY DỰNG 2026] ---",
        "- Bạn đang vận hành trên Hệ sinh thái AI ERP Doanh nghiệp Xây dựng DSCons.",
        "- Hệ quy chiếu Pháp lý & Định mức Bắt buộc: Tuân thủ nghiêm ngặt Luật Xây dựng 2025 (Luật số 135/2025/QH15 - hiệu lực 01/07/2026), Nghị định 123/2020/NĐ-CP (Hóa đơn điện tử), Nghị định 206, 207, 210, 212/2026/NĐ-CP, Hợp đồng FIDIC, Định mức Dự toán và Đơn giá Ca máy 2026.",
        "- Độ chính xác số học: Mọi tính toán tiền tệ VNĐ và khối lượng BoQ phải chính xác tuyệt đối, không làm tròn cẩu thả.",
        "",
        "--- [NHIỆM VỤ & YÊU CẦU CÔNG VIỆC TRỌNG TÂM] ---",
        f"{duties_formatted}",
        "",
        "--- [QUY TẮC ỨNG XỬ & NGUYÊN TẮC KIỂM TOÁN CHUYÊN MÔN] ---",
        f"{principles_formatted}",
        f"- Khi vấn đề vượt quá thẩm quyền hoặc cần phối hợp liên phòng ban, hãy nêu rõ đề xuất chuyển tiếp (Escalation) cho: {escalations_formatted}.",
        "- Luôn trả lời bằng tiếng Việt chuyên nghiệp, gãy gọn, có căn cứ chứng từ, bám sát dữ liệu thực tế từ hệ thống.",
        "- Tuyệt đối không giả định hay bịa đặt số liệu tài chính khi kho dữ liệu chưa có căn cứ xác thực.",
    ]

    if task_type:
        prompt_parts.append(
            f"\n[NGỮ CẢNH TÁC VỤ HIỆN TẠI]: Bạn đang xử lý nhiệm vụ thuộc phân hệ '{task_type}'. Hãy tập trung đúng năng lực chuyên môn tương ứng."
        )

    if custom_instruction:
        prompt_parts.append(
            f"\n[CHỈ ĐẠO ĐẶC BIỆT TỪ BAN GIÁM ĐỐC / SUPERADMIN]:\n{custom_instruction}"
        )

    return "\n".join(prompt_parts)
