"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/contexts/AuthContext";
import { productsApi } from "@/lib/api";
import ProductForm from "@/components/admin/ProductForm";

export default function NewProductPage() {
  const { token } = useAuth();
  const router = useRouter();
  const [isSubmitting, setIsSubmitting] = useState(false);

  const handleSubmit = async (data: any) => {
    if (!token) return;
    setIsSubmitting(true);

    try {
      const product = await productsApi.adminCreate(data, token);
      router.push(`/admin/products/${product.id}`);
    } catch (err) {
      alert("제품 생성에 실패했습니다.");
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <div>
      <h1 className="text-2xl font-bold text-gray-900 mb-8">새 제품 등록</h1>
      <ProductForm onSubmit={handleSubmit} isSubmitting={isSubmitting} />
    </div>
  );
}
