// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2026 Dragonscale Team

use std::collections::HashMap;
use std::sync::Arc;
use tempfile::tempdir;
use uni_db::core::schema::SchemaManager;
use uni_db::query::executor::Executor;

use uni_db::query::planner::QueryPlanner;
use uni_db::runtime::property_manager::PropertyManager;
use uni_db::storage::manager::StorageManager;

#[tokio::test]
async fn test_explain() -> anyhow::Result<()> {
    let _ = env_logger::builder().is_test(true).try_init();
    let temp_dir = tempdir()?;
    let path = temp_dir.path();

    let schema_manager = SchemaManager::load(&path.join("schema.json")).await?;
    schema_manager.add_label("Person")?;
    let schema_manager = Arc::new(schema_manager);
    let storage = Arc::new(
        StorageManager::new(
            path.join("storage").to_str().unwrap(),
            schema_manager.clone(),
        )
        .await?,
    );

    let query = "EXPLAIN MATCH (n:Person) RETURN n";

    let ast = uni_cypher::parse(query)?;

    let planner = QueryPlanner::new(schema_manager.schema());
    let plan = planner.plan(ast)?;

    let executor = Executor::new(storage.clone());
    let prop_mgr = PropertyManager::new(storage.clone(), schema_manager.clone(), 100);

    let result = executor.execute(plan, &prop_mgr, &HashMap::new()).await?;

    assert_eq!(result.len(), 1);
    let plan_str = result[0].get("plan").unwrap().as_str().unwrap();
    println!("Plan:\n{}", plan_str);

    assert!(plan_str.contains("Scan"));
    assert!(plan_str.contains("Project"));

    Ok(())
}
