#!/usr/bin/env python3
"""
MIRA Memory System Configuration
==============================

This module provides centralized configuration management for the MIRA memory system.
It handles memory directory resolution, environment variables, and ensures consistent
paths across all memory components.

Purpose:
    - Centralized configuration management
    - Memory directory path resolution with fallback logic
    - Automatic directory structure creation
    - Environment variable support for deployment flexibility

Directory Resolution Priority:
    1. MIRA_MEMORY_DIR environment variable (highest priority)
    2. Workspace .mira directory (project-local)
    3. Home directory .mira (fallback)

Usage:
    ```python
    from config import MEMORY_DIR
    
    # Use the centrally configured memory directory
    my_file = MEMORY_DIR / "my_data.json"
    ```

Environment Variables:
    - MIRA_MEMORY_DIR: Override memory directory location

Directory Structure:
    The following subdirectories are automatically created:
    - conversations/: Conversation data and indexes
    - memories/: Core memory storage
    - lightning_vidmem/: Fast memory video cache
    - cache/: Temporary cache files
    - state/: System state files
    - videos/: Generated memory videos
    - reports/: Analysis reports
    - indexes/: Search indexes
    - secure_journal/: Encrypted memory storage
    - neural_state/: Neural network state

Thread Safety:
    Configuration is loaded once at import time and is read-only thereafter.
    All directory operations are thread-safe.

Author: MIRA Memory System
Version: 1.1 (Enhanced Documentation)
"""

import os
from pathlib import Path
from typing import Optional

# Import centralized path resolver
from core.mira_path_resolver import get_mira_memory_dir

def get_memory_directory() -> Path:
    """
    Get the memory directory path using centralized resolver.
    
    This now uses the centralized MIRA path resolver for consistency
    across all components.
    """
    try:
        # Use centralized path resolver
        memory_dir_str = get_mira_memory_dir()
        return Path(memory_dir_str)
    except Exception as e:
        # Fallback to current working directory if resolver fails
        fallback_dir = Path.cwd() / '.mira'
        fallback_dir.mkdir(parents=True, exist_ok=True)
        return fallback_dir

# Global memory directory for the session
MEMORY_DIR = get_memory_directory()

# Ensure the directory exists
MEMORY_DIR.mkdir(parents=True, exist_ok=True)

# Create subdirectories
SUBDIRS = [
    'conversations',
    'memories', 
    'lightning_vidmem',
    'lightning_vidmem/frame_cache',
    'cache',
    'state',
    'videos',
    'reports',
    'indexes',
    'secure_journal',
    'neural_state'
]

for subdir in SUBDIRS:
    (MEMORY_DIR / subdir).mkdir(parents=True, exist_ok=True)

# Export the main directory
__all__ = ['MEMORY_DIR', 'get_memory_directory']