// Core memory interfaces
export interface MemoryUnit {
    id: string;
    type: MemoryType;
    content: MemoryContent;
    temporal: TemporalProperties;
    emotional: EmotionalProperties;
    connections: MemoryConnection[];
}

export type MemoryType = 'episodic' | 'semantic' | 'procedural' | 'sensory' | 'llm_context' | 'embedding' | 'prompt' | 'life_event';

export interface MemoryContent {
    data: any;
    metadata: Record<string, any>;
    confidence: number; // 0-1 scale
    source: string;
}

export interface TemporalProperties {
    created: Date;
    modified: Date;
    lastAccessed: Date;
    decayRate: number; // How quickly the memory fades (0-1)
    importance: number; // 0-1 scale
    lifeEvents: {
        eventId: string;
        type: 'personal' | 'professional' | 'social' | 'health';
        startDate: Date;
        endDate?: Date;
        intensity: number; // 0-1 scale
        emotionalImpact: {
            valence: number; // -1 to 1
            arousal: number; // 0-1
            persistence: number; // How long the impact lasts
        };
        relatedMemories: string[]; // IDs of memories formed during this period
    }[];
    temporalContext: {
        period: string; // e.g., "post-breakup", "during-pandemic"
        moodState: string; // e.g., "depressed", "euphoric"
        cognitiveBias: string[]; // e.g., "overestimation", "pessimism"
    };
}

export interface EmotionalProperties {
    valence: number; // -1 to 1 (negative to positive)
    arousal: number; // 0-1 scale
    emotionalTags: string[];
    triggers: {
        type: 'memory' | 'sensory' | 'contextual';
        source: string;
        intensity: number;
        associatedEvent?: string; // ID of related life event
        biasEffect: {
            type: 'amplification' | 'distortion' | 'suppression';
            strength: number;
            duration: number; // How long the bias lasts
        };
    }[];
    emotionalState: {
        baseline: number; // Normal emotional state
        current: number; // Current emotional state
        volatility: number; // How easily emotions change
    };
}

export interface MemoryConnection {
    targetId: string;
    type: ConnectionType;
    strength: number; // 0-1 scale
    context: string;
    emotionalContext: {
        formedDuring: string; // Life event ID
        emotionalBias: number; // How much the connection is influenced by emotions
        temporalRelevance: number; // How relevant the connection is to the current period
    };
    triggers: {
        type: string;
        intensity: number;
        associatedEvent?: string;
    }[];
}

export type ConnectionType = 'association' | 'causation' | 'similarity' | 'contrast';

// Specialized memory types
export interface EpisodicMemory extends MemoryUnit {
    type: 'episodic';
    content: {
        data: {
            event: {
                type: 'conversation' | 'interaction' | 'system_event';
                participants: string[];
                location?: string;
                duration?: number;
            };
            narrative: string;
            keyPoints: string[];
        };
        metadata: {
            context: Record<string, any>;
            emotionalSignificance: number;
        };
        confidence: number;
        source: string;
    };
}

export interface SemanticMemory extends MemoryUnit {
    type: 'semantic';
    content: {
        data: {
            concept: string;
            definition: string;
            examples: string[];
            relationships: {
                type: string;
                targetId: string;
                description: string;
            }[];
        };
        metadata: {
            category: string;
            reliability: number;
        };
        confidence: number;
        source: string;
    };
}

export interface ProceduralMemory extends MemoryUnit {
    type: 'procedural';
    content: {
        data: {
            skill: string;
            steps: string[];
            conditions: string[];
            exceptions: string[];
        };
        metadata: {
            proficiency: number;
            lastPracticed: Date;
        };
        confidence: number;
        source: string;
    };
}

// Working memory (active processing)
export interface WorkingMemory {
    activeUnits: MemoryUnit[];
    focus: {
        primary: string;
        secondary: string[];
        attentionWeights: Record<string, number>;
    };
    context: {
        currentTask: string;
        relevantMemories: string[];
        emotionalState: EmotionalState;
    };
}

export interface EmotionalState {
    mood: number; // -1 to 1
    energy: number; // 0-1
    stress: number; // 0-1
    focus: number; // 0-1
}

// Memory processing interfaces
export interface MemoryRetrieval {
    query: string;
    context: Record<string, any>;
    filters: {
        type?: MemoryType;
        timeRange?: [Date, Date];
        emotionalThreshold?: number;
        confidenceThreshold?: number;
    };
}

export interface MemoryConsolidation {
    sourceMemories: string[];
    newMemory: Partial<MemoryUnit>;
    consolidationStrength: number;
}

// Database schema mapping
export interface DatabaseMemory {
    id: string;
    type: MemoryType;
    content: string; // JSON stringified MemoryContent
    temporal: string; // JSON stringified TemporalProperties
    emotional: string; // JSON stringified EmotionalProperties
    connections: string; // JSON stringified MemoryConnection[]
    created_at: Date;
    updated_at: Date;
}

// LLM-specific memory types
export interface LLMContextMemory extends MemoryUnit {
    type: 'llm_context';
    content: {
        data: {
            model: string;
            contextWindow: number;
            tokens: {
                prompt: string;
                completion: string;
                total: number;
            };
            parameters: {
                temperature: number;
                top_p: number;
                frequency_penalty: number;
                presence_penalty: number;
            };
            conversation: {
                role: 'system' | 'user' | 'assistant';
                content: string;
                timestamp: Date;
            }[];
        };
        metadata: {
            modelVersion: string;
            contextType: 'conversation' | 'instruction' | 'retrieval';
            performance: {
                latency: number;
                tokensPerSecond: number;
            };
        };
        confidence: number;
        source: string;
    };
}

export interface EmbeddingMemory extends MemoryUnit {
    type: 'embedding';
    content: {
        data: {
            text: string;
            embedding: number[];
            dimensions: number;
            model: string;
        };
        metadata: {
            similarityThreshold: number;
            retrievalMethod: 'dot' | 'cosine' | 'euclidean';
        };
        confidence: number;
        source: string;
    };
}

export interface PromptMemory extends MemoryUnit {
    type: 'prompt';
    content: {
        data: {
            template: string;
            variables: Record<string, any>;
            examples: {
                input: any;
                output: any;
            }[];
            constraints: string[];
        };
        metadata: {
            category: string;
            successRate: number;
            averageTokens: number;
        };
        confidence: number;
        source: string;
    };
}

// LLM-specific connection types
export type LLMConnectionType = ConnectionType |
    'token_similarity' |
    'embedding_distance' |
    'prompt_chain' |
    'context_flow';

// Enhanced working memory for LLM context
export interface LLMWorkingMemory extends WorkingMemory {
    llmContext: {
        currentModel: string;
        contextWindow: {
            used: number;
            available: number;
        };
        recentPrompts: string[];
        temperature: number;
        systemPrompt: string;
    };
    embeddings: {
        model: string;
        cache: Map<string, number[]>;
        similarityThreshold: number;
    };
}

// LLM-specific retrieval interface
export interface LLMMemoryRetrieval extends MemoryRetrieval {
    llmFilters: {
        model?: string;
        contextWindow?: number;
        temperature?: number;
        embeddingSimilarity?: number;
        promptTemplate?: string;
    };
}

// Life event memory type
export interface LifeEventMemory extends MemoryUnit {
    type: 'life_event';
    content: {
        data: {
            event: {
                type: 'transition' | 'breakup' | 'loss' | 'achievement' | 'trauma';
                description: string;
                participants: string[];
                location?: string;
                duration: {
                    start: Date;
                    end?: Date;
                    phases?: {
                        phase: string;
                        start: Date;
                        end?: Date;
                        characteristics: string[];
                    }[];
                };
            };
            impact: {
                emotional: {
                    immediate: string[];
                    longTerm: string[];
                    triggers: string[];
                };
                cognitive: {
                    biases: string[];
                    thoughtPatterns: string[];
                    copingMechanisms: string[];
                };
                behavioral: {
                    changes: string[];
                    adaptations: string[];
                };
            };
        };
        metadata: {
            recoveryStage?: string;
            healingProgress: number;
            relatedEvents: string[];
        };
        confidence: number;
        source: string;
    };
}

// Memory formation context
export interface MemoryFormationContext {
    temporalContext: {
        period: string;
        lifeEvent?: string;
        emotionalState: string;
    };
    cognitiveState: {
        biases: string[];
        attention: number;
        clarity: number;
    };
    emotionalInfluence: {
        currentMood: number;
        emotionalTriggers: string[];
        biasEffects: {
            type: string;
            strength: number;
            direction: 'positive' | 'negative';
        }[];
    };
} 