//! ai-memory-core 基础用法示例
//!
//! 运行: cargo run --example basic_usage

use ai_memory_core::{
    categories::{MemoryHall, MemoryPath, Wing},
    handle_memory_save, inject_memories_into_system_prompt, init, load_memories,
    memory_save_schema,
};

fn main() {
    // 1. 初始化数据目录
    let data_dir = tempfile::tempdir().expect("Failed to create temp dir");
    init(data_dir.path().to_path_buf());

    println!("=== ai-memory-core 基础用法演示 ===\n");
    println!("数据目录: {:?}\n", data_dir.path());

    // 2. 保存记忆
    println!("--- 保存记忆 ---");

    let result = handle_memory_save("Preferences/programming-languages", "偏好使用 TypeScript");
    match result {
        Ok(msg) => println!("  {}", msg),
        Err(e) => println!("  错误: {}", e),
    }

    let result = handle_memory_save("Preferences/programming-languages", "偏好使用 Rust");
    match result {
        Ok(msg) => println!("  {}", msg),
        Err(e) => println!("  错误: {}", e),
    }

    let result = handle_memory_save("Decisions/database-choice", "采用 PostgreSQL");
    match result {
        Ok(msg) => println!("  {}", msg),
        Err(e) => println!("  错误: {}", e),
    }

    let result = handle_memory_save("Project Knowledge/frontend-stack", "使用 React 18 + TypeScript");
    match result {
        Ok(msg) => println!("  {}", msg),
        Err(e) => println!("  错误: {}", e),
    }

    // 3. 加载并显示记忆
    println!("\n--- 记忆文件内容 ---");
    if let Some(memories) = load_memories() {
        println!("{}", memories);
    }

    // 4. 注入到 system prompt
    println!("\n--- 注入到 System Prompt ---");
    let system_prompt = "You are a helpful AI assistant.";
    let enriched = inject_memories_into_system_prompt(system_prompt);
    println!("{}", enriched);

    // 5. 空间路径示例
    println!("\n--- 空间路径解析 ---");

    // 2 层路径
    let path: MemoryPath = "Preferences/programming-languages"
        .parse()
        .unwrap();
    println!("2 层路径: {}", path.display());
    println!("  Markdown: {}", path.section_title().replace('\n', " / "));

    // 3 层路径
    let path: MemoryPath = "project/Decisions/database-choice"
        .parse()
        .unwrap();
    println!("3 层路径: {}", path.display());
    println!("  Wing: {:?}, Hall: {:?}, Room: {}",
        path.wing, path.hall, path.room);

    // 6. 枚举列表
    println!("\n--- 枚举列表 ---");
    println!("Wings: {:?}", Wing::all());
    println!("Halls: {:?}", MemoryHall::all().iter().map(|h| h.display_name()).collect::<Vec<_>>());

    // 7. Tool Schema
    println!("\n--- MemorySave Tool Schema ---");
    let schema = memory_save_schema();
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
