/* sections.jsx — shared layout chrome + homepage sections.
   Reads window.Ico, window.I18N, window.DEPARTMENTS, window.DOCTORS. */

const { Ico, I18N, DEPARTMENTS, DOCTORS } = window;

// ---- RoyalSeal ----------------------------------------------------------
// Circular medallion treatment of the stag mark. Used as the hero's brand
// signature watermark — curved type around an outer ring, inner ring with
// the stag, decorative dots flanking horizontally.
function RoyalSeal({ className }) {
  return (
    <svg
      className={className}
      viewBox="0 0 400 400"
      fill="none"
      stroke="currentColor"
      aria-hidden="true"
    >
      <defs>
        {/* CCW arc through the top — text drawn here reads right-side up */}
        <path id="rs-top" d="M 60 200 A 140 140 0 0 1 340 200" fill="none" />
        {/* CW arc through the bottom — text drawn here reads right-side up */}
        <path id="rs-bot" d="M 340 200 A 140 140 0 0 1 60 200" fill="none" />
      </defs>

      {/* Outer ring */}
      <circle cx="200" cy="200" r="195" strokeWidth="1" strokeOpacity="0.85" />
      {/* Mid ring */}
      <circle cx="200" cy="200" r="170" strokeWidth="0.5" strokeOpacity="0.4" />
      {/* Inner stag enclosure */}
      <circle cx="200" cy="200" r="125" strokeWidth="1" strokeOpacity="0.55" />

      {/* Curved type — top */}
      <text
        fontFamily="Manrope, system-ui, sans-serif"
        fontSize="13"
        fontWeight="600"
        letterSpacing="6"
        fill="currentColor"
        stroke="none"
      >
        <textPath href="#rs-top" startOffset="50%" textAnchor="middle">
          ROYAL · ARTEMIS · PRIVATE · HOSPITAL
        </textPath>
      </text>

      {/* Curved type — bottom (mirrored arc keeps it right-side-up) */}
      <text
        fontFamily="Manrope, system-ui, sans-serif"
        fontSize="11"
        fontWeight="500"
        letterSpacing="6"
        fill="currentColor"
        stroke="none"
        opacity="0.75"
      >
        <textPath href="#rs-bot" startOffset="50%" textAnchor="middle">
          PAPHOS · CYPRUS · MCMXC
        </textPath>
      </text>

      {/* Cardinal dots at the 3 and 9 o'clock positions */}
      <g fill="currentColor" stroke="none">
        <circle cx="60" cy="200" r="2.5" />
        <circle cx="340" cy="200" r="2.5" />
      </g>

      {/* Stag glyph — same paths as Ico.Stag, scaled & re-centered */}
      <g
        fill="currentColor"
        stroke="none"
        transform="translate(200 205) scale(1.65)"
      >
        <path d="M -28 -34 C -34 -46 -32 -56 -22 -58 C -24 -50 -20 -44 -14 -42 C -18 -32 -22 -28 -28 -26 Z" />
        <path d="M 28 -34 C 34 -46 32 -56 22 -58 C 24 -50 20 -44 14 -42 C 18 -32 22 -28 28 -26 Z" />
        <path d="M -18 -50 C -22 -54 -22 -60 -18 -62 C -16 -56 -12 -54 -8 -54 L -10 -46 Z" />
        <path d="M 18 -50 C 22 -54 22 -60 18 -62 C 16 -56 12 -54 8 -54 L 10 -46 Z" />
        <ellipse cx="-22" cy="-18" rx="7" ry="11" transform="rotate(-25 -22 -18)" />
        <ellipse cx="22" cy="-18" rx="7" ry="11" transform="rotate(25 22 -18)" />
        <path d="M -16 -20 C -18 -8 -14 4 -8 12 C -4 18 -2 26 0 32 C 2 26 4 18 8 12 C 14 4 18 -8 16 -20 C 12 -28 4 -30 0 -30 C -4 -30 -12 -28 -16 -20 Z" />
      </g>
    </svg>
  );
}

// ---- Initials helper ----------------------------------------------------
function initials(name) {
  const parts = name.replace(/^(Dr|Δρ)\s/, '').split(' ');
  return ((parts[0]?.[0] || '') + (parts[parts.length-1]?.[0] || '')).toUpperCase();
}

// ---- Topbar -------------------------------------------------------------
function Topbar({ t }) {
  return (
    <div className="topbar">
      <div className="container topbar-inner">
        <div className="topbar-left">
          <a href="tel:+35726961600" className="emerg"><Ico.Ambulance className="icon" /> {t.topbar.emerg}</a>
          <span className="divider hide-sm"></span>
          <a href="#contact" className="hide-sm"><Ico.Pin className="icon" /> {t.topbar.addr}</a>
        </div>
        <div className="topbar-right">
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
            <Ico.Shield className="icon" style={{ color: 'var(--bone)' }} /> {t.topbar.gesy}
          </span>
        </div>
      </div>
    </div>
  );
}

// ---- Header (with real SVG logo) ---------------------------------------
function Header({ t, activePage, onToggleTheme, theme }) {
  return (
    <header className="header">
      <div className="container header-inner">
        <a className="brand-link" href="index.html" aria-label="Royal Artemis Hospital home">
          <span className="brand-logo">
            <img src="assets/royal-logo.svg" alt="Royal Artemis Hospital" />
          </span>
        </a>

        <nav className="nav" aria-label="Primary">
          {t.nav.map((item, i) => (
            <a key={i} href={item.href}
               className={activePage === item.label ? 'active' : ''}>
              {item.label}
            </a>
          ))}
        </nav>

        <div className="header-cta">
          <button className="btn btn-ghost btn-sm" aria-label="Toggle theme" onClick={onToggleTheme}>
            {theme === 'dark' ? <Ico.Sun className="icon" /> : <Ico.Moon className="icon" />}
          </button>
          <div className="lang-toggle" role="tablist" aria-label="Language">
            <button className="on" aria-pressed="true">EN</button>
            <button disabled aria-disabled="true" title="Greek translation coming soon">EL</button>
            <span className="lang-coming-soon">{t.langComing}</span>
          </div>
          <a className="btn btn-primary" href="book.html">
            <Ico.Calendar className="icon" /> {t.book}
          </a>
        </div>
      </div>
    </header>
  );
}

// ---- Hero (Editorial cinematic with navy panel) -----------------------
// Full-bleed photo background. The left ~45% is a near-solid navy panel
// (panel concept retained from the asymmetric direction) that hosts the
// editorial display headline, lede, CTAs and stats. A faded Royal Artemis
// seal anchors the right side over the photo.
//
// Photo: real exterior shot of the hospital. Re-shoot at higher res for
// production.
const HERO_PHOTO = "assets/hero-royal-clinic.webp";

function Hero({ t }) {
  return (
    <section className="hero hero-editorial">
      {/* Inline SVG filter — gentle wind ripple. Animated baseFrequency
          creates "moving" noise; displacement scale stays low so only
          high-detail areas (palm fronds) visibly flutter. */}
      <svg className="ed-defs" aria-hidden="true" focusable="false">
        <defs>
          <filter id="palmWind" x="-2%" y="-2%" width="104%" height="104%">
            <feTurbulence
              type="fractalNoise"
              baseFrequency="0.012 0.022"
              numOctaves="2"
              seed="3"
              result="noise"
            >
              <animate
                attributeName="baseFrequency"
                dur="14s"
                values="0.012 0.022; 0.014 0.026; 0.011 0.020; 0.013 0.024; 0.012 0.022"
                repeatCount="indefinite"
              />
            </feTurbulence>
            <feDisplacementMap in="SourceGraphic" in2="noise" scale="5" xChannelSelector="R" yChannelSelector="G" />
          </filter>
        </defs>
      </svg>

      <div className="ed-bg">
        <img src={HERO_PHOTO} alt="Royal Artemis Private Hospital — Paphos" />
      </div>
      <RoyalSeal className="ed-seal" />

      <div className="ed-rail" aria-hidden="true">
        <span className="vtext">Royal Artemis · Paphos · MMXXV</span>
        <span className="line"></span>
        <Ico.Star className="star" />
        <span className="line"></span>
        <span className="vtext">№ 01 — Welcome</span>
      </div>

      <div className="ed-content">
        <span className="eyebrow">{t.hero.eyebrow}</span>
        <h1>
          <span className="l1">{t.hero.title[0]}</span>
          <span className="l2">{t.hero.title[1]}</span>
          <span className="l3">{t.hero.title[2]}</span>
        </h1>
        <p className="lede">{t.hero.lede}</p>
        <div className="ed-cta">
          <a href="book.html" className="btn btn-primary btn-lg">
            <Ico.Calendar className="icon" /> {t.hero.cta1}
          </a>
          <a href="doctors.html" className="btn-link">
            {t.hero.cta2} <Ico.ArrowUpRight className="icon" />
          </a>
        </div>
        <div className="ed-stats">
          {t.hero.stats.map((s, i) => (
            <div className="stat" key={i}>
              <div className="n">{s.n}</div>
              <span className="l">{s.l}</span>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---- Quick actions ------------------------------------------------------
function QuickActions({ t }) {
  return (
    <section className="quick">
      <div className="container">
        <div className="quick-intro">
          <div className="quick-ornament" aria-hidden="true">
            <span className="rule"></span>
            <Ico.Star className="glyph" />
            <span className="rule"></span>
          </div>
          <span className="eyebrow">How we can help</span>
          <h2>Where would you like to <em>begin?</em></h2>
        </div>
        <div className="quick-grid">
          {t.quick.map((q, i) => {
            const Icon = Ico[q.ic];
            return (
              <a className={"quick-tile" + (q.emerg ? " emerg" : "")} key={i} href={q.href}>
                <div className="ic"><Icon className="icon icon-lg" /></div>
                <div>
                  <div className="t">{q.t}</div>
                  <div className="s">{q.s}</div>
                </div>
                <Ico.ArrowUpRight className="icon arrow" />
            </a>
          );
        })}
      </div>
      </div>
    </section>
  );
}

// ---- Credentials --------------------------------------------------------
function Credentials({ t }) {
  return (
    <section className="creds">
      <div className="container">
        <div className="creds-row">
          <div className="label">{t.creds.label}</div>
          <div className="creds-list">
            {t.creds.items.map((c, i) => (
              <div className="cred" key={i}>
                <div className="y">{c.y}</div>
                <div className="n">{c.n}</div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

// ---- Values (3 pillars: CARE / TRUST / EXPERTISE) ----------------------
function Values({ t }) {
  return (
    <section className="values">
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.values.eyebrow}</span>
            <h2 className="h1">{t.values.title}</h2>
            <p className="lede">{t.values.lede}</p>
          </div>
        </div>
        <div className="values-grid">
          {t.values.items.map((v, i) => {
            const Icon = Ico[v.ic];
            return (
              <div className="value" key={i}>
                <span className="num">{v.num}</span>
                <div className="ic"><Icon className="icon icon-lg" /></div>
                <h3>{v.name}</h3>
                <p>{v.body}</p>
              </div>
            );
          })}
        </div>
      </div>
    </section>
  );
}

// ---- Departments grid (uses shared DEPARTMENTS) ------------------------
function Departments({ t }) {
  return (
    <section id="departments">
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.depts.eyebrow}</span>
            <h2 className="h1">{t.depts.title}</h2>
            <p className="lede">{t.depts.lede}</p>
          </div>
          <div className="actions">
            <a className="btn btn-secondary" href="doctors.html">{t.depts.cta} <Ico.Arrow className="icon" /></a>
          </div>
        </div>

        <div className="depts-grid">
          {DEPARTMENTS.map((d, i) => {
            const Icon = Ico[d.ic];
            return (
              <a className="dept" href={`doctors.html?dept=${d.slug}`} key={i}>
                <div className="dept-ic"><Icon className="icon icon-lg" /></div>
                <Ico.ArrowUpRight className="icon dept-arrow" />
                <div>
                  <div className="dept-name">{d.name}</div>
                  <div className="dept-desc">{d.desc}</div>
                </div>
              </a>
            );
          })}
        </div>
      </div>
    </section>
  );
}

// ---- Doctors preview (homepage — 4 cards) ------------------------------
function DoctorsPreview({ t }) {
  const preview = DOCTORS.slice(0, 4);
  return (
    <section id="doctors" style={{ background: 'var(--surface)' }}>
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.docs.eyebrow}</span>
            <h2 className="h1">{t.docs.title}</h2>
            <p className="lede">{t.docs.lede}</p>
          </div>
          <div className="actions">
            <a className="btn btn-secondary" href="doctors.html">{t.docs.cta} <Ico.Arrow className="icon" /></a>
          </div>
        </div>

        <div className="docs-grid">
          {preview.map((d, i) => (
            <a className="doc" href={`doctors.html#${d.slug}`} key={i}>
              <div className="doc-photo">
                <div className="ph">{initials(d.name)}</div>
                <div className="specialty">{d.spec}</div>
              </div>
              <div className="doc-body">
                <div className="doc-name">{d.name}</div>
                <div className="doc-role">{d.role}</div>
                <div className="doc-meta">
                  <span><Ico.Globe className="icon" style={{ width: 14, height: 14 }} /> {d.langs.join(' · ')}</span>
                  <span><Ico.Clock className="icon" style={{ width: 14, height: 14 }} /> {d.years} yrs</span>
                </div>
              </div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---- International ------------------------------------------------------
function International({ t }) {
  return (
    <section id="international">
      <div className="container">
        <div className="intl">
          <div>
            <span className="eyebrow">{t.intl.eyebrow}</span>
            <h2 className="h1" style={{ color: '#fff', marginTop: 14, marginBottom: 20 }}>{t.intl.title}</h2>
            <p className="lede">{t.intl.lede}</p>
            <div style={{ marginTop: 32, display: 'flex', gap: 12, flexWrap: 'wrap' }}>
              <a className="btn" href="book.html" style={{ background: 'var(--bone)', color: '#091B40' }}>
                <Ico.Globe className="icon" /> {t.intl.cta}
              </a>
              <a className="btn" href="tel:+35726961600" style={{ background: 'transparent', color: '#fff', border: '1px solid rgba(255,255,255,.3)' }}>
                <Ico.Phone className="icon" /> +357 26 961 600
              </a>
            </div>
          </div>

          <div className="intl-list">
            {t.intl.items.map((it, i) => {
              const Icon = Ico[it.ic];
              return (
                <div className="intl-item" key={i}>
                  <div className="ic"><Icon className="icon icon-lg" /></div>
                  <div>
                    <div className="t">{it.t}</div>
                    <div className="s">{it.s}</div>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </div>
    </section>
  );
}

// ---- News ---------------------------------------------------------------
function News({ t }) {
  return (
    <section id="news" style={{ background: 'var(--surface)' }}>
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.news.eyebrow}</span>
            <h2 className="h1">{t.news.title}</h2>
          </div>
          <div className="actions">
            <a className="btn btn-secondary" href="#">{t.news.cta} <Ico.Arrow className="icon" /></a>
          </div>
        </div>
        <div className="news-grid">
          {t.news.items.map((n, i) => (
            <a className="news" href="#" key={i}>
              <div className="news-img">
                <div className="tag">{n.tag}</div>
                <div className="stagmotif"><Ico.Stag /></div>
              </div>
              <div className="news-body">
                <div className="news-meta">{n.date}</div>
                <div className="news-title">{n.title}</div>
                <div className="news-excerpt">{n.excerpt}</div>
                <span className="news-link">Read article <Ico.Arrow className="icon" style={{ width: 14, height: 14 }} /></span>
              </div>
            </a>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---- Testimonials -------------------------------------------------------
function Testimonials({ t }) {
  return (
    <section className="testimonials">
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.testi.eyebrow}</span>
            <h2 className="h1">{t.testi.title}</h2>
          </div>
        </div>
        <div className="testi-grid">
          {t.testi.items.map((it, i) => (
            <div className="testi" key={i}>
              <div className="stars">
                {[0,1,2,3,4].map(j => <Ico.Star key={j} className="icon" style={{ width: 16, height: 16 }} />)}
              </div>
              <div className="quote">&ldquo;{it.q}&rdquo;</div>
              <div className="who">
                <div className="av">{it.n.split(' ').map(p => p[0]).slice(0,2).join('')}</div>
                <div>
                  <div className="nm">{it.n}</div>
                  <div className="rl">{it.r}</div>
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---- Contact ------------------------------------------------------------
function Contact({ t }) {
  return (
    <section id="contact">
      <div className="container">
        <div className="section-head">
          <div className="meta">
            <span className="eyebrow">{t.contact.eyebrow}</span>
            <h2 className="h1">{t.contact.title}</h2>
            <p className="lede">{t.contact.lede}</p>
          </div>
        </div>

        <div className="contact-grid">
          <div className="contact-card">
            <div className="contact-row">
              <div className="ic"><Ico.Pin className="icon icon-lg" /></div>
              <div>
                <div className="label">{t.contact.addr.label}</div>
                <div className="val">{t.contact.addr.val}<span className="alt">{t.contact.addr.alt}</span></div>
              </div>
            </div>
            <div className="contact-row">
              <div className="ic"><Ico.Phone className="icon icon-lg" /></div>
              <div>
                <div className="label">{t.contact.phone.label}</div>
                <div className="val">{t.contact.phone.val}<span className="alt">{t.contact.phone.alt}</span></div>
              </div>
            </div>
            <div className="contact-row">
              <div className="ic"><Ico.Mail className="icon icon-lg" /></div>
              <div>
                <div className="label">{t.contact.mail.label}</div>
                <div className="val">{t.contact.mail.val}<span className="alt">{t.contact.mail.alt}</span></div>
              </div>
            </div>
            <div className="contact-row">
              <div className="ic"><Ico.Clock className="icon icon-lg" /></div>
              <div>
                <div className="label">{t.contact.hours.label}</div>
                <div className="val">{t.contact.hours.val}<span className="alt">{t.contact.hours.alt}</span></div>
              </div>
            </div>
            <div style={{ display: 'flex', gap: 10, marginTop: 8, flexWrap: 'wrap' }}>
              <a className="btn btn-primary" href="book.html"><Ico.Calendar className="icon" /> {t.contact.cta1}</a>
              <a className="btn btn-secondary" href="#"><Ico.Pin className="icon" /> {t.contact.cta2}</a>
            </div>
          </div>

          <div className="map" role="img" aria-label="Map of Paphos showing hospital location">
            <div className="grid-overlay"></div>
            <div className="water"></div>
            <div className="road-1"></div>
            <div className="road-2"></div>
            <div className="road-3"></div>
            <div className="pin"></div>
            <div className="pin-card">
              <Ico.Stag className="mk" />
              <div>
                <div className="nm">Royal Artemis Hospital</div>
                <div className="ad">Pavlou Crinaiou 2, Paphos</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ---- Footer -------------------------------------------------------------
function Footer({ t }) {
  return (
    <footer className="footer">
      <div className="container">
        <div className="footer-top">
          <div className="brand-block">
            <img className="footer-logo" src="assets/royal-logo.svg" alt="Royal Artemis Hospital" />
            <div className="tag">{t.footer.tag}</div>
            <div className="socials">
              <a href="https://www.facebook.com/royalartemisclinic" aria-label="Facebook"><Ico.FB className="icon" /></a>
              <a href="https://www.linkedin.com/company/royal-artemis-private-hospital-ltd" aria-label="LinkedIn"><Ico.LI className="icon" /></a>
              <a href="#" aria-label="YouTube"><Ico.YT className="icon" /></a>
            </div>
          </div>
          {t.footer.cols.map((c, i) => (
            <div key={i}>
              <h4>{c.h}</h4>
              <ul>
                {c.links.map((l, j) => <li key={j}><a href={l[1]}>{l[0]}</a></li>)}
              </ul>
            </div>
          ))}
        </div>
        <div className="footer-bot">
          <div>{t.footer.copy}</div>
          <div className="legal"><span>{t.footer.built}</span></div>
        </div>
      </div>
    </footer>
  );
}

// ---- Page chrome wrapper (for inner pages) -----------------------------
function PageChrome({ children, activePage, t }) {
  const [theme, setTheme] = React.useState(() => document.documentElement.getAttribute('data-theme') || 'light');
  const toggleTheme = () => {
    const next = theme === 'dark' ? 'light' : 'dark';
    document.documentElement.setAttribute('data-theme', next);
    setTheme(next);
    try { localStorage.setItem('ra-theme', next); } catch(e) {}
  };
  React.useEffect(() => {
    try {
      const saved = localStorage.getItem('ra-theme');
      if (saved) {
        document.documentElement.setAttribute('data-theme', saved);
        setTheme(saved);
      }
    } catch(e) {}
  }, []);

  return (
    <>
      <Topbar t={t} />
      <Header t={t} activePage={activePage} theme={theme} onToggleTheme={toggleTheme} />
      <main>{children}</main>
      <Footer t={t} />
    </>
  );
}

// ---- Page hero (smaller — for inner pages) -----------------------------
function PageHero({ eyebrow, title, lede, breadcrumb }) {
  return (
    <section className="page-hero">
      <div className="container">
        <div>
          {breadcrumb && (
            <div className="breadcrumb">
              <a href="index.html">Home</a>
              <span className="sep">/</span>
              <span>{breadcrumb}</span>
            </div>
          )}
          <span className="eyebrow">{eyebrow}</span>
          <h1 className="h1">{title}</h1>
        </div>
        <div>
          <p className="lede">{lede}</p>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, {
  Topbar, Header, Hero, QuickActions, Credentials, Values, Departments,
  DoctorsPreview, International, News, Testimonials, Contact, Footer,
  PageChrome, PageHero, initials,
});
