use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;

#[test]
fn test_basic_help() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("ls-plus"));
}

#[test]
fn test_version() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("ls-plus"));
}

#[test]
fn test_list_current_directory() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.assert().success();
}

#[test]
fn test_list_with_all_flag() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.arg("-a").assert().success();
}

#[test]
fn test_long_format() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.arg("-l").assert().success();
}

#[test]
fn test_config_show() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["config", "show"]).assert().success();
}

#[test]
fn test_nonexistent_directory() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.arg("/nonexistent/directory")
        .assert()
        .failure()
        .stderr(predicate::str::contains("Error"));
}

#[test]
fn test_tree_command() {
    let temp_dir = TempDir::new().unwrap();
    let temp_path = temp_dir.path();

    // Create test directory structure
    fs::create_dir(temp_path.join("subdir")).unwrap();
    fs::write(temp_path.join("file1.txt"), "content").unwrap();
    fs::write(temp_path.join("subdir").join("file2.txt"), "content").unwrap();

    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["tree", temp_path.to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn test_describe_set_and_get() {
    let temp_dir = TempDir::new().unwrap();
    let temp_path = temp_dir.path();

    // Set description
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args([
        "describe",
        "set",
        "Test directory description",
        temp_path.to_str().unwrap(),
    ])
    .assert()
    .success()
    .stdout(predicate::str::contains("已为目录").and(predicate::str::contains("设置描述")));

    // Get description - in the same test to ensure the temp directory still exists
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["describe", "get", temp_path.to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("Test directory description"));

    // Test cleanup by removing the description
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["describe", "remove", temp_path.to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn test_describe_list() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["describe", "list"]).assert().success();
}

#[test]
fn test_describe_search() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["describe", "search", "nonexistent"])
        .assert()
        .success()
        .stdout(predicate::str::contains("未找到"));
}

#[test]
fn test_describe_cleanup() {
    let mut cmd = Command::cargo_bin("ls-plus").unwrap();
    cmd.args(["describe", "cleanup"]).assert().success();
}
