/* web_sheets.jsx — bottom-sheet popups for the web + full-screen onboarding boards.
   Ports the mobile build's slide-up sheet pattern (grabber handle, accent top-rule,
   scrim) so the web dashboard has real popups instead of toast-only stubs.

   Anchors to the nearest positioned ancestor — both boards render these inside the
   app surface's relative container, so the sheet sits over the dashboard, not the page. */

/* ---------- shared shell: scrim + slide-up panel with grabber ------------- */
const WebSheet = ({ onClose, children, maxHeight = "88%" }) => (
  <>
    <div onClick={onClose} className="yo-focus-backdrop" style={{ position: "absolute", inset: 0, zIndex: 300, background: "rgba(0,0,0,0.5)", animation: "scrim-in 200ms ease both" }} />
    <div role="dialog" aria-modal="true" style={{ position: "absolute", left: 0, right: 0, bottom: 0, zIndex: 301, background: "var(--bg)", borderTop: "1px solid var(--accent)", maxHeight, display: "flex", flexDirection: "column", boxShadow: "0 -18px 50px -24px rgba(0,0,0,0.6)", animation: "sheet-in 320ms cubic-bezier(0.16,1,0.30,1) both" }}>
      <div style={{ display: "flex", justifyContent: "center", paddingTop: 8, flex: "none" }}>
        <span style={{ width: 36, height: 4, background: "var(--rule-2)", borderRadius: 999 }} />
      </div>
      {children}
    </div>
  </>
);

const SheetHead = ({ eyebrow, title, onClose }) => (
  <div style={{ flex: "none", padding: "10px 20px 12px", display: "flex", alignItems: "flex-start", gap: 10 }}>
    <div style={{ minWidth: 0, flex: 1 }}>
      {eyebrow && <Eyebrow style={{ marginBottom: 4 }}>{eyebrow}</Eyebrow>}
      <div style={{ fontFamily: "var(--f-display)", fontSize: 20, fontWeight: 700, letterSpacing: "-0.02em", color: "var(--ink)", textWrap: "pretty" }}>{title}</div>
    </div>
    <button className="press" onClick={onClose} aria-label="Close" style={{ flex: "none", marginTop: 2, background: "none", border: "none", padding: 4, margin: "-2px -4px 0 0", color: "var(--ink-3)", cursor: "pointer", display: "flex" }}>
      <Icon name="close" size={20} color="var(--ink-3)" />
    </button>
  </div>
);

/* ---------- activity / transaction detail · receipt-style sheet ----------- */
const ACT_CATEGORY = { spending: "Spending", income: "Income", transfers: "Transfer", automations: "Automation", investments: "Investment" };

const ActivitySheet = ({ item, onClose }) => {
  const upcoming = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)/.test(item.when || "");
  const hasAmount = item.amount != null;
  const positive = (item.amount || 0) > 0 || (item.amountLabel && item.amountLabel.startsWith("+"));
  const cat = ACT_CATEGORY[item.cat] || "Activity";
  const detailRows = [
    ["Status", upcoming ? "Scheduled" : "Completed"],
    ["When", item.when],
    ["Category", cat],
    ["Detail", item.sub],
  ];
  return (
    <WebSheet onClose={onClose}>
      <SheetHead eyebrow={cat} title={item.title} onClose={onClose} />
      <div className="scroll" style={{ padding: "0 20px 4px" }}>
        {/* amount block */}
        <div style={{ padding: "6px 0 16px", borderBottom: "1px solid var(--rule)" }}>
          {hasAmount ? (
            <Money value={item.amount} size={34} weight={500} sign color={positive ? "var(--accent-pos)" : "var(--ink)"} />
          ) : (
            <span style={{ fontFamily: "var(--f-display)", fontSize: 26, fontWeight: 700, letterSpacing: "-0.02em", color: "var(--ink)" }}>{item.amountLabel || "—"}</span>
          )}
        </div>
        {/* receipt rows */}
        <div style={{ padding: "4px 0 8px" }}>
          {detailRows.map(([k, v], i) => (
            <div key={k} style={{ display: "grid", gridTemplateColumns: "92px 1fr", gap: 12, padding: "11px 0", borderBottom: i === detailRows.length - 1 ? "none" : "1px dashed var(--rule)" }}>
              <span style={{ fontFamily: "var(--f-display)", fontSize: 10.5, fontWeight: 700, letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--ink-3)" }}>{k}</span>
              <span style={{ fontFamily: "var(--f-display)", fontSize: 13.5, color: "var(--ink)", textWrap: "pretty" }}>{v}</span>
            </div>
          ))}
        </div>
        <div style={{ display: "flex", gap: 8, alignItems: "flex-start", padding: "10px 0 2px", color: "var(--ink-3)" }}>
          <span style={{ flex: "none", marginTop: 1 }}><Icon name="shield" size={14} color="var(--ink-3)" /></span>
          <span style={{ fontFamily: "var(--f-display)", fontSize: 11.5, lineHeight: 1.5, color: "var(--ink-3)", textWrap: "pretty" }}>
            {upcoming ? "Scheduled moves still need your approval before they run." : "Full history and receipts live in the Accounts tab."}
          </span>
        </div>
      </div>
      <div style={{ flex: "none", padding: "12px 20px 22px" }}>
        <Btn full onClick={onClose}>Done</Btn>
      </div>
    </WebSheet>
  );
};

/* ---------- add money · funding bottom sheet ------------------------------ */
const AddMoneySheet = ({ onConnect, onClose }) => {
  const Opt = ({ icon, title, sub, onClick, primary }) => (
    <button className="press" onClick={onClick} style={{
      width: "100%", textAlign: "left", display: "grid", gridTemplateColumns: "auto 1fr auto", gap: 13, alignItems: "center",
      padding: "14px 15px", background: "var(--bg-card)", cursor: "pointer", borderRadius: 12,
      border: primary ? "1px solid var(--accent)" : "1px solid var(--rule-2)",
      boxShadow: primary ? "0 0 0 4px color-mix(in srgb, var(--accent) 13%, transparent)" : "none" }}>
      <span style={{ width: 36, height: 36, flex: "none", borderRadius: 9, display: "grid", placeItems: "center", border: "1px solid var(--rule-2)", color: "var(--ink)" }}>
        <Icon name={icon} size={18} stroke={1.6} color="var(--ink)" />
      </span>
      <span style={{ minWidth: 0 }}>
        <span style={{ display: "block", fontFamily: "var(--f-display)", fontSize: 14.5, fontWeight: 700, letterSpacing: "-0.01em", color: "var(--ink)" }}>{title}</span>
        <span style={{ display: "block", fontFamily: "var(--f-display)", fontSize: 11.5, color: "var(--ink-3)", marginTop: 2, textWrap: "pretty" }}>{sub}</span>
      </span>
      <Icon name="back" size={15} color="var(--ink-3)" style={{ transform: "scaleX(-1)" }} />
    </button>
  );
  return (
    <WebSheet onClose={onClose} maxHeight="80%">
      <SheetHead eyebrow="Funding" title="Add money to Yoshi" onClose={onClose} />
      <div className="scroll" style={{ padding: "0 20px 4px", display: "grid", gap: 10 }}>
        <Opt primary icon="route" title="Connect a bank" sub="Link with Plaid and transfer in — instant, no fees." onClick={onConnect} />
        <Opt icon="bank" title="Wire or ACH" sub="Send from any bank using your Yoshi account and routing numbers." onClick={onClose} />
      </div>
      <div style={{ flex: "none", padding: "14px 20px 22px" }}>
        <div style={{ display: "flex", gap: 8, alignItems: "flex-start", color: "var(--ink-3)" }}>
          <span style={{ flex: "none", marginTop: 1 }}><Icon name="shield" size={14} color="var(--ink-3)" /></span>
          <span style={{ fontFamily: "var(--f-display)", fontSize: 11.5, lineHeight: 1.5, color: "var(--ink-3)", textWrap: "pretty" }}>
            Funds at Yoshi are FDIC-insured up to $250,000 through our partner banks. Every move still needs your approval.
          </span>
        </div>
      </div>
    </WebSheet>
  );
};

Object.assign(window, { WebSheet, SheetHead, ActivitySheet, AddMoneySheet });
