import { BetaSignup } from './beta-signup'

async function getStarCount(): Promise<string> {
  try {
    const res = await fetch('https://api.github.com/repos/GreyhavenHQ/greywall', {
      next: { revalidate: 3600 },
      headers: { Accept: 'application/vnd.github+json' },
    })
    if (!res.ok) return '138 stars'
    const data = await res.json()
    return typeof data.stargazers_count === 'number' ? `${data.stargazers_count} stars` : '138 stars'
  } catch {
    return '138 stars'
  }
}

export async function Hero() {
  const stars = await getStarCount()
  const badges = [
    { href: 'https://github.com/GreyhavenHQ/greywall', label: 'GitHub', value: stars },
    { href: 'https://github.com/GreyhavenHQ/greywall/blob/main/LICENSE', label: 'License', value: 'Apache-2.0' },
    { href: 'https://github.com/GreyhavenHQ/greywall/releases', label: 'Release', value: 'v0.3.1' },
    { href: 'https://github.com/GreyhavenHQ/greywall', label: 'Go', value: 'v1.25' },
    { href: 'https://www.producthunt.com/products/greywall?launch=greywall', label: 'Product Hunt', value: 'Greywall' },
  ]
  return (
    <section className="relative w-full px-4 pt-8 pb-6 sm:px-6 sm:pt-10 sm:pb-8">
      <div className="relative z-10 mx-auto max-w-5xl text-center">
        <div className="flex flex-col items-center gap-3">
          <div className="w-full text-center">
            <h1 className="title-serif mx-auto w-full max-w-[980px] text-[40px] leading-[1.0] tracking-normal font-semibold md:text-[72px]">
              Contain and observe AI agents without friction.
            </h1>
          </div>

          <div className="text-center">
            <p className="text-serif mx-auto max-w-[380px] text-[18px] leading-[1.2] font-semibold tracking-[-2.2%] text-greyhaven-grey7 md:max-w-[720px] md:text-[28px]">
              Default-deny filesystem, network, and command controls around AI agents on Linux and macOS, with records of what they tried to do.
            </p>
          </div>

          <div className="mt-1 flex flex-wrap items-center justify-center gap-2">
            {badges.map((badge) => (
              <a
                key={badge.label}
                href={badge.href}
                target="_blank"
                rel="noopener noreferrer"
                className="inline-flex items-center overflow-hidden rounded-md border border-border/60 bg-card/90 text-[13px] leading-none transition-colors hover:border-primary/30 md:text-[14px]"
              >
                <span className="bg-greyhaven-grey8 px-3 py-1.5 font-sans text-greyhaven-offwhite md:px-3.5 md:py-2">
                  {badge.label}
                </span>
                <span className="bg-primary px-3 py-1.5 font-sans text-primary-foreground md:px-3.5 md:py-2">
                  {badge.value}
                </span>
              </a>
            ))}
          </div>

          <BetaSignup
            subject="Greywall managed governance inquiry (hero)"
            message="(hero governance CTA)"
            inputClassName="w-56 rounded-md border border-border/40 bg-background/40 px-4 py-2.5 text-sm font-sans text-foreground placeholder:text-muted-foreground/60 transition-colors focus:border-primary/40 focus:outline-none sm:w-64"
            submitClassName="px-4 py-2.5 font-sans text-sm font-medium md:text-sm"
            helperTextClassName="text-xs text-muted-foreground/60 font-serif"
            wrapperClassName="mt-2 flex flex-col items-center gap-1.5"
          />
        </div>
      </div>
    </section>
  )
}
