"""Runs the live agent through the same scenario as the original DEMO_SCRIPT and captures
structured output per turn, for comparison. Writes JSON to /tmp for the PDF report step.
"""
import json

from agent_core import ReasoningAgent

TURNS = [
    "I have a customer who received a cancellation notice due to a missed DD. He wants to "
    "complain because he called previously and was told we would reapply the DD after 10 days.",
    "Billing history shows 5 missed Direct Debits this year, and account notes confirm the "
    "previous advisor told him the DD would be reapplied.",
    "He says his hours were cut at work and he is earning less lately, causing the missed "
    "payments.",
    "He said yes, he is happy for us to record it.",
    "Please go ahead and offer him the resolution.",
    "Customer accepts the resolution.",
    "ready",
]

if __name__ == "__main__":
    agent = ReasoningAgent()
    history = []
    facts = None
    results = []
    for i, text in enumerate(TURNS, 1):
        print(f"--- running turn {i}/{len(TURNS)} ---", flush=True)
        result = agent.run(text, history=history, carried=facts)
        facts = result["facts"]
        answer = result["answer"]
        results.append({
            "turn": i,
            "input": text,
            "stage": answer["stage"],
            "headline": answer["headline"],
            "next_moves": answer["next_moves"],
            "suggested_wording": answer["suggested_wording"],
            "citations": [c["reference"] for c in answer["citations"]],
            "case_note": answer["case_note"],
            "ok": result["verification"]["ok"],
            "violations": result["verification"]["violations"],
        })
        history.append({"note": text, "response_json": result.get("_response_json")})
        print(f"  ok={result['verification']['ok']} stage={answer['stage']}", flush=True)

    with open("/tmp/agent2_e2e_results.json", "w") as f:
        json.dump(results, f, indent=2)
    print("Wrote /tmp/agent2_e2e_results.json")
