/* flow_unhappy.jsx — the two terminal states the onboarding can route into
   instead of "off to work": a manual-review HOLD (identity couldn't be
   auto-confirmed, or a screening answer needs a human) and a hard DECLINE.
   Same vocabulary as the happy flow — plainspoken, specific, no hype. */

/* A small ringed glyph used as each screen's emblem. */
const StatusEmblem = ({ icon, color, ring = true, pulse = false }) => (
  <span style={{ position: "relative", width: 86, height: 86, display: "grid", placeItems: "center", flex: "none" }}>
    {pulse && <span className="yo-ring2" style={{ position: "absolute", inset: 0, borderRadius: 999, border: `1.5px solid ${color}` }} />}
    <span style={{ width: 72, height: 72, borderRadius: 999, border: ring ? `1.5px solid ${color}` : "none", display: "grid", placeItems: "center", color, background: "var(--bg)" }}>
      {icon}
    </span>
  </span>
);

/* Vertical status ladder — submitted ● → in review (active) → opens ○ */
const ReviewLadder = ({ steps }) => (
  <div style={{ marginTop: 4 }}>
    {steps.map((st, i) => {
      const last = i === steps.length - 1;
      const done = st.state === "done";
      const active = st.state === "active";
      const c = done || active ? "var(--accent)" : "var(--ink-3)";
      return (
        <div key={st.t} style={{ display: "flex", gap: 14 }}>
          <div style={{ display: "flex", flexDirection: "column", alignItems: "center", flex: "none", width: 26 }}>
            <span style={{ width: 26, height: 26, flex: "none", borderRadius: 999, border: `1.5px solid ${c}`, background: done ? "var(--accent)" : "var(--bg)", display: "grid", placeItems: "center", position: "relative" }}>
              {done && <Icon name="check" size={13} stroke={2.4} color="var(--accent-ink)" />}
              {active && <LiveDot size={7} />}
            </span>
            {!last && <span style={{ flex: 1, width: 1.5, minHeight: 22, background: "repeating-linear-gradient(var(--rule-2) 0 4px, transparent 4px 8px)", margin: "3px 0" }} />}
          </div>
          <div style={{ paddingBottom: last ? 0 : 16, paddingTop: 2 }}>
            <div style={{ fontFamily: "var(--f-display)", fontSize: 15, fontWeight: 600, letterSpacing: "-0.015em", color: done || active ? "var(--ink)" : "var(--ink-2)" }}>{st.t}</div>
            <div style={{ fontFamily: "var(--f-display)", fontSize: 12, color: "var(--ink-3)", marginTop: 2, lineHeight: 1.4 }}>{st.s}</div>
          </div>
        </div>
      );
    })}
  </div>
);

/* =============================================================
   HOLD — manual review. Reached two ways:
     · identity couldn't be auto-confirmed (from Plaid liveness)
     · a screening answer needs a human (from Checks)
   The account isn't open yet; there's nothing for the user to do.
   ============================================================= */
const ReviewHold = ({ reason = "kyc", email, onReturn }) => {
  const displayEmail = email || "the email on file";
  const copy = "Your application needs a manual review, a routine step. Most are done within one business day.";

  return (
    <Screen style={{ position: "relative" }}>
      {onReturn && <ReturnText onTap={onReturn} />}
      <Body style={{ display: "flex", flexDirection: "column", padding: "6px 46px 8px" }}>
        <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", textAlign: "left", paddingTop: 18 }}>
          <StatusEmblem pulse ring={false} color="var(--accent)" icon={<Icon name="clock" size={32} color="var(--accent)" />} />
          <H size={30}>We're taking a<br /><Accent>closer look.</Accent></H>
          <Seam draw style={{ width: 72, margin: "16px 0 0" }} />
          <Sub style={{ maxWidth: "42ch", marginTop: 14 }}>{copy}</Sub>
        </div>

        {/* status ladder */}
        <div style={{ marginTop: 22 }}>
          <Eyebrow style={{ marginBottom: 12 }}>Where things stand</Eyebrow>
          <ReviewLadder steps={[
            { state: "done", t: "Application submitted", s: "Everything you entered is in." },
            { state: "active", t: "In manual review", s: "A specialist is checking it now." },
            { state: "pending", t: "Account opens", s: "We finish setting you up." },
          ]} />
        </div>

        {/* we'll email you */}
        <div style={{ display: "flex", alignItems: "center", gap: 12, padding: "13px 14px", background: "var(--bg-card)", border: "1px solid var(--accent)", borderRadius: 12, marginTop: 20 }}>
          <span style={{ width: 36, height: 36, flex: "none", borderRadius: 999, border: "1px solid var(--accent)", display: "grid", placeItems: "center", color: "var(--accent)" }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none"><rect x="2" y="4" width="20" height="16" rx="1" stroke="currentColor" strokeWidth="1.5" /><path d="M2 7l10 7 10-7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="square" /></svg>
          </span>
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontFamily: "var(--f-display)", fontSize: 13.5, fontWeight: 600 }}>We'll email you when it's ready</div>
            <div style={{ fontFamily: "var(--f-display)", fontSize: 12, color: "var(--ink-3)", marginTop: 1 }}>To {displayEmail}, nothing you need to do</div>
          </div>
        </div>
      </Body>
      <Dock>
        <Btn kind="ghost" full><Icon name="lifebuoy" size={17} color="var(--ink)" /> Contact support</Btn>
      </Dock>
    </Screen>
  );
};

/* The email that lands a day later — an iOS Mail-style notification that slides
   in, stays, and is tappable to bring the customer back into Yoshi.
   (Demo-compressed to a few seconds.) */
const IOS_UI_U = '-apple-system, "SF Pro Text", "SF Pro", system-ui, "Segoe UI", sans-serif';
const ReturnText = ({ onTap }) => {
  const [shown, setShown] = useState(false);
  useEffect(() => { const t = setTimeout(() => setShown(true), 2600); return () => clearTimeout(t); }, []);
  return (
    <div onClick={onTap} role="button" style={{
      position: "absolute", top: 12, left: 9, right: 9, zIndex: 60, cursor: "pointer",
      transform: shown ? "translateY(0)" : "translateY(-180%)", opacity: shown ? 1 : 0,
      transition: "transform 560ms cubic-bezier(0.22,1.08,0.36,1), opacity 320ms ease",
      background: "rgba(248,248,248,0.80)", WebkitBackdropFilter: "blur(24px) saturate(1.5)", backdropFilter: "blur(24px) saturate(1.5)",
      borderRadius: 24, boxShadow: "0 18px 40px -12px rgba(0,0,0,0.42), 0 1px 2px rgba(0,0,0,0.10)", padding: "12px 15px 13px" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 5 }}>
        <span style={{ width: 18, height: 18, borderRadius: 5, background: "#147EFB", display: "grid", placeItems: "center" }}>
          <svg width="12" height="12" viewBox="0 0 24 24" fill="none"><rect x="2" y="4" width="20" height="16" rx="1" fill="none" stroke="#fff" strokeWidth="2"/><path d="M2 7l10 7 10-7" stroke="#fff" strokeWidth="2" strokeLinecap="round"/></svg>
        </span>
        <span style={{ fontFamily: IOS_UI_U, fontSize: 11.5, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase", color: "#8a8a8e" }}>Mail</span>
        <span style={{ marginLeft: "auto", fontFamily: IOS_UI_U, fontSize: 12.5, color: "#8a8a8e" }}>now</span>
      </div>
      <div style={{ fontFamily: IOS_UI_U, fontSize: 15, fontWeight: 600, color: "#000", letterSpacing: "-0.01em", marginBottom: 1 }}>Your Yoshi account is ready</div>
      <div style={{ fontFamily: IOS_UI_U, fontSize: 14.5, color: "#1c1c1e", lineHeight: 1.32, textWrap: "pretty" }}>Your review is complete. Tap to finish setting up your account.</div>
    </div>
  );
};

/* =============================================================
   REVIEW CLEARED — the customer tapped the text and came back. The
   hold resolved in their favor; we welcome them and resume setup
   exactly where the happy path picks up (Income · Profile).
   ============================================================= */
const ReviewApproved = ({ onNext }) => {
  const web = usePlatform() === "web";
  return (
  <Screen>
    <Body style={{ display: "flex", flexDirection: "column", padding: "6px 46px 8px" }}>
      <div style={{ display: "flex", flexDirection: "column", alignItems: web ? "flex-start" : "center", textAlign: web ? "left" : "center", paddingTop: 30 }}>
        <StatusEmblem color="var(--accent)" icon={<Icon name="check" size={36} stroke={2.2} color="var(--accent)" />} />
        <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 22 }}>
          <LiveDot size={6} />
          <Eyebrow color="var(--accent)" style={{ letterSpacing: "0.16em" }}>Review complete</Eyebrow>
        </div>
        <H size={31}>You're <Accent>all set.</Accent></H>
        <Seam draw style={{ width: 72, margin: "16px 0 0" }} />
        <Sub style={{ maxWidth: web ? "40ch" : "30ch", marginTop: 14 }}>Thanks for waiting. Your review cleared and your account is open. Let's pick up where you left off.</Sub>
      </div>

      <div style={{ marginTop: 26 }}>
        <ReviewLadder steps={[
          { state: "done", t: "Application submitted", s: "Everything you entered is in." },
          { state: "done", t: "Review complete", s: "A specialist cleared it by hand." },
          { state: "done", t: "Account open", s: "Ready for you to finish setup." },
        ]} />
      </div>
    </Body>
    <Dock style={web ? { paddingLeft: 46 } : undefined}>
      <Btn full onClick={onNext}>Finish setting up <Icon name="arrow" size={15} color="var(--accent-ink)" /></Btn>
    </Dock>
  </Screen>
  );
};

/* =============================================================
   DECLINED — a hard stop. No account opened. Calm, specific,
   institutional on the rights paragraph (ECOA), plainspoken above.
   ============================================================= */
const Declined = () => {
  const web = usePlatform() === "web";
  return (
  <Screen>
    <Body style={{ display: "flex", flexDirection: "column", padding: "6px 46px 8px" }}>
      <div style={{ display: "flex", flexDirection: "column", alignItems: web ? "flex-start" : "center", textAlign: web ? "left" : "center", paddingTop: 18 }}>
        <StatusEmblem color="var(--ink-3)" icon={
          <svg width="34" height="34" viewBox="0 0 24 24" style={{ display: "block" }}>
            <circle cx="12" cy="12" r="8.5" fill="none" stroke="var(--ink-3)" strokeWidth="1.5" />
            <line x1="7.5" y1="12" x2="16.5" y2="12" stroke="var(--ink-3)" strokeWidth="1.5" strokeLinecap="square" />
          </svg>
        } />
        <Eyebrow style={{ letterSpacing: "0.16em", marginTop: 20 }}>Application · closed</Eyebrow>
        <H size={30}>We can't open an<br />account right now.</H>
        <div style={{ width: 72, height: 1, background: "var(--rule-2)", margin: "16px 0 0" }} />
        <Sub style={{ maxWidth: web ? "40ch" : "32ch", marginTop: 14 }}>We reviewed your application and can't open a Yoshi account at this time.</Sub>
      </div>

    </Body>
    <Dock>
      <Btn full><Icon name="doc" size={17} color="var(--accent-ink)" /> Request the reasons</Btn>
      <Btn kind="ghost" full><Icon name="lifebuoy" size={17} color="var(--ink)" /> Contact support</Btn>
    </Dock>
  </Screen>
  );
};

Object.assign(window, { ReviewHold, ReviewApproved, Declined, ReturnText });
