/* simple/arriving.jsx — the arriving-funds experience.
   An order or transfer that dips into money still in flight becomes a ONE-TIME
   AUTOMATION: the WHOLE thing is scheduled and runs when the funds settle —
   there is no "some now, some later" split. It's placed through the same
   `placeOrder` pipeline into the one In-motion list (tagged `fundedBy` the
   transfer), so it gets the standard row, the standard pending detail
   (TxnDetailSheet), and the standard cancel. Scheduling happens straight from
   the trade/move entry: "Schedule with passkey" places it and lands you in
   Activity · In motion — no separate review step, no funding split. */

/* place a queued order as a one-time automation into the ONE In-motion list —
   same bridge (`placeOrder`) the after-hours trade uses. Tagged `fundedBy` the
   transfer + `funds` (the in-transit slice it consumes) so the ceiling can read
   what's committed. */
const placeQueued = (item) => { if (window.yoshiTrack && window.yoshiTrack.placed) window.yoshiTrack.placed(item); };

/* schedule a BUY riding on money still in flight — the whole order runs when
   the transfer settles (then the next market open if the market is closed). */
const scheduleQueuedTrade = ({ sel, amtNum, avail, t }) => {
  const part = +(amtNum - avail).toFixed(2); // the slice funded by the pending transfer
  placeQueued({
    icon: "trade", title: `Buy ${usd(amtNum)} of ${sel.ticker}`,
    category: "Investment", kind: "once", state: "queued", agent: false,
    net: -amtNum, scope: "yoshi", acct: "One-time order · Yoshi",
    fundedBy: t.id, funds: part, trigger: `${t.fromShort || "Chase"} transfer settles${!MARKET_OPEN ? " · then market open" : ""}`,
    cancelNote: `Prepped and waiting — Yoshi buys ${sel.ticker} the moment your ${usd(t.amount)} ${t.fromShort || "Chase"} transfer settles${!MARKET_OPEN ? ", at the next market open" : ""}. Cancel any time before then.`,
    steps: [["Prepared", "Just now"], [`Waiting on your ${t.fromShort || "Chase"} transfer`, "You are here"], [`Buys at ${!MARKET_OPEN ? "the next market open" : "the live price"}`, "after the funds settle"]], stepAt: 1,
    settle: { icon: "trade", title: `Bought ${sel.ticker}`, detail: "Notional order · funds settled", category: "Investment", accountScope: "yoshi", net: -amtNum },
  });
};

/* schedule a MOVE riding on money still in flight — transfers only wait on the
   funds (no market-open leg). */
const scheduleQueuedTransfer = ({ fromName, toName, amtNum, avail, t }) => {
  const part = +(amtNum - avail).toFixed(2);
  placeQueued({
    icon: "swap", title: `Move ${usd(amtNum)} to ${toName}`,
    category: "Transfer", kind: "once", state: "queued", agent: false,
    trigger: `${t.fromShort || "Chase"} transfer settles`,
    net: -amtNum, scope: "yoshi", acct: `${fromName} → ${toName}`, rail: "Standard ACH",
    fundedBy: t.id, funds: part,
    cancelNote: `Prepped and waiting — Yoshi sends this the moment your ${usd(t.amount)} ${t.fromShort || "Chase"} transfer settles. Cancel any time before then.`,
    steps: [["Prepared", "Just now"], [`Waiting on your ${t.fromShort || "Chase"} transfer`, "You are here"], ["Sends · Standard ACH", "after the funds settle"]], stepAt: 1,
    settle: { icon: "swap", title: toName, detail: "Standard ACH · funds settled", category: "Transfer", accountScope: "yoshi", net: -amtNum },
  });
};

Object.assign(window, { scheduleQueuedTrade, scheduleQueuedTransfer });
