/* global React */
const { useState, useEffect, useRef } = React;

/* ============================================================
   Atoms
   ============================================================ */

const Eyebrow = ({ children }) => (<div className="v-eyebrow">{children}</div>);

const Rule = ({ width = 64 }) => (
  <div className="rule" style={{ '--rule-w': width + 'px' }}>
    <span className="rule-line" />
    <span className="rule-dia">◆</span>
    <span className="rule-line" />
  </div>
);

const SectionHeader = ({ eyebrow, title, align = "center" }) => (
  <div className={`section-head section-head--${align}`}>
    {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
    {eyebrow && <Rule />}
    {title && (
      <h2 className="section-title">
        {title.split('\n').map((l, i) => <span key={i}>{l}<br/></span>)}
      </h2>
    )}
  </div>
);

const Btn = ({ variant = "primary", icon, children, href = "#", type, onClick }) => {
  const klass = `btn btn--${variant}`;
  const inner = (
    <>
      {icon && <span className="btn__icon">{icon}</span>}
      <span>{children}</span>
    </>
  );
  if (type === "submit" || type === "button") {
    return <button className={klass} type={type} onClick={onClick}>{inner}</button>;
  }
  return <a className={klass} href={href} onClick={onClick}>{inner}</a>;
};

/* ============================================================
   Iconography
   ============================================================ */

const Icon = ({ name, size = 20, stroke = 1.5 }) => {
  const props = {
    width: size, height: size, viewBox: "0 0 24 24",
    fill: "none", stroke: "currentColor",
    strokeWidth: stroke, strokeLinecap: "round", strokeLinejoin: "round",
  };
  switch (name) {
    case "instagram":
      return <svg {...props}><rect x="2" y="2" width="20" height="20" rx="5"/><circle cx="12" cy="12" r="4.2"/><circle cx="17.5" cy="6.5" r="0.8" fill="currentColor"/></svg>;
    case "whatsapp":
      return <svg {...props}><path d="M21 11.5a8.4 8.4 0 0 1-12.5 7.3L3 21l2.2-5.5A8.4 8.4 0 1 1 21 11.5z"/><path d="M8.5 9.5c0 4 3 6.5 6.5 6.5l1-1.5-2-1-1 .8a4.7 4.7 0 0 1-2.3-2.3l.8-1-1-2-1.5 1c0 .2 0 .3-.5.5z" strokeLinejoin="round"/></svg>;
    case "youtube":
      return <svg {...props}><rect x="2.5" y="6" width="19" height="12" rx="3"/><polygon points="10,9 16,12 10,15" fill="currentColor"/></svg>;
    case "pin":
    case "location":
      return <svg {...props}><path d="M12 22s7-7 7-12a7 7 0 1 0-14 0c0 5 7 12 7 12z"/><circle cx="12" cy="10" r="2.5"/></svg>;
    case "menu":
      return <svg {...props}><line x1="4" y1="7" x2="20" y2="7"/><line x1="4" y1="12" x2="20" y2="12"/><line x1="4" y1="17" x2="20" y2="17"/></svg>;
    case "ring":
      return <svg {...props}><circle cx="12" cy="15" r="6"/><path d="M9 9l3-5 3 5z" /></svg>;
    case "bands":
      return <svg {...props}><ellipse cx="10.5" cy="12" rx="6.5" ry="3"/><ellipse cx="14" cy="13" rx="6.5" ry="3" opacity="0.55"/></svg>;
    case "diamond":
      return <svg {...props}><path d="M6 9 12 3 18 9 12 21z"/><path d="M6 9h12"/><path d="M9 9l3 12 3-12"/></svg>;
    case "sparkle":
      return <svg {...props}><path d="M12 3 L13.5 10.5 L21 12 L13.5 13.5 L12 21 L10.5 13.5 L3 12 L10.5 10.5 Z"/></svg>;
    case "play":
      return <svg {...props}><polygon points="8,5 19,12 8,19" fill="currentColor" strokeLinejoin="round"/></svg>;
    case "arrow":
      return <svg {...props}><line x1="4" y1="12" x2="20" y2="12"/><polyline points="14,6 20,12 14,18"/></svg>;
    case "heart":
      return <svg {...props}><path d="M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21.2l7.8-7.7 1-1.1a5.5 5.5 0 0 0 0-7.8z"/></svg>;
    case "user":
      return <svg {...props}><circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/></svg>;
    case "calendar":
      return <svg {...props}><rect x="3" y="5" width="18" height="16" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="8" y1="3" x2="8" y2="7"/><line x1="16" y1="3" x2="16" y2="7"/></svg>;
    case "globe":
      return <svg {...props}><circle cx="12" cy="12" r="10"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M12 2a15 15 0 0 1 4 10 15 15 0 0 1-4 10 15 15 0 0 1-4-10 15 15 0 0 1 4-10z"/></svg>;
    case "tiktok":
      return <svg {...props}><path d="M16 4v3a4 4 0 0 0 4 4"/><path d="M16 4v10.5a4.5 4.5 0 1 1-4.5-4.5"/></svg>;
    case "pinterest":
      return <svg {...props}><circle cx="12" cy="12" r="10"/><path d="M12 7v10"/><path d="M9 14c2 2 5 1 6-1s0-5-2.5-5S9 11 10 14l-2 6"/></svg>;
    case "scrollDot":
      return <svg width={size} height={size*1.6} viewBox="0 0 12 20" fill="none" stroke="currentColor" strokeWidth="1.2"><rect x="1" y="1" width="10" height="18" rx="5"/><circle cx="6" cy="6" r="1.2" fill="currentColor"/></svg>;
    case "check":
      return <svg {...props}><polyline points="4,12 10,18 20,6"/></svg>;
    default: return null;
  }
};

/* ============================================================
   Photo placeholders
   ============================================================ */
const PhotoPlaceholder = ({ tone = "ring", aspect = "1/1", className = "", caption }) => {
  const renderJewel = () => {
    if (tone === "ring") return (
      <g stroke="#E8CBA8" fill="none">
        <ellipse cx="100" cy="120" rx="44" ry="14" strokeWidth="2.5"/>
        <ellipse cx="100" cy="120" rx="44" ry="14" strokeWidth="6" stroke="rgba(232,203,168,.25)"/>
        <path d="M76 92 L100 60 L124 92 Z" strokeWidth="2"/>
        <path d="M76 92 L124 92" strokeWidth="2"/>
        <path d="M88 92 L100 132 L112 92" strokeWidth="1.5" opacity=".65"/>
        <circle cx="100" cy="78" r="1.5" fill="#fff"/>
      </g>
    );
    if (tone === "bands") return (
      <g stroke="#E8CBA8" fill="none" strokeWidth="2.5">
        <ellipse cx="86" cy="118" rx="46" ry="12"/>
        <ellipse cx="116" cy="124" rx="46" ry="12" opacity=".7"/>
        <path d="M50 118 q36 -22 72 0" strokeWidth="1.2" opacity=".5"/>
      </g>
    );
    if (tone === "pendant") return (
      <g stroke="#E8CBA8" fill="none">
        <path d="M40 40 Q100 60 160 40" strokeWidth="1.2" opacity=".55"/>
        <line x1="100" y1="56" x2="100" y2="100" strokeWidth="1"/>
        <circle cx="100" cy="120" r="22" strokeWidth="2"/>
        <path d="M86 116 L100 100 L114 116 L100 142 Z" strokeWidth="1.5"/>
        <circle cx="100" cy="116" r="1.5" fill="#fff"/>
      </g>
    );
    if (tone === "emerald") return (
      <g stroke="#E8CBA8" fill="none">
        <ellipse cx="100" cy="135" rx="34" ry="10" strokeWidth="2.5"/>
        <rect x="80" y="74" width="40" height="56" strokeWidth="2"/>
        <line x1="86" y1="80" x2="114" y2="80" strokeWidth="1" opacity=".6"/>
        <line x1="86" y1="124" x2="114" y2="124" strokeWidth="1" opacity=".6"/>
        <line x1="86" y1="80" x2="86" y2="124" strokeWidth="1" opacity=".6"/>
        <line x1="114" y1="80" x2="114" y2="124" strokeWidth="1" opacity=".6"/>
        <circle cx="100" cy="92" r="1.5" fill="#fff"/>
      </g>
    );
    if (tone === "store") return (
      <g>
        <rect x="0" y="0" width="200" height="200" fill="#0A1729"/>
        <rect x="20" y="60" width="160" height="140" fill="#16294A" stroke="rgba(232,203,168,.3)"/>
        <rect x="30" y="80" width="40" height="50" fill="rgba(232,203,168,.18)" stroke="#E8CBA8" strokeWidth="0.8"/>
        <rect x="80" y="80" width="40" height="50" fill="rgba(232,203,168,.18)" stroke="#E8CBA8" strokeWidth="0.8"/>
        <rect x="130" y="80" width="40" height="50" fill="rgba(232,203,168,.18)" stroke="#E8CBA8" strokeWidth="0.8"/>
        <text x="100" y="155" fontFamily="serif" fill="#E8CBA8" fontSize="14" textAnchor="middle" letterSpacing="2">VENTA</text>
        <text x="100" y="170" fontFamily="serif" fill="rgba(232,203,168,.6)" fontSize="6" textAnchor="middle" letterSpacing="3">JEWELRY</text>
        <rect x="50" y="180" width="100" height="20" fill="#0A1729"/>
      </g>
    );
    if (tone === "interior") return (
      <g>
        <rect x="0" y="0" width="200" height="200" fill="#0F203A"/>
        <rect x="0" y="0" width="200" height="120" fill="url(#warm)"/>
        <ellipse cx="100" cy="40" rx="22" ry="8" fill="rgba(232,203,168,.25)"/>
        <line x1="100" y1="0" x2="100" y2="32" stroke="rgba(232,203,168,.4)" strokeWidth="0.6"/>
        <circle cx="100" cy="44" r="2" fill="#E8CBA8"/>
        <rect x="20" y="130" width="60" height="40" fill="rgba(232,203,168,.12)" stroke="rgba(232,203,168,.3)" strokeWidth="0.6"/>
        <rect x="120" y="130" width="60" height="40" fill="rgba(232,203,168,.12)" stroke="rgba(232,203,168,.3)" strokeWidth="0.6"/>
      </g>
    );
    if (tone === "workshop") return (
      <g>
        <rect x="0" y="0" width="200" height="200" fill="#0A1729"/>
        <ellipse cx="100" cy="105" rx="80" ry="40" fill="rgba(232,203,168,.18)"/>
        <line x1="20" y1="40" x2="92" y2="100" stroke="#E8CBA8" strokeWidth="3"/>
        <line x1="20" y1="60" x2="92" y2="100" stroke="#E8CBA8" strokeWidth="3"/>
        <path d="M92 92 L100 100 L92 108 L84 100 Z" fill="#fff" stroke="#E8CBA8"/>
        <ellipse cx="160" cy="170" rx="60" ry="20" fill="rgba(15,32,58,.6)"/>
      </g>
    );
    return null;
  };
  return (
    <div className={`photo photo--${tone} ${className}`} style={{ aspectRatio: aspect }}>
      <svg viewBox="0 0 200 200" preserveAspectRatio="xMidYMid slice" className="photo-svg">
        <defs>
          <radialGradient id={`bg-${tone}`} cx="50%" cy="40%" r="70%">
            <stop offset="0%" stopColor="#16294A"/>
            <stop offset="100%" stopColor="#0A1729"/>
          </radialGradient>
          <linearGradient id="warm" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="rgba(232,203,168,.35)"/>
            <stop offset="100%" stopColor="rgba(15,32,58,0)"/>
          </linearGradient>
        </defs>
        {tone !== "store" && tone !== "interior" && tone !== "workshop" &&
          <rect width="200" height="200" fill={`url(#bg-${tone})`}/>}
        <g opacity=".15">
          <ellipse cx="40" cy="170" rx="80" ry="40" fill="#0A1729"/>
          <ellipse cx="160" cy="40" rx="80" ry="40" fill="rgba(232,203,168,.08)"/>
        </g>
        {renderJewel()}
      </svg>
      {caption && <span className="photo-cap">◆ {caption}</span>}
    </div>
  );
};

const Wordmark = ({ size = "sm" }) => (
  <div className={`wordmark wordmark--${size}`}>
    <div className="wordmark__name">VENTA</div>
    <div className="wordmark__sub">JEWELRY</div>
  </div>
);

/* ============================================================
   STICKY TOPBAR (separate from hero so it can stick)
   ============================================================ */

const TopBar = ({ brand }) => (
  <header className="topbar topbar--sticky">
    <div className="topbar__inner">
      <Wordmark size="sm" />
      <div className="topbar__icons">
        <a className="ic-btn" href={brand.instagram} aria-label="Instagram"><Icon name="instagram" size={18}/></a>
        <a className="ic-btn" href={brand.youtube}   aria-label="YouTube"><Icon name="youtube" size={18}/></a>
        <a className="ic-btn" href={brand.whatsapp}  aria-label="WhatsApp"><Icon name="whatsapp" size={18}/></a>
        <a className="ic-btn" href={brand.location}  aria-label="Konum"><Icon name="location" size={18}/></a>
      </div>
    </div>
  </header>
);

/* ============================================================
   1. HERO (no topbar inside anymore)
   ============================================================ */

const Hero = ({ data }) => (
  <section className="hero" data-screen-label="01 Hero">
    <h1 className="hero__title">
      {data.title.split('\n').map((l,i)=>(<span key={i}>{l}<br/></span>))}
    </h1>
    <p className="hero__sub">{data.sub}</p>
    <div className="hero__ctas">
      <Btn variant="primary" href={data.primaryCta.href}>{data.primaryCta.label}</Btn>
      <Btn variant="outline" href={data.secondaryCta.href}>{data.secondaryCta.label}</Btn>
    </div>
    <div className="scroll-cue">
      <Icon name="scrollDot" size={14} />
      <span>KAYDIR</span>
    </div>
  </section>
);

/* ============================================================
   2. QUICK ACCESS
   ============================================================ */
const QuickAccess = ({ items }) => (
  <section className="quick">
    <Eyebrow>HIZLI ERİŞİM</Eyebrow>
    <div className="quick__grid">
      {items.map(it => (
        <a key={it.id} className="tile" href={it.href}>
          <Icon name={it.icon} size={30} stroke={1.4}/>
          <div className="tile__label">
            {it.label.split('\n').map((l,i)=><span key={i}>{l}<br/></span>)}
          </div>
        </a>
      ))}
    </div>
  </section>
);

/* ============================================================
   3. FEATURED PIECES (now before collections)
   ============================================================ */
const FeaturedCard = ({ p }) => (
  <article className="feat">
    <div className="feat__photo-wrap">
      <PhotoPlaceholder tone="ring" aspect="1/1" className="feat__photo"/>
      <button className="heart" aria-label="Kaydet"><Icon name="heart" size={12} stroke={1.6}/></button>
    </div>
    <h4 className="feat__name">{p.name}</h4>
    <div className="feat__price">{p.price}</div>
  </article>
);

const Featured = ({ header, items }) => {
  const [page, setPage] = useState(0);
  return (
    <section className="featured" data-screen-label="03 Öne Çıkan Parçalar">
      <Eyebrow>{header.eyebrow}</Eyebrow>
      <div className="featured__rail">
        {items.map(p => <FeaturedCard key={p.id} p={p}/>)}
      </div>
      <div className="dots">
        {[0,1,2].map(i => <span key={i} className={`dot ${page===i?'is-active':''}`} onClick={()=>setPage(i)}/>)}
      </div>
      <Btn variant="outline" href="#tum">TÜM KOLEKSİYONLAR</Btn>
    </section>
  );
};

/* ============================================================
   4. COLLECTIONS
   ============================================================ */
const CollectionCard = ({ c }) => (
  <a className="col-card" href={c.href}>
    <PhotoPlaceholder tone={c.tone} aspect="4/3" className="col-card__photo"/>
    <div className="col-card__overlay">
      <h3 className="col-card__title">{c.title}</h3>
      <span className="col-card__cta">
        Keşfet <Icon name="arrow" size={14} stroke={1.4}/>
      </span>
    </div>
  </a>
);

const Collections = ({ header, items }) => {
  const railRef = useRef(null);
  const [page, setPage] = useState(0);
  const onScroll = () => {
    const el = railRef.current; if (!el) return;
    const w = el.clientWidth * 0.85;
    setPage(Math.round(el.scrollLeft / w));
  };
  return (
    <section className="collections" data-screen-label="04 Koleksiyonlar">
      <SectionHeader eyebrow={header.eyebrow} title={header.title}/>
      <div className="collections__rail" ref={railRef} onScroll={onScroll}>
        {items.map(c => <CollectionCard key={c.id} c={c}/>)}
      </div>
      <div className="dots dots--collections">
        {items.map((_,i) => <span key={i} className={`dot ${page===i?'is-active':''}`}/>)}
      </div>
    </section>
  );
};

/* ============================================================
   5. BESPOKE VIDEO
   ============================================================ */
const BespokeVideo = ({ data }) => (
  <section className="bespoke" data-screen-label="05 Özel Tasarım">
    <div className="bespoke__media">
      <PhotoPlaceholder tone="workshop" aspect="16/11" className="bespoke__photo"/>
      <button className="play-btn" aria-label="Videoyu oynat">
        <Icon name="play" size={20} stroke={1.6}/>
      </button>
    </div>
    <Eyebrow>{data.eyebrow}</Eyebrow>
    <h2 className="bespoke__title">
      {data.title.split('\n').map((l,i)=><span key={i}>{l}<br/></span>)}
    </h2>
    <p className="bespoke__sub">{data.sub}</p>
    <Btn variant="outline" href={data.cta.href}>{data.cta.label}</Btn>
    <div className="bespoke__dia">◆</div>
  </section>
);

/* ============================================================
   6. APPOINTMENT FORM
   ============================================================ */
const FieldLabel = ({ children }) => (
  <label className="field__label">{children}</label>
);

const Appointment = ({ data }) => {
  const [interest, setInterest] = useState(data.interests[0]);
  const [submitted, setSubmitted] = useState(false);
  const onSubmit = (e) => { e.preventDefault(); setSubmitted(true); };
  return (
    <section className="appt" id="randevu" data-screen-label="06 Randevu">
      <Eyebrow>{data.eyebrow}</Eyebrow>
      <Rule />
      <h2 className="appt__title">
        {data.title.split('\n').map((l,i)=><span key={i}>{l}<br/></span>)}
      </h2>
      <p className="appt__sub">{data.sub}</p>

      {submitted ? (
        <div className="appt__success">
          <div className="success__ic"><Icon name="check" size={24} stroke={1.6}/></div>
          <h3>Talebiniz Alındı</h3>
          <p>{data.foot}</p>
        </div>
      ) : (
        <form className="appt__form" onSubmit={onSubmit}>
          <div className="field">
            <FieldLabel>{data.nameLabel}</FieldLabel>
            <input className="field__input" type="text" placeholder={data.namePh} required />
          </div>
          <div className="field">
            <FieldLabel>{data.phoneLabel}</FieldLabel>
            <input className="field__input" type="tel" placeholder={data.phonePh} required />
          </div>
          <div className="field-row">
            <div className="field">
              <FieldLabel>{data.dateLabel}</FieldLabel>
              <input className="field__input" type="date" required />
            </div>
            <div className="field">
              <FieldLabel>{data.timeLabel}</FieldLabel>
              <input className="field__input" type="time" required />
            </div>
          </div>
          <div className="field">
            <FieldLabel>{data.interestLabel}</FieldLabel>
            <div className="chips">
              {data.interests.map(opt => (
                <button
                  type="button" key={opt}
                  className={`chip ${interest===opt ? 'is-active' : ''}`}
                  onClick={()=>setInterest(opt)}
                >{opt}</button>
              ))}
            </div>
          </div>
          <Btn variant="primary" type="submit">{data.cta.label}</Btn>
          <p className="appt__foot">{data.foot}</p>
        </form>
      )}
    </section>
  );
};

/* ============================================================
   7. STORE (Mağazamız) — single photo placeholder + CTA
   ============================================================ */
const Store = ({ data }) => (
  <section className="store" data-screen-label="07 Mağazamız">
    <Eyebrow>{data.eyebrow}</Eyebrow>
    <Rule />

    <div className="store__hero">
      <div className="store__photo-slot" aria-label="Mağaza fotoğrafı için alan">
        <span className="slot__dia">◆</span>
        <span className="slot__hint">Mağaza fotoğrafı buraya gelecek</span>
      </div>
    </div>
  </section>
);

/* ============================================================
   8. WORLDWIDE
   ============================================================ */
const WorldDots = () => {
  const dots = [];
  for (let r = 0; r < 18; r++) {
    for (let c = 0; c < 36; c++) {
      const seed = (Math.sin(r*7.3 + c*1.7) + 1) / 2;
      if (seed > 0.55) {
        dots.push(<circle key={`${r}-${c}`} cx={c*8 + 4} cy={r*8 + 4} r="0.9" fill="rgba(232,203,168,.35)"/>);
      }
    }
  }
  return <svg viewBox="0 0 292 148" preserveAspectRatio="xMidYMid slice" className="world-dots">{dots}</svg>;
};

const Worldwide = ({ data }) => (
  <section className="worldwide">
    <WorldDots/>
    <div className="worldwide__inner">
      <div className="worldwide__dia">◆</div>
      <Eyebrow>{data.eyebrow}</Eyebrow>
      <p className="worldwide__sub">
        {data.sub.split('\n').map((l,i)=><span key={i}>{l}<br/></span>)}
      </p>
      <div className="v-signature worldwide__sig">{data.signature}</div>
    </div>
  </section>
);

/* ============================================================
   9. CONNECT
   ============================================================ */
const Connect = ({ data, socials, brand, copyright }) => (
  <section className="connect" data-screen-label="09 İletişim">
    <Eyebrow>{data.eyebrow}</Eyebrow>
    <Rule />
    <h2 className="connect__title">{data.title}</h2>
    <p className="connect__sub">
      {data.sub.split('\n').map((l,i)=><span key={i}>{l}<br/></span>)}
    </p>
    <div className="connect__ctas">
      <Btn variant="primary" icon={<Icon name={data.primary.icon} size={16} stroke={1.5}/>} href={brand.whatsapp}>
        {data.primary.label}
      </Btn>
      <Btn variant="outline" icon={<Icon name={data.secondary.icon} size={16} stroke={1.5}/>} href={brand.website}>
        {data.secondary.label}
      </Btn>
      <Btn variant="outline" icon={<Icon name="location" size={16} stroke={1.5}/>} href={brand.location}>
        YOL TARİFİ AL
      </Btn>
    </div>
    <div className="socials">
      {socials.map(s => (
        <a key={s.id} className="soc" href={s.href} aria-label={s.label}>
          <Icon name={s.icon} size={20} stroke={1.5}/>
        </a>
      ))}
    </div>
    <div className="copyright">{copyright}</div>
  </section>
);

/* ============================================================
   APP — new section order (no Promise)
   ============================================================ */
const App = () => {
  const D = window.VENTA_DATA;
  return (
    <div className="page" data-screen-label="01 Instagram Link Sayfası">
      <TopBar brand={D.brand}/>
      <Hero data={D.hero}/>
      <Featured header={D.featuredHeader} items={D.featured}/>
      <Collections header={D.collectionsHeader} items={D.collections}/>
      <BespokeVideo data={D.bespoke}/>
      <Appointment data={D.appointment}/>
      <Store data={D.store}/>
      <Worldwide data={D.worldwide}/>
      <Connect data={D.connect} socials={D.socials} brand={D.brand} copyright={D.copyright}/>
    </div>
  );
};

window.App = App;
