use markbase::filetree::{mode, FileTree};
use rusqlite::Connection;
use uuid::Uuid;

fn temp_db() -> (Connection, String) {
    let user_id = format!("test_{}", Uuid::new_v4());
    let conn = FileTree::init_user_db(&user_id).unwrap();
    (conn, user_id)
}

#[test]
fn test_tree_mode_render() {
    let (conn, user_id) = temp_db();
    let mut tree = FileTree::load(&conn, &user_id).unwrap();

    let root = FileTree::new_folder("Root", None);
    let child1 = FileTree::new_folder("Child1", Some(root.node_id.clone()));
    let child2 = FileTree::new_folder("Child2", Some(root.node_id.clone()));

    tree.insert_node(&conn, &root).unwrap();
    tree.insert_node(&conn, &child1).unwrap();
    tree.insert_node(&conn, &child2).unwrap();

    let mode = mode::get_mode("tree").expect("tree mode should exist");
    let rendered = mode.render(&tree);

    assert!(rendered.is_object());
    assert!(rendered["nodes"].is_array());
    assert_eq!(rendered["nodes"].as_array().unwrap().len(), 3);
}

#[test]
fn test_list_mode_render() {
    let (conn, user_id) = temp_db();
    let mut tree = FileTree::load(&conn, &user_id).unwrap();

    let folder = FileTree::new_folder("Videos", None);
    tree.insert_node(&conn, &folder).unwrap();

    let mode = mode::get_mode("list").expect("list mode should exist");
    let rendered = mode.render(&tree);

    assert!(rendered.is_object());
    assert!(rendered["nodes"].is_array());
    assert_eq!(rendered["nodes"].as_array().unwrap().len(), 1);
}

#[test]
fn test_grid_sm_mode_render() {
    let (conn, user_id) = temp_db();
    let mut tree = FileTree::load(&conn, &user_id).unwrap();

    let folder = FileTree::new_folder("Images", None);
    tree.insert_node(&conn, &folder).unwrap();

    let mode = mode::get_mode("grid_sm").expect("grid_sm mode should exist");
    let rendered = mode.render(&tree);

    assert!(rendered.is_object());
    assert!(rendered["nodes"].is_array());
}

#[test]
fn test_grid_lg_mode_render() {
    let (conn, user_id) = temp_db();
    let mut tree = FileTree::load(&conn, &user_id).unwrap();

    let folder = FileTree::new_folder("Documents", None);
    tree.insert_node(&conn, &folder).unwrap();

    let mode = mode::get_mode("grid_lg").expect("grid_lg mode should exist");
    let rendered = mode.render(&tree);

    assert!(rendered.is_object());
    assert!(rendered["nodes"].is_array());
}

#[test]
fn test_invalid_mode() {
    let mode = mode::get_mode("invalid_mode");
    assert!(mode.is_none(), "invalid mode should return None");
}

#[test]
fn test_mode_name() {
    let tree_mode = mode::get_mode("tree").unwrap();
    assert_eq!(tree_mode.name(), "tree");

    let list_mode = mode::get_mode("list").unwrap();
    assert_eq!(list_mode.name(), "list");

    let grid_sm_mode = mode::get_mode("grid_sm").unwrap();
    assert_eq!(grid_sm_mode.name(), "grid_sm");

    let grid_lg_mode = mode::get_mode("grid_lg").unwrap();
    assert_eq!(grid_lg_mode.name(), "grid_lg");
}

#[test]
fn test_mode_sort_options() {
    let mode = mode::get_mode("list").unwrap();
    let sort_options = mode.sort_options();

    assert!(!sort_options.is_empty(), "sort options should not be empty");
}

#[test]
fn test_mode_filter_options() {
    let mode = mode::get_mode("list").unwrap();
    let filter_options = mode.filter_options();

    assert!(
        !filter_options.is_empty(),
        "filter options should not be empty"
    );
}

#[test]
fn test_list_modes() {
    let modes = mode::list_modes();

    assert_eq!(modes.len(), 4, "should have 4 display modes");

    let names: Vec<&str> = modes.iter().map(|m| m.name()).collect();
    assert!(names.contains(&"tree"));
    assert!(names.contains(&"list"));
    assert!(names.contains(&"grid_sm"));
    assert!(names.contains(&"grid_lg"));
}
