// ============================================================
// AgenticX — Página comercial: seções de layout + PricingPage
// ============================================================

// ---------- Hero ----------
const PricingHero = ({ annual, setAnnual }) => (
  <section id="precos" data-screen-label="P01 Hero Preços" className="relative pt-16 sm:pt-20 pb-4">
    <Container>
      <div className="max-w-3xl">
        <Eyebrow>Planos e preços</Eyebrow>
        <h1 className="mt-5 text-[2.75rem] leading-[1.05] sm:text-6xl lg:text-[4.25rem] lg:leading-[1.02] font-medium text-black">
          Atendimento com IA,<br/>Kanban e suporte<br/>que <span className="font-em text-forest">escala</span> com você
        </h1>
        <p className="mt-7 max-w-xl text-base sm:text-lg text-slate leading-relaxed">
          Centralize seus canais, organize a operação e aumente a produtividade do time com automações, inteligência artificial e gestão visual. Escolha o plano adequado ao tamanho da sua operação.
        </p>
      </div>
      <div className="mt-10">
        <BillingToggle annual={annual} onChange={setAnnual}/>
      </div>
    </Container>
  </section>
);

// ---------- Plans grid ----------
const PlansGrid = ({ annual }) => (
  <section data-screen-label="P02 Planos" className="pt-10 pb-20 sm:pb-24">
    <Container>
      <div className="grid lg:grid-cols-3 gap-5 lg:gap-6 items-start">
        {PLANS.map((p) => <PlanCard key={p.id} plan={p} annual={annual}/>)}
      </div>
      <p className="mt-8 text-center text-xs text-slate">
        Ao selecionar o plano anual, os valores mensais exibidos correspondem à contratação por 12 meses.
      </p>
    </Container>
  </section>
);

// ---------- Plan infographics ----------
const INFOGRAPHICS = [
  { id: 'starter', name: 'Starter', src: 'assets/plano-starter.png' },
  { id: 'business', name: 'Business', src: 'assets/plano-business.png' },
  { id: 'enterprise', name: 'Enterprise', src: 'assets/plano-enterprise.png' },
];

const PlanInfographics = () => {
  const [active, setActive] = React.useState('business');
  const current = INFOGRAPHICS.find((p) => p.id === active) || INFOGRAPHICS[0];
  return (
    <section id="precos-detalhes" data-screen-label="P02b Infográficos" className="pb-20 sm:pb-24">
      <Container>
        <div className="max-w-2xl mb-9">
          <Eyebrow>Como cada plano funciona</Eyebrow>
          <h2 className="mt-4 text-3xl sm:text-4xl font-medium text-black leading-[1.1]">
            Veja cada plano em <span className="font-em text-forest">detalhe</span>
          </h2>
        </div>

        <div className="flex justify-center mb-8">
          <div className="relative inline-flex rounded-full bg-sage p-1 ring-1 ring-line">
            <span
              className="absolute top-1 bottom-1 rounded-full bg-forest transition-all duration-300 ease-out"
              style={{ left: `calc(${INFOGRAPHICS.findIndex((p) => p.id === active)} * (100% - 0.5rem) / 3 + 0.25rem)`, width: 'calc((100% - 0.5rem) / 3)' }}
            ></span>
            {INFOGRAPHICS.map((p) => {
              const on = p.id === active;
              return (
                <button key={p.id} onClick={() => setActive(p.id)}
                  className={`relative z-10 px-5 sm:px-8 py-2 text-sm font-medium rounded-full transition-colors ${on ? 'text-white' : 'text-forest/70 hover:text-forest'}`}>
                  {p.name}
                </button>
              );
            })}
          </div>
        </div>

        <div className="rounded-3xl border border-line bg-white overflow-hidden shadow-sm">
          <img key={current.id} src={current.src} alt={`Infográfico do plano ${current.name}`}
               className="w-full h-auto block fade-in-img" loading="lazy"/>
        </div>
      </Container>
    </section>
  );
};

// ---------- Comparison table ----------
const Cell = ({ value }) => {
  if (value === true) return <span className="inline-grid place-items-center h-6 w-6 rounded-full bg-sage text-forest"><ICheck size={13}/></span>;
  if (value === false) return <span className="text-line text-lg leading-none">—</span>;
  return <span className="text-sm text-black/80">{value}</span>;
};

const ComparisonTable = () => (
  <section id="precos-comparar" data-screen-label="P03 Comparativo" className="py-20 sm:py-24 bg-[#FAFAF8] border-y border-line">
    <Container>
      <div className="max-w-2xl mb-12">
        <Eyebrow>Comparativo</Eyebrow>
        <h2 className="mt-4 text-3xl sm:text-4xl font-medium text-black leading-[1.1]">
          Tudo o que cada<br/>plano <span className="font-em text-forest">inclui</span>
        </h2>
      </div>

      <div className="overflow-x-auto no-scrollbar -mx-6 px-6 sm:mx-0 sm:px-0">
        <table className="w-full min-w-[44rem] border-collapse">
          <thead>
            <tr className="sticky top-0">
              <th className="text-left align-bottom pb-5 pr-4 w-[30%]"></th>
              {PLANS.map((p) => (
                <th key={p.id} className="text-left align-bottom pb-5 px-4">
                  <div className={`text-lg font-medium ${p.highlight ? 'text-forest' : 'text-black'}`}>{p.name}</div>
                  <div className="text-xs text-slate mt-0.5">{p.fromPrefix ? 'a partir de ' : ''}{p.monthly}/mês</div>
                </th>
              ))}
            </tr>
          </thead>
          <tbody>
            {COMPARE_GROUPS.map((g, gi) => (
              <React.Fragment key={gi}>
                <tr>
                  <td colSpan={4} className="pt-7 pb-2.5">
                    <span className="eyebrow">{g.label}</span>
                  </td>
                </tr>
                {g.rows.map((r, ri) => (
                  <tr key={ri} className="border-t border-line">
                    <td className="py-3.5 pr-4 text-sm font-medium text-black">{r.f}</td>
                    {r.v.map((val, vi) => (
                      <td key={vi} className="py-3.5 px-4">
                        <Cell value={val}/>
                      </td>
                    ))}
                  </tr>
                ))}
              </React.Fragment>
            ))}
          </tbody>
        </table>
      </div>
    </Container>
  </section>
);

// ---------- Add-ons ----------
const AddOns = () => (
  <section data-screen-label="P04 Adicionais" className="py-20 sm:py-24">
    <Container>
      <div className="grid lg:grid-cols-2 gap-8 lg:gap-16 items-end mb-12">
        <div>
          <Eyebrow>Recursos adicionais</Eyebrow>
          <h2 className="mt-4 text-3xl sm:text-4xl font-medium text-black leading-[1.1]">
            Expanda sem trocar<br/>de <span className="font-em text-forest">plano</span>
          </h2>
        </div>
        <p className="text-slate text-base lg:pb-1">
          Cresça a operação conforme a demanda. Recursos adicionais são cobrados de forma recorrente sobre o seu plano atual.
        </p>
      </div>

      <div className="grid sm:grid-cols-2 gap-4">
        {ADDONS.map((a, i) => (
          <div key={i} className="flex items-start justify-between gap-6 rounded-2xl border border-line p-7 hover:border-forest transition-colors">
            <div className="max-w-sm">
              <h3 className="text-lg font-medium text-black">{a.title}</h3>
              <p className="mt-2 text-sm text-slate leading-relaxed">{a.note}</p>
            </div>
            <div className="text-right shrink-0">
              <div className="text-2xl font-medium text-forest tracking-tight">{a.price}</div>
              <div className="text-xs text-slate mt-0.5">{a.unit}</div>
            </div>
          </div>
        ))}
      </div>
    </Container>
  </section>
);

// ---------- Capabilities ----------
const Capabilities = () => (
  <section data-screen-label="P05 Recursos" className="py-20 sm:py-24 bg-[#FAFAF8] border-y border-line">
    <Container>
      <div className="max-w-2xl mb-12">
        <Eyebrow>O que está incluído</Eyebrow>
        <h2 className="mt-4 text-3xl sm:text-4xl font-medium text-black leading-[1.1]">
          Uma plataforma<br/>completa de <span className="font-em text-forest">operação</span>
        </h2>
      </div>

      <div className="grid sm:grid-cols-2 lg:grid-cols-3 gap-4">
        {CAPABILITIES.map((c, i) => {
          const I = c.I;
          return (
            <div key={i} className="value-card rounded-2xl border border-line bg-white p-7 flex flex-col">
              <div className="value-bg" style={{ background: '#EBE8D8', borderTopRightRadius: '6rem' }}></div>
              <div className="h-12 w-12 grid place-items-center rounded-full border border-line bg-white text-forest">
                <I size={20}/>
              </div>
              <h3 className="mt-5 text-lg font-medium text-black">{c.title}</h3>
              <p className="mt-1.5 text-sm text-slate">{c.text}</p>
              <ul className="mt-4 flex flex-wrap gap-x-2 gap-y-1.5">
                {c.items.map((it, j) => (
                  <li key={j} className="text-xs text-black/70 bg-sage/60 rounded-full px-3 py-1">{it}</li>
                ))}
              </ul>
            </div>
          );
        })}
      </div>
    </Container>
  </section>
);

// ---------- Important info ----------
const ImportantInfo = () => (
  <section data-screen-label="P06 Informações" className="py-20 sm:py-24">
    <Container>
      <div className="satin-soft rounded-[2rem] px-8 sm:px-12 py-12 sm:py-14">
        <div className="grid lg:grid-cols-[1fr_1.6fr] gap-10">
          <div>
            <Eyebrow className="!text-cream">Informações importantes</Eyebrow>
            <h2 className="mt-4 text-2xl sm:text-3xl font-medium text-white leading-snug">
              O que considerar<br/>antes de contratar
            </h2>
          </div>
          <ul className="grid sm:grid-cols-2 gap-x-8 gap-y-5">
            {IMPORTANT.map((t, i) => (
              <li key={i} className="flex items-start gap-3">
                <span className="mt-1 shrink-0 h-1.5 w-1.5 rounded-full bg-cream"></span>
                <span className="text-sm text-white/80 leading-relaxed">{t}</span>
              </li>
            ))}
          </ul>
        </div>
      </div>
    </Container>
  </section>
);

// ---------- Commercial FAQ ----------
const PricingFAQ = () => {
  const [open, setOpen] = React.useState(0);
  return (
    <section id="precos-faq" data-screen-label="P07 FAQ comercial" className="py-20 sm:py-24 bg-[#FAFAF8] border-y border-line">
      <Container>
        <div className="grid lg:grid-cols-[2.8fr_4.1fr] gap-12 lg:gap-24">
          <div>
            <Eyebrow>Dúvidas comerciais</Eyebrow>
            <h2 className="mt-4 text-3xl sm:text-4xl font-medium text-black leading-[1.1]">
              Perguntas<br/>frequentes
            </h2>
            <p className="mt-5 text-slate">Não encontrou o que procura? <a href="#contact" className="text-forest underline underline-offset-2">Fale com nosso time.</a></p>
          </div>
          <div>
            {PRICING_FAQS.map((f, i) => (
              <div key={i} className="border-b border-line">
                <button onClick={() => setOpen(open === i ? -1 : i)} className="w-full flex items-center justify-between gap-6 py-5 text-left">
                  <span className="text-lg sm:text-xl font-medium text-black">{f.q}</span>
                  <span className={`acc-plus shrink-0 text-slate ${open === i ? 'open' : ''}`}><IPlus size={20}/></span>
                </button>
                <div className={`acc-body ${open === i ? 'open' : ''}`}>
                  <div><p className="pb-5 max-w-xl text-slate leading-relaxed">{f.a}</p></div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
};

// ---------- Final CTA ----------
const PricingCTA = () => (
  <section data-screen-label="P08 CTA" className="py-20 sm:py-24">
    <Container>
      <div className="satin relative rounded-[2rem] overflow-hidden px-8 sm:px-14 py-16 sm:py-20 text-center">
        <div className="relative z-10 max-w-2xl mx-auto">
          <h2 className="text-3xl sm:text-5xl font-medium text-white leading-[1.08]">
            Pronto para organizar<br/>seu <span className="font-em text-cream">atendimento</span>?
          </h2>
          <p className="mt-5 text-white/75 text-lg max-w-md mx-auto">
            Comece no plano ideal para hoje e expanda quando precisar. Sem trocar de ferramenta.
          </p>
          <div className="mt-9 flex flex-wrap items-center justify-center gap-4">
            <a href="#contact" className="btn-slide group bg-white text-forest px-7 py-4 text-sm font-semibold hover:bg-cream">
              <span className="btn-arrow bg-forest text-white"><IArrow size={13}/></span>
              <span className="btn-label">Começar agora</span>
            </a>
            <a href="#contact" className="text-white/90 text-sm font-medium underline underline-offset-4 hover:text-white">Falar com vendas</a>
          </div>
        </div>
        <div className="hidden sm:block absolute top-12 left-16 text-white"><Sparkle size={52} color="#ffffff"/></div>
        <div className="hidden sm:block absolute bottom-12 right-20 text-white"><Sparkle size={36} color="#ffffff"/></div>
      </div>
    </Container>
  </section>
);

// ---------- Page ----------
const PricingPage = () => {
  const [annual, setAnnual] = React.useState(false);
  return (
    <main data-screen-label="Página comercial">
      <PricingHero annual={annual} setAnnual={setAnnual}/>
      <PlansGrid annual={annual}/>
      <PlanInfographics/>
      <ComparisonTable/>
      <AddOns/>
      <Capabilities/>
      <ImportantInfo/>
      <PricingFAQ/>
      <PricingCTA/>
    </main>
  );
};

Object.assign(window, {
  PricingHero, PlansGrid, PlanInfographics, ComparisonTable, AddOns, Capabilities,
  ImportantInfo, PricingFAQ, PricingCTA, PricingPage,
});
