use folk_builder::codegen::{generate_cargo_toml, generate_main_rs};
use folk_builder::config::{BuildConfig, BuildMeta, PluginEntry};

fn two_plugin_config() -> BuildConfig {
    BuildConfig {
        build: BuildMeta {
            output: "my-folk".into(),
            rust_version: "1.85".into(),
        },
        plugin: vec![
            PluginEntry {
                crate_name: "folk_plugin_http".into(),
                version: Some("0.1".into()),
                path: None,
                git: None,
                config_key: "http".into(),
            },
            PluginEntry {
                crate_name: "folk_plugin_jobs".into(),
                version: Some("0.1".into()),
                path: None,
                git: None,
                config_key: "jobs".into(),
            },
        ],
    }
}

#[test]
fn codegen_main_contains_both_plugins() {
    let main_rs = generate_main_rs(&two_plugin_config());
    assert!(main_rs.contains("folk_plugin_http_factory"));
    assert!(main_rs.contains("folk_plugin_jobs_factory"));
}

#[test]
fn codegen_main_contains_config_keys() {
    let main_rs = generate_main_rs(&two_plugin_config());
    assert!(main_rs.contains(r#"get_table("http")"#));
    assert!(main_rs.contains(r#"get_table("jobs")"#));
}

#[test]
fn codegen_cargo_toml_contains_plugins() {
    let cargo = generate_cargo_toml(&two_plugin_config());
    assert!(cargo.contains(r#"folk_plugin_http = "0.1""#));
    assert!(cargo.contains(r#"folk_plugin_jobs = "0.1""#));
    assert!(cargo.contains(r#"name = "my-folk""#));
}

#[test]
fn codegen_cargo_toml_path_override() {
    let cfg = BuildConfig {
        build: BuildMeta {
            output: "test".into(),
            rust_version: "1.85".into(),
        },
        plugin: vec![PluginEntry {
            crate_name: "my_plugin".into(),
            version: None,
            path: Some("../my-plugin".into()),
            git: None,
            config_key: String::new(),
        }],
    };
    let cargo = generate_cargo_toml(&cfg);
    assert!(cargo.contains(r#"my_plugin = { path = "../my-plugin" }"#));
}

#[test]
fn codegen_cargo_toml_git_override() {
    let cfg = BuildConfig {
        build: BuildMeta {
            output: "test".into(),
            rust_version: "1.85".into(),
        },
        plugin: vec![PluginEntry {
            crate_name: "my_plugin".into(),
            version: Some("0.2".into()),
            path: None,
            git: Some("https://github.com/example/plugin".into()),
            config_key: String::new(),
        }],
    };
    let cargo = generate_cargo_toml(&cfg);
    assert!(
        cargo.contains(
            r#"my_plugin = { git = "https://github.com/example/plugin", version = "0.2" }"#
        )
    );
}

#[test]
fn codegen_main_uses_crate_name_as_config_key_when_empty() {
    let cfg = BuildConfig {
        build: BuildMeta {
            output: "test".into(),
            rust_version: "1.85".into(),
        },
        plugin: vec![PluginEntry {
            crate_name: "folk_plugin_http".into(),
            version: Some("0.1".into()),
            path: None,
            git: None,
            config_key: String::new(),
        }],
    };
    let main_rs = generate_main_rs(&cfg);
    assert!(main_rs.contains(r#"get_table("folk_plugin_http")"#));
}
