use beads_rust::cli::CreateArgs;
use beads_rust::cli::commands::create;
use beads_rust::config::CliOverrides;
use beads_rust::output::OutputContext;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

#[test]
#[ignore = "Path traversal restriction still active; feature to allow .. in CLI input not yet implemented"]
fn test_create_from_file_rejects_parent_dir() {
    // This test verifies the current behavior which might be considered a bug/over-restriction.
    // We want to see if it fails.

    let temp = TempDir::new().unwrap();
    let subdir = temp.path().join("subdir");
    fs::create_dir(&subdir).unwrap();

    // Create a markdown file in the parent dir (temp.path)
    let md_path = temp.path().join("issues.md");
    fs::write(&md_path, "## Test Issue").unwrap();

    // Try to access it from subdir using ..
    let relative_path = PathBuf::from("..").join("issues.md");

    // We need to run execute_import logic.
    // But execute_import is private. We can call execute with args.

    // We need to set CWD to subdir for the relative path to make sense physically,
    // but the validation checks the string content of the path arg.

    let args = CreateArgs {
        file: Some(relative_path.to_str().unwrap().to_string().into()),
        ..Default::default() // other fields
    };

    let overrides = CliOverrides::default();
    let ctx = OutputContext::from_flags(false, false, true);

    // We expect this to SUCCEED now (path traversal check removed for CLI input)
    let result = create::execute(&args, &overrides, &ctx);

    if let Err(e) = result {
        // If it fails for other reasons (e.g. invalid markdown), that's fine,
        // but it shouldn't fail for path traversal.
        // But here we provided valid markdown "## Test Issue".
        // So it should succeed.
        let err_str = e.to_string();
        assert!(
            !err_str.contains("path must not contain '..'"),
            "Test failed: Path traversal check still active!"
        );
        // Other errors might happen (e.g. no DB found if we didn't mock it?
        // Wait, create::execute tries to open storage.
        // We are in a temp dir. discover_beads_dir might fail or init new?
        // discover_beads_dir looks for .beads.
        // We didn't create .beads in temp_dir.
        // So execute() might fail with "beads directory not found".
        // That's acceptable for this test, as long as it's not the traversal error.
    }
}
