import { createFileRoute, Link, redirect } from '@tanstack/react-router'
import { Coins, Plus } from 'lucide-react'
import { Skeleton } from '@/components/ui/skeleton'
import { SmsComposer } from '@/components/SmsComposer'
import { getServerUser } from '@/lib/identity.functions'
import { getDashboard } from '@/lib/account.functions'

export const Route = createFileRoute('/send')({
  beforeLoad: async () => {
    const user = await getServerUser()
    if (!user) throw redirect({ to: '/login', search: { redirect: '/send' } })
  },
  loader: () => getDashboard(),
  head: () => ({ meta: [{ title: 'Send bulk SMS — AkotoCreative SMS' }] }),
  pendingComponent: SendSkeleton,
  component: SendPage,
})

function SendPage() {
  const data = Route.useLoaderData()
  return (
    <div className="pb-16">
      <section className="px-2 pt-2 sm:px-4">
        <div className="stage grain overflow-hidden rounded-[2rem]">
          <div className="aurora opacity-80" aria-hidden />
          <div className="relative z-10 mx-auto flex max-w-content flex-col gap-6 px-5 pt-10 pb-8 sm:px-8 lg:flex-row lg:items-end lg:justify-between">
            <div className="rise">
              <p className="text-sm text-white/65">
                <Link to="/dashboard" className="underline-offset-4 hover:underline">
                  Dashboard
                </Link>{' '}
                / Send SMS
              </p>
              <h1 className="mt-3 text-5xl font-extrabold tracking-[-0.03em] sm:text-6xl">
                Send <span className="text-gold">bulk SMS</span>
              </h1>
              <p className="mt-3 max-w-lg text-white/60">
                Write one message and send it to a single number or thousands at once. Paste numbers or import a
                CSV, check the cost, then confirm.
              </p>
            </div>
            <div className="glass rise flex items-center gap-4 rounded-2xl px-5 py-4" style={{ animationDelay: '0.15s' }}>
              <Coins className="size-6 text-[var(--gold-glow)]" aria-hidden />
              <div>
                <p className="font-display text-3xl font-extrabold tabular-nums">
                  <span className="text-gold">{data.credits.toLocaleString()}</span>
                </p>
                <p className="text-xs text-white/55">SMS credits available</p>
              </div>
              <Link
                to="/buy"
                className="btn-gold ml-2 inline-flex h-10 shrink-0 items-center gap-1.5 rounded-full px-4 text-sm font-bold outline-none focus-visible:ring-4 focus-visible:ring-white/40"
              >
                <Plus className="size-4" aria-hidden /> Top up
              </Link>
            </div>
          </div>
          <div className="kente-band relative z-10 h-2 opacity-90" aria-hidden />
        </div>
      </section>

      <div className="mx-auto mt-10 max-w-content px-4 sm:px-6">
        <SmsComposer credits={data.credits} defaultSenderId={data.senderId} senderIds={data.usableSenderIds} />
      </div>
    </div>
  )
}

function SendSkeleton() {
  return (
    <div className="px-2 pt-2 pb-16 sm:px-4" aria-busy>
      <Skeleton className="h-56 rounded-[2rem]" />
      <div className="mx-auto mt-10 grid max-w-content gap-6 px-2 lg:grid-cols-[1.35fr_0.65fr]">
        <Skeleton className="h-[36rem] rounded-[1.75rem]" />
        <Skeleton className="h-80 rounded-[1.75rem]" />
      </div>
    </div>
  )
}
