import React, { useState } from 'react'; import { GoogleGenAI, Type } from "@google/genai"; import { Scenario } from '../types'; import { CBT_GLOSSARY } from '../constants'; import { Sparkles, X, Loader2, AlertCircle, Info, Target, UserCircle2, BrainCircuit, Check } from 'lucide-react'; interface ScenarioGeneratorProps { onGenerate: (scenario: Scenario) => void; onCancel: () => void; } const ScenarioGenerator: React.FC = ({ onGenerate, onCancel }) => { const [prompt, setPrompt] = useState(''); const [selectedConcepts, setSelectedConcepts] = useState([]); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const toggleConcept = (term: string) => { setSelectedConcepts(prev => prev.includes(term) ? prev.filter(t => t !== term) : [...prev, term] ); }; const handleGenerate = async () => { if (!prompt.trim() && selectedConcepts.length === 0) return; setIsLoading(true); setError(null); try { const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); const schema = { type: Type.OBJECT, properties: { id: { type: Type.STRING }, title: { type: Type.STRING }, description: { type: Type.STRING }, category: { type: Type.STRING, enum: ['Social Anxiety', 'Workplace', 'Relationships', 'Self-Talk'] }, difficulty: { type: Type.STRING, enum: ['Beginner', 'Intermediate', 'Advanced'] }, systemInstruction: { type: Type.STRING }, initialMessage: { type: Type.STRING }, cbtFocus: { type: Type.ARRAY, items: { type: Type.STRING } } }, required: ['id', 'title', 'description', 'category', 'difficulty', 'systemInstruction', 'initialMessage', 'cbtFocus'] }; const selectedContext = selectedConcepts.length > 0 ? `Focus specifically on these CBT concepts: ${selectedConcepts.join(', ')}.` : ''; const result = await ai.models.generateContent({ model: 'gemini-3-flash-preview', config: { responseMimeType: 'application/json', responseSchema: schema, systemInstruction: ` You are a Clinical CBT Scenario Architect. Your goal is to transform a user's prompt into a structured clinical simulation. CORE INSTRUCTIONS: 1. CLINICAL FOCUS: ${selectedContext} 2. CATEGORIZATION: You MUST assign the scenario to exactly one of the following categories based on its primary context: - 'Social Anxiety': Situations involving social performance, fear of judgment, or networking. - 'Workplace': Scenarios involving colleagues, supervisors, career growth, or professional conflict. - 'Relationships': Dynamics with family, friends, or romantic partners; boundary setting. - 'Self-Talk': Purely internal cognitive restructuring, rumination, or self-compassion work. 3. DIFFICULTY CALIBRATION: Assign a level based on cognitive load: - 'Beginner': Low stakes, high support, clear-cut distortions. - 'Intermediate': Ambiguous stakes, moderate social friction, subtle distortions. - 'Advanced': High emotional triggers, persistent clinical resistance from AI character, deep-seated patterns. 4. INTERNAL LOGIC: The 'systemInstruction' must include a detailed "INTERNAL CBT LOGIC" section. This tells the AI how to react to the user's progress or cognitive lapses. 5. CONSISTENCY: Ensure 'cbtFocus' includes the selected concepts. ` }, contents: [ { role: 'user', parts: [{ text: `Generate a clinical scenario for: ${prompt || 'A scenario exploring the chosen CBT concepts.'}` }] } ] }); if (result.text) { const scenario = JSON.parse(result.text) as Scenario; scenario.id = `custom-${Date.now()}-${scenario.id}`; onGenerate(scenario); } else { throw new Error("No content generated"); } } catch (err: any) { console.error("Generation error:", err); setError("Clinical scenario synthesis failed. Please refine your parameters."); } finally { setIsLoading(false); } }; const getConceptColor = (category: string, isSelected: boolean) => { if (!isSelected) return 'bg-white border-slate-200 text-slate-500 hover:border-slate-300'; switch (category) { case 'Distortion': return 'bg-rose-600 border-rose-600 text-white shadow-md shadow-rose-100'; case 'Technique': return 'bg-teal-600 border-teal-600 text-white shadow-md shadow-teal-100'; case 'Concept': return 'bg-amber-600 border-amber-600 text-white shadow-md shadow-amber-100'; default: return 'bg-slate-800 border-slate-800 text-white'; } }; return (
{/* Header */}

Scenario Synthesis

Construct an evidence-based clinical environment.

{/* Prompting Guide */}

Clinical Blueprint

Character Role

Define who the AI plays.

The Challenge

Describe the social friction.

CBT Focus

Target specific distortions.

{/* Clinical Targets (Selection) */}
{CBT_GLOSSARY.map(concept => { const isSelected = selectedConcepts.includes(concept.term); return ( ); })}