"""
reconcile_fills.py -- [2026-10-07] The user: "update the correct exit on the report so we can go back and have the right
trade value". The bots record a trade at the price THEY saw when deciding (DNA 10/7: a bad 10.82 print -> logged -$72.08;
the broker really filled 11.76 -> about +$54). This rewrites each closed trade of <bot>/data/trades/<date>_trades.jsonl
with the broker's real fills:
  entry_price / exit_price / pl_pct / pl_dollars  <- the filled prices (entry_order_id; exit_order_id, or the SELL order
                                                     of that symbol submitted within 90 s of the logged exit)
  entry_price_bot / exit_price_bot / pl_pct_bot / pl_dollars_bot  <- the bot's original numbers (kept)
  fills_reconciled: true
and rewrites <date>_summary.json. Idempotent. Run after the close (cron 16:03) and once for past days.
    python3 reconcile_fills.py /var/www/screener/trade 2026-10-07 [more dates | all]
"""
import json
import re
import sys
from datetime import datetime, timedelta
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))


def client_for(bot_dir):
    """the bot's OWN account (its .env) -- the trade bot and the Star bot use different paper accounts"""
    from alpaca.trading.client import TradingClient
    env = {}
    for l in open(Path(bot_dir) / ".env"):
        if "=" in l and not l.strip().startswith("#"):
            k, v = l.split("=", 1); env[k.strip()] = v.strip().strip("'\"")
    return TradingClient(env["ALPACA_API_KEY"], env["ALPACA_SECRET_KEY"], paper=True)


def sell_orders(tc, symbol, day):
    from alpaca.trading.requests import GetOrdersRequest
    from alpaca.trading.enums import QueryOrderStatus
    d0 = datetime.fromisoformat(day)
    req = GetOrdersRequest(status=QueryOrderStatus.CLOSED, symbols=[symbol], after=d0, until=d0 + timedelta(days=1), limit=500)
    return [o for o in tc.get_orders(req) if str(o.side).lower().endswith("sell") and o.filled_avg_price]


def main(bot_dir, days):
    tc = client_for(bot_dir)
    tdir = Path(bot_dir) / "data" / "trades"
    if days == ["all"]:
        days = sorted(p.name[:10] for p in tdir.glob("*_trades.jsonl"))
    for day in days:
        f = tdir / f"{day}_trades.jsonl"
        if not f.exists():
            continue
        T = [json.loads(l) for l in open(f) if l.strip()]
        cache, changed, used = {}, 0, set()
        for t in T:
            if t.get("status") != "closed" or not t.get("exit_price"):
                continue
            if "entry_price_bot" not in t:
                t["entry_price_bot"], t["exit_price_bot"] = t["entry_price"], t["exit_price"]
                t["pl_pct_bot"], t["pl_dollars_bot"] = t.get("pl_pct"), t.get("pl_dollars")
            ef = xf = None
            try:
                if t.get("entry_order_id"):
                    o = tc.get_order_by_id(t["entry_order_id"])
                    ef = float(o.filled_avg_price) if o.filled_avg_price else None
                if t.get("exit_order_id"):
                    # [2026-10-07] a sell can be a limit + the market remainder ("id1,id2"): quantity-weighted average
                    num = den = 0.0
                    for oid in str(t["exit_order_id"]).split(","):
                        o = tc.get_order_by_id(oid.strip())
                        if o.filled_avg_price and float(o.filled_qty or 0) > 0:
                            num += float(o.filled_avg_price) * float(o.filled_qty); den += float(o.filled_qty)
                    xf = num / den if den else None
                if xf is None:
                    s = t["symbol"]
                    if s not in cache:
                        cache[s] = sell_orders(tc, s, day)
                    xt = datetime.fromisoformat(t["exit_time"])
                    cands = [o for o in cache[s] if str(o.id) not in used
                             and -5 <= (o.submitted_at - xt).total_seconds() <= 90]
                    if cands:
                        o = min(cands, key=lambda o: abs((o.submitted_at - xt).total_seconds()))
                        used.add(str(o.id)); t["exit_order_id"] = str(o.id); xf = float(o.filled_avg_price)
            except Exception as e:
                print(f"  {day} {t['symbol']}: lookup failed {e}")
            e = ef or t["entry_price_bot"]; x = xf or t["exit_price_bot"]
            t["entry_price"], t["exit_price"] = e, x
            t["pl_pct"] = round((x / e - 1) * 100, 3) if e else 0.0
            t["pl_dollars"] = round((x - e) * t["qty"], 2)
            t["fills_reconciled"] = bool(ef and xf)
            if abs((t["pl_dollars_bot"] or 0) - t["pl_dollars"]) >= 0.01:
                changed += 1
                if abs((t["pl_dollars_bot"] or 0) - t["pl_dollars"]) >= 5:
                    print(f"  {day} {t['symbol']:5} bot ${t['pl_dollars_bot']:+.2f} -> real ${t['pl_dollars']:+.2f} "
                          f"(buy {t['entry_price_bot']} -> {e}, sell {t['exit_price_bot']} -> {x})")
        tmp = f.with_suffix(".tmp")
        with open(tmp, "w") as fh:
            for t in T:
                fh.write(json.dumps(t) + "\n")
        tmp.rename(f)
        closed = [t for t in T if t.get("status") == "closed" and t.get("exit_price")]
        tot = sum(t["pl_dollars"] for t in closed); tot_bot = sum((t.get("pl_dollars_bot") or 0) for t in closed)
        sp = tdir / f"{day}_summary.json"
        if sp.exists():
            try:
                sm = json.loads(sp.read_text())
                if isinstance(sm, dict):
                    sm.update(total_pl=round(tot, 2), total_pl_bot=round(tot_bot, 2), fills_reconciled=True)
                    sp.write_text(json.dumps(sm, indent=2))
            except Exception:
                pass
        annotate_log(bot_dir, day, closed)
        print(f"{day}: {len(closed)} trades, {changed} changed | bot's numbers ${tot_bot:+.2f} -> real fills ${tot:+.2f}")


def annotate_log(bot_dir, day, closed):
    """[2026-10-07] the user: the LOG should show the right values too -- append the real fill to each [EXIT] line
    (once) and add a corrected day total at the end."""
    logs = list((Path(bot_dir) / "logs").glob(f"*_{day}.log"))
    if not logs:
        return
    lp = logs[0]; lines = open(lp).read().split("\n")
    by_key = {}
    for t in closed:
        xt = datetime.fromisoformat(t["exit_time"]).astimezone(__import__("zoneinfo").ZoneInfo("America/New_York"))
        by_key.setdefault((t["symbol"], xt.strftime("%H:%M:%S")), t)
    out, n = [], 0
    for ln in lines:
        m = re.search(r"^\S+ (\d\d:\d\d:\d\d) .*\[EXIT\] (\S+) SELL", ln)
        if m and "[REAL FILL" not in ln:
            t = by_key.get((m.group(2), m.group(1)))
            if t and t.get("pl_dollars_bot") is not None:
                ln += (f"  [REAL FILL: bought {t['entry_price']}, sold {t['exit_price']} -> P/L {t['pl_pct']:+.2f}% "
                       f"(${t['pl_dollars']:+.2f})]"); n += 1
        out.append(ln)
    tot = sum(t["pl_dollars"] for t in closed)
    tag = f"[RECONCILED] {day}: {len(closed)} trades, real broker P/L ${tot:+.2f}"
    if not any(l.find("[RECONCILED]") >= 0 for l in out):
        out.append(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} [INFO] reconcile_fills: {tag}")
    tmp = lp.with_suffix(".tmp"); tmp.write_text("\n".join(out)); tmp.rename(lp)


if __name__ == "__main__":
    main(sys.argv[1], sys.argv[2:] or [datetime.now().strftime("%Y-%m-%d")])
