use fast_clean::{remove_files, run, Cli};
use fast_clean::remover::MockRemover;

use std::fs::File;
use std::io::Write;
use std::time::{SystemTime, Duration};
use tempfile::tempdir;
use filetime::{FileTime, set_file_mtime};

/// 构造测试文件环境
///
/// 创建三个文件：
///
/// 1. old_small.txt   → 10 bytes, 10 天前
/// 2. old_big.log     → 1000 bytes, 10 天前
/// 3. new_small.txt   → 10 bytes, 今天
///
fn setup_env() -> tempfile::TempDir {
    let dir = tempdir().unwrap();

    let old_small_txt = dir.path().join("old_small.txt");
    let old_big_log = dir.path().join("old_big.log");
    let new_small_txt = dir.path().join("new_small.txt");

    // 创建文件并设置大小
    let mut f1 = File::create(&old_small_txt).unwrap();
    f1.write_all(&[0u8; 10]).unwrap();

    let mut f2 = File::create(&old_big_log).unwrap();
    f2.write_all(&[0u8; 1000]).unwrap();

    let mut f3 = File::create(&new_small_txt).unwrap();
    f3.write_all(&[0u8; 10]).unwrap();

    // 设置修改时间：10 天前
    let ten_days_ago = SystemTime::now() - Duration::from_secs(10 * 86400);
    let ft = FileTime::from_system_time(ten_days_ago);

    set_file_mtime(&old_small_txt, ft).unwrap();
    set_file_mtime(&old_big_log, ft).unwrap();

    dir
}

/// 创建默认 Cli
fn base_cli(dir: &str) -> Cli {
    Cli {
        dir: dir.to_string(),
        filter_dir: None,
        file_ext: None,
        day: None,
        size: None,
        is_skip_dir: false,
        is_test: true
    }
}

#[test]
fn test_type_only() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.file_ext = Some(vec!["txt".to_string()]);

    let mut mock = MockRemover::new();
    remove_files(&cli, &mut mock).unwrap();

    // 两个 txt 文件
    assert_eq!(mock.removed.borrow().len(), 2);
}

#[test]
fn test_size_only() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.size = Some("100B".to_string()); // <=100 bytes
    let ret = run(&cli).unwrap();
    // 两个 small 文件
    assert_eq!(ret.len(), 2);
}

#[test]
fn test_day_only() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.day = Some(7); // 超过 7 天

    let ret = run(&cli).unwrap();
    // 两个 small 文件
    assert_eq!(ret.len(), 2);
}

#[test]
fn test_type_and_size() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.file_ext = Some(vec!["txt".to_string()]);
    cli.size = Some("100B".to_string());

    let ret = run(&cli).unwrap();
    assert_eq!(ret.len(), 2);
}

#[test]
fn test_type_and_day() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.file_ext = Some(vec!["txt".to_string()]);
    cli.day = Some(7);

    let ret = run(&cli).unwrap();
    assert_eq!(ret.len(), 1);
}

#[test]
fn test_size_and_day() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.size = Some("100B".to_string());
    cli.day = Some(7);

    let ret = run(&cli).unwrap();
    assert_eq!(ret.len(), 1);
}

#[test]
fn test_type_size_day() {
    let dir = setup_env();
    let mut cli = base_cli(dir.path().to_str().unwrap());
    cli.file_ext = Some(vec!["txt".to_string()]);
    cli.size = Some("100B".to_string());
    cli.day = Some(7);

    let ret = run(&cli).unwrap();
    assert_eq!(ret.len(), 1);
}
