"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Sparkles, Plus, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; import { getRiskGrade } from "@/lib/constants"; import { addHazard } from "@/actions/risk-assessment"; interface SuggestedHazard { category: string; hazard: string; consequence: string; severity: number; likelihood: number; existingControls: string; } export function AiHazardPanel({ assessmentId }: { assessmentId: string }) { const router = useRouter(); const [activity, setActivity] = useState(""); const [loading, setLoading] = useState(false); const [suggestions, setSuggestions] = useState([]); const [error, setError] = useState(""); async function handleSuggest() { if (!activity.trim()) return; setLoading(true); setError(""); setSuggestions([]); try { const res = await fetch("/api/ai/suggest-hazards", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ activity }), }); const data = await res.json(); if (data.error) throw new Error(data.error); setSuggestions(data.hazards || []); } catch (err) { setError(err instanceof Error ? err.message : "AI 오류"); } setLoading(false); } async function handleAdd(hazard: SuggestedHazard) { await addHazard(assessmentId, { category: hazard.category, activity: activity, hazard: hazard.hazard, consequence: hazard.consequence, severity: hazard.severity, likelihood: hazard.likelihood, existingControls: hazard.existingControls, }); router.refresh(); } return ( AI 유해위험요인 추천