import { createFileRoute, Link } from '@tanstack/react-router'
import {
  ArrowRight,
  ArrowUpRight,
  BadgeCheck,
  CheckCheck,
  Code2,
  CreditCard,
  KeyRound,
  RotateCcw,
  Send,
  ShieldCheck,
  Signal,
  Users,
  Zap,
} from 'lucide-react'
import { Button } from '@/components/ui/button'
import { PricingGrid } from '@/components/PricingGrid'
import { ApiCodeSamples } from '@/components/ApiCodeSamples'
import { ChatWidget } from '@/components/ChatWidget'
import { cn } from '@/lib/utils'

export const Route = createFileRoute('/')({
  component: Home,
})

const bubbles = [
  {
    from: 'GraceChurch',
    time: '7:02',
    text: 'Dear member, Sunday service starts 8:30am. God bless you!',
    delay: '0.5s',
  },
  {
    from: 'AmaFabrics',
    time: '9:41',
    text: 'Weekend sale! 20% off all kente & batik. Osu shop, Sat–Sun.',
    delay: '0.9s',
  },
  {
    from: 'StMarysJHS',
    time: '12:15',
    text: 'PTA meeting Friday 4pm at the school hall. Kindly attend.',
    delay: '1.3s',
  },
]

const audiences = [
  'Churches & mosques',
  'Basic & senior high schools',
  'Market traders',
  'Pharmacies',
  'Susu groups',
  'Hotels & guesthouses',
  'Clinics',
  'Event planners',
  'Old students’ unions',
  'Logistics & dispatch',
]

function Home() {
  return (
    <>
      <Hero />
      <Audiences />
      <HowItWorks />
      <Features />
      <DeveloperApi />
      <Pricing />
      <FinalCta />
      <ChatWidget />
    </>
  )
}

function Hero() {
  return (
    <section className="px-2 pt-2 sm:px-4">
      <div className="stage grain overflow-hidden rounded-[2rem] sm:rounded-[2.5rem]">
        <div className="aurora" aria-hidden />
        <div
          className="line-grid absolute inset-0 -z-10 [mask-image:radial-gradient(ellipse_70%_60%_at_70%_40%,black,transparent)]"
          aria-hidden
        />
        <div className="relative z-10 mx-auto grid max-w-content gap-16 px-5 pt-16 pb-10 sm:px-8 lg:grid-cols-[1.1fr_0.9fr] lg:items-center lg:pt-24">
          <div>
            <p className="glass rise inline-flex items-center gap-2.5 rounded-full py-1.5 pr-4 pl-2 text-sm font-medium text-white/85">
              <span className="pulse-dot ml-1.5 size-2 rounded-full bg-[var(--gold-glow)]" aria-hidden />
              Pay with MoMo or card · from GH₵0.02/SMS
            </p>
            <h1
              className="rise mt-7 font-display text-[3.2rem] leading-[0.95] font-extrabold tracking-[-0.035em] sm:text-7xl lg:text-[5.4rem]"
              style={{ animationDelay: '0.1s' }}
            >
              Bulk SMS that reaches <span className="text-gold">every phone</span>{' '}
              <span className="text-white/55">in Ghana.</span>
            </h1>
            <p className="rise mt-7 max-w-xl text-lg leading-relaxed text-white/70" style={{ animationDelay: '0.2s' }}>
              Buy SMS credit in seconds, paste your contact list and send announcements, promos and reminders under your
              own sender name — to MTN, Telecel and AirtelTigo numbers alike.
            </p>
            <div className="rise mt-9 flex flex-wrap gap-3" style={{ animationDelay: '0.3s' }}>
              <Link
                to="/signup"
                className="btn-gold group inline-flex h-13 items-center gap-2 rounded-full px-7 text-base font-bold outline-none focus-visible:ring-4 focus-visible:ring-white/40"
              >
                Start sending
                <ArrowRight className="size-4 transition-transform group-hover:translate-x-1" aria-hidden />
              </Link>
              <Link
                to="/"
                hash="pricing"
                className="glass inline-flex h-13 items-center gap-2 rounded-full px-7 text-base font-semibold text-white transition-colors hover:bg-white/15 focus-visible:ring-4 focus-visible:ring-white/30 outline-none"
              >
                See prices
              </Link>
            </div>
            <div
              className="rise mt-10 flex items-center gap-4 text-sm text-white/60"
              style={{ animationDelay: '0.4s' }}
            >
              <ShieldCheck className="size-5 text-[var(--gold-glow)]" aria-hidden />
              Secure checkout by Moolre · Unsent credits refunded automatically
            </div>
          </div>

          <PhoneMockup />
        </div>

        <div className="relative z-10 mx-auto max-w-content px-5 pb-8 sm:px-8">
          <dl className="glass grid grid-cols-3 divide-x divide-white/10 rounded-2xl">
            {[
              ['3', 'networks covered'],
              ['5,000', 'numbers per send'],
              ['GH₵0', 'monthly fees'],
            ].map(([value, label]) => (
              <div key={label} className="flex flex-col-reverse px-4 py-5 sm:px-8">
                <dt className="text-xs text-white/55 sm:text-sm">{label}</dt>
                <dd className="font-display text-2xl font-extrabold tabular-nums sm:text-4xl">{value}</dd>
              </div>
            ))}
          </dl>
        </div>
        <div className="kente-band relative z-10 h-2.5 opacity-90" aria-hidden />
      </div>
    </section>
  )
}

function PhoneMockup() {
  return (
    <div className="relative mx-auto w-full max-w-[22rem] lg:mr-4">
      <div
        className="absolute -inset-10 rounded-full bg-[radial-gradient(circle,var(--gold-glow),transparent_65%)] opacity-35 blur-2xl"
        aria-hidden
      />
      <div className="float-slow relative [--tilt:2deg]">
        <div className="rounded-[2.8rem] bg-gradient-to-b from-white/25 via-white/5 to-white/15 p-[1.5px] shadow-[0_40px_80px_-20px_rgba(0,0,0,0.6)]">
          <div className="rounded-[2.75rem] bg-[var(--forest-950)] p-2.5">
            <div className="overflow-hidden rounded-[2.2rem] bg-gradient-to-b from-[oklch(0.985_0.01_85)] to-[oklch(0.95_0.02_85)] text-foreground">
              <div className="flex items-center justify-between px-6 pt-3 pb-1 text-[11px] font-semibold">
                <span>9:41</span>
                <span className="h-5 w-20 rounded-full bg-[var(--forest-950)]" aria-hidden />
                <Signal className="size-3.5" aria-hidden />
              </div>
              <div className="px-4 pt-3 pb-5">
                <p className="px-1 font-display text-2xl font-extrabold">Messages</p>
                <ul className="mt-4 grid gap-3" aria-label="Example messages">
                  {bubbles.map((b) => (
                    <li key={b.from} className="bubble-in flex gap-3" style={{ animationDelay: b.delay }}>
                      <span
                        className="grid size-9 shrink-0 place-items-center rounded-full bg-gradient-to-br from-primary to-[oklch(0.5_0.12_165)] text-xs font-bold text-primary-foreground"
                        aria-hidden
                      >
                        {b.from.slice(0, 2).toUpperCase()}
                      </span>
                      <div className="min-w-0">
                        <p className="flex items-baseline justify-between gap-2 px-1 text-xs">
                          <span className="font-bold">{b.from}</span>
                          <span className="text-muted-foreground">{b.time}</span>
                        </p>
                        <p className="mt-1 rounded-2xl rounded-tl-sm bg-white px-3.5 py-2.5 text-[13px] leading-snug shadow-sm ring-1 ring-black/5">
                          {b.text}
                        </p>
                      </div>
                    </li>
                  ))}
                </ul>
                <div className="mt-5 flex items-center justify-between rounded-2xl bg-gradient-to-r from-primary to-[oklch(0.42_0.1_165)] px-4 py-3 text-primary-foreground shadow-lg shadow-primary/30">
                  <span className="text-sm font-semibold">Sent to 2,418 contacts</span>
                  <Send className="size-4" aria-hidden />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div className="glass float-slower absolute top-16 -left-6 hidden items-center gap-3 rounded-2xl px-4 py-3 text-white shadow-xl sm:flex [--tilt:-4deg] lg:-left-16">
        <span className="grid size-9 place-items-center rounded-xl bg-[var(--emerald-glow)]/30">
          <CheckCheck className="size-5 text-[oklch(0.85_0.12_160)]" aria-hidden />
        </span>
        <span>
          <span className="block text-xs text-white/60">Delivered</span>
          <span className="font-display text-lg font-bold tabular-nums">2,407</span>
        </span>
      </div>
      <div className="glass float-slow absolute -right-4 bottom-24 hidden items-center gap-3 rounded-2xl px-4 py-3 text-white shadow-xl sm:flex [--tilt:3deg] lg:-right-10">
        <Zap className="size-5 text-[var(--gold-glow)]" aria-hidden />
        <span>
          <span className="block text-xs text-white/60">Bulk rate</span>
          <span className="font-display text-lg font-bold">GH₵0.02/SMS</span>
        </span>
      </div>
    </div>
  )
}

function Audiences() {
  const row = [...audiences, ...audiences]
  return (
    <section aria-label="Who uses AkotoCreative SMS" className="relative overflow-hidden py-10">
      <p className="mb-5 text-center text-xs font-bold tracking-[0.25em] text-muted-foreground uppercase">
        Made for announcements from
      </p>
      <div className="[mask-image:linear-gradient(90deg,transparent,black_12%,black_88%,transparent)]">
        <ul className="marquee gap-3">
          {row.map((a, i) => (
            <li
              key={i}
              aria-hidden={i >= audiences.length || undefined}
              className="flex shrink-0 items-center gap-3 rounded-full border bg-card px-5 py-2.5 font-display text-sm font-bold whitespace-nowrap shadow-deep"
            >
              <span className="size-2 rounded-full bg-gradient-to-br from-[var(--gold-glow)] to-[var(--ember-glow)]" />
              {a}
            </li>
          ))}
        </ul>
      </div>
    </section>
  )
}

function SectionLabel({ children }: { children: React.ReactNode }) {
  return (
    <p className="inline-flex items-center gap-2 font-display text-xs font-bold tracking-[0.25em] text-primary uppercase">
      <span className="h-px w-8 bg-gradient-to-r from-transparent to-primary" aria-hidden />
      {children}
    </p>
  )
}

function HowItWorks() {
  const steps = [
    {
      icon: CreditCard,
      title: 'Buy credit',
      text: 'Pick a bundle and pay with Mobile Money or card through Moolre’s secure checkout.',
    },
    {
      icon: Users,
      title: 'Add your contacts',
      text: 'Paste numbers from Excel or WhatsApp. We clean up 024…, +233… and 233… formats for you.',
    },
    {
      icon: Send,
      title: 'Hit send',
      text: 'Your message goes out under your sender name. Undelivered batches are refunded automatically.',
    },
  ]
  return (
    <section id="how" className="mx-auto max-w-content scroll-mt-20 px-4 py-20 sm:px-6 lg:py-28">
      <div className="grid gap-14 lg:grid-cols-[0.8fr_1.2fr] lg:gap-20">
        <div className="lg:sticky lg:top-28 lg:self-start">
          <SectionLabel>How it works</SectionLabel>
          <h2 className="mt-4 text-4xl font-extrabold sm:text-5xl lg:text-6xl">
            From top-up to <span className="text-forest">delivered</span> in under a minute.
          </h2>
          <p className="mt-5 max-w-md text-muted-foreground">
            No contracts, no setup calls. Create an account, fund it with MoMo and your first campaign can be on its way
            before your tea gets cold.
          </p>
        </div>
        <ol className="relative grid gap-5">
          <span
            className="absolute top-8 bottom-8 left-[2.15rem] w-px bg-gradient-to-b from-[var(--gold-glow)] via-primary to-transparent"
            aria-hidden
          />
          {steps.map((step, i) => (
            <li
              key={step.title}
              className={cn(
                'group relative grid grid-cols-[auto_1fr] gap-5 rounded-3xl border bg-card p-6 shadow-deep transition-all duration-300 hover:-translate-y-1 hover:border-primary/30 sm:p-7',
                i === 1 && 'lg:ml-10',
                i === 2 && 'lg:ml-20',
              )}
            >
              <span className="relative grid size-11 place-items-center rounded-2xl bg-gradient-to-br from-primary to-[oklch(0.45_0.11_165)] text-primary-foreground shadow-lg shadow-primary/25 transition-transform group-hover:rotate-[-6deg]">
                <step.icon className="size-5" aria-hidden />
              </span>
              <div>
                <p className="font-display text-xs font-bold tracking-widest text-muted-foreground">STEP 0{i + 1}</p>
                <h3 className="mt-1 text-2xl font-bold">{step.title}</h3>
                <p className="mt-2 text-muted-foreground">{step.text}</p>
              </div>
              <span
                className="pointer-events-none absolute top-4 right-6 font-display text-7xl font-extrabold text-foreground/[0.04]"
                aria-hidden
              >
                0{i + 1}
              </span>
            </li>
          ))}
        </ol>
      </div>
    </section>
  )
}

function Features() {
  return (
    <section className="px-2 sm:px-4">
      <div className="stage grain overflow-hidden rounded-[2rem] sm:rounded-[2.5rem]">
        <div className="aurora opacity-60" aria-hidden />
        <div className="relative z-10 mx-auto max-w-content px-5 py-20 sm:px-8 lg:py-28">
          <div className="max-w-2xl">
            <p className="inline-flex items-center gap-2 font-display text-xs font-bold tracking-[0.25em] text-[var(--gold-glow)] uppercase">
              <span className="h-px w-8 bg-gradient-to-r from-transparent to-[var(--gold-glow)]" aria-hidden />
              Why AkotoCreative
            </p>
            <h2 className="mt-4 text-4xl font-extrabold sm:text-5xl">
              Built for how Ghana <span className="text-gold">actually</span> communicates.
            </h2>
          </div>

          <div className="mt-14 grid gap-4 md:grid-cols-6">
            <article className="glass gradient-border rounded-3xl p-7 md:col-span-4 md:row-span-2">
              <Signal className="size-7 text-[var(--gold-glow)]" aria-hidden />
              <h3 className="mt-5 text-3xl font-bold">Every network, one dashboard</h3>
              <p className="mt-3 max-w-md text-white/65">
                Reach MTN, Telecel and AirtelTigo subscribers with a single send. We sort the numbers, you write the
                message.
              </p>
              <div className="mt-10 grid gap-3">
                {[
                  {
                    name: 'MTN',
                    share: 68,
                    color: 'from-[oklch(0.86_0.16_95)] to-[var(--gold-glow)]',
                  },
                  {
                    name: 'Telecel',
                    share: 21,
                    color: 'from-[var(--ember-glow)] to-[oklch(0.58_0.2_25)]',
                  },
                  {
                    name: 'AirtelTigo',
                    share: 11,
                    color: 'from-[oklch(0.7_0.1_200)] to-[var(--emerald-glow)]',
                  },
                ].map((n) => (
                  <div key={n.name} className="grid grid-cols-[6rem_1fr_3rem] items-center gap-3 text-sm">
                    <span className="font-semibold">{n.name}</span>
                    <span className="h-2.5 overflow-hidden rounded-full bg-white/10">
                      <span
                        className={cn('block h-full rounded-full bg-gradient-to-r', n.color)}
                        style={{ width: `${n.share}%` }}
                      />
                    </span>
                    <span className="text-right text-white/55 tabular-nums">{n.share}%</span>
                  </div>
                ))}
                <p className="text-xs text-white/40">Example split for a 2,418-contact church list</p>
              </div>
            </article>

            <article className="glass rounded-3xl p-7 md:col-span-2">
              <BadgeCheck className="size-7 text-[var(--gold-glow)]" aria-hidden />
              <h3 className="mt-5 text-xl font-bold">Your own sender ID</h3>
              <p className="mt-2 text-sm text-white/65">
                Messages show your brand name (up to 11 characters) instead of a random number.
              </p>
              <div className="mt-5 inline-flex items-center gap-2 rounded-xl bg-white/10 px-3 py-2 font-mono text-sm">
                <span className="size-2 rounded-full bg-[oklch(0.8_0.15_150)]" aria-hidden />
                AmaFabrics
              </div>
            </article>

            <article className="glass rounded-3xl p-7 md:col-span-2">
              <RotateCcw className="size-7 text-[var(--gold-glow)]" aria-hidden />
              <h3 className="mt-5 text-xl font-bold">Nothing is wasted</h3>
              <p className="mt-2 text-sm text-white/65">
                If a batch can’t reach the network, its credits go straight back to your balance.
              </p>
            </article>

            <article className="glass rounded-3xl p-7 md:col-span-3">
              <h3 className="text-xl font-bold">Honest counting</h3>
              <p className="mt-2 text-sm text-white/65">
                See exactly how many SMS parts and credits a message uses before you send it.
              </p>
              <div className="mt-6 flex items-end gap-1.5" aria-hidden>
                {[100, 100, 64].map((h, i) => (
                  <span key={i} className="flex-1 rounded-lg bg-white/10 p-1">
                    <span
                      className="block rounded-md bg-gradient-to-t from-[var(--emerald-glow)] to-[var(--gold-glow)]"
                      style={{ height: `${h * 0.4}px` }}
                    />
                  </span>
                ))}
              </div>
              <p className="mt-3 font-mono text-xs text-white/50">412 chars · 3 SMS · GSM-7</p>
            </article>

            <article className="glass rounded-3xl p-7 md:col-span-3">
              <h3 className="text-xl font-bold">Pay the Ghanaian way</h3>
              <p className="mt-2 text-sm text-white/65">
                Top up with Mobile Money or a bank card through Moolre. Credits land as soon as the payment is
                confirmed.
              </p>
              <div className="mt-6 flex flex-wrap gap-2">
                {['MTN MoMo', 'Telecel Cash', 'AirtelTigo Money', 'Card'].map((p) => (
                  <span
                    key={p}
                    className="rounded-full border border-white/15 px-3 py-1 text-xs font-semibold text-white/80"
                  >
                    {p}
                  </span>
                ))}
              </div>
            </article>
          </div>
        </div>
      </div>
    </section>
  )
}

function DeveloperApi() {
  const points = [
    {
      icon: KeyRound,
      title: 'Your own API key',
      text: 'Create keys in your dashboard and revoke them any time. Each key spends from your normal credit balance.',
    },
    {
      icon: Send,
      title: 'One call to send',
      text: 'POST a sender ID, a message and up to 5,000 numbers. We clean up the numbers and send them in batches.',
    },
    {
      icon: RotateCcw,
      title: 'Same fair rules',
      text: 'Same prices as the dashboard, and failed batches are refunded. Check your balance and results through the API.',
    },
  ]
  return (
    <section id="developers" className="mx-auto max-w-content scroll-mt-20 px-4 py-24 sm:px-6 lg:py-32">
      <div className="grid gap-14 lg:grid-cols-[0.9fr_1.1fr] lg:items-center lg:gap-16">
        <div>
          <SectionLabel>Developer API</SectionLabel>
          <h2 className="mt-4 text-4xl font-extrabold sm:text-5xl">
            Send SMS from <span className="text-forest">your own website</span> or app.
          </h2>
          <p className="mt-5 max-w-lg text-muted-foreground">
            Send order confirmations, OTPs, school results or appointment reminders automatically. Connect your shop,
            school portal or booking system to AkotoCreative SMS with a simple REST API.
          </p>
          <ul className="mt-9 grid gap-5">
            {points.map((p) => (
              <li key={p.title} className="grid grid-cols-[auto_1fr] gap-4">
                <span className="grid size-10 place-items-center rounded-xl bg-primary/10 text-primary ring-1 ring-primary/15">
                  <p.icon className="size-5" aria-hidden />
                </span>
                <span>
                  <strong className="block font-display text-lg">{p.title}</strong>
                  <span className="text-sm text-muted-foreground">{p.text}</span>
                </span>
              </li>
            ))}
          </ul>
          <div className="mt-10 flex flex-wrap gap-3">
            <Button asChild className="h-12 rounded-full px-6">
              <Link to="/developers">
                <Code2 /> Read the API docs
              </Link>
            </Button>
            <Button asChild variant="outline" className="h-12 rounded-full px-6">
              <Link to="/dashboard" hash="api-keys">
                <KeyRound /> Get an API key
              </Link>
            </Button>
          </div>
        </div>
        <ApiCodeSamples />
      </div>
    </section>
  )
}

function Pricing() {
  return (
    <section id="pricing" className="relative mx-auto max-w-content scroll-mt-20 px-4 py-24 sm:px-6 lg:py-32">
      <div
        className="pointer-events-none absolute inset-x-0 top-20 -z-10 mx-auto h-96 max-w-3xl rounded-full bg-[radial-gradient(closest-side,color-mix(in_oklch,var(--gold-glow)_22%,transparent),transparent)] blur-2xl"
        aria-hidden
      />
      <div className="mb-16 flex flex-col gap-6 md:flex-row md:items-end md:justify-between">
        <div className="max-w-2xl">
          <SectionLabel>Pricing</SectionLabel>
          <h2 className="mt-4 text-4xl font-extrabold sm:text-5xl lg:text-6xl">
            Pay as you go. <span className="text-forest">In cedis.</span>
          </h2>
        </div>
        <p className="max-w-sm text-muted-foreground">
          One credit sends one SMS (160 characters) to one number. Bigger bundles, cheaper messages.
        </p>
      </div>
      <PricingGrid
        renderAction={(pkg) =>
          pkg.popular ? (
            <Link
              to="/buy"
              search={{ package: pkg.id }}
              className="btn-gold flex h-11 w-full items-center justify-center gap-2 rounded-full text-sm font-bold outline-none focus-visible:ring-4 focus-visible:ring-white/40"
            >
              Buy {pkg.name} <ArrowUpRight className="size-4" aria-hidden />
            </Link>
          ) : (
            <Button asChild className="h-11 w-full rounded-full">
              <Link to="/buy" search={{ package: pkg.id }}>
                Buy {pkg.name}
              </Link>
            </Button>
          )
        }
      />
    </section>
  )
}

function FinalCta() {
  return (
    <section className="px-2 sm:px-4">
      <div className="stage grain relative overflow-hidden rounded-[2rem] sm:rounded-[2.5rem]">
        <div className="aurora" aria-hidden />
        <div className="relative z-10 mx-auto flex max-w-content flex-col items-start gap-8 px-5 py-16 sm:px-8 md:flex-row md:items-center md:justify-between lg:py-20">
          <h2 className="max-w-2xl text-4xl font-extrabold sm:text-5xl">
            Your next announcement is <span className="text-gold">one paste away.</span>
          </h2>
          <Link
            to="/signup"
            className="btn-gold group inline-flex h-14 shrink-0 items-center gap-2 rounded-full px-8 text-base font-bold outline-none focus-visible:ring-4 focus-visible:ring-white/40"
          >
            Create free account
            <ArrowRight className="size-4 transition-transform group-hover:translate-x-1" aria-hidden />
          </Link>
        </div>
      </div>
    </section>
  )
}
