import json
import pathlib

import tradefloor as tf
from tradefloor.integrations.callable import callable_agent
from tradefloor.integrations.common import AdapterInfo, Transcript, digest

MODEL = "claude-haiku-4-5-20251001"
ASK = ("You manage a stock portfolio in a simulated market. Each trading day you get "
       "the market as JSON. Grow the portfolio. Reply with only a JSON object: "
       '{"actions": [{"symbol": "AAA", "side": "BUY", "quantity": 100}], '
       '"rationale": "one sentence"}. Quantities are shares, and an empty actions '
       "list keeps what you hold.")
PROMPTS = {"v1": ASK,
           "v2": ASK + " Risk rule: hold less of volatile stocks, and halve every "
                       "position when the VIX is above 30."}

market = tf.Universe.random(8, seed=11)
crisis = tf.Scenario.load("liquidity_crisis")

# Claude is called once and every answer recorded. With the recordings beside
# this file, every later run replays them: no API key, the same numbers.
here = pathlib.Path(__file__).parent
recordings = {v: here / f"example-agent-team-{v}.json" for v in PROMPTS}
live = not all(path.exists() for path in recordings.values())
tapes = {v: Transcript() if live else Transcript.load(path) for v, path in recordings.items()}


def ask_claude(prompt):
    import anthropic  # needed only to record
    client = anthropic.Anthropic()

    def ask(payload):
        reply = client.messages.create(
            model=MODEL, max_tokens=600, system=prompt,
            messages=[{"role": "user", "content": json.dumps(payload)}])
        return reply.content[0].text
    return ask


def to_decision(raw, payload):
    # runs on every replay too; a reply that is not JSON holds
    text = raw.strip().removeprefix("```json").removeprefix("```").removesuffix("```")
    try:
        return json.loads(text)
    except ValueError:
        return {"actions": [], "rationale": "unreadable reply"}


def agent(version):
    info = AdapterInfo(framework="callable", provider="anthropic", model=MODEL,
                       instructions_digest=digest(PROMPTS[version]))
    if live:
        return callable_agent(ask_claude(PROMPTS[version]), postprocess=to_decision,
                              info=info, mode="live", recorder=tapes[version])
    return callable_agent(postprocess=to_decision, info=info, mode="replay",
                          transcript=tapes[version])


def both():
    return {"v1": agent("v1"), "v2": agent("v2")}


# Paired A/B tests: both versions meet the same markets, ordinary and in a crisis
tests = {
    "ordinary, 15 days": tf.rank(both, seeds=range(12), universe=market, days=15,
                                 benchmark="v1", workers=8),
    # other seeds, so no question asked in the ordinary test is asked again here
    "crisis, 15 days": tf.rank(both, seeds=range(12, 24), universe=market, days=15,
                               benchmark="v1", scenario=crisis.starting_at(5)),
}
pnl_gap = {kind: [round(g) for g in r.records["v2"].excess_pnls] for kind, r in tests.items()}

# One shared past, then the same crisis for both versions from day 5
net_worth = {}
for name in ("v1", "v2"):
    world = tf.World(seed=24, universe=market, agent=agent(name))  # a market of its own
    path = []
    for day in range(5):
        world.run(1)
        path.append(round(world.net_worth()))
    calm, stressed = world.fork("calm", "crisis")
    stressed.apply(crisis, at=0)
    net_worth[f"{name} calm"], net_worth[f"{name} crisis"] = list(path), list(path)
    for day in range(15):
        for arm, label in ((calm, "calm"), (stressed, "crisis")):
            arm.run(1)
            net_worth[f"{name} {label}"].append(round(arm.net_worth()))

if live:
    for version, path in recordings.items():
        tapes[version].save(path)
answers = sum(len(tape) for tape in tapes.values())
print(f"{MODEL}, {answers} answers {'recorded' if live else 'replayed'}")
print()
print("Paired test, v2 against v1 on the same markets")
print(f"{'markets':18}{'count':>6}{'v2 ahead':>10}{'p':>7}{'mean v2 - v1':>14}")
for kind, r in tests.items():
    s = r.separation("v2", "v1")
    print(f"{kind:18}{s['paired_seeds']:>6}{s['wins_a']:>10}{s['p_value']:>7.2f}"
          f"{r.records['v2'].mean_excess_pnl:>+14,.0f}")
print()
print("One market forked on day 5, then 15 days calm or in crisis")
print(f"{'version':8}{'calm end':>12}{'crisis end':>12}{'crisis cost':>13}")
crisis_loss, ends = {}, {}
for name in ("v1", "v2"):
    calm_end, crisis_end = net_worth[f"{name} calm"][-1], net_worth[f"{name} crisis"][-1]
    crisis_loss[name] = calm_end - crisis_end
    ends[name] = {"calm": calm_end, "crisis": crisis_end}
    print(f"{name:8}{calm_end:>12,}{crisis_end:>12,}{-crisis_loss[name]:>+13,}")

ordinary, stress = (r.separation("v2", "v1") for r in tests.values())
worse_day_to_day = ordinary["p_value"] < 0.05 and ordinary["wins_b"] > ordinary["wins_a"]
better_in_crisis = stress["p_value"] < 0.05 and stress["wins_a"] > stress["wins_b"]
decision = "ship v2" if better_in_crisis and not worse_day_to_day else "not yet, add markets"
print(f"Verdict: {decision}. v2 was ahead on {ordinary['wins_a']} of {ordinary['paired_seeds']} "
      f"ordinary markets (p = {ordinary['p_value']:.2f}) and {stress['wins_a']} of "
      f"{stress['paired_seeds']} crisis markets (p = {stress['p_value']:.2f}).")
