import { getAdminUser } from "@/lib/admin-permissions";
import NewProductForm from "@/components/admin/NewProductForm";
import { PageHeader } from "@/components/admin/design-system";
import { Package as PackageIcon } from "lucide-react";
import { getBrands } from "@/actions/admin/users";
import { redirect } from "next/navigation";
import Link from "next/link";

export default async function NewProductPage() {
  const adminUser = await getAdminUser();
  if (!adminUser?.can_manage_products) redirect("/admin/pickup");

  // Resolve brand from the signed-in admin. For brand_admin / store_employee
  // this is their assigned brand. For platform_admin (brand_id === null) we
  // fetch the full brand list so they can choose.
  const isPlatformAdmin = !adminUser.brand_id;
  let brands: { id: string; name: string }[] = [];
  if (isPlatformAdmin) {
    const result = await getBrands();
    brands = result.brands ?? [];
  }

  return (
    <main className="min-h-screen bg-[var(--admin-bg)] px-4 sm:px-6 md:px-8 py-6 sm:py-8">
      <div className="mx-auto max-w-4xl">
        <div className="ha-eyebrow mb-2">Operations</div>
        <PageHeader
          icon={<PackageIcon className="h-5 w-5" strokeWidth={1.75} />}
          title="New product"
          subtitle="Add a product to your catalog with pricing, type, and availability."
        />

        <div className="mb-6">
          <Link
            href="/admin/products"
            className="text-sm text-[var(--admin-text-muted)] hover:text-[var(--admin-text-primary)] transition-colors"
          >
            ← Back to Products
          </Link>
        </div>

        <div className="rounded-2xl bg-[var(--admin-card-bg)] p-8 shadow-sm border border-[var(--admin-border)]">
          <NewProductForm
            defaultBrandId={adminUser.brand_id ?? ""}
            brands={brands}
            lockBrand={!isPlatformAdmin}
          />
        </div>
      </div>
    </main>
  );
}