from __future__ import annotations

from app.services.steward_action_contracts import StewardActionPlanBuilder
from app.services.steward_action_executor import StewardActionExecutor
from app.services.steward_constants import BUSINESS_CANONICAL_FIELDS
from app.services.steward_intent_registry import (
    IntentDescriptor,
    register_intent,
)
from app.services.steward_query_executors import (
    build_travel_standard_query_steps,
    execute_travel_standard_query,
)


def bootstrap_intents() -> None:
    """注册小财管家支持的全部业务意图。

    新增意图时在这里追加一个 register_intent 调用即可,
    无需改动识别 / 动作生成 / 执行分发链路。
    """
    application_builder = StewardActionPlanBuilder()

    register_intent(
        IntentDescriptor(
            task_type="expense_application",
            assigned_agent="application_assistant",
            label="费用申请",
            action_steps_builder=application_builder.build_application_steps,
            signal_keywords=(
                "申请", "出差", "差旅", "费用", "交通", "住宿", "采购", "会务", "会议",
                "客户现场", "项目", "拜访", "调研", "驻场", "上线", "验收",
            ),
            ontology_field_allowlist=tuple(BUSINESS_CANONICAL_FIELDS),
            noop_actions=(
                "fill_application_fields",
                "build_application_preview",
                "validate_required_fields",
            ),
            side_effect_actions=("save_application_draft", "submit_application", "run_duplicate_precheck"),
            executor=StewardActionExecutor._dispatch_application_action,
            flow_id="travel_application",
            prompt_fragment=(
                "用户描述未来出差、差旅计划、去某地几天、部署、支撑、拜访或会议安排时,"
                "即使没有出现“申请”两个字,也必须优先识别为 expense_application。"
            ),
        )
    )

    register_intent(
        IntentDescriptor(
            task_type="reimbursement",
            assigned_agent="reimbursement_assistant",
            label="费用报销",
            action_steps_builder=application_builder.build_reimbursement_steps,
            signal_keywords=("报销", "报账", "票据", "发票", "凭证", "行程单", "付款截图", "小票", "收据"),
            ontology_field_allowlist=tuple(BUSINESS_CANONICAL_FIELDS),
            noop_actions=(
                "fill_reimbursement_fields",
                "build_reimbursement_preview",
                "validate_required_fields",
            ),
            side_effect_actions=(
                "create_reimbursement_draft",
                "link_existing_application",
                "associate_attachments",
            ),
            executor=StewardActionExecutor._dispatch_reimbursement_action,
            flow_id="travel_reimbursement",
            prompt_fragment=(
                "用户描述已经发生的费用、昨天/前天费用、票据或明确报销诉求时,"
                "才识别为 reimbursement。"
            ),
        )
    )

    register_intent(
        IntentDescriptor(
            task_type="query_travel_standard",
            assigned_agent="policy_query_assistant",
            label="差旅标准查询",
            action_steps_builder=build_travel_standard_query_steps,
            signal_keywords=(
                "差旅标准", "住宿标准", "出差标准", "交通标准", "出差补助",
                "差旅补贴", "住宿补助", "交通补助", "职级标准", "差标",
            ),
            ontology_field_allowlist=(
                "location",
                "employee_grade",
                "standard_category",
                "expense_type",
            ),
            noop_actions=(),
            side_effect_actions=("execute_travel_standard_query",),
            executor=execute_travel_standard_query,
            flow_id=None,
            prompt_fragment=(
                "用户询问差旅住宿标准、交通标准、出差补助或差旅补贴标准时,"
                "必须识别为 query_travel_standard,而不是 expense_application 或 reimbursement。"
                "差旅标准查询不创建任何单据,只返回标准数值。"
            ),
        )
    )


def _ensure_bootstrapped() -> None:
    if not all_task_types():
        bootstrap_intents()


# 导入即注册,保证 registry 在首次 import 后即处于可用状态。
bootstrap_intents()
