// Home.jsx — Desk home, reframed as a value proposition.
// Same props and handlers (onScan, onBoard, onEndDay, onRetro) — the flow is
// unchanged. What changes: the screen now sells the promise (an AI agent that
// reads the market, drafts the trades, and hands you the call) and points you
// at your single best next move.

function DeskHome({ nickname, board, retroReady, onScan, onBoard, onEndDay, onRetro }) {
  const deployed = (board || []).length;
  const live = (board || []).filter((s) => s.column === "review").length;
  const paper = (board || []).filter((s) => s.column === "doing").length;
  const closed = (board || []).filter((s) => s.column === "done").length;
  const inFlight = (board || []).filter((s) => s.column !== "todo").length; // anything past backlog
  const hasBoard = deployed > 0;
  const canEnd = inFlight > 0;

  // The agent's single recommended next move — always an enabled card.
  const recommend = retroReady ? "retro" : !hasBoard ? "scan" : "board";

  const kicker = retroReady
    ? "A goal's in the books. Sharpen the system before the next run."
    : !hasBoard
      ? "The market's open. Let's find your first edge."
      : `${live} live · ${paper} on paper · ${inFlight} working — keep the pressure on.`;

  const Icon = {
    scan: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <circle cx="11" cy="11" r="7" /><line x1="21" y1="21" x2="16.5" y2="16.5" />
      </svg>
    ),
    board: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <rect x="3" y="4" width="5" height="16" rx="1" /><rect x="10" y="4" width="5" height="11" rx="1" /><rect x="17" y="4" width="4" height="7" rx="1" />
      </svg>
    ),
    review: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <circle cx="12" cy="12" r="9" /><polyline points="12 7 12 12 15 14" />
      </svg>
    ),
    retro: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <path d="M3 12a9 9 0 1 0 3-6.7L3 8" /><polyline points="3 3 3 8 8 8" />
      </svg>
    ),
  };

  // Card configs — built once, then ordered with the recommended move first.
  const configs = [];
  if (retroReady) configs.push({
    id: "retro", enabled: true, onClick: onRetro, icon: Icon.retro,
    title: "Run the retrospective",
    desc: "A trading goal closed out in full. Run the deep process review and bank the lesson.",
  });
  configs.push({
    id: "scan", enabled: true, onClick: onScan, icon: Icon.scan,
    title: "Scan the market",
    desc: "The agent surfaces the strongest regime and stages strategies tuned to it.",
  });
  configs.push({
    id: "board", enabled: hasBoard, onClick: onBoard, icon: Icon.board,
    title: "Work the board",
    desc: hasBoard ? "Advance each strategy — paper, live, then book the result." : "Stage strategies first to open the board.",
    meta: hasBoard ? `${deployed} staged · ${live} live · ${closed} closed` : null,
  });
  configs.push({
    id: "review", enabled: canEnd, onClick: onEndDay, icon: Icon.review,
    title: "Strategy review",
    desc: canEnd ? "AI-graded proposals on every strategy — even ones still paper-trading. Keep, dig in, or cut." : "Put a strategy in play first.",
  });

  const ordered = configs.slice().sort((a, b) => (b.id === recommend) - (a.id === recommend));

  const Card = ({ enabled, accent, pick, icon, title, desc, meta, onClick }) => (
    <button
      onClick={enabled ? onClick : undefined}
      disabled={!enabled}
      style={{
        position: "relative",
        textAlign: "left",
        width: "100%",
        background: accent ? "var(--color-brand-primary)" : "var(--color-surface-default)",
        color: accent ? "var(--color-text-on-brand)" : "var(--color-text-primary)",
        border: accent ? "none" : "1px solid var(--color-border-subtle)",
        borderRadius: "var(--radius-md)",
        padding: "14px 14px",
        cursor: enabled ? "pointer" : "not-allowed",
        opacity: enabled ? 1 : 0.45,
        display: "flex",
        alignItems: "center",
        gap: 13,
        boxShadow: accent ? "var(--shadow-3)" : "none",
        font: "inherit",
        transition: "transform var(--motion-base) var(--ease-standard)",
      }}
      onMouseDown={(e) => enabled && (e.currentTarget.style.transform = "scale(0.99)")}
      onMouseUp={(e) => (e.currentTarget.style.transform = "scale(1)")}
      onMouseLeave={(e) => (e.currentTarget.style.transform = "scale(1)")}
    >
      <span style={{
        width: 42, height: 42, borderRadius: "50%", flexShrink: 0,
        background: accent ? "rgba(255,255,255,0.18)" : "var(--color-surface-subtle)",
        color: accent ? "#fff" : "var(--color-brand-primary)",
        display: "inline-flex", alignItems: "center", justifyContent: "center",
      }}>{icon}</span>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <span style={{ fontFamily: "var(--font-family-sans)", fontWeight: 700, fontSize: 15.5, letterSpacing: "-0.005em" }}>{title}</span>
          {pick && (
            <span style={{
              fontSize: 8.5, fontWeight: 700, letterSpacing: "0.1em", textTransform: "uppercase",
              padding: "2px 7px", borderRadius: "var(--radius-pill)", flexShrink: 0,
              background: "rgba(255,255,255,0.22)", color: "#fff",
              display: "inline-flex", alignItems: "center", gap: 4,
            }}>
              <svg width="9" height="9" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M11.5 2.3a.5.5 0 0 1 .96 0l1.7 5.04a2 2 0 0 0 1.26 1.26l5.04 1.7a.5.5 0 0 1 0 .96l-5.04 1.7a2 2 0 0 0-1.26 1.26l-1.7 5.04a.5.5 0 0 1-.96 0l-1.7-5.04a2 2 0 0 0-1.26-1.26l-5.04-1.7a.5.5 0 0 1 0-.96l5.04-1.7a2 2 0 0 0 1.26-1.26z" /></svg>
              Agent's pick
            </span>
          )}
        </div>
        <div style={{ fontSize: 12.5, lineHeight: "17px", color: accent ? "rgba(255,255,255,0.88)" : "var(--color-text-secondary)", marginTop: 2 }}>{desc}</div>
        {meta && (
          <div style={{ fontSize: 11, fontWeight: 600, marginTop: 5, color: accent ? "rgba(255,255,255,0.92)" : "var(--color-text-muted)", fontVariantNumeric: "tabular-nums", letterSpacing: "0.02em" }}>{meta}</div>
        )}
      </div>
      <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0, opacity: 0.7 }}>
        <polyline points="9 18 15 12 9 6" />
      </svg>
    </button>
  );

  // The three-beat promise: the agent does the heavy lifting, you stay in command.
  const loop = [
    { label: "Reads the\nmarket", icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7" /><line x1="21" y1="21" x2="16.5" y2="16.5" /></svg> },
    { label: "Drafts the\ntrades", icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M11.5 2.3a.5.5 0 0 1 .96 0l1.7 5.04a2 2 0 0 0 1.26 1.26l5.04 1.7a.5.5 0 0 1 0 .96l-5.04 1.7a2 2 0 0 0-1.26 1.26l-1.7 5.04a.5.5 0 0 1-.96 0l-1.7-5.04a2 2 0 0 0-1.26-1.26l-5.04-1.7a.5.5 0 0 1 0-.96l5.04-1.7a2 2 0 0 0 1.26-1.26z" /></svg> },
    { label: "You make\nthe call", icon: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12" /></svg> },
  ];

  return (
    <div style={{ display: "flex", flexDirection: "column", height: "100%", padding: "0 20px 24px", gap: 14, boxSizing: "border-box", overflowY: "auto" }}>
      <style>{`
        @keyframes dhPulse { 0%{transform:scale(1);opacity:.5} 70%{transform:scale(2.4);opacity:0} 100%{opacity:0} }
        @keyframes dhRise { from{opacity:0;transform:translateY(7px)} to{opacity:1;transform:translateY(0)} }
        .dh-rise{animation:dhRise .5s var(--ease-standard) both}
      `}</style>

      {/* Agent identity bar */}
      <div className="dh-rise" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 10, paddingTop: 12 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10, minWidth: 0 }}>
          <span style={{
            width: 34, height: 34, borderRadius: "50%", flexShrink: 0,
            background: "var(--color-brand-primary)", color: "#fff",
            display: "inline-flex", alignItems: "center", justifyContent: "center",
            boxShadow: "var(--shadow-2)",
          }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M11.5 2.3a.5.5 0 0 1 .96 0l1.7 5.04a2 2 0 0 0 1.26 1.26l5.04 1.7a.5.5 0 0 1 0 .96l-5.04 1.7a2 2 0 0 0-1.26 1.26l-1.7 5.04a.5.5 0 0 1-.96 0l-1.7-5.04a2 2 0 0 0-1.26-1.26l-5.04-1.7a.5.5 0 0 1 0-.96l5.04-1.7a2 2 0 0 0 1.26-1.26z" /></svg>
          </span>
          <div style={{ minWidth: 0 }}>
            <div className="ds-overline" style={{ color: "var(--color-brand-primary)", marginBottom: 1 }}>Trade Wizard</div>
            <div style={{ fontSize: 12, fontWeight: 600, color: "var(--color-text-secondary)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
              Desk agent{nickname ? ` · ${nickname}` : ""}
            </div>
          </div>
        </div>
        <span style={{
          display: "inline-flex", alignItems: "center", gap: 6, flexShrink: 0,
          padding: "4px 10px 4px 9px", borderRadius: "var(--radius-pill)",
          background: "var(--color-surface-subtle)", border: "1px solid var(--color-border-subtle)",
          fontSize: 10.5, fontWeight: 700, letterSpacing: "0.06em", textTransform: "uppercase",
          color: "var(--color-text-secondary)",
        }}>
          <span style={{ position: "relative", width: 8, height: 8, flexShrink: 0 }}>
            <span style={{ position: "absolute", inset: 0, borderRadius: "50%", background: "var(--color-success)", animation: "dhPulse 1.8s var(--ease-standard) infinite" }} />
            <span style={{ position: "absolute", inset: 0, borderRadius: "50%", background: "var(--color-success)" }} />
          </span>
          Awake
        </span>
      </div>

      {/* Value proposition */}
      <div className="dh-rise" style={{ animationDelay: "60ms" }}>
        <h1 style={{ margin: 0, fontFamily: "var(--font-family-sans)", fontWeight: 600, fontSize: 29, lineHeight: "34px", letterSpacing: "-0.02em", color: "var(--color-text-primary)", textWrap: "balance" }}>
          Trade at the top of your game.
        </h1>
        <p style={{ margin: "9px 0 0", fontSize: 13.5, lineHeight: "19px", color: "var(--color-text-secondary)", textWrap: "pretty" }}>
          Your AI desk agent reads every market, drafts the strategies, and pressure-tests the risk.
          You make the calls that matter — <strong style={{ color: "var(--color-text-primary)", fontWeight: 600 }}>approve, refine, and keep only what earns its place.</strong>
        </p>
      </div>

      {/* The loop — agent does the work, you stay in command */}
      <div className="dh-rise" style={{
        animationDelay: "120ms",
        display: "flex", alignItems: "stretch",
        background: "var(--color-surface-subtle)", border: "1px solid var(--color-border-subtle)",
        borderRadius: "var(--radius-md)", padding: "12px 6px", marginTop: 2,
      }}>
        {loop.map((step, i) => (
          <React.Fragment key={i}>
            {i > 0 && (
              <div style={{ display: "flex", alignItems: "center", color: "var(--color-text-muted)", opacity: 0.6 }}>
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6" /></svg>
              </div>
            )}
            <div style={{ flex: 1, minWidth: 0, display: "flex", flexDirection: "column", alignItems: "center", gap: 6, textAlign: "center" }}>
              <span style={{
                width: 30, height: 30, borderRadius: "50%", flexShrink: 0,
                background: i === 2 ? "rgba(11,107,58,0.12)" : "var(--color-surface-promo)",
                color: i === 2 ? "var(--color-success)" : "var(--color-brand-primary)",
                display: "inline-flex", alignItems: "center", justifyContent: "center",
              }}>{step.icon}</span>
              <span style={{ fontSize: 10.5, fontWeight: 700, lineHeight: "13px", letterSpacing: "0.02em", color: "var(--color-text-secondary)", whiteSpace: "pre-line" }}>{step.label}</span>
            </div>
          </React.Fragment>
        ))}
      </div>

      {/* Board status strip */}
      {hasBoard && (
        <div style={{ display: "flex", gap: 8 }}>
          {[["Staged", deployed], ["Live", live], ["Closed", closed]].map(([l, v]) => (
            <div key={l} style={{ flex: 1, background: "var(--color-surface-subtle)", borderRadius: "var(--radius-md)", padding: "10px 12px" }}>
              <div style={{ fontFamily: "var(--font-family-sans)", fontWeight: 700, fontSize: 20, lineHeight: 1, color: "var(--color-text-primary)", fontVariantNumeric: "tabular-nums" }}>{v}</div>
              <div style={{ fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--color-text-muted)", fontWeight: 600, marginTop: 3 }}>{l}</div>
            </div>
          ))}
        </div>
      )}

      {/* Agent's nudge + next moves */}
      <div style={{ display: "flex", alignItems: "center", gap: 7, marginTop: 2, color: "var(--color-text-muted)" }}>
        <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0, color: "var(--color-brand-primary)" }}><circle cx="12" cy="12" r="9" /><path d="M12 16v-4" /><path d="M12 8h.01" /></svg>
        <span style={{ fontSize: 11.5, fontWeight: 600, color: "var(--color-text-secondary)", textWrap: "pretty" }}>{kicker}</span>
      </div>

      <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        {ordered.map((c) => (
          <Card
            key={c.id}
            enabled={c.enabled}
            accent={c.id === recommend}
            pick={c.id === recommend}
            icon={c.icon}
            title={c.title}
            desc={c.desc}
            meta={c.meta}
            onClick={c.onClick}
          />
        ))}
      </div>
    </div>
  );
}

Object.assign(window, { DeskHome });
