/* global React */
/* ============================================================
   Blog index — listing of articles
   - Mobile-first card grid
   - Featured (first article) takes a full-bleed hero card
   - Empty/upcoming state is honest (we have one article now)
   - Newsletter / social-connect CTA at the bottom
   ============================================================ */

window.BlogHero = function BlogHero({ lang }) {
  const t = lang === "it" ? {
    eyebrow: "Insights & News",
    h1a: "Idee, analisi,",
    h1b: "parliamo di...",
    lead: "Analisi del mercato, sintesi di ricerca e note operative dal nostro lavoro con le aziende italiane. Una vista di insieme oggi, verso il futuro.",
  } : {
    eyebrow: "Insights & News",
    h1a: "Ideas, analysis,",
    h1b: "signals from the field.",
    lead: "Original writing, research summaries and operational notes from our work with Italian companies. No \"AI trends\" — only what we actually observe.",
  };
  return (
    <div style={{ position: "relative", overflow: "hidden", color: "#fff", paddingTop: 160, paddingBottom: 96 }}>
      <div style={{ position: "absolute", inset: 0, background: "var(--tl-gradient-mesh)", zIndex: 0 }} />
      <window.Container style={{ position: "relative", zIndex: 2, maxWidth: 880 }}>
        <window.Eyebrow light>{t.eyebrow}</window.Eyebrow>
        <h1 style={{ fontSize: "clamp(36px, 5.5vw, 68px)", fontWeight: 800, lineHeight: 1.1, letterSpacing: "-0.03em", margin: 0 }}>
          <span style={{ display: "block", color: "#fff" }}>{t.h1a}</span>
          <span style={{ display: "block", paddingBottom: "0.12em", background: "linear-gradient(135deg,#7EE3FF 0%,#28D1FE 40%,#1E8BFF 100%)", WebkitBackgroundClip: "text", backgroundClip: "text", color: "transparent" }}>{t.h1b}</span>
        </h1>
        <p style={{ fontSize: "clamp(16px, 1.4vw, 19px)", lineHeight: 1.55, color: "rgba(255,255,255,0.78)", maxWidth: 640, marginTop: 24 }}>{t.lead}</p>
      </window.Container>
    </div>
  );
};

/* Featured card — large, full-width on mobile, 2-col on desktop */
window.BlogFeaturedCard = function BlogFeaturedCard({ article, lang }) {
  const t = lang === "it" ? { read: "Leggi l'articolo", min: "min", featured: "In evidenza" } : { read: "Read the article", min: "min", featured: "Featured" };
  const href = `blog-${article.slug}.html`;
  return (
    <a href={href} className="tl-blog-featured" style={{
      display: "grid",
      gridTemplateColumns: "1.05fr 1fr",
      gap: 0,
      background: "#fff",
      borderRadius: 24,
      overflow: "hidden",
      border: "1px solid var(--border)",
      boxShadow: "var(--shadow-sm)",
      color: "inherit",
      transition: "transform 280ms cubic-bezier(.22,1,.36,1), box-shadow 280ms cubic-bezier(.22,1,.36,1)",
    }}
    onMouseEnter={(e) => { e.currentTarget.style.transform = "translateY(-4px)"; e.currentTarget.style.boxShadow = "var(--shadow-lg)"; }}
    onMouseLeave={(e) => { e.currentTarget.style.transform = "translateY(0)"; e.currentTarget.style.boxShadow = "var(--shadow-sm)"; }}
    >
      {/* Visual */}
      {article.coverImage ? (
        <div style={{
          position: "relative",
          minHeight: 320,
          background: "#050E2B",
          overflow: "hidden",
          display: "flex",
          alignItems: "flex-end",
        }}>
          <img
            src={article.coverImage.src}
            alt={article.coverImage.alt[lang]}
            loading="lazy"
            style={{
              position: "absolute",
              inset: 0,
              width: "100%",
              height: "100%",
              objectFit: "cover",
              objectPosition: article.coverImage.focal || "center",
              display: "block",
            }}
          />
          {/* Bottom gradient + category chip so the label always reads */}
          <div style={{
            position: "absolute",
            inset: 0,
            background: "linear-gradient(180deg, rgba(5,14,43,0) 50%, rgba(5,14,43,0.72) 100%)",
            pointerEvents: "none",
          }} />
          <div style={{
            position: "relative",
            padding: 24,
            display: "inline-flex",
            alignItems: "center",
            gap: 8,
          }}>
            <span style={{
              display: "inline-flex", alignItems: "center", gap: 8,
              padding: "6px 12px",
              borderRadius: 999,
              background: "rgba(5,14,43,0.7)",
              backdropFilter: "blur(8px)",
              WebkitBackdropFilter: "blur(8px)",
              border: "1px solid rgba(126,227,255,.28)",
              fontFamily: "var(--font-mono)",
              fontSize: 11,
              fontWeight: 600,
              color: "#7EE3FF",
              textTransform: "uppercase",
              letterSpacing: ".12em",
            }}>
              <span style={{ width: 6, height: 6, borderRadius: 999, background: "#28D1FE" }} />
              {article.category[lang]}
            </span>
          </div>
        </div>
      ) : (
        <div style={{
          position: "relative",
          minHeight: 320,
          background: "linear-gradient(135deg,#050E2B 0%,#0228A3 50%,#1E8BFF 100%)",
          color: "#fff",
          display: "flex",
          alignItems: "flex-end",
          padding: 32,
          overflow: "hidden",
        }}>
          <svg style={{ position: "absolute", inset: 0, opacity: 0.18 }} viewBox="0 0 600 400" preserveAspectRatio="none">
            <defs><pattern id="fcg" width="40" height="40" patternUnits="userSpaceOnUse"><path d="M40 0H0V40" fill="none" stroke="#7EE3FF" strokeWidth="0.5" /></pattern></defs>
            <rect width="100%" height="100%" fill="url(#fcg)" />
          </svg>
          <div style={{ position: "absolute", top: 28, right: 28, width: 18, height: 18, borderRadius: 4, background: "linear-gradient(135deg,#1E8BFF,#28D1FE)", boxShadow: "0 8px 24px rgba(40,209,254,.5)" }} />
          <div style={{ position: "relative" }}>
            <div style={{
              fontFamily: "var(--font-mono)",
              fontSize: 80,
              fontWeight: 600,
              lineHeight: 1,
              background: "linear-gradient(135deg,#7EE3FF 0%,#28D1FE 60%,#1E8BFF 100%)",
              WebkitBackgroundClip: "text",
              backgroundClip: "text",
              color: "transparent",
              letterSpacing: "-0.02em",
            }}>{article.visualNumber}</div>
            <div style={{ marginTop: 12, fontSize: 12, fontFamily: "var(--font-mono)", color: "rgba(255,255,255,.7)", textTransform: "uppercase", letterSpacing: ".1em" }}>
              {article.category[lang]}
            </div>
          </div>
        </div>
      )}
      {/* Body */}
      <div style={{ padding: "32px clamp(24px, 3vw, 40px)", display: "flex", flexDirection: "column", justifyContent: "space-between", gap: 24 }}>
        <div>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 8, padding: "4px 10px", borderRadius: 999, background: "rgba(11,87,224,.10)", color: "var(--tl-blue-primary)", fontSize: 11, fontWeight: 600, letterSpacing: ".08em", textTransform: "uppercase", marginBottom: 16 }}>
            <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--tl-blue-primary)" }} />
            {t.featured}
          </div>
          <h2 style={{ fontSize: "clamp(22px, 2.4vw, 30px)", fontWeight: 700, letterSpacing: "-0.02em", lineHeight: 1.2, margin: 0, color: "var(--fg-1)" }}>
            {article.title[lang]}
          </h2>
          <p style={{ fontSize: 15, lineHeight: 1.6, color: "var(--fg-2)", marginTop: 14 }}>
            {article.excerpt[lang]}
          </p>
        </div>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", gap: 16, flexWrap: "wrap" }}>
          <div style={{ display: "flex", alignItems: "center", gap: 14, fontSize: 12.5, color: "var(--fg-3)", fontFamily: "var(--font-mono)" }}>
            <time dateTime={article.publishedISO}>{article.publishedLabel[lang]}</time>
            <span aria-hidden="true">·</span>
            <span>{article.readingMinutes} {t.min}</span>
          </div>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 6, fontSize: 13, fontWeight: 600, color: "var(--tl-blue-primary)" }}>
            {t.read} <window.Icon name="arrow" size={14} />
          </span>
        </div>
      </div>
      <style>{`
        @media (max-width: 820px) {
          .tl-blog-featured { grid-template-columns: 1fr !important; }
          .tl-blog-featured > div:first-child { min-height: 200px !important; }
        }
      `}</style>
    </a>
  );
};

/* Standard card — for upcoming/secondary articles */
window.BlogCard = function BlogCard({ article, lang }) {
  if (!article) return null;
  const href = `blog-${article.slug}.html`;
  const t = lang === "it" ? { read: "Leggi", min: "min" } : { read: "Read", min: "min" };
  return (
    <a href={href} className="tl-blog-card" style={{
      display: "flex", flexDirection: "column",
      background: "#fff", border: "1px solid var(--border)", borderRadius: 20,
      overflow: "hidden", color: "inherit", boxShadow: "var(--shadow-xs)",
      transition: "transform 240ms cubic-bezier(.22,1,.36,1), box-shadow 240ms cubic-bezier(.22,1,.36,1)",
    }}
    onMouseEnter={(e) => { e.currentTarget.style.transform = "translateY(-3px)"; e.currentTarget.style.boxShadow = "var(--shadow-md)"; }}
    onMouseLeave={(e) => { e.currentTarget.style.transform = "translateY(0)"; e.currentTarget.style.boxShadow = "var(--shadow-xs)"; }}
    >
      <div style={{
        height: 160,
        background: "linear-gradient(135deg,#050E2B 0%,#0228A3 70%,#1E8BFF 100%)",
        position: "relative",
        display: "flex",
        alignItems: "flex-end",
        padding: 20,
        color: "#fff",
        overflow: "hidden",
      }}>
        <svg style={{ position: "absolute", inset: 0, opacity: 0.15 }} viewBox="0 0 200 100" preserveAspectRatio="none">
          <defs><pattern id={`bcg-${article.slug}`} width="20" height="20" patternUnits="userSpaceOnUse"><path d="M20 0H0V20" fill="none" stroke="#7EE3FF" strokeWidth="0.4" /></pattern></defs>
          <rect width="100%" height="100%" fill={`url(#bcg-${article.slug})`} />
        </svg>
        <div style={{
          position: "relative",
          fontFamily: "var(--font-mono)", fontSize: 12, color: "rgba(255,255,255,.8)",
          textTransform: "uppercase", letterSpacing: ".1em",
        }}>{article.category[lang]}</div>
      </div>
      <div style={{ padding: 22, display: "flex", flexDirection: "column", flex: 1, gap: 14 }}>
        <h3 style={{ fontSize: 18, fontWeight: 700, letterSpacing: "-0.01em", lineHeight: 1.3, margin: 0, color: "var(--fg-1)" }}>
          {article.title[lang]}
        </h3>
        <p style={{ fontSize: 14, lineHeight: 1.6, color: "var(--fg-2)", margin: 0, flex: 1 }}>
          {article.excerpt[lang]}
        </p>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", paddingTop: 10, borderTop: "1px solid var(--border)" }}>
          <div style={{ fontSize: 12, color: "var(--fg-3)", fontFamily: "var(--font-mono)" }}>
            <time dateTime={article.publishedISO}>{article.publishedLabel[lang]}</time> · {article.readingMinutes} {t.min}
          </div>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 13, fontWeight: 600, color: "var(--tl-blue-primary)" }}>
            {t.read} <window.Icon name="arrow" size={12} />
          </span>
        </div>
      </div>
    </a>
  );
};

/* Empty/upcoming card — honest placeholder, not fake content */
window.BlogUpcomingCard = function BlogUpcomingCard({ lang }) {
  const t = lang === "it" ? {
    eyebrow: "Prossimamente",
    title: "Altri articoli in arrivo.",
    body: "Stiamo scrivendo. Iscriviti su LinkedIn per essere avvisato quando pubblichiamo.",
  } : {
    eyebrow: "Coming soon",
    title: "More articles coming.",
    body: "We're writing. Follow us on LinkedIn to be notified when we publish.",
  };
  return (
    <div className="tl-blog-card" style={{
      display: "flex", flexDirection: "column",
      background: "var(--bg-subtle)",
      border: "1px dashed var(--tl-grey-300)",
      borderRadius: 20,
      padding: 28, gap: 12,
      minHeight: 260,
      justifyContent: "center",
    }}>
      <div style={{
        width: 44, height: 44, borderRadius: 12,
        background: "rgba(11,87,224,.10)",
        display: "inline-flex", alignItems: "center", justifyContent: "center",
        color: "var(--tl-blue-primary)",
        marginBottom: 6,
      }}>
        <window.Icon name="sparkles" size={20} />
      </div>
      <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, fontWeight: 600, color: "var(--tl-blue-primary)", textTransform: "uppercase", letterSpacing: ".12em" }}>{t.eyebrow}</div>
      <h3 style={{ fontSize: 18, fontWeight: 700, letterSpacing: "-0.01em", lineHeight: 1.3, margin: 0, color: "var(--fg-1)" }}>{t.title}</h3>
      <p style={{ fontSize: 14, lineHeight: 1.6, color: "var(--fg-2)", margin: 0 }}>{t.body}</p>
      <a href="https://www.linkedin.com/company/tecnolife-theinnovationfactory" target="_blank" rel="noopener noreferrer" style={{
        display: "inline-flex", alignItems: "center", gap: 8,
        fontSize: 13, fontWeight: 600, color: "var(--tl-blue-primary)",
        marginTop: 6,
      }}>
        <window.Icon name="linkedin" size={14} /> {lang === "it" ? "Seguici su LinkedIn" : "Follow on LinkedIn"}
      </a>
    </div>
  );
};

window.BlogList = function BlogList({ lang }) {
  const articles = window.BLOG_ARTICLES || [];
  const featured = articles[0];
  const rest = articles.slice(1);
  return (
    <window.Section style={{ background: "var(--bg)", paddingTop: "clamp(48px, 6vw, 80px)" }}>
      <window.Container>
        {featured && <window.BlogFeaturedCard article={featured} lang={lang} />}
        <div className="tl-blog-grid" style={{
          marginTop: 32,
          display: "grid",
          gridTemplateColumns: "repeat(3, 1fr)",
          gap: 24,
        }}>
          {rest.map((a) => <window.BlogCard key={a.slug} article={a} lang={lang} />)}
          {/* Always show at least one upcoming card while we have <4 articles */}
          {articles.length < 4 && <window.BlogUpcomingCard lang={lang} />}
          {articles.length < 3 && <window.BlogUpcomingCard lang={lang} />}
          {articles.length < 2 && <window.BlogUpcomingCard lang={lang} />}
        </div>
      </window.Container>
      <style>{`
        @media (max-width: 980px) { .tl-blog-grid { grid-template-columns: repeat(2, 1fr) !important; } }
        @media (max-width: 640px) { .tl-blog-grid { grid-template-columns: 1fr !important; } }
      `}</style>
    </window.Section>
  );
};

/* Social/CTA band — replaces the standard HomeCTA: this is the
   "connect with us on social" angle the blog hub is designed around. */
window.BlogSocialCTA = function BlogSocialCTA({ lang }) {
  const t = lang === "it" ? {
    eyebrow: "Resta in contatto",
    title: "I prossimi articoli arrivano prima su LinkedIn.",
    body: "Pubblichiamo analisi, segnali dal campo e annunci dal nostro team. Seguici per riceverli appena escono.",
    linkedin: "Seguici su LinkedIn",
    contact: "Parla con noi",
  } : {
    eyebrow: "Stay in touch",
    title: "New articles drop first on LinkedIn.",
    body: "We post analysis, field signals and team announcements. Follow us to catch them as they go live.",
    linkedin: "Follow on LinkedIn",
    contact: "Talk to us",
  };
  return (
    <window.Section style={{ background: "var(--bg)", paddingTop: 0 }}>
      <window.Container>
        <div className="tl-blog-cta" style={{
          background: "linear-gradient(135deg,#050E2B 0%,#0228A3 50%,#0B57E0 100%)",
          borderRadius: 28,
          padding: "clamp(36px, 5vw, 64px)",
          color: "#fff",
          display: "grid",
          gridTemplateColumns: "1.3fr 1fr",
          gap: 40,
          alignItems: "center",
          position: "relative",
          overflow: "hidden",
        }}>
          <svg style={{ position: "absolute", inset: 0, opacity: 0.16 }} viewBox="0 0 600 300" preserveAspectRatio="none">
            <defs><pattern id="bctag" width="40" height="40" patternUnits="userSpaceOnUse"><path d="M40 0H0V40" fill="none" stroke="#7EE3FF" strokeWidth="0.5" /></pattern></defs>
            <rect width="100%" height="100%" fill="url(#bctag)" />
          </svg>
          <div style={{ position: "relative" }}>
            <window.Eyebrow light>{t.eyebrow}</window.Eyebrow>
            <h2 style={{ fontSize: "clamp(26px,3.2vw,40px)", fontWeight: 700, letterSpacing: "-0.02em", lineHeight: 1.15, margin: 0 }}>
              {t.title}
            </h2>
            <p style={{ fontSize: 16, color: "rgba(255,255,255,.78)", marginTop: 14, lineHeight: 1.55, maxWidth: 520 }}>
              {t.body}
            </p>
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 12, position: "relative" }}>
            <window.Button size="lg" variant="glass-light" href="https://www.linkedin.com/company/tecnolife-theinnovationfactory" icon={<window.Icon name="arrow-up-right" size={16} />}>
              {t.linkedin}
            </window.Button>
            <window.Button size="lg" variant="glass" href="contatti.html" icon={<window.Icon name="arrow" size={16} />}>
              {t.contact}
            </window.Button>
          </div>
        </div>
        <style>{`
          @media (max-width: 820px) { .tl-blog-cta { grid-template-columns: 1fr !important; gap: 24px !important; } }
        `}</style>
      </window.Container>
    </window.Section>
  );
};
