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

// ───────────────────────────────────────────────────────────
// Placeholder artwork generators — abstract compositions
// so portfolio + IG cells look distinctive without real images.
// ───────────────────────────────────────────────────────────
const Art = {
  Sofa: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#1A1A14" />
      <rect x="40" y="160" width="320" height="100" rx="12" fill="#2A2A1F" />
      <rect x="40" y="120" width="320" height="60" rx="10" fill="#33332A" />
      <rect x="56" y="132" width="92" height="36" rx="6" fill="#3F3F32" />
      <rect x="156" y="132" width="92" height="36" rx="6" fill="#3F3F32" />
      <rect x="256" y="132" width="92" height="36" rx="6" fill="#3F3F32" />
      <line x1="40" y1="260" x2="60" y2="280" stroke="#1A1A14" strokeWidth="4" />
      <line x1="360" y1="260" x2="340" y2="280" stroke="#1A1A14" strokeWidth="4" />
      <circle cx="340" cy="60" r="22" fill="#BEFF00" opacity="0.9" />
    </svg>,

  Cafe: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <defs>
        <linearGradient id="cf" x1="0" x2="1" y1="0" y2="1">
          <stop offset="0" stopColor="#1F1A12" />
          <stop offset="1" stopColor="#0E0C08" />
        </linearGradient>
      </defs>
      <rect width="400" height="300" fill="url(#cf)" />
      <ellipse cx="200" cy="220" rx="120" ry="22" fill="#0A0A0A" opacity="0.6" />
      <rect x="160" y="100" width="80" height="100" rx="6" fill="#2C261B" />
      <rect x="170" y="92" width="60" height="14" rx="3" fill="#3C3527" />
      <path d="M180 110 Q200 70 220 110" stroke="#BEFF00" strokeWidth="2" fill="none" opacity="0.7" />
      <path d="M188 108 Q200 78 212 108" stroke="#BEFF00" strokeWidth="1.5" fill="none" opacity="0.5" />
      <circle cx="80" cy="60" r="42" fill="none" stroke="#2A2519" strokeWidth="1" />
      <circle cx="80" cy="60" r="60" fill="none" stroke="#2A2519" strokeWidth="1" />
    </svg>,

  Mesh: ({ hue = 80 }) =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <defs>
        <radialGradient id={`m${hue}a`} cx="0.2" cy="0.3">
          <stop offset="0" stopColor={`hsl(${hue}, 95%, 55%)`} stopOpacity="0.5" />
          <stop offset="1" stopColor="#0A0A0A" stopOpacity="0" />
        </radialGradient>
        <radialGradient id={`m${hue}b`} cx="0.8" cy="0.8">
          <stop offset="0" stopColor={`hsl(${(hue + 40) % 360}, 70%, 45%)`} stopOpacity="0.35" />
          <stop offset="1" stopColor="#0A0A0A" stopOpacity="0" />
        </radialGradient>
      </defs>
      <rect width="400" height="300" fill="#0A0A0A" />
      <rect width="400" height="300" fill={`url(#m${hue}a)`} />
      <rect width="400" height="300" fill={`url(#m${hue}b)`} />
      <g fill="none" stroke="rgba(255,255,255,0.04)" strokeWidth="0.5">
        {Array.from({ length: 12 }).map((_, i) => <line key={i} x1="0" y1={i * 25} x2="400" y2={i * 25} />)}
        {Array.from({ length: 16 }).map((_, i) => <line key={i} x1={i * 25} y1="0" x2={i * 25} y2="300" />)}
      </g>
    </svg>,

  Showroom: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#15140F" />
      <polygon points="0,300 400,300 320,150 80,150" fill="#1F1D17" />
      <polygon points="80,150 320,150 280,80 120,80" fill="#2A271E" />
      <rect x="140" y="190" width="50" height="60" fill="#36322A" />
      <rect x="210" y="190" width="50" height="60" fill="#36322A" />
      <rect x="265" y="200" width="35" height="50" fill="#36322A" />
      <rect x="105" y="200" width="35" height="50" fill="#36322A" />
      <line x1="200" y1="80" x2="200" y2="40" stroke="#BEFF00" strokeWidth="1" opacity="0.6" />
      <circle cx="200" cy="36" r="3" fill="#BEFF00" />
    </svg>,

  Chair: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#18170F" />
      <path d="M150 80 Q150 60 170 60 L230 60 Q250 60 250 80 L250 180 L150 180 Z" fill="#2E2B20" />
      <rect x="155" y="180" width="90" height="20" fill="#363225" />
      <line x1="170" y1="200" x2="160" y2="250" stroke="#1F1D14" strokeWidth="3" />
      <line x1="230" y1="200" x2="240" y2="250" stroke="#1F1D14" strokeWidth="3" />
      <text x="300" y="48" fontFamily="monospace" fontSize="10" fill="#5C5847" letterSpacing="2">NO.07</text>
    </svg>,

  Type: ({ text = "AG" }) =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#0F0F0F" />
      <text x="200" y="200" textAnchor="middle" fontFamily="Instrument Serif, serif"
    fontSize="200" fill="#BEFF00" fontStyle="italic">{text}</text>
      <line x1="0" y1="220" x2="400" y2="220" stroke="#1F1F1F" />
      <text x="20" y="270" fontFamily="monospace" fontSize="9" fill="#666" letterSpacing="3">{text} / CAMPAIGN 2025</text>
    </svg>,

  Coffee: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#100E0A" />
      <circle cx="200" cy="160" r="80" fill="#2A2418" />
      <circle cx="200" cy="160" r="68" fill="#3D3322" />
      <circle cx="200" cy="160" r="56" fill="#23200F" />
      <ellipse cx="200" cy="155" rx="40" ry="12" fill="#0A0805" opacity="0.5" />
      <path d="M180 100 Q200 70 220 100" stroke="#BEFF00" strokeWidth="1.5" fill="none" opacity="0.5" />
    </svg>,

  Boutique: () =>
  <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" width="100%" height="100%">
      <rect width="400" height="300" fill="#171612" />
      <rect x="60" y="40" width="280" height="220" fill="none" stroke="#262420" strokeWidth="1" />
      <rect x="80" y="80" width="50" height="120" fill="#252319" />
      <rect x="150" y="80" width="50" height="120" fill="#2C2A1E" />
      <rect x="220" y="80" width="50" height="120" fill="#252319" />
      <rect x="290" y="80" width="30" height="120" fill="#2C2A1E" />
      <text x="200" y="290" textAnchor="middle" fontFamily="monospace" fontSize="9" fill="#555" letterSpacing="3">MOBİLYA / SS25</text>
    </svg>

};

// ─────────────────────────────────────────────
// NAV
// ─────────────────────────────────────────────
function Nav({ onMenu, mobileOpen }) {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 30);
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);
  return (
    <nav className={`nav ${scrolled ? 'scrolled' : ''}`}>
      <a href="#top" className="nav-logo">
        <img src="assets/balabandigital_logo_transparent.png" alt="Balaban Digital" />
      </a>
      <div className="nav-links">
        <a href="#top" className="nav-link">Ana Sayfa</a>
        <a href="#hakkimda" className="nav-link">Hakkımda</a>
        <a href="#hizmetler" className="nav-link">Hizmetler</a>
        <a href="#iletisim" className="nav-link">İletişim</a>
      </div>
      <div className="nav-cta">
        <a href="#iletisim" className="btn btn-primary">
          <span className="dot"></span>
          Teklif Al
        </a>
      </div>
      <button className="btn mobile-menu-btn" onClick={onMenu} aria-label="Menü">
        {mobileOpen ? <Icons.Close /> : <Icons.Menu />}
      </button>
    </nav>);

}

// ─────────────────────────────────────────────
// HERO PHONE — animated live stats mockup
// ─────────────────────────────────────────────
const PHONE_BRANDS = ['Atölye 21', 'Mola Kahve', 'Konak Showroom', 'Form & Function', 'AG Studio', 'Brew House'];

function HeroPhone() {
  const [revenue, setRevenue] = useState(48250);
  const [orders, setOrders] = useState(127);
  const [views, setViews] = useState(12480);
  const [likes, setLikes] = useState(2147);
  const [time, setTime] = useState('—');
  const [bars, setBars] = useState([38, 52, 44, 68, 55, 78, 64]);
  const [feed, setFeed] = useState([
    { id: 'a', type: 'sale', value: '+₺2.450', label: 'Yeni satış', who: 'Atölye 21' },
    { id: 'b', type: 'like', value: '+24', label: 'Yeni beğeni', who: 'Mola Kahve' },
    { id: 'c', type: 'view', value: '+1.2K', label: 'Görüntülenme', who: 'Konak Showroom' }
  ]);
  const nextId = useRef(100);
  const [pulse, setPulse] = useState(0);

  useEffect(() => {
    const setNow = () => {
      const d = new Date();
      const hh = String(d.getHours()).padStart(2, '0');
      const mm = String(d.getMinutes()).padStart(2, '0');
      setTime(`${hh}:${mm}`);
    };
    setNow();
    const t = setInterval(setNow, 30000);
    return () => clearInterval(t);
  }, []);

  useEffect(() => {
    const pickWho = () => PHONE_BRANDS[Math.floor(Math.random() * PHONE_BRANDS.length)];
    const fmt = (n) => n.toLocaleString('tr-TR');

    const events = [
      () => {
        const amt = (Math.floor(Math.random() * 28) + 4) * 50;
        setRevenue(r => r + amt);
        setOrders(o => o + 1);
        return { type: 'sale', value: `+₺${fmt(amt)}`, label: 'Yeni satış', who: pickWho() };
      },
      () => {
        const v = Math.floor(Math.random() * 1100) + 180;
        setViews(x => x + v);
        return { type: 'view', value: `+${v >= 1000 ? (v / 1000).toFixed(1) + 'K' : v}`, label: 'Görüntülenme', who: pickWho() };
      },
      () => {
        const l = Math.floor(Math.random() * 36) + 6;
        setLikes(x => x + l);
        return { type: 'like', value: `+${l}`, label: 'Yeni beğeni', who: pickWho() };
      },
      () => {
        const f = Math.floor(Math.random() * 9) + 2;
        return { type: 'follow', value: `+${f}`, label: 'Yeni takipçi', who: pickWho() };
      }
    ];

    const tick = () => {
      const ev = events[Math.floor(Math.random() * events.length)]();
      setFeed(prev => [{ ...ev, id: nextId.current++ }, ...prev].slice(0, 4));
      setBars(b => [...b.slice(1), Math.floor(Math.random() * 60) + 30]);
      setPulse(p => p + 1);
    };
    const id = setInterval(tick, 1900);
    return () => clearInterval(id);
  }, []);

  const fmtTR = (n) => n.toLocaleString('tr-TR');
  const maxBar = Math.max(...bars, 100);

  return (
    <div className="hero-phone" aria-hidden="true">
      <div className="hero-phone-frame">
        <div className="hero-phone-notch"></div>
        <div className="hero-phone-screen">
          {/* status bar */}
          <div className="phone-status">
            <span>{time}</span>
            <span className="phone-status-icons">
              <span className="phone-bars"><i></i><i></i><i></i><i></i></span>
              <span className="phone-batt"><b></b></span>
            </span>
          </div>

          {/* app header */}
          <div className="phone-header">
            <div className="phone-app">
              <span className="phone-app-dot"></span>
              <span>balaban.digital</span>
            </div>
            <div className="phone-live">
              <span className="phone-live-dot"></span>
              CANLI
            </div>
          </div>

          {/* hero metric */}
          <div className="phone-revenue" key={pulse}>
            <div className="phone-revenue-lbl">Bugünkü ciro</div>
            <div className="phone-revenue-val">
              ₺{fmtTR(revenue)}
              <span className="phone-revenue-up">↑</span>
            </div>
            <div className="phone-revenue-sub">{orders} sipariş · son 24 saat</div>
          </div>

          {/* chart */}
          <div className="phone-chart">
            {bars.map((h, i) => (
              <div key={i} className="phone-bar" style={{ height: `${(h / maxBar) * 100}%` }}>
                <span style={{ height: `${(h / maxBar) * 100}%` }}></span>
              </div>
            ))}
          </div>

          {/* mini stats */}
          <div className="phone-mini">
            <div>
              <div className="phone-mini-lbl">Görüntülenme</div>
              <div className="phone-mini-val">{fmtTR(views)}</div>
            </div>
            <div>
              <div className="phone-mini-lbl">Beğeni</div>
              <div className="phone-mini-val">{fmtTR(likes)}</div>
            </div>
          </div>

          {/* live feed */}
          <div className="phone-feed">
            {feed.map((f, i) => (
              <div key={f.id} className={`phone-feed-row t-${f.type}`} style={{ '--i': i }}>
                <div className="phone-feed-icon">
                  {f.type === 'sale' && '₺'}
                  {f.type === 'view' && <Icons.Eye size={12} />}
                  {f.type === 'like' && <Icons.Heart size={12} />}
                  {f.type === 'follow' && '+'}
                </div>
                <div className="phone-feed-text">
                  <div className="phone-feed-label">{f.label}</div>
                  <div className="phone-feed-who">{f.who}</div>
                </div>
                <div className="phone-feed-val">{f.value}</div>
              </div>
            ))}
          </div>
        </div>
        <div className="hero-phone-glare"></div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────
// HERO
// ─────────────────────────────────────────────
function Hero() {
  return (
    <section id="top" className="hero wrap">
      <HeroPhone />
      <h1 className="hero-headline">
        <span className="line">Markanızı</span>
        <span className="line indent"><em>dijitalde</em> öne</span>
        <span className="line">çıkaralım.</span>
      </h1>

      <div className="hero-foot">
        <p>
          Sosyal medya, içerik, web ve reklam yönetimini tek bir disiplin
          altında birleştiren dijital pazarlama stüdyosu. Marka hikâyenizi
          ölçülebilir büyümeye çeviriyoruz.
        </p>
        <div className="hero-ctas">
          <a href="#hizmetler" className="btn btn-primary">
            <span className="dot"></span>
            Çalışmaya başlayalım
          </a>
          <a href="#iletisim" className="btn btn-arrow">
            İletişime geç
            <Icons.Arrow size={14} />
          </a>
        </div>
      </div>
    </section>);

}

// ─────────────────────────────────────────────
// MARQUEE
// ─────────────────────────────────────────────
function Marquee() {
  const items = ["Sosyal Medya", "Reklam Yönetimi", "Web Tasarım", "İçerik Üretimi", "SEO", "Marka Stratejisi", "Meta Ads", "Google Ads"];
  const all = [...items, ...items];
  return (
    <div className="marquee" aria-hidden="true">
      <div className="marquee-track">
        {all.map((t, i) =>
        <span key={i}>
            <i>✦</i>
            {t}
          </span>
        )}
      </div>
    </div>);

}

// ─────────────────────────────────────────────
// ABOUT
// ─────────────────────────────────────────────
function About() {
  return (
    <section id="hakkimda" className="wrap">
      <div className="about-grid">
        <div className="about-photo reveal">
          <img src="assets/murat_foto.jpeg" alt="Murat Balaban" />
          <span className="about-photo-corner"></span>
          <span className="about-photo-tag">Murat Balaban / Kurucu</span>
        </div>

        <div className="about-copy reveal">
          <div className="num" style={{ marginBottom: 24 }}>— 01 / Hakkımda</div>
          <h3>Markanızın <em>dijital kimliğini</em> kuruyorum.</h3>
          <p className="lede">
            Balaban Digital, İnegöl ve çevresindeki işletmelere özel kurulmuş bir tek
            kişilik stüdyo. Bölgenin dinamiklerini, müşterilerinizin nasıl karar verdiğini
            ve hangi reklamın işe yaradığını biliyorum.
          </p>
          <p>
            7 yıllık dijital pazarlama deneyimimle; sosyal medya yönetiminden Meta &amp; Google
            reklam kampanyalarına, kurumsal websitelerinden SEO stratejisine kadar dijital
            varlığınızın her noktasını planlıyor, uyguluyor ve ölçüyorum. Her şey tek elden,
            her şey ölçülebilir.
          </p>

          <div className="skill-tags">
            {["Meta Ads", "Google Ads", "SEO", "Marka Stratejisi", "İçerik Yönetimi", "UX/UI", "Webflow & WP", "Analitik"].map((s) =>
            <span key={s} className="skill-tag">{s}</span>
            )}
          </div>

          <div className="about-stats">
            <div>
              <div className="stat-num"><em>40+</em></div>
              <div className="stat-lbl">İnegöl Markası</div>
            </div>
            <div>
              <div className="stat-num"><em>7 yıl</em></div>
              <div className="stat-lbl">Dijital Deneyim</div>
            </div>
          </div>
        </div>
      </div>
    </section>);

}

// ─────────────────────────────────────────────
// SERVICES
// ─────────────────────────────────────────────
const SERVICES = [
{
  num: "S/01", title: "Sosyal Medya Yönetimi",
  desc: "Instagram, Facebook ve TikTok hesaplarınızı stratejiyle, takvimle ve tutarlı bir sesle yönetiyoruz.",
  meta: "Aylık paket", icon: "Share2", formValue: "Sosyal medya yönetimi"
},
{
  num: "S/02", title: "İçerik & Fotoğraf",
  desc: "İşletmenizde çektiğimiz profesyonel görsellerle, içeriklerinizi algoritmaların favorisi haline getiriyoruz.",
  meta: "Çekim + kurgu", icon: "Camera", formValue: "İçerik & fotoğraf"
},
{
  num: "S/03", title: "Web Sitesi Kurulumu",
  desc: "Markanıza özel, hızlı, mobil uyumlu ve SEO uyumlu websiteleri. Tasarım, geliştirme ve teslim.",
  meta: "Tek seferlik", icon: "Code", formValue: "Web sitesi kurulumu"
},
{
  num: "S/04", title: "Meta & Google Ads",
  desc: "Reklam kampanyalarını günlük takiple, hedef kitle testleri ve optimize edilmiş bütçelerle yönetiyoruz.",
  meta: "Performans", icon: "Megaphone", formValue: "Meta & Google reklamları"
},
{
  num: "S/05", title: "SEO Optimizasyonu",
  desc: "Hedef kitleniz sizi Google'da ararken üst sıralarda görmesi için teknik ve içerik tabanlı SEO çalışmaları.",
  meta: "3–6 ay süreç", icon: "TrendingUp", formValue: "SEO optimizasyonu"
},
{
  num: "S/06", title: "Dijital Strateji",
  desc: "Markanızın dijital yol haritasını, hedef kitlenizi ve büyüme planını birlikte kuruyoruz.",
  meta: "Danışmanlık", icon: "Compass", formValue: "Dijital strateji / danışmanlık"
}];

function requestServiceInfo(formValue) {
  window.open('https://wa.me/905346602639', '_blank', 'noopener');
}


function Services() {
  return (
    <section id="hizmetler">
      <div className="wrap">
        <div className="section-head reveal">
          <div className="num">— 02 / Hizmetler</div>
          <div>
            <h2>İhtiyacınız olan <em>her şey</em>, tek elden.</h2>
            <div className="head-meta" style={{ marginTop: 16 }}>
              Altı temel hizmet, tek bir yaratıcı disiplinde birleşiyor.
              İster aylık paket, ister tek seferlik proje olsun.
            </div>
          </div>
        </div>

        <div className="services-grid reveal">
          {SERVICES.map((s) => {
            const Ic = Icons[s.icon];
            return (
              <div key={s.num} className="service">
                <div className="service-head">
                  <span className="service-num">{s.num}</span>
                  <div className="service-icon"><Ic size={18} /></div>
                </div>
                <div className="service-body">
                  <h3 className="service-title">{s.title}</h3>
                  <p className="service-desc">{s.desc}</p>
                  <div className="service-actions">
                    <a
                      href="https://wa.me/905346602639"
                      target="_blank"
                      rel="noopener"
                      className="service-info-btn"
                      onClick={(e) => e.stopPropagation()}>
                      <Icons.Info size={14} />
                      Bilgi al
                    </a>
                    <span className="service-meta-tag">{s.meta}</span>
                  </div>
                  <div className="service-meta">
                    <span className="service-arrow"><Icons.ArrowUpRight size={16} /></span>
                  </div>
                </div>
              </div>);

          })}
        </div>
      </div>
    </section>);

}

window.NavBar = Nav;
window.Hero = Hero;
window.Marquee = Marquee;
window.About = About;
window.Services = Services;
window.Art = Art;