"use client";

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

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

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

    try {
      const solution = await solutionsApi.adminCreate(data, token);
      router.push(`/admin/solutions/${solution.id}`);
    } catch (err) {
      alert("솔루션 생성에 실패했습니다.");
    } finally {
      setIsSubmitting(false);
    }
  };

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