import { Category, Product, Supplier } from "@prisma/client";
import { ProductImageField } from "@/components/admin/ProductImageField";
import { productImages } from "@/lib/utils";

export function ProductForm({
  action,
  product,
  suppliers,
  categories
}: {
  action: (form: FormData) => void;
  product?: Product;
  suppliers: Supplier[];
  categories: Category[];
}) {
  const image = product ? productImages(product)[0] : "/products/gold-bar.svg";
  return (
    <form action={action} className="grid gap-5">
      <section className="panel grid gap-4 p-5 md:grid-cols-2">
        <h2 className="text-xl font-black md:col-span-2">Product Details</h2>
        <label>Name<input name="name" defaultValue={product?.name} required /></label>
        <label>Slug<input name="slug" defaultValue={product?.slug} required /></label>
        <label>Metal<select name="metalType" defaultValue={product?.metalType || "GOLD"}><option value="GOLD">Gold</option><option value="SILVER">Silver</option></select></label>
        <label>Product Type<select name="productType" defaultValue={product?.productType || "BAR"}><option value="COIN">Coin</option><option value="BAR">Bar</option><option value="ROUND">Round</option><option value="COLLECTIBLE">Collectible</option><option value="DIGITAL">Digital Request</option></select></label>
        <label>Item Classification<select name="itemKind" defaultValue={product?.itemKind || "PHYSICAL_BULLION"}><option value="PHYSICAL_BULLION">Physical Bullion</option><option value="DIGITAL_REQUEST">Digital Request</option><option value="SERVICE">Service</option></select></label>
        <label>Weight / Gramaj<input name="weight" defaultValue={product?.weight} required /></label>
        <label>Purity / Ayar<input name="purity" defaultValue={product?.purity} required /></label>
        <label>Karat optional<input name="karat" defaultValue={product?.karat || ""} /></label>
        <label>Brand / Mint<input name="brandMint" defaultValue={product?.brandMint || product?.mintBrand || ""} required /></label>
        <input type="hidden" name="mintBrand" value={product?.mintBrand || product?.brandMint || "Generic Bullion"} />
        <label>Internal Supplier<select name="supplierId" defaultValue={product?.supplierId}>{suppliers.map((s) => <option key={s.id} value={s.id}>{s.name}</option>)}</select></label>
        <label>Category<select name="categoryId" defaultValue={product?.categoryId || ""}><option value="">None</option>{categories.map((c) => <option key={c.id} value={c.id}>{c.name}</option>)}</select></label>
        <label>Stock Quantity<input name="stockQuantity" type="number" defaultValue={product?.stockQuantity || 10} required /></label>
        <label>Stock Status<select name="stockStatus" defaultValue={product?.stockStatus || product?.inventoryStatus || "IN_STOCK"}><option value="IN_STOCK">In Stock</option><option value="LOW_STOCK">Low Stock</option><option value="OUT_OF_STOCK">Out of Stock</option></select></label>
        <ProductImageField initialValue={image} />
        <label className="md:col-span-2">Short Description<input name="shortDescription" defaultValue={product?.shortDescription || ""} /></label>
        <label className="md:col-span-2">Long Description<textarea name="description" defaultValue={product?.description} required /></label>
      </section>

      <section className="panel grid gap-4 p-5 md:grid-cols-2">
        <h2 className="text-xl font-black md:col-span-2">Manual Pricing</h2>
        <label>Pricing Mode<select name="pricingMode" defaultValue={product?.pricingMode || "DIRECT_MANUAL"}><option value="DIRECT_MANUAL">Direct Manual Price</option><option value="SUPPLIER_MARKUP">Supplier + Markup Price</option></select></label>
        <label>Manual Customer Price<input name="manualPrice" type="number" step="0.01" defaultValue={product ? String(product.manualPrice || product.customerPrice || product.totalSalePrice) : "100"} /></label>
        <label>Supplier Price<input name="supplierPrice" type="number" step="0.01" defaultValue={product ? String(product.supplierPrice) : "0"} /></label>
        <label>Markup Percent<input name="markupPercent" type="number" step="0.01" defaultValue={product ? String(product.markupPercent) : "10"} /></label>
        <p className="text-sm text-[#666] md:col-span-2">If Direct Manual Price is selected, the customer-facing price equals Manual Customer Price. If Supplier + Markup is selected, customer price is calculated from supplier price and markup percent.</p>
      </section>

      <section className="panel grid gap-4 p-5 md:grid-cols-2">
        <h2 className="text-xl font-black md:col-span-2">Recommendation Engine</h2>
        <label>Premium %<input name="premiumPercent" type="number" min="0" step="0.01" defaultValue={product ? String(product.premiumPercent) : "0"} /></label>
        <label>Spread %<input name="spreadPercent" type="number" min="0" step="0.01" defaultValue={product ? String(product.spreadPercent) : "0"} /></label>
        <label>Liquidity Score<select name="liquidityScore" defaultValue={String(product?.liquidityScore || 3)}>{[1, 2, 3, 4, 5].map((score) => <option key={score} value={score}>{score}</option>)}</select></label>
        <label>Shipping Speed Score<select name="shippingSpeedScore" defaultValue={String(product?.shippingSpeedScore || 3)}>{[1, 2, 3, 4, 5].map((score) => <option key={score} value={score}>{score}</option>)}</select></label>
        <label className="md:col-span-2">Recommendation Notes<textarea name="recommendationNotes" defaultValue={product?.recommendationNotes || ""} /></label>
        <p className="text-sm text-[#666] md:col-span-2">Used for storefront recommendation scoring. Not financial advice.</p>
      </section>

      <section className="panel grid gap-4 p-5 md:grid-cols-2">
        <h2 className="text-xl font-black md:col-span-2">Publishing & SEO</h2>
        <label className="flex-row items-center gap-2"><input className="h-4 min-h-0 w-4" type="checkbox" name="isIraEligible" defaultChecked={product?.isIraEligible} /> IRA Eligible</label>
        <label className="flex-row items-center gap-2"><input className="h-4 min-h-0 w-4" type="checkbox" name="isFeatured" defaultChecked={product?.isFeatured} /> Featured</label>
        <label>SEO Title<input name="seoTitle" defaultValue={product?.seoTitle || ""} /></label>
        <label>SEO Description<input name="seoDescription" defaultValue={product?.seoDescription || ""} /></label>
      </section>

      <button className="btn btn-primary w-fit">Save Product</button>
    </form>
  );
}
