import { useState, useEffect } from 'react';
import { memoryService } from '../services/fusemind/memoryService';
import { MemoryUnit, MemoryRetrieval, EmotionalState } from '../types/fusemind/memory';

export const useFusemindMemory = () => {
    const [activeMemories, setActiveMemories] = useState<MemoryUnit[]>([]);
    const [emotionalState, setEmotionalState] = useState<EmotionalState>(
        memoryService.getCurrentEmotionalState()
    );

    // Store a new memory
    const storeMemory = async (memory: MemoryUnit) => {
        const id = await memoryService.storeMemory(memory);
        return id;
    };

    // Retrieve memories based on query
    const retrieveMemories = async (query: MemoryRetrieval) => {
        const memories = await memoryService.retrieveMemory(query);
        setActiveMemories(memories);
        return memories;
    };

    // Update emotional state
    const updateEmotionalState = (update: Partial<EmotionalState>) => {
        memoryService.updateEmotionalState(update);
        setEmotionalState(memoryService.getCurrentEmotionalState());
    };

    // Effect to sync with memory service
    useEffect(() => {
        const interval = setInterval(() => {
            setEmotionalState(memoryService.getCurrentEmotionalState());
        }, 1000); // Update every second

        return () => clearInterval(interval);
    }, []);

    return {
        activeMemories,
        emotionalState,
        storeMemory,
        retrieveMemories,
        updateEmotionalState
    };
}; 