import React, { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Badge } from '@/components/ui/badge' import { Card, CardContent } from '@/components/ui/card' import { Progress } from '@/components/ui/progress' import { Wand2, ArrowRight, ArrowLeft, CheckCircle, Brain, Tags, FolderTree, Sparkles, FileText, Code, Database, Star, Lightbulb, Target, Clock, X } from 'lucide-react' interface MemoryCreationWizardProps { open: boolean onOpenChange: (open: boolean) => void onCreateMemory: (content: string, metadata: MemoryMetadata) => Promise availableProjects: string[] availableTags: string[] recentCategories?: string[] } interface MemoryMetadata { category?: string project?: string tags: string[] priority?: 'low' | 'medium' | 'high' contentType?: 'text' | 'code' | 'structured' title?: string summary?: string } interface MemoryTemplate { id: string name: string icon: React.ReactNode description: string contentTemplate: string suggestedTags: string[] category: string contentType: 'text' | 'code' | 'structured' } const memoryTemplates: MemoryTemplate[] = [ { id: 'meeting-notes', name: 'Meeting Notes', icon: , description: 'Capture important points from meetings', contentTemplate: `# Meeting Notes - {{date}} ## Attendees - ## Key Points - ## Action Items - [ ] ## Next Steps - `, suggestedTags: ['meeting', 'notes'], category: 'work', contentType: 'structured' }, { id: 'code-snippet', name: 'Code Snippet', icon: , description: 'Save useful code snippets', contentTemplate: `# {{title}} ## Description Brief description of what this code does ## Code \`\`\`{{language}} // Your code here \`\`\` ## Usage How to use this code snippet ## Notes - Any important notes or considerations`, suggestedTags: ['code', 'snippet'], category: 'code', contentType: 'code' }, { id: 'idea', name: 'Idea / Insight', icon: , description: 'Capture creative ideas and insights', contentTemplate: `# {{title}} ## The Idea Describe your idea or insight here ## Context What led to this idea? ## Potential Applications - How could this be used? - What problems might it solve? ## Next Steps - [ ] Research further - [ ] Discuss with team - [ ] Create prototype`, suggestedTags: ['idea', 'insight'], category: 'research', contentType: 'structured' }, { id: 'research-notes', name: 'Research Notes', icon: , description: 'Document research findings', contentTemplate: `# Research: {{topic}} ## Objective What are you trying to learn or understand? ## Key Findings - ## Sources - ## Conclusions What did you learn? How will you apply this? ## Follow-up Questions - `, suggestedTags: ['research', 'notes'], category: 'research', contentType: 'structured' }, { id: 'quick-note', name: 'Quick Note', icon: , description: 'Simple text note', contentTemplate: '', suggestedTags: ['note'], category: 'personal', contentType: 'text' }, { id: 'decision-log', name: 'Decision Log', icon: , description: 'Document important decisions', contentTemplate: `# Decision: {{title}} ## Context What situation led to this decision? ## Options Considered 1. Option A - pros/cons 2. Option B - pros/cons 3. Option C - pros/cons ## Decision What was decided and why? ## Impact Expected outcomes and next steps ## Review Date When should this decision be reviewed?`, suggestedTags: ['decision', 'log'], category: 'work', contentType: 'structured' } ] const STEPS = [ { id: 'template', title: 'Choose Template', icon: }, { id: 'content', title: 'Add Content', icon: }, { id: 'metadata', title: 'Organize', icon: }, { id: 'review', title: 'Review', icon: } ] export function MemoryCreationWizard({ open, onOpenChange, onCreateMemory, availableProjects, availableTags, recentCategories = [] }: MemoryCreationWizardProps) { const [currentStep, setCurrentStep] = useState(0) const [selectedTemplate, setSelectedTemplate] = useState(null) const [content, setContent] = useState('') const [metadata, setMetadata] = useState({ tags: [], priority: 'medium', contentType: 'text' }) const [tagInput, setTagInput] = useState('') const [isCreating, setIsCreating] = useState(false) const [suggestions, setSuggestions] = useState<{ category?: string tags: string[] contentType?: 'text' | 'code' | 'structured' }>({ tags: [] }) // Reset wizard when closed useEffect(() => { if (!open) { setCurrentStep(0) setSelectedTemplate(null) setContent('') setMetadata({ tags: [], priority: 'medium', contentType: 'text' }) setTagInput('') setSuggestions({ tags: [] }) } }, [open]) // Auto-detect content type and suggest categories useEffect(() => { if (content.trim()) { const newSuggestions = analyzeContent(content) setSuggestions(newSuggestions) // Auto-apply suggestions if not already set if (!metadata.category && newSuggestions.category) { setMetadata(prev => ({ ...prev, category: newSuggestions.category })) } if (!metadata.contentType && newSuggestions.contentType) { setMetadata(prev => ({ ...prev, contentType: newSuggestions.contentType })) } } }, [content, metadata.category, metadata.contentType]) const analyzeContent = (text: string) => { const lowerText = text.toLowerCase() const suggestions: any = { tags: [] } // Detect content type if (text.includes('```') || text.includes('function') || text.includes('const ') || text.includes('import ')) { suggestions.contentType = 'code' suggestions.tags.push('code') } else if (text.includes('##') || text.includes('- [ ]') || text.includes('###')) { suggestions.contentType = 'structured' } else { suggestions.contentType = 'text' } // Suggest category based on content if (lowerText.includes('meeting') || lowerText.includes('attendees') || lowerText.includes('action item')) { suggestions.category = 'work' suggestions.tags.push('meeting') } else if (lowerText.includes('bug') || lowerText.includes('error') || lowerText.includes('fix')) { suggestions.category = 'code' suggestions.tags.push('debugging') } else if (lowerText.includes('research') || lowerText.includes('study') || lowerText.includes('findings')) { suggestions.category = 'research' suggestions.tags.push('research') } else if (lowerText.includes('idea') || lowerText.includes('concept') || lowerText.includes('brainstorm')) { suggestions.category = 'research' suggestions.tags.push('idea') } // Extract hashtags const hashtagMatches = text.match(/#(\w+)/g) if (hashtagMatches) { suggestions.tags.push(...hashtagMatches.map(tag => tag.substring(1))) } return suggestions } const applyTemplate = (template: MemoryTemplate) => { setSelectedTemplate(template) const now = new Date() const templatedContent = template.contentTemplate .replace(/{{date}}/g, now.toLocaleDateString()) .replace(/{{title}}/g, metadata.title || 'Title') .replace(/{{topic}}/g, 'Topic') .replace(/{{language}}/g, 'javascript') setContent(templatedContent) setMetadata(prev => ({ ...prev, category: template.category, contentType: template.contentType, tags: [...new Set([...prev.tags, ...template.suggestedTags])] })) setCurrentStep(1) } const addTag = (tag: string) => { if (tag.trim() && !metadata.tags.includes(tag.trim())) { setMetadata(prev => ({ ...prev, tags: [...prev.tags, tag.trim()] })) } setTagInput('') } const removeTag = (tagToRemove: string) => { setMetadata(prev => ({ ...prev, tags: prev.tags.filter(tag => tag !== tagToRemove) })) } const handleNext = () => { if (currentStep < STEPS.length - 1) { setCurrentStep(currentStep + 1) } } const handlePrevious = () => { if (currentStep > 0) { setCurrentStep(currentStep - 1) } } const handleCreate = async () => { if (!content.trim()) return setIsCreating(true) try { await onCreateMemory(content, { ...metadata, title: extractTitle(content), summary: extractSummary(content) }) onOpenChange(false) } catch (error) { console.error('Failed to create memory:', error) } finally { setIsCreating(false) } } const extractTitle = (text: string): string => { const lines = text.split('\n').filter(line => line.trim()) const firstLine = lines[0]?.trim() if (firstLine?.startsWith('#')) { return firstLine.replace(/^#+\s*/, '').trim() } return firstLine?.substring(0, 60) + (firstLine?.length > 60 ? '...' : '') || 'Untitled Memory' } const extractSummary = (text: string): string => { const lines = text.split('\n').filter(line => line.trim() && !line.startsWith('#')) const firstParagraph = lines.slice(0, 3).join(' ').trim() return firstParagraph.substring(0, 150) + (firstParagraph.length > 150 ? '...' : '') } const canProceed = () => { switch (currentStep) { case 0: return true // Template selection is optional case 1: return content.trim().length > 0 case 2: return true // Metadata is optional case 3: return content.trim().length > 0 default: return false } } const renderStepContent = () => { switch (currentStep) { case 0: // Template Selection return (

Choose a Template

Select a template to get started quickly, or skip to create from scratch

{memoryTemplates.map((template) => ( applyTemplate(template)} >
{template.icon}

{template.name}

{template.description}

{template.suggestedTags.slice(0, 2).map(tag => ( {tag} ))}
))}
) case 1: // Content Creation return (

Add Your Content

{selectedTemplate ? `Using ${selectedTemplate.name} template` : 'Write your memory content'}

{selectedTemplate && ( {selectedTemplate.icon} {selectedTemplate.name} )}