'use client' import { useState } from 'react' import { useParams } from 'next/navigation' import { INTERVIEW_QUESTIONS, answersToNarrativeText, type InterviewAnswer, type InterviewQuestion } from './_types' type InputMode = 'interview' | 'wizard' | 'form' export default function IACEInterviewPage() { const { projectId } = useParams<{ projectId: string }>() const [mode, setMode] = useState('interview') const [answers, setAnswers] = useState([]) const [currentQ, setCurrentQ] = useState(0) const [currentSection, setCurrentSection] = useState(1) const [analyzing, setAnalyzing] = useState(false) const [result, setResult] = useState(null) const [inputValue, setInputValue] = useState('') const [multiValue, setMultiValue] = useState([]) const setAnswer = (qId: string, value: string | string[] | number) => { setAnswers(prev => { const existing = prev.findIndex(a => a.questionId === qId) if (existing >= 0) { prev[existing].value = value; return [...prev] } return [...prev, { questionId: qId, value }] }) } const getAnswer = (qId: string) => answers.find(a => a.questionId === qId)?.value || '' const handleAnalyze = async () => { setAnalyzing(true) const narrativeText = answersToNarrativeText(answers) try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/parse-narrative`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ narrative_text: narrativeText }), }) if (res.ok) setResult(await res.json()) } catch { /* ignore */ } setAnalyzing(false) } const q = INTERVIEW_QUESTIONS[currentQ] const sections = [...new Set(INTERVIEW_QUESTIONS.map(q => q.section))] const sectionQuestions = (s: number) => INTERVIEW_QUESTIONS.filter(q => q.section === s) // Interview mode: advance to next question const handleInterviewNext = () => { if (q.type === 'multiselect') { setAnswer(q.id, multiValue); setMultiValue([]) } else if (inputValue) { setAnswer(q.id, inputValue); setInputValue('') } if (currentQ < INTERVIEW_QUESTIONS.length - 1) setCurrentQ(currentQ + 1) } return (
{/* Mode Switcher */}

CE-Risikobeurteilung — Datenerfassung

{([['interview', 'Interview'], ['wizard', 'Wizard'], ['form', 'Formular']] as [InputMode, string][]).map(([m, label]) => ( ))}
{/* Result */} {result && (

Analyse-Ergebnis (deterministisch)

{result.components?.length || 0}
Komponenten
{result.suggested_hazards?.length || 0}
Gefahren
{result.matched_patterns || 0}
Patterns
{result.energy_sources?.length || 0}
Energiequellen
{result.suggested_hazards?.length > 0 && (

Erkannte Gefahren:

{result.suggested_hazards.map((h: any, i: number) => (
= 90 ? 'bg-red-100 text-red-700' : h.priority >= 70 ? 'bg-orange-100 text-orange-700' : 'bg-yellow-100 text-yellow-700'}`}>P{h.priority} {h.pattern_name || h.category} {h.category}
))}
)}
)} {/* ═══════════════ INTERVIEW MODE ═══════════════ */} {mode === 'interview' && !result && (
{/* Previous answers (chat history) */}
{INTERVIEW_QUESTIONS.slice(0, currentQ).map((pq, i) => { const ans = getAnswer(pq.id) if (!ans || (Array.isArray(ans) && ans.length === 0)) return null return (
{pq.question}
{Array.isArray(ans) ? ans.join(', ') : String(ans)}
) })}
{/* Current question */} {currentQ < INTERVIEW_QUESTIONS.length && (
Frage {currentQ + 1}/{INTERVIEW_QUESTIONS.length} — {q.sectionTitle}
{q.question}
{q.helpText &&

{q.helpText}

} {q.type === 'text' && ( setInputValue(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleInterviewNext()} placeholder={q.placeholder} className="w-full px-4 py-2 border border-gray-200 rounded-lg text-sm focus:ring-2 focus:ring-purple-500" autoFocus /> )} {q.type === 'textarea' && (