/* ============================================================
   KHAL DESIGN — Account, Bespoke fitting, Wishlist
   ============================================================ */

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

  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">
              {[["enquiries", "Enquiries"], ["wishlist", "Wishlist"], ["fittings", "Fittings"], ["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 === "enquiries" &&
              <div>
                  <h2 className="serif acct-h">Your enquiries</h2>
                  {[["KD-48201", "Awaiting reply", "Suede Tassel Loafer"], ["KD-47330", "Confirmed", "Two-Tone Suede Boot"]].map(([id, st, items]) =>
                <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 === "Confirmed" ? "ok" : "warn")}>{st}</span></div>
                        <div className="serif" style={{ fontSize: 19, margin: "4px 0" }}>{items}</div>
                      </div>
                      <button className="link-arrow" onClick={() => onNav("shop", {})}>View style</button>
                    </div>
                )}
                </div>
              }
              {tab === "fittings" &&
              <div>
                  <h2 className="serif acct-h">Your fittings</h2>
                  <div className="acct-fitting">
                    <div><Eyebrow>Upcoming</Eyebrow><h3 className="serif" style={{ fontSize: 24, margin: "6px 0" }}>Virtual fitting</h3><p style={{ color: "var(--ink-soft)" }}>Confirmed with the workshop directly.</p></div>
                    <Btn variant="ghost" arrow={false} onClick={() => onNav("booking", {})}>Reschedule</Btn>
                  </div>
                  <button className="acct-add" onClick={() => onNav("booking", {})}><Icon name="plus" size={18} /> Book a new fitting</button>
                </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" /></div>
                    <div className="field"><label>Last name</label><input className="input" /></div>
                    <div className="field span2"><label>Email</label><input className="input" /></div>
                    <div className="field span2"><label>Phone</label><input className="input" /></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="ATELIER · MEASURING" ratio="tall" />
        <div className="auth-media-cap"><Logo size={26} /><p className="serif" style={{ fontSize: 28, marginTop: 16, color: "#f6f1e7" }}>Handcrafted, underfoot.</p></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) || "Khal Design")}</h1>
          <p style={{ color: "var(--ink-soft)", marginBottom: 26 }}>{mode === "login" ? "Sign in to track enquiries & fittings." : "Create an account for faster enquiries and early access."}</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>
          <div className="auth-or"><span>or</span></div>
          <button className="auth-guest" onClick={() => onNav("shop", {})}>Continue as guest →</button>
        </div>
      </div>
    </div>);

}

/* ---------------- BESPOKE FITTING REQUEST ---------------- */
function BespokeHeroPhoto() {
  const src = (window.RB_IMGS || {})["ATELIER · MEASURING"] || "";
  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("Loafers");
  const [mode, setMode] = useState("Virtual");
  return (
    <div className="fade-page">
      <section className="bespoke-hero">
        <BespokeHeroPhoto />
        <div className="about-hero-ov" />
        <div className="wrap about-hero-content">
          <Eyebrow style={{ color: "var(--accent-bright)" }}>Bespoke Fittings</Eyebrow>
          <h1 className="serif about-hero-h">Request a fitting.</h1>
          <p className="lede on-img" style={{ maxWidth: "44ch", marginTop: 16 }}>Commission a one-of-one pair, measured to you — in the Lagos workshop, or virtually from anywhere.</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", "Request your fitting", "Pick a date and whether you'd like to meet in person or virtually."], ["02", "Design together", "Choose your leather, last and finishings with the workshop."], ["03", "We confirm & price it", "The workshop costs it precisely before anything is cut."], ["04", "Delivered when ready", "Your finished pair ships once complete."]].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" }}>Fitting requested</h3>
                <p style={{ color: "var(--ink-soft)" }}>Thank you. The workshop will confirm your {mode.toLowerCase()} fitting directly.</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 fitting</h2>
                <p style={{ color: "var(--ink-soft)", marginBottom: 22, fontSize: 14 }}>Complimentary &amp; no obligation.</p>
                <label className="bk-label">What are you commissioning?</label>
                <div className="bk-pills">{["Loafers", "Oxfords", "Boots", "Slides", "Other"].map((t) => <button type="button" key={t} className={"chip" + (type === t ? " active" : "")} onClick={() => setType(t)}>{t}</button>)}</div>
                <label className="bk-label">Fitting type</label>
                <div className="bk-pills">{["Virtual", (window.BRAND?.addresses.hq.label) || "Lagos workshop"].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"><label>Preferred date</label><input className="input" type="date" /></div>
                  <div className="field span2"><label>Anything we should know?</label><textarea className="input" rows="3" placeholder="Sizing notes, leather preference…"></textarea></div>
                </div>
                <Btn block arrow={false} type="submit" style={{ marginTop: 18 }}>Request fitting</Btn>
              </form>
            }
          </div>
        </div>
      </section>
    </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 piece to save it here.</p>
            <Btn onClick={() => onNav("shop", {})}>Browse the collection</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, BespokePage, WishlistPage });
