/* home_dayone_web.jsx - core app Day 0 Home for the web onboarding build.
   It follows the core app Home screen shape; the chart area is replaced by the
   Day 0 Get started checklist and its existing completion logic. */

if (typeof window !== "undefined") window.__YOSHI_WEB = true;

const WH_CONTENT_W = 430;
const WH_IOS = Object.freeze({
  largeTitle: 34,
  title1: 28,
  title2: 22,
  title3: 20,
  headline: 17,
  body: 17,
  callout: 16,
  subheadline: 15,
  footnote: 13,
  caption1: 12,
  caption2: 11,
});

const WHShell = ({ children }) => (
  <div style={{
    width: "min(100%, " + WH_CONTENT_W + "px)",
    margin: "0 auto",
  }}>
    {children}
  </div>
);

const WHHeader = ({ nav, flash }) => (
  <div style={{
    flex: "none",
    minHeight: 40,
    padding: "4px 16px 4px",
    display: "flex",
    alignItems: "center",
    gap: 12,
  }}>
    <button className="press" onClick={() => nav && nav.tab && nav.tab("profile")} aria-label="Menu" style={{
      width: 32,
      height: 32,
      marginLeft: -4,
      display: "grid",
      placeItems: "center",
      background: "none",
      border: "none",
      color: "var(--ink)",
      cursor: "pointer",
    }}>
      <Icon name="menu" size={22} stroke={1.6} color="var(--ink)" />
    </button>
    <div style={{
      fontFamily: "var(--f-display)",
      fontSize: 22,
      fontWeight: 500,
      letterSpacing: "-0.025em",
      color: "var(--ink)",
    }}>
      Home
    </div>
    <button className="press" onClick={() => flash && flash("Nothing needs you yet")} aria-label="Alerts" style={{
      marginLeft: "auto",
      width: 34,
      height: 34,
      display: "grid",
      placeItems: "center",
      background: "none",
      border: "none",
      color: "var(--ink)",
      cursor: "pointer",
    }}>
      <Icon name="bell" size={21} stroke={1.6} color="var(--ink)" />
    </button>
  </div>
);

const WHSectionKicker = ({ children, style }) => (
  <span style={{
    fontFamily: "var(--f-display)",
    fontSize: 14,
    fontWeight: 500,
    letterSpacing: "0.16em",
    textTransform: "uppercase",
    color: "var(--accent)",
    ...style,
  }}>
    {children}
  </span>
);

const WHSectionAction = ({ children, onClick }) => (
  <button className="press" onClick={onClick} style={{
    marginLeft: "auto",
    background: "none",
    border: "none",
    padding: "2px 0",
    display: "inline-flex",
    alignItems: "center",
    gap: 7,
    color: "var(--accent)",
    fontFamily: "var(--f-display)",
    fontSize: 17,
    fontWeight: 500,
    letterSpacing: "-0.01em",
    cursor: "pointer",
  }}>
    {children}
    <Icon name="back" size={18} stroke={1.8} color="var(--accent)" style={{ transform: "scaleX(-1)" }} />
  </button>
);

const WHGetStarted = ({ steps, onDismiss }) => (
  <div style={{
    position: "relative",
    marginTop: 20,
    background: "var(--bg-card)",
    border: "1px solid var(--accent)",
    borderRadius: 12,
    padding: "15px 18px 8px",
    overflow: "hidden",
  }}>
    <div style={{
      marginBottom: 4,
      paddingRight: 26,
      fontFamily: "var(--f-display)",
      fontSize: WH_IOS.subheadline,
      fontWeight: 500,
      letterSpacing: "-0.01em",
      color: "var(--accent)",
    }}>
      Get started
    </div>
    <button className="press" onClick={onDismiss} aria-label="Dismiss" style={{
      position: "absolute",
      top: 11,
      right: 11,
      width: 24,
      height: 24,
      display: "grid",
      placeItems: "center",
      background: "none",
      border: "none",
      color: "var(--ink-3)",
      cursor: "pointer",
      zIndex: 2,
    }}>
      <Icon name="close" size={14} stroke={2} color="var(--ink-3)" />
    </button>
    <div>
      {steps.map((s, i) => (
        <button key={s.key} className="press" onClick={s.onClick} disabled={s.done} style={{
          width: "100%",
          minHeight: 54,
          textAlign: "left",
          display: "grid",
          gridTemplateColumns: "30px 1fr auto",
          alignItems: "center",
          gap: 13,
          padding: "10px 0",
          background: "none",
          border: "none",
          borderBottom: i === steps.length - 1 ? "none" : "1px solid rgba(255,255,255,0.1)",
          color: "var(--ink)",
          cursor: s.done ? "default" : "pointer",
          opacity: s.done ? 0.5 : 1,
        }}>
          <span style={{
            width: 30,
            height: 30,
            borderRadius: 999,
            display: "grid",
            placeItems: "center",
            background: s.done ? "var(--accent)" : "transparent",
            color: s.done ? "var(--bg-card)" : "var(--ink)",
          }}>
            {s.done
              ? <Icon name="check" size={15} stroke={2.4} color="var(--bg-card)" />
              : <Icon name={s.icon} size={17} stroke={1.7} color="var(--ink)" />}
          </span>
          <span style={{ minWidth: 0 }}>
            <span style={{
              display: "block",
              fontFamily: "var(--f-display)",
              fontSize: WH_IOS.subheadline,
              fontWeight: 500,
              letterSpacing: "-0.01em",
              color: "var(--ink)",
              textDecoration: s.done ? "line-through" : "none",
            }}>
              {s.title}
            </span>
            <span style={{
              display: "block",
              marginTop: 2,
              fontFamily: "var(--f-display)",
              fontSize: WH_IOS.caption1,
              lineHeight: 1.28,
              color: "var(--ink-3)",
              textWrap: "pretty",
            }}>
              {s.sub}
            </span>
          </span>
          {!s.done && <Icon name="back" size={15} color="var(--ink-3)" style={{ transform: "scaleX(-1)" }} />}
        </button>
      ))}
    </div>
  </div>
);

const WHBalanceBand = ({ balance, showActivation, getStartedDismissed, steps, onDismiss }) => (
  <section style={{ padding: "22px 18px 4px" }}>
    <WHSectionKicker style={{ letterSpacing: "0.1em" }}>Yoshi balance</WHSectionKicker>
    <div style={{ marginTop: 8, lineHeight: 1 }}>
      <Money value={balance} size={56} weight={500} />
    </div>
    <div style={{ marginTop: 7, display: "flex", alignItems: "baseline", gap: 7 }}>
      <Delta abs={0} pct={0} size={WH_IOS.footnote} />
      <span style={{
        fontFamily: "var(--f-display)",
        fontSize: WH_IOS.footnote,
        color: "var(--ink-3)",
      }}>
        Today
      </span>
    </div>
    {showActivation && !getStartedDismissed && <WHGetStarted steps={steps} onDismiss={onDismiss} />}
  </section>
);

const WHNeedsYouCard = ({ flash }) => (
  <div style={{
    border: "1px solid var(--rule-2)",
    background: "var(--bg-card)",
    borderRadius: 12,
    padding: "15px 17px 16px",
    minHeight: 178,
    display: "flex",
    flexDirection: "column",
    boxSizing: "border-box",
  }}>
    <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
      <span style={{ width: 8, height: 8, borderRadius: 999, flex: "none", background: "var(--accent)" }} />
      <span style={{
        fontFamily: "var(--f-display)",
        fontSize: 10.5,
        fontWeight: 500,
        letterSpacing: "0.06em",
        color: "var(--ink-3)",
      }}>
        Yoshi agent
      </span>
    </div>
    <div style={{
      marginTop: 14,
      display: "grid",
      gridTemplateColumns: "minmax(0, 1fr) auto",
      alignItems: "start",
      gap: 14,
    }}>
      <div style={{
        fontFamily: "var(--f-display)",
        fontSize: 19,
        fontWeight: 500,
        letterSpacing: "-0.02em",
        lineHeight: 1.2,
        color: "var(--ink)",
        textWrap: "pretty",
      }}>
        Buy $500.00 of NVDA every market day
      </div>
      <div style={{
        fontFamily: "var(--f-display)",
        fontSize: 19,
        fontWeight: 500,
        letterSpacing: "-0.015em",
        color: "var(--ink)",
        whiteSpace: "nowrap",
      }}>
        $500.00
      </div>
    </div>
    <button className="press" onClick={() => flash && flash("Review opens in the full app")} style={{
      marginTop: "auto",
      width: "100%",
      minHeight: 54,
      borderRadius: 12,
      background: "var(--accent)",
      border: "1px solid var(--accent)",
      color: "var(--accent-ink)",
      display: "inline-flex",
      alignItems: "center",
      justifyContent: "center",
      gap: 12,
      fontFamily: "var(--f-display)",
      fontSize: 15,
      fontWeight: 500,
      letterSpacing: "0.01em",
      cursor: "pointer",
    }}>
      Review
    </button>
  </div>
);

const WHNeedsYouSection = ({ flash }) => (
  <section style={{ padding: "28px 18px 6px" }}>
    <div style={{ display: "flex", alignItems: "baseline", gap: 10 }}>
      <WHSectionKicker>Needs you</WHSectionKicker>
      <WHSectionAction onClick={() => flash && flash("More ideas open in the full app")}>+9 more</WHSectionAction>
    </div>
    <div style={{ marginTop: 17 }}>
      <WHNeedsYouCard flash={flash} />
    </div>
  </section>
);

const WHHoldingRow = ({ h, last, flash }) => {
  const up = h.dch >= 0;
  const priceMove = h.last * h.dch / 100;
  return (
    <button className="press" onClick={() => flash && flash(h.ticker + " opens in the full app")} style={{
      width: "100%",
      minHeight: 78,
      display: "grid",
      gridTemplateColumns: "62px minmax(0, 1fr) auto",
      alignItems: "center",
      gap: 14,
      padding: "13px 18px",
      background: "none",
      border: "none",
      borderBottom: last ? "none" : "1px solid var(--rule)",
      color: "var(--ink)",
      textAlign: "left",
      cursor: "pointer",
    }}>
      <span style={{
        width: 48,
        height: 48,
        borderRadius: 10,
        display: "grid",
        placeItems: "center",
        background: "color-mix(in srgb, var(--bg) 76%, var(--bg-card))",
        fontFamily: "var(--f-display)",
        fontSize: 15,
        fontWeight: 500,
        letterSpacing: "-0.03em",
        color: "var(--accent)",
      }}>
        {h.ticker}
      </span>
      <span style={{ minWidth: 0 }}>
        <span style={{
          display: "block",
          fontFamily: "var(--f-display)",
          fontSize: 20,
          fontWeight: 500,
          lineHeight: 1.16,
          letterSpacing: "-0.025em",
          color: "var(--ink)",
          whiteSpace: "nowrap",
          overflow: "hidden",
          textOverflow: "ellipsis",
        }}>
          {h.name}
        </span>
        <span style={{
          display: "block",
          marginTop: 5,
          fontFamily: "var(--f-mono)",
          fontSize: 15,
          letterSpacing: "-0.01em",
          color: "var(--ink-3)",
        }}>
          {usd(h.value, 2)}
        </span>
      </span>
      <span style={{ textAlign: "right", whiteSpace: "nowrap" }}>
        <span style={{ display: "block" }}>
          <Money value={h.last} size={20} weight={500} />
        </span>
        <span style={{
          display: "block",
          marginTop: 5,
          fontFamily: "var(--f-mono)",
          fontSize: 13,
          letterSpacing: "-0.01em",
          color: up ? "var(--accent-pos)" : "var(--signal-neg)",
        }}>
          {signed(priceMove)} ({Math.abs(h.dch).toFixed(2)}%)
        </span>
      </span>
    </button>
  );
};

const WHHoldingsSection = ({ flash }) => {
  const order = ["vti", "btc", "voo", "aapl", "nvda", "sol"];
  const holdings = order.map((id) => HOLDINGS.find((h) => h.id === id)).filter(Boolean);
  return (
    <section style={{ padding: "30px 18px 34px" }}>
      <div style={{ display: "flex", alignItems: "baseline", gap: 10 }}>
        <WHSectionKicker>Holdings</WHSectionKicker>
        <WHSectionAction onClick={() => flash && flash("Holdings open in the full app")}>View all</WHSectionAction>
      </div>
      <div style={{
        marginTop: 16,
        border: "1px solid var(--rule-2)",
        borderRadius: 12,
        overflow: "hidden",
        background: "var(--bg-card)",
      }}>
        {holdings.map((h, i) => (
          <WHHoldingRow key={h.id} h={h} flash={flash} last={i === holdings.length - 1} />
        ))}
      </div>
    </section>
  );
};

const WHChatBubble = ({ children }) => (
  <div style={{
    alignSelf: "flex-start",
    maxWidth: "86%",
    background: "var(--bg-card)",
    border: "1px solid var(--rule)",
    borderRadius: "14px 14px 14px 4px",
    padding: "10px 13px",
    fontFamily: "var(--f-display)",
    fontSize: 14,
    lineHeight: 1.45,
    color: "var(--ink)",
    textWrap: "pretty",
  }}>
    {children}
  </div>
);

const WHChatChip = ({ children, onClick }) => (
  <button className="press" onClick={onClick} style={{
    flex: "none",
    padding: "7px 12px",
    borderRadius: 999,
    background: "color-mix(in srgb, var(--ink) 8%, var(--bg-2))",
    border: "none",
    color: "var(--ink)",
    fontFamily: "var(--f-display)",
    fontSize: 12,
    fontWeight: 500,
    whiteSpace: "nowrap",
    cursor: "pointer",
  }}>
    {children}
  </button>
);

const WHChatPillar = ({ flash, onAddMoney, onConnect }) => (
  <aside style={{
    minWidth: 0,
    minHeight: 0,
    borderRight: "1px solid var(--rule)",
    display: "flex",
    flexDirection: "column",
    background: "var(--bg)",
  }}>
    <div style={{
      flex: "none",
      padding: "14px 16px 12px",
      borderBottom: "1px solid var(--rule)",
      display: "flex",
      alignItems: "center",
      gap: 10,
    }}>
      <Logo size={22} color="var(--ink)" />
      <div style={{
        fontFamily: "var(--f-display)",
        fontSize: 15,
        fontWeight: 500,
        letterSpacing: "-0.015em",
        color: "var(--ink)",
      }}>
        Yoshi
      </div>
    </div>

    <div className="scroll" style={{
      flex: 1,
      minHeight: 0,
      padding: "14px 16px 8px",
      display: "flex",
      flexDirection: "column",
      gap: 10,
    }}>
      <div style={{
        textAlign: "center",
        padding: "2px 0 8px",
        fontFamily: "var(--f-display)",
        fontSize: 10,
        color: "var(--ink-3)",
        letterSpacing: "0.06em",
        textTransform: "uppercase",
      }}>
        Today · 9:24 AM
      </div>
      <WHChatBubble>Welcome to Yoshi. Add money or connect your accounts and I can start finding ideas you can approve.</WHChatBubble>
      <WHChatBubble>You stay in control. I will draft the move, show the impact, and wait for your review before anything runs.</WHChatBubble>
    </div>

    <div style={{ flex: "none", borderTop: "1px solid var(--rule)" }}>
      <div className="hcar" style={{ display: "flex", gap: 7, overflowX: "auto", padding: "10px 16px 6px" }}>
        <WHChatChip onClick={onAddMoney}>Add money</WHChatChip>
        <WHChatChip onClick={onConnect}>Connect accounts</WHChatChip>
        <WHChatChip onClick={() => flash && flash("Yoshi chat opens in the full app")}>What can you do?</WHChatChip>
      </div>
      <div style={{ padding: "4px 14px 16px" }}>
        <button className="press" onClick={() => flash && flash("Yoshi chat opens in the full app")} style={{
          width: "100%",
          minHeight: 44,
          borderRadius: 12,
          border: "1px solid var(--rule-2)",
          background: "var(--bg-2)",
          color: "var(--ink-3)",
          display: "grid",
          gridTemplateColumns: "1fr auto",
          alignItems: "center",
          gap: 10,
          padding: "0 12px",
          textAlign: "left",
          fontFamily: "var(--f-display)",
          fontSize: 14,
          cursor: "pointer",
        }}>
          <span>Talk to Yoshi...</span>
          <Icon name="send" size={16} stroke={1.5} color="var(--ink-3)" />
        </button>
      </div>
    </div>
  </aside>
);

const WHActivityPillar = ({ externals, nav }) => (
  <aside style={{
    minWidth: 0,
    minHeight: 0,
    borderLeft: "1px solid var(--rule)",
    display: "flex",
    flexDirection: "column",
    background: "var(--bg)",
  }}>
    <div style={{
      flex: "none",
      padding: "16px 18px 10px",
      display: "flex",
      alignItems: "center",
      gap: 10,
    }}>
      <span style={{
        fontFamily: "var(--f-display)",
        fontSize: 17,
        fontWeight: 500,
        letterSpacing: "-0.01em",
        color: "var(--ink)",
      }}>
        Activity
      </span>
      <button className="press" onClick={() => nav && nav.tab && nav.tab("activity")} aria-label="Open activity" style={{
        marginLeft: "auto",
        background: "none",
        border: "none",
        padding: 2,
        display: "flex",
        color: "var(--ink-3)",
        cursor: "pointer",
      }}>
        <Icon name="back" size={15} color="var(--ink-3)" style={{ transform: "scaleX(-1)" }} />
      </button>
    </div>
    <div className="scroll" style={{ flex: 1, minHeight: 0, overflowY: "auto" }}>
      <StreamView externals={externals} compact onOpen={nav && nav.sheet ? (item) => nav.sheet({ type: "activity", item }) : undefined} />
      <div style={{ height: 18 }} />
    </div>
  </aside>
);

const WebHomeDayOne = ({ balance = 0, externals = [], linkedCount, nav, flash, onAddMoney, onConnect }) => {
  const [getStartedDismissed, setGetStartedDismissed] = React.useState(false);
  const [wide, setWide] = React.useState(() => (typeof window !== "undefined" ? window.innerWidth >= 900 : true));

  React.useEffect(() => {
    const sync = () => setWide(window.innerWidth >= 900);
    sync();
    window.addEventListener("resize", sync);
    return () => window.removeEventListener("resize", sync);
  }, []);

  const linked = externals.length || (linkedCount || 0);
  const hasFunding = balance > 0;
  const hasConnections = linked > 1;
  const showActivation = !(hasFunding && hasConnections);

  const steps = [
    !hasFunding && {
      key: "fund",
      icon: "plus",
      title: "Add money",
      sub: "So Yoshi can start growing your money",
      done: false,
      onClick: onAddMoney,
    },
    !hasConnections && {
      key: "connect",
      icon: "route",
      title: "Connect accounts",
      sub: "So Yoshi can spot ideas everywhere",
      done: false,
      onClick: onConnect,
    },
  ].filter(Boolean);

  const homeColumn = (
    <div style={{
      height: "100%",
      minHeight: 0,
      background: "var(--bg)",
      color: "var(--ink)",
      display: "flex",
      flexDirection: "column",
    }}>
      <WHShell>
        <WHHeader nav={nav} flash={flash} />
      </WHShell>
      <div className="scroll" style={{ flex: 1, minHeight: 0, overflowY: "auto" }}>
        <WHShell>
          <WHBalanceBand
            balance={balance}
            showActivation={showActivation}
            getStartedDismissed={getStartedDismissed}
            steps={steps}
            onDismiss={() => setGetStartedDismissed(true)}
          />
          <WHNeedsYouSection flash={flash} />
          <WHHoldingsSection flash={flash} />
          <div style={{ height: 18 }} />
        </WHShell>
      </div>
    </div>
  );

  if (!wide) return homeColumn;

  return (
    <div style={{
      height: "100%",
      minHeight: 0,
      background: "var(--bg)",
      color: "var(--ink)",
      display: "grid",
      gridTemplateColumns: "minmax(248px, 1fr) minmax(390px, 430px) minmax(258px, 0.9fr)",
      overflow: "hidden",
    }}>
      <WHChatPillar flash={flash} onAddMoney={onAddMoney} onConnect={onConnect} />
      <main style={{ minWidth: 0, minHeight: 0, overflow: "hidden" }}>
        {homeColumn}
      </main>
      <WHActivityPillar externals={externals} nav={nav} />
    </div>
  );
};

window.WebHomeDayOne = WebHomeDayOne;
