use std::path::PathBuf;

use dear_file_browser::{FileSystem, StdFileSystem};

fn unique_temp_dir(prefix: &str) -> PathBuf {
    let mut p = std::env::temp_dir();
    let pid = std::process::id();
    let t = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    p.push(format!("dear-file-browser-{prefix}-{pid}-{t}"));
    p
}

#[test]
fn std_fs_rename_and_remove_file() {
    let fs = StdFileSystem;
    let dir = unique_temp_dir("fs_ops");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let from = dir.join("a.txt");
    let to = dir.join("b.txt");
    std::fs::write(&from, b"hello").unwrap();

    fs.rename(&from, &to).unwrap();
    assert!(!from.exists());
    assert!(to.exists());

    fs.remove_file(&to).unwrap();
    assert!(!to.exists());

    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn std_fs_copy_file() {
    let fs = StdFileSystem;
    let dir = unique_temp_dir("copy_file");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let from = dir.join("a.txt");
    let to = dir.join("b.txt");
    std::fs::write(&from, b"hello").unwrap();

    let n = fs.copy_file(&from, &to).unwrap();
    assert_eq!(n, 5);
    assert!(to.exists());
    assert_eq!(std::fs::read(&to).unwrap(), b"hello");

    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn std_fs_remove_dir() {
    let fs = StdFileSystem;
    let dir = unique_temp_dir("remove_dir");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let sub = dir.join("empty");
    std::fs::create_dir(&sub).unwrap();

    fs.remove_dir(&sub).unwrap();
    assert!(!sub.exists());

    std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn std_fs_remove_dir_all() {
    let fs = StdFileSystem;
    let dir = unique_temp_dir("remove_dir_all");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();

    let sub = dir.join("sub");
    let sub2 = sub.join("nested");
    std::fs::create_dir_all(&sub2).unwrap();
    std::fs::write(sub.join("a.txt"), b"hello").unwrap();
    std::fs::write(sub2.join("b.txt"), b"world").unwrap();

    fs.remove_dir_all(&sub).unwrap();
    assert!(!sub.exists());

    std::fs::remove_dir_all(&dir).unwrap();
}
