"""Execute one database-backed V2 test using only a demand word."""

from __future__ import annotations

import argparse
import json

from find_agent_v2.demand_context import prepare_latest_v2_demand_run_by_name
from find_agent_v2.runner import run_find_agent_v2


def main() -> None:
    parser = argparse.ArgumentParser(
        description="按需求词查询线上最新 demand_grade 上下文并执行寻找 Agent v2",
    )
    parser.add_argument("demand_word", help="与 demand_grade.demand_name 精确匹配的需求词")
    args = parser.parse_args()

    prepared = prepare_latest_v2_demand_run_by_name(args.demand_word)
    result = run_find_agent_v2(prepared.user_input, run_id=prepared.run_id)
    output = {
        "prepared": prepared.summary(),
        "executed": True,
        "result": {
            "status": result.status,
            "succeeded": result.succeeded,
            "valid_primary_count": result.valid_primary_count,
            "stop_reason": result.stop_reason,
            "final_output": result.final_output,
        },
    }
    print(json.dumps(output, ensure_ascii=False, default=str, indent=2))


if __name__ == "__main__":
    main()
