import { prisma } from '@/lib/prisma';
import { isAdminAuthenticated } from '@/lib/adminAuth';
import AdminNav from '@/components/AdminNav';
import ShippingRatesForm from '@/components/ShippingRatesForm';
import Link from 'next/link';

export default async function ShippingRatesPage() {
  const isAdmin = await isAdminAuthenticated();
  if (!isAdmin) {
    return <div className="px-6 py-12 text-center">Unauthorized</div>;
  }

  const shippingRates = await prisma.shippingRate.findMany({
    orderBy: { isInternational: 'asc' },
  });

  return (
    <div className="mx-auto max-w-4xl px-6 py-12">
      <AdminNav />
      <div className="flex items-baseline justify-between gap-3">
        <h1 className="font-display text-3xl">Shipping Rates</h1>
        <Link href="/admin/dashboard" className="text-sm text-clay hover:text-clay-dark">
          Back to dashboard
        </Link>
      </div>

      <p className="mt-2 text-sm text-muted">
        Manage Royal Mail shipping rates. Changes apply immediately to checkout.
      </p>

      <ShippingRatesForm existingRates={shippingRates} />
    </div>
  );
}
