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

// ============================================================
// ICONS — simple geometric only
// ============================================================
// Default width/height baked into each SVG so they NEVER render at the browser's
// default 300x150 size if styles.css fails to load or is delayed. CSS rules still
// override (CSS beats SVG attributes).
const ICON_BASE = { width: 24, height: 24 };
const Icon = {
  Phone: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.37 1.9.72 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.91.35 1.85.59 2.81.72A2 2 0 0 1 22 16.92z"/></svg>,
  WA: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9s-.5-.1-.7.2-.7.9-.9 1.1c-.2.2-.3.2-.6.1-.3-.1-1.2-.5-2.3-1.4-.9-.8-1.4-1.7-1.6-2-.2-.3 0-.5.1-.6.1-.1.3-.3.4-.5.1-.2.2-.3.3-.5.1-.2 0-.4 0-.5s-.7-1.7-1-2.3c-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.8.4-.3.3-1 1-1 2.4s1 2.8 1.2 3 2.1 3.2 5 4.5c.7.3 1.3.5 1.7.6.7.2 1.4.2 1.9.1.6-.1 1.7-.7 2-1.4.2-.7.2-1.2.2-1.4-.1-.1-.3-.2-.6-.3zM12 2a10 10 0 0 0-8.5 15.2L2 22l4.9-1.3A10 10 0 1 0 12 2z"/></svg>,
  Check: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="20 6 9 17 4 12"/></svg>,
  Star: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="currentColor" {...p}><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26"/></svg>,
  Arrow: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>,
  Shield: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><polyline points="9 12 11 14 15 10"/></svg>,
  Drop: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M12 2.5s-7 8-7 13a7 7 0 0 0 14 0c0-5-7-13-7-13z"/></svg>,
  Pipe: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 7h6v4H7v6H3z"/><path d="M21 17h-6v-4h2V7h4z"/><path d="M9 11h6v2H9z"/></svg>,
  Wrench: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M14.7 6.3a4 4 0 0 0-5.4 5.4L3 18l3 3 6.3-6.3a4 4 0 0 0 5.4-5.4l-2.5 2.5-2-2z"/></svg>,
  Toilet: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M5 3h14v6H5z"/><path d="M6 9l2 8h8l2-8"/><circle cx="12" cy="13" r="2"/></svg>,
  Grid: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/></svg>,
  Bolt: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="currentColor" {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10"/></svg>,
  Water: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 12c2 0 2-2 4-2s2 2 4 2 2-2 4-2 2 2 4 2"/><path d="M3 18c2 0 2-2 4-2s2 2 4 2 2-2 4-2 2 2 4 2"/></svg>,
  Factory: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M3 21V10l5 3V10l5 3V7l8 5v9z"/><line x1="7" y1="17" x2="7" y2="18"/><line x1="11" y1="17" x2="11" y2="18"/><line x1="15" y1="17" x2="15" y2="18"/><line x1="19" y1="17" x2="19" y2="18"/></svg>,
  Plus: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" {...p}><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>,
  Lock: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>,
  Clock: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>,
  Truck: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="1" y="6" width="13" height="11"/><path d="M14 9h5l3 4v4h-8z"/><circle cx="5.5" cy="18.5" r="1.5"/><circle cx="18.5" cy="18.5" r="1.5"/></svg>,
  X: (p) => <svg {...ICON_BASE} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" {...p}><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>,
};

// ============================================================
// HELPERS
// ============================================================
const PHONE = "5562992910049";
const PHONE_DISPLAY = "(62) 99291-0049";

function waLink(message) {
  const m = message || "Olá! Preciso de atendimento urgente da Desentupidora Rei.";
  return `https://wa.me/${PHONE}?text=${encodeURIComponent(m)}`;
}

function maskPhone(v) {
  const d = v.replace(/\D/g, "").slice(0, 11);
  if (d.length <= 2) return d ? `(${d}` : "";
  if (d.length <= 7) return `(${d.slice(0,2)}) ${d.slice(2)}`;
  return `(${d.slice(0,2)}) ${d.slice(2,7)}-${d.slice(7)}`;
}

function StarRow({ n = 5 }) {
  return (
    <div className="stars">
      {Array.from({ length: n }).map((_, i) => <Icon.Star key={i} />)}
    </div>
  );
}

// ============================================================
// BUTTONS
// ============================================================
function WAButton({ children, message, size, block, kind = "wa" }) {
  const cls = `btn btn-${kind} ${size === "lg" ? "btn-lg" : ""} ${block ? "btn-block" : ""}`;
  return (
    <a className={cls} href={waLink(message)} target="_blank" rel="noopener">
      <Icon.WA />
      <span>{children}</span>
    </a>
  );
}

function CallButton({ children, kind = "ghost", size, block }) {
  const cls = `btn btn-${kind} ${size === "lg" ? "btn-lg" : ""} ${block ? "btn-block" : ""}`;
  return (
    <a className={cls} href={`tel:+${PHONE}`}>
      <Icon.Phone />
      <span>{children || `LIGAR ${PHONE_DISPLAY}`}</span>
    </a>
  );
}

// ============================================================
// TOPBAR
// ============================================================
function Topbar() {
  return (
    <header className="topbar">
      <div className="container topbar-inner">
        <a className="brand" href="#top">
          <div className="brand-crown"><img src="assets/logo-rei.webp" alt="" /></div>
          <div>
            <div className="brand-name">Desentupidora Rei</div>
            <div className="brand-sub">Goiânia · 24h</div>
          </div>
        </a>
        <div className="topbar-actions">
          <span className="badge-24h"><span className="live" />Atendimento 24h</span>
          <a className="tel-link" href={`tel:+${PHONE}`}>
            <span className="pulse-ring"><Icon.Phone /></span>
            <span>{PHONE_DISPLAY}</span>
          </a>
          <WAButton message="Olá! Preciso de atendimento urgente.">WhatsApp</WAButton>
        </div>
      </div>
    </header>
  );
}

// ============================================================
// ALERT BAR
// ============================================================
function AlertBar({ equipes = 3 }) {
  return (
    <div className="alert-bar">
      <span className="dot" />
      <span>Alerta de rota: <b>{equipes} equipes</b> em atendimento agora · Sua região ainda tem técnico disponível</span>
    </div>
  );
}

// Export to window for cross-file usage
Object.assign(window, { Icon, PHONE, PHONE_DISPLAY, waLink, maskPhone, StarRow, WAButton, CallButton, Topbar, AlertBar });
