/* ============================================================
   SMB DESIGNS — Account, Wishlist
   ============================================================ */

/* ---------------- ACCOUNT ---------------- */
function AccountPage({ onNav }) {
  const [loggedIn, setLoggedIn] = useState(false);
  const [mode, setMode] = useState("login");
  const [tab, setTab] = useState("orders");

  if (loggedIn) {
    return (
      <div className="fade-page page-shell">
        <div className="wrap-wide">
          <div className="acct-head">
            <div><Eyebrow>Welcome back</Eyebrow><h1 className="serif page-h" style={{ margin: "8px 0 0" }}>Welcome, friend</h1></div>
            <button className="link-arrow" onClick={() => setLoggedIn(false)}>Sign out</button>
          </div>
          <div className="acct-grid">
            <aside className="acct-nav">
              {[["orders", "Orders"], ["wishlist", "Wishlist"], ["details", "Details"]].map(([k, l]) => (
                <button key={k} className={"acct-link" + (tab === k ? " on" : "")} onClick={() => k === "wishlist" ? onNav("wishlist", {}) : setTab(k)}>{l}<Icon name="chevron" size={15} /></button>
              ))}
            </aside>
            <div className="acct-body">
              {tab === "orders" && (
                <div>
                  <h2 className="serif acct-h">Your orders</h2>
                  {[["SMB-48201", "Delivered", "Classic Court Heel", "32,000"], ["SMB-47330", "In transit", "Ankle Boot", "34,000"]].map(([id, st, items, amt]) => (
                    <div className="acct-order" key={id}>
                      <div className="acct-order-media"><Ph label="" ratio="square" /></div>
                      <div className="acct-order-body">
                        <div className="spread"><span className="mono">#{id}</span><span className={"order-status " + (st === "Delivered" ? "ok" : "warn")}>{st}</span></div>
                        <div className="serif" style={{ fontSize: 19, margin: "4px 0" }}>{items}</div>
                        <span style={{ color: "var(--ink-soft)", fontSize: 13 }}>₦{amt}</span>
                      </div>
                      <button className="link-arrow" onClick={() => onNav("shop", {})}>Buy again</button>
                    </div>
                  ))}
                </div>
              )}
              {tab === "details" && (
                <div>
                  <h2 className="serif acct-h">Your details</h2>
                  <div className="form-grid" style={{ maxWidth: 520 }}>
                    <div className="field"><label>First name</label><input className="input" placeholder="First name" /></div>
                    <div className="field"><label>Last name</label><input className="input" placeholder="Last name" /></div>
                    <div className="field span2"><label>Email</label><input className="input" placeholder="you@email.com" /></div>
                    <div className="field span2"><label>Phone</label><input className="input" placeholder="+234 …" /></div>
                  </div>
                  <Btn arrow={false} style={{ marginTop: 18 }}>Save changes</Btn>
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="fade-page auth-page">
      <div className="auth-media">
        <Ph label="HEELS · CLASSIC COURT" ratio="tall" />
        <div className="auth-media-cap"><Logo size={30} /></div>
      </div>
      <div className="auth-form-side">
        <div className="auth-box">
          <div className="auth-tabs">
            <button className={mode === "login" ? "on" : ""} onClick={() => setMode("login")}>Sign in</button>
            <button className={mode === "register" ? "on" : ""} onClick={() => setMode("register")}>Create account</button>
          </div>
          <h1 className="serif" style={{ fontSize: 38, margin: "22px 0 6px" }}>{mode === "login" ? "Welcome back" : "Join " + ((window.BRAND && window.BRAND.name) || "smb designs")}</h1>
          <p style={{ color: "var(--ink-soft)", marginBottom: 26 }}>{mode === "login" ? "Sign in to track your orders." : "Create an account for faster checkout and order history."}</p>
          <form onSubmit={(e) => { e.preventDefault(); setLoggedIn(true); }} className="stack" style={{ gap: 16 }}>
            {mode === "register" && <div className="field"><label>Full name</label><input className="input" required placeholder="Your name" /></div>}
            <div className="field"><label>Email</label><input className="input" type="email" required placeholder="you@email.com" /></div>
            <div className="field"><label>Password</label><input className="input" type="password" required placeholder="••••••••" /></div>
            {mode === "login" && <button type="button" className="opt-link" style={{ alignSelf: "flex-end" }}>Forgot password?</button>}
            <Btn block arrow={false} type="submit">{mode === "login" ? "Sign in" : "Create account"}</Btn>
          </form>
          <button className="auth-guest" onClick={() => onNav("shop", {})}>Continue as guest →</button>
        </div>
      </div>
    </div>
  );
}

/* ---------------- WISHLIST ---------------- */
function WishlistPage({ onNav }) {
  const { wishlist, addToCart, toggleWish } = useContext(RBCtx);
  const items = RB.PRODUCTS.filter((p) => wishlist.includes(p.id));
  return (
    <div className="fade-page page-shell">
      <div className="wrap-wide">
        <h1 className="serif page-h">Your wishlist</h1>
        {items.length === 0 ? (
          <div className="center" style={{ padding: "60px 0" }}>
            <Icon name="heart" size={42} stroke={1} style={{ color: "var(--ink-faint)" }} />
            <p className="serif" style={{ fontSize: 28, margin: "16px 0 10px" }}>No saved pieces yet</p>
            <p style={{ color: "var(--ink-soft)", marginBottom: 24 }}>Tap the heart on any pair to save it here.</p>
            <Btn onClick={() => onNav("shop", {})}>Browse the shop</Btn>
          </div>
        ) : (
          <div className="product-grid" style={{ marginTop: 30 }}>
            {items.map((p, i) => (
              <Reveal key={p.id} delay={i % 4 + 1}>
                <ProductCard p={p} onNav={onNav} onAdd={addToCart} wished={true} onWish={toggleWish} />
              </Reveal>
            ))}
          </div>
        )}
      </div>
    </div>
  );
}

Object.assign(window, { AccountPage, WishlistPage });

/* ---------------- BESPOKE ---------------- */
function BespokeHeroImg() {
  const src = (window.RB_IMGS || {})["HEELS · STRAPPY SANDAL"] || "";
  return (
    <div className="about-hero-bg" aria-hidden="true">
      <img src={src} alt="" style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", objectPosition: "center 30%" }} />
    </div>
  );
}

function BespokePage({ onNav }) {
  const [sent, setSent] = useState(false);
  const [type, setType] = useState("Heels");
  const [mode, setMode] = useState("Virtual consultation");
  return (
    <div className="fade-page">
      <section className="bespoke-hero">
        <BespokeHeroImg />
        <div className="about-hero-ov" />
        <div className="wrap about-hero-content">
          <Eyebrow style={{ color: "var(--accent-bright)" }}>Bespoke Orders</Eyebrow>
          <h1 className="serif about-hero-h">Made to your measure.</h1>
          <p className="lede on-img" style={{ maxWidth: "44ch", marginTop: 16 }}>Request a custom size, colour or style from smb designs — talk it through virtually or in person in Abuja.</p>
        </div>
      </section>
      <section className="section-pad">
        <div className="wrap-wide bespoke-book-grid">
          <div className="bespoke-steps-col">
            <Eyebrow line>How it works</Eyebrow>
            {[["01", "Tell us what you want", "Style, size, colour — share as much detail as you can."], ["02", "We confirm with you", "Your request is reviewed and we follow up to confirm details and price."], ["03", "Made and delivered", "Once confirmed, your pair is made and shipped nationwide or worldwide."]].map(([n, t, d]) => (
              <div className="bk-step" key={n}><span className="mono">{n}</span><div><strong>{t}</strong><p>{d}</p></div></div>
            ))}
          </div>
          <div className="bespoke-form-card">
            {sent ? (
              <div className="contact-sent" style={{ padding: "30px 0" }}>
                <div className="confirm-badge sm"><Icon name="check" size={26} /></div>
                <h3 className="serif" style={{ fontSize: 30, margin: "14px 0 8px" }}>Request sent</h3>
                <p style={{ color: "var(--ink-soft)" }}>Thank you. We'll follow up about your {mode.toLowerCase()} {type.toLowerCase()} request soon.</p>
                <Btn style={{ marginTop: 20 }} onClick={() => onNav("home", {})}>Back to home</Btn>
              </div>
            ) : (
              <form onSubmit={(e) => { e.preventDefault(); setSent(true); }}>
                <h2 className="serif" style={{ fontSize: 30, marginBottom: 6 }}>Request a bespoke order</h2>
                <p style={{ color: "var(--ink-soft)", marginBottom: 22, fontSize: 14 }}>No obligation — we'll confirm details and price with you first.</p>
                <label className="bk-label">What are you commissioning?</label>
                <div className="bk-pills">{["Heels", "Sneakers", "Sandals", "Flats", "Boots", "Other"].map((t) => <button type="button" key={t} className={"chip" + (type === t ? " active" : "")} onClick={() => setType(t)}>{t}</button>)}</div>
                <label className="bk-label">How would you like to talk it through?</label>
                <div className="bk-pills">{["Virtual consultation", "In person (Abuja)"].map((m) => <button type="button" key={m} className={"chip" + (mode === m ? " active" : "")} onClick={() => setMode(m)}>{m}</button>)}</div>
                <div className="form-grid" style={{ marginTop: 6 }}>
                  <div className="field"><label>Name</label><input className="input" required placeholder="Your name" /></div>
                  <div className="field"><label>Email</label><input className="input" type="email" required placeholder="you@email.com" /></div>
                  <div className="field span2"><label>Describe what you have in mind</label><textarea className="input" rows="3" placeholder="Style, size, colour, occasion…"></textarea></div>
                </div>
                <Btn block arrow={false} type="submit" style={{ marginTop: 18 }}>Send request</Btn>
              </form>
            )}
          </div>
        </div>
      </section>
    </div>
  );
}
Object.assign(window, { BespokePage });
