/* AmorSaúde — Agenda Aberta poster (Stories vertical + WhatsApp quadrado) */

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "formato": "Vertical (Stories)",
  "especialidade": "Oftalmologia",
  "dias": "08, 09, 10",
  "mes": "Junho",
  "urgencia": "Vagas para os próximos dias",
  "headline": "Agenda aberta",
  "cta": "Garanta seu horário e cuide da sua saúde!",
  "whatsapp": "(49) 99177-9006",
  "telefone": "(49) 3191-1077",
  "accent": "#E94B3C"
}/*EDITMODE-END*/;

const TEAL = "#1f9aa1";
const TEAL_DEEP = "#127077";
const INK = "#33474f";

/* ---- tiny inline icons (simple shapes only) ---- */
function CalIcon({ size = 46, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="3" y="4.5" width="18" height="16.5" rx="3.2" stroke={color} strokeWidth="1.9" />
      <line x1="3" y1="9.2" x2="21" y2="9.2" stroke={color} strokeWidth="1.9" />
      <line x1="7.5" y1="2.5" x2="7.5" y2="6" stroke={color} strokeWidth="1.9" strokeLinecap="round" />
      <line x1="16.5" y1="2.5" x2="16.5" y2="6" stroke={color} strokeWidth="1.9" strokeLinecap="round" />
    </svg>
  );
}
function WhatsIcon({ size = 34, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 3.2A8.8 8.8 0 0 0 4.3 16.4L3.3 20.7l4.45-1.02A8.8 8.8 0 1 0 12 3.2Z" stroke={color} strokeWidth="1.7" strokeLinejoin="round" />
      <path d="M9.2 8.2c.25-.02.5 0 .64.32.16.36.6 1.46.65 1.57.05.11.08.24 0 .38-.07.14-.34.5-.55.71-.1.1-.21.21-.09.42.13.21.57.93 1.25 1.5.86.74 1.57.96 1.79 1.07.15.06.27.05.38-.07.13-.15.55-.64.7-.86.13-.2.27-.16.45-.1.18.07 1.16.55 1.36.65.2.1.33.15.38.23.05.09.05.5-.12.97-.18.47-1.04.92-1.42.95-.38.04-.74.18-2.5-.53-2.12-.84-3.46-3.02-3.56-3.16-.1-.14-.85-1.13-.85-2.16 0-1.03.54-1.53.73-1.74.18-.21.4-.26.53-.26Z" fill={color} />
    </svg>
  );
}
function PhoneIcon({ size = 32, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M6.2 3.5h3l1.4 4-2 1.5a13 13 0 0 0 5.4 5.4l1.5-2 4 1.4v3c0 1-.8 1.8-1.8 1.7C12.6 23 2 13.4 2 5.3 2 4.3 2.8 3.5 3.8 3.5h2.4Z" stroke={color} strokeWidth="1.7" strokeLinejoin="round" />
    </svg>
  );
}
function AppIcon({ size = 32, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="6.5" y="2.5" width="11" height="19" rx="2.6" stroke={color} strokeWidth="1.7" />
      <line x1="10.5" y1="18.6" x2="13.5" y2="18.6" stroke={color} strokeWidth="1.7" strokeLinecap="round" />
    </svg>
  );
}
function GlobeIcon({ size = 32, color = "#fff" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="9" stroke={color} strokeWidth="1.7" />
      <ellipse cx="12" cy="12" rx="4" ry="9" stroke={color} strokeWidth="1.7" />
      <line x1="3.2" y1="9.2" x2="20.8" y2="9.2" stroke={color} strokeWidth="1.7" />
      <line x1="3.2" y1="14.8" x2="20.8" y2="14.8" stroke={color} strokeWidth="1.7" />
    </svg>
  );
}

function DayChip({ day, mes, accent, w = 176, dayFont = 82, mesFont = 24 }) {
  return (
    <div style={{
      width: w, borderRadius: 22, overflow: "hidden",
      background: "#fff", boxShadow: "0 5px 14px rgba(20,90,95,.14)",
      border: "1px solid rgba(18,112,119,.10)"
    }}>
      <div style={{
        background: accent, color: "#fff", textAlign: "center",
        fontWeight: 800, fontSize: mesFont, letterSpacing: 3, padding: "9px 0",
        textTransform: "uppercase"
      }}>{mes}</div>
      <div style={{
        textAlign: "center", color: TEAL_DEEP, fontWeight: 900,
        fontSize: dayFont, lineHeight: 1, padding: dayFont > 70 ? "12px 0 16px" : "10px 0 13px"
      }}>{day}</div>
    </div>
  );
}

function ContactRow({ icon, label, value, iconSize = 58, valueFont = 32, labelFont = 22 }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 18 }}>
      <div style={{
        width: iconSize, height: iconSize, borderRadius: iconSize * 0.28, flexShrink: 0,
        background: "rgba(255,255,255,.14)",
        display: "flex", alignItems: "center", justifyContent: "center"
      }}>{icon}</div>
      <div style={{ display: "flex", flexDirection: "column", lineHeight: 1.1 }}>
        <span style={{ fontSize: labelFont, fontWeight: 700, color: "rgba(255,255,255,.62)", letterSpacing: .3 }}>{label}</span>
        <span style={{ fontSize: valueFont, fontWeight: 800, color: "#fff" }}>{value}</span>
      </div>
    </div>
  );
}

function KickerPill({ text, fontSize = 28, pad = "15px 32px" }) {
  return (
    <div style={{
      display: "inline-flex", alignItems: "center", gap: 13,
      background: TEAL, color: "#fff", padding: pad, borderRadius: 999,
      fontSize: fontSize, fontWeight: 800, letterSpacing: 3.5, textTransform: "uppercase",
      boxShadow: "0 4px 10px rgba(20,90,95,.16)"
    }}>
      <span style={{ width: 13, height: 13, borderRadius: "50%", background: "#bff6db", boxShadow: "0 0 0 6px rgba(255,255,255,.18)" }} />
      {text}
    </div>
  );
}

function LogoBlock({ w }) {
  return (
    <img src="assets/amorsaude-logo.svg" alt="AmorSaúde"
      style={{ width: w, height: "auto", display: "block" }} />
  );
}

/* ---------- Vertical (Stories / Status) — 1080x1920 ---------- */
function VerticalPoster({ t, accent, days, mesAbbr }) {
  return (
    <React.Fragment>
      <div className="deco-blob" style={{ position: "absolute", top: -180, left: -160, width: 520, height: 520, borderRadius: "50%", background: "radial-gradient(circle, rgba(31,154,161,.16), rgba(31,154,161,0) 70%)" }} />
      <div className="deco-blob" style={{ position: "absolute", bottom: 520, right: -190, width: 460, height: 460, borderRadius: "50%", background: "radial-gradient(circle, rgba(233,75,60,.12), rgba(233,75,60,0) 70%)" }} />

      <div style={{
        position: "relative", width: "100%", height: "100%",
        display: "flex", flexDirection: "column", alignItems: "center",
        padding: "60px 80px 56px", textAlign: "center", justifyContent: "space-between"
      }}>
        <LogoBlock w={560} />

        <div style={{ marginTop: 16 }}><KickerPill text={t.urgencia} /></div>

        <div style={{ marginTop: 16, display: "flex", alignItems: "center", justifyContent: "center", gap: 24 }}>
          <CalIcon size={78} color={TEAL_DEEP} />
          <h1 style={{ fontWeight: 900, fontSize: 112, lineHeight: .92, letterSpacing: -2, color: TEAL_DEEP, textTransform: "lowercase" }}>{t.headline}</h1>
        </div>

        <div style={{ marginTop: 28, width: "100%" }}>
          <div style={{ fontSize: 28, fontWeight: 800, color: INK, letterSpacing: 5, textTransform: "uppercase", opacity: .7 }}>Atendimento em</div>
          <div style={{
            marginTop: 14, display: "inline-block", background: accent, color: "#fff", borderRadius: 26,
            padding: "22px 50px", fontSize: 66, fontWeight: 900, lineHeight: 1, textTransform: "uppercase", letterSpacing: -.5,
            boxShadow: "0 6px 15px rgba(170,45,36,.24)"
          }}>{t.especialidade}</div>
        </div>

        <div style={{ marginTop: 32, display: "flex", gap: 26, justifyContent: "center", flexWrap: "wrap" }}>
          {days.map((d, i) => <DayChip key={i} day={d} mes={mesAbbr} accent={accent} />)}
        </div>

        <p style={{ marginTop: 30, fontSize: 42, fontWeight: 800, color: TEAL_DEEP, lineHeight: 1.2, maxWidth: 820, textWrap: "balance" }}>{t.cta}</p>

        <div style={{
          marginTop: 30, width: "100%", background: "linear-gradient(160deg, #167a81 0%, #0f5c62 100%)",
          borderRadius: 38, padding: "38px 52px", boxShadow: "0 10px 26px rgba(15,92,98,.22)"
        }}>
          <div style={{
            display: "flex", alignItems: "center", justifyContent: "center", gap: 16,
            color: "#fff", fontSize: 33, fontWeight: 900, letterSpacing: 1,
            paddingBottom: 22, marginBottom: 24, borderBottom: "1.5px solid rgba(255,255,255,.16)"
          }}>Marque seu horário</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 20, alignItems: "flex-start", textAlign: "left" }}>
            <ContactRow icon={<WhatsIcon />} label="WhatsApp" value={t.whatsapp} />
            <ContactRow icon={<PhoneIcon />} label="Telefone fixo" value={t.telefone} />
            <ContactRow icon={<AppIcon />} label="Aplicativo" value="Cartão de TODOS" />
            <ContactRow icon={<GlobeIcon />} label="Site" value="www.amorsaude.com.br" />
          </div>
        </div>
      </div>
    </React.Fragment>
  );
}

/* ---------- Quadrado (WhatsApp) — 1080x1080 ---------- */
function SquarePoster({ t, accent, days, mesAbbr }) {
  return (
    <React.Fragment>
      <div className="deco-blob" style={{ position: "absolute", top: -150, left: -130, width: 380, height: 380, borderRadius: "50%", background: "radial-gradient(circle, rgba(31,154,161,.16), rgba(31,154,161,0) 70%)" }} />
      <div className="deco-blob" style={{ position: "absolute", bottom: -120, right: -120, width: 360, height: 360, borderRadius: "50%", background: "radial-gradient(circle, rgba(233,75,60,.12), rgba(233,75,60,0) 70%)" }} />

      <div style={{
        position: "relative", width: "100%", height: "100%",
        display: "flex", flexDirection: "column", alignItems: "center",
        padding: "34px 56px 34px", textAlign: "center", justifyContent: "space-between"
      }}>
        <LogoBlock w={364} pad={12} radius={22} />

        <div style={{ marginTop: 14 }}><KickerPill text={t.urgencia} fontSize={21} pad="12px 26px" /></div>

        <div style={{ marginTop: 14, display: "flex", alignItems: "center", justifyContent: "center", gap: 18 }}>
          <CalIcon size={56} color={TEAL_DEEP} />
          <h1 style={{ fontWeight: 900, fontSize: 78, lineHeight: .92, letterSpacing: -2, color: TEAL_DEEP, textTransform: "lowercase" }}>{t.headline}</h1>
        </div>

        <div style={{ marginTop: 22, width: "100%" }}>
          <div style={{ fontSize: 21, fontWeight: 800, color: INK, letterSpacing: 4, textTransform: "uppercase", opacity: .7 }}>Atendimento em</div>
          <div style={{
            marginTop: 10, display: "inline-block", background: accent, color: "#fff", borderRadius: 18,
            padding: "14px 38px", fontSize: 47, fontWeight: 900, lineHeight: 1, textTransform: "uppercase", letterSpacing: -.5,
            boxShadow: "0 6px 15px rgba(170,45,36,.24)"
          }}>{t.especialidade}</div>
        </div>

        <div style={{ marginTop: 24, display: "flex", gap: 18, justifyContent: "center", flexWrap: "wrap" }}>
          {days.map((d, i) => <DayChip key={i} day={d} mes={mesAbbr} accent={accent} w={132} dayFont={58} mesFont={18} />)}
        </div>

        <p style={{ marginTop: 24, fontSize: 31, fontWeight: 800, color: TEAL_DEEP, lineHeight: 1.18, maxWidth: 740, textWrap: "balance" }}>{t.cta}</p>

        <div style={{
          marginTop: 28, width: "100%", background: "linear-gradient(160deg, #167a81 0%, #0f5c62 100%)",
          borderRadius: 28, padding: "28px 44px", boxShadow: "0 10px 26px rgba(15,92,98,.22)"
        }}>
          <div style={{
            display: "flex", alignItems: "center", justifyContent: "center", gap: 12,
            color: "#fff", fontSize: 25, fontWeight: 900, letterSpacing: .5,
            paddingBottom: 16, marginBottom: 20, borderBottom: "1.5px solid rgba(255,255,255,.16)"
          }}>Marque seu horário</div>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "22px 30px", textAlign: "left" }}>
            <ContactRow icon={<WhatsIcon size={28} />} label="WhatsApp" value={t.whatsapp} iconSize={50} valueFont={26} labelFont={18} />
            <ContactRow icon={<PhoneIcon size={26} />} label="Telefone fixo" value={t.telefone} iconSize={50} valueFont={26} labelFont={18} />
            <ContactRow icon={<AppIcon size={26} />} label="Aplicativo" value="Cartão de TODOS" iconSize={50} valueFont={26} labelFont={18} />
            <ContactRow icon={<GlobeIcon size={26} />} label="Site" value="www.amorsaude.com.br" iconSize={50} valueFont={26} labelFont={18} />
          </div>
        </div>
      </div>
    </React.Fragment>
  );
}

/* ---- Persistência local (Cloudflare standalone, sem host de editor) ---- */
const LS_KEY = "amorsaude-agenda-tweaks";
function loadSavedTweaks() {
  try { return JSON.parse(localStorage.getItem(LS_KEY)) || {}; } catch (e) { return {}; }
}

function App() {
  const initialDefaults = React.useMemo(() => ({ ...TWEAK_DEFAULTS, ...loadSavedTweaks() }), []);
  const [t, setTweakRaw] = useTweaks(initialDefaults);
  // Persiste cada alteração no navegador para sobreviver ao reload.
  const setTweak = React.useCallback((keyOrEdits, val) => {
    setTweakRaw(keyOrEdits, val);
    try {
      const edits = (typeof keyOrEdits === 'object' && keyOrEdits !== null) ? keyOrEdits : { [keyOrEdits]: val };
      localStorage.setItem(LS_KEY, JSON.stringify({ ...loadSavedTweaks(), ...edits }));
    } catch (e) { /* ignore */ }
  }, [setTweakRaw]);
  const accent = t.accent || "#E94B3C";
  const mesAbbr = (t.mes || "").trim().slice(0, 3).toUpperCase();
  const days = (t.dias || "").split(",").map(d => d.trim()).filter(Boolean).slice(0, 4);
  const square = t.formato === "Quadrado (WhatsApp)";

  React.useLayoutEffect(() => {
    const p = document.getElementById('poster');
    if (!p) return;
    p.style.width = "1080px";
    p.style.height = square ? "1080px" : "1920px";
    if (window.fitPoster) window.fitPoster();
  }, [square]);

  return (
    <React.Fragment>
      {square
        ? <SquarePoster t={t} accent={accent} days={days} mesAbbr={mesAbbr} />
        : <VerticalPoster t={t} accent={accent} days={days} mesAbbr={mesAbbr} />}

      <TweaksPanel>
        <TweakSection label="Formato" />
        <TweakSelect label="Formato" value={t.formato}
          options={["Vertical (Stories)", "Quadrado (WhatsApp)"]}
          onChange={(v) => setTweak('formato', v)} />
        <TweakSection label="Conteúdo" />
        <TweakText label="Especialidade" value={t.especialidade} onChange={(v) => setTweak('especialidade', v)} />
        <TweakText label="Dias (separe por vírgula)" value={t.dias} onChange={(v) => setTweak('dias', v)} />
        <TweakText label="Mês" value={t.mes} onChange={(v) => setTweak('mes', v)} />
        <TweakSection label="Textos" />
        <TweakText label="Selo (topo)" value={t.urgencia} onChange={(v) => setTweak('urgencia', v)} />
        <TweakText label="Título" value={t.headline} onChange={(v) => setTweak('headline', v)} />
        <TweakText label="Chamada" value={t.cta} onChange={(v) => setTweak('cta', v)} />
        <TweakSection label="Contato" />
        <TweakText label="WhatsApp" value={t.whatsapp} onChange={(v) => setTweak('whatsapp', v)} />
        <TweakText label="Telefone fixo" value={t.telefone} onChange={(v) => setTweak('telefone', v)} />
        <TweakSection label="Cor de destaque" />
        <TweakColor label="Destaque" value={t.accent}
          options={["#E94B3C", "#1f9aa1", "#127077", "#f0883e"]}
          onChange={(v) => setTweak('accent', v)} />
      </TweaksPanel>
    </React.Fragment>
  );
}

ReactDOM.createRoot(document.getElementById('poster')).render(<App />);
