/* home_dayone.jsx - day-zero Home in the core mobile shell. */

const SectionKicker = ({ children, style }) => (
  <span style={{
    fontFamily: "var(--f-display)",
    fontSize: 12,
    fontWeight: 600,
    letterSpacing: "0.18em",
    textTransform: "uppercase",
    color: "var(--accent)",
    ...style,
  }}>{children}</span>
);

const GetStartedCard = ({ steps, onDismiss }) => (
  <div style={{
    position: "relative",
    background: "#15140f",
    border: "1px solid var(--accent)",
    borderRadius: 12,
    padding: "12px 13px 4px",
    overflow: "hidden",
  }}>
    <div style={{ display: "flex", alignItems: "center", paddingRight: 24 }}>
      <span style={{
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.subheadline,
        fontWeight: 400,
        letterSpacing: 0,
        color: "var(--accent)",
      }}>Get started</span>
    </div>

    <button className="press" onClick={onDismiss} aria-label="Dismiss" style={{
      position: "absolute",
      top: 8,
      right: 8,
      width: 24,
      height: 24,
      display: "grid",
      placeItems: "center",
      background: "none",
      border: "none",
      color: "#9b9685",
      cursor: "pointer",
      zIndex: 2,
    }}>
      <Icon name="close" size={14} stroke={2} color="#9b9685" />
    </button>

    <div style={{ marginTop: 6 }}>
      {steps.map((s, i) => (
        <button key={s.key} className="press" onClick={s.onClick} disabled={s.done} style={{
          width: "100%",
          minHeight: 59,
          textAlign: "left",
          display: "grid",
          gridTemplateColumns: "30px 1fr auto",
          alignItems: "center",
          gap: 11,
          padding: "9px 0",
          background: "none",
          border: "none",
          borderBottom: i === steps.length - 1 ? "none" : "1px solid rgba(255,255,255,0.1)",
          cursor: s.done ? "default" : "pointer",
          opacity: s.done ? 0.5 : 1,
          color: "#f0ece2",
        }}>
          <span style={{
            width: 30,
            height: 30,
            display: "grid",
            placeItems: "center",
            borderRadius: 999,
            background: s.done ? "var(--accent)" : "transparent",
            color: s.done ? "#15140f" : "#f0ece2",
          }}>
            {s.done
              ? <Icon name="check" size={15} stroke={2.4} color="#15140f" />
              : <Icon name={s.icon} size={18} stroke={1.7} color="#f0ece2" />}
          </span>
          <span style={{ minWidth: 0 }}>
            <span style={{
              display: "block",
              fontFamily: "var(--f-display)",
              fontSize: IOS_TYPE.body,
              fontWeight: 400,
              letterSpacing: 0,
              lineHeight: 1.18,
              color: "#f0ece2",
              textDecoration: s.done ? "line-through" : "none",
              whiteSpace: "nowrap",
              overflow: "hidden",
              textOverflow: "ellipsis",
            }}>{s.title}</span>
            <span style={{
              display: "block",
              fontFamily: "var(--f-display)",
              fontSize: IOS_TYPE.footnote,
              lineHeight: 1.25,
              letterSpacing: 0,
              color: "#9b9685",
              marginTop: 1,
              textWrap: "pretty",
            }}>{s.sub}</span>
          </span>
          {!s.done && <Icon name="back" size={16} color="#9b9685" style={{ transform: "scaleX(-1)" }} />}
        </button>
      ))}
    </div>
  </div>
);

const NeedsYouCard = ({ onReview }) => (
  <div style={{
    border: "1px solid var(--rule-2)",
    background: "var(--bg-card)",
    padding: "17px 15px 15px",
    borderRadius: 12,
    height: 171,
    boxSizing: "border-box",
    display: "flex",
    flexDirection: "column",
  }}>
    <div style={{ display: "flex", alignItems: "center", gap: 7 }}>
      <span style={{ width: 7, height: 7, borderRadius: 999, flex: "none", background: "var(--accent)" }} />
      <span style={{
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.subheadline,
        fontWeight: 500,
        letterSpacing: "-0.01em",
        color: "var(--ink-3)",
      }}>Yoshi agent</span>
    </div>
    <div style={{
      display: "grid",
      gridTemplateColumns: "1fr auto",
      gap: 14,
      alignItems: "start",
      marginTop: 15,
    }}>
      <div style={{
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.title3,
        fontWeight: 400,
        letterSpacing: "-0.025em",
        lineHeight: 1.26,
        color: "var(--ink)",
        textWrap: "pretty",
      }}>Buy $500.00 of NVDA every market day</div>
      <div style={{
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.title3,
        fontWeight: 400,
        letterSpacing: "-0.02em",
        color: "var(--ink)",
        whiteSpace: "nowrap",
        paddingTop: 1,
      }}>$500.00</div>
    </div>
    <Btn full onClick={onReview} style={{
      marginTop: "auto",
      minHeight: 48,
      borderRadius: 12,
      fontSize: IOS_TYPE.title3,
      fontWeight: 400,
      letterSpacing: "-0.01em",
    }}>Review <Icon name="arrow" size={20} color="var(--accent-ink)" /></Btn>
  </div>
);

const NeedsYouSection = ({ flash }) => (
  <section style={{ padding: "21px 18px 2px" }}>
    <div style={{ display: "flex", alignItems: "center", gap: 7 }}>
      <SectionKicker>Needs you</SectionKicker>
      <button className="press" onClick={() => flash && flash("More ideas open in the full app")} style={{
        marginLeft: "auto",
        background: "none",
        border: "none",
        padding: "2px 0",
        cursor: "pointer",
        display: "inline-flex",
        alignItems: "center",
        gap: 7,
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.body,
        fontWeight: 500,
        letterSpacing: "-0.01em",
        color: "var(--accent)",
      }}>
        +9 more <Icon name="back" size={18} stroke={1.8} color="var(--accent)" style={{ transform: "scaleX(-1)" }} />
      </button>
    </div>
    <div style={{ marginTop: 20 }}>
      <NeedsYouCard onReview={() => flash && flash("Review opens in the full app")} />
    </div>
  </section>
);

const HoldingRow = ({ 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: 68,
      display: "grid",
      gridTemplateColumns: "48px 1fr auto",
      alignItems: "center",
      gap: 12,
      padding: "12px 14px",
      background: "none",
      border: "none",
      borderBottom: last ? "none" : "1px solid var(--rule)",
      color: "var(--ink)",
      textAlign: "left",
      cursor: "pointer",
    }}>
      <span style={{
        width: 42,
        height: 42,
        borderRadius: 9,
        display: "grid",
        placeItems: "center",
        background: "color-mix(in srgb, var(--bg) 76%, var(--bg-card))",
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.callout,
        fontWeight: 600,
        letterSpacing: "-0.03em",
        color: "var(--accent)",
      }}>{h.ticker}</span>
      <span style={{ minWidth: 0 }}>
        <span style={{
          display: "block",
          fontFamily: "var(--f-display)",
          fontSize: IOS_TYPE.title3,
          fontWeight: 500,
          lineHeight: 1.18,
          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: IOS_TYPE.subheadline,
          color: "var(--ink-3)",
          letterSpacing: "-0.01em",
        }}>{usd(h.value, 2)}</span>
      </span>
      <span style={{ textAlign: "right", whiteSpace: "nowrap" }}>
        <span style={{ display: "block" }}>
          <Money value={h.last} size={IOS_TYPE.title3} weight={500} />
        </span>
        <span style={{
          display: "block",
          marginTop: 5,
          fontFamily: "var(--f-mono)",
          fontSize: IOS_TYPE.subheadline,
          color: up ? "var(--accent-pos)" : "var(--signal-neg)",
          letterSpacing: "-0.01em",
        }}>{signed(priceMove)} ({Math.abs(h.dch).toFixed(2)}%)</span>
      </span>
    </button>
  );
};

const HoldingsList = ({ flash }) => {
  const order = ["vti", "btc", "voo", "aapl", "nvda", "sol"];
  const source = (typeof window !== "undefined" && window.HOLDINGS) || (typeof HOLDINGS !== "undefined" ? HOLDINGS : []);
  const holdings = order
    .map((id) => source.find((h) => h.id === id))
    .filter(Boolean);
  return (
  <section style={{ padding: "29px 18px 26px" }}>
    <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
      <SectionKicker>Holdings</SectionKicker>
      <button className="press" onClick={() => flash && flash("Holdings open in the full app")} style={{
        marginLeft: "auto",
        background: "none",
        border: "none",
        padding: "2px 0",
        cursor: "pointer",
        display: "inline-flex",
        alignItems: "center",
        gap: 7,
        fontFamily: "var(--f-display)",
        fontSize: IOS_TYPE.body,
        fontWeight: 500,
        letterSpacing: "-0.01em",
        color: "var(--accent)",
      }}>
        View all <Icon name="back" size={18} stroke={1.8} color="var(--accent)" style={{ transform: "scaleX(-1)" }} />
      </button>
    </div>
    <div style={{
      marginTop: 16,
      border: "1px solid var(--rule-2)",
      borderRadius: 12,
      overflow: "hidden",
      background: "var(--bg-card)",
    }}>
      {holdings.map((h, i) => <HoldingRow key={h.id} h={h} flash={flash} last={i === holdings.length - 1} />)}
    </div>
  </section>
  );
};

const HomeDayOne = ({ balance = 0, goal, externals = [], linkedCount, nav, flash, onAddMoney, onConnect, onPickGoal }) => {
  const [getStartedDismissed, setGetStartedDismissed] = React.useState(false);

  const accts = externals.length ? externals : [];
  const linked = accts.length || (linkedCount || 0);

  const hasFunding = balance > 0;
  const hasConnections = linked > 1;
  const showActivation = !(hasFunding && hasConnections);

  const connectTitle = hasConnections
    ? `${linked} account${linked === 1 ? "" : "s"} connected`
    : "Connect accounts";
  const connectSub = hasConnections
    ? "Add more anytime in Accounts"
    : "So Yoshi can spot ideas everywhere";

  const steps = [
    !hasFunding && {
      key: "fund",
      icon: "plus",
      title: hasFunding ? `Added ${usd(balance, 0)}` : "Add money",
      sub: hasFunding ? "Ready to put to work" : "So Yoshi can start growing your money",
      done: hasFunding,
      onClick: onAddMoney,
    },
    !hasConnections && {
      key: "connect",
      icon: "route",
      title: connectTitle,
      sub: connectSub,
      done: hasConnections,
      onClick: onConnect,
    },
  ].filter(Boolean);

  return (
    <div style={{ flex: 1, display: "flex", flexDirection: "column", minHeight: 0 }}>
      <TabTopBar nav={nav} title="Home" />

      <div className="scroll">
        <section style={{ padding: "0 18px 4px" }}>
          <button className="press" onClick={() => flash && flash("Accounts open after your first trade")} style={{
            display: "block",
            width: "100%",
            textAlign: "left",
            background: "none",
            border: "none",
            padding: 0,
            cursor: "pointer",
          }}>
            <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}>
              <SectionKicker>Yoshi balance</SectionKicker>
              <Icon name="back" size={14} color="var(--ink-3)" style={{ transform: "scaleX(-1)" }} />
            </span>
            <div style={{ marginTop: 7, lineHeight: 1 }}>
              <Money value={balance} size={56} weight={500} />
            </div>
          </button>

          {showActivation && !getStartedDismissed && (
            <div style={{ marginTop: 21 }}>
              <GetStartedCard steps={steps} onDismiss={() => setGetStartedDismissed(true)} />
            </div>
          )}
        </section>

        <NeedsYouSection flash={flash} />
        <HoldingsList flash={flash} />

        <div style={{ height: 18 }} />
      </div>
    </div>
  );
};

window.HomeDayOne = HomeDayOne;
