use alef::core::config::{Language, PackageMetadataConfig, ResolvedCrateConfig};
use alef::core::ir::ApiSurface;
use alef::scaffold::scaffold;
use std::path::PathBuf;

#[test]
fn scaffold_elixir_mix_exs_omits_missing_native_src_directory() {
    // This test verifies that when the standard native/<nif>/src directory
    // does not exist (e.g., because the Rust source is at lib/lib.rs instead),
    // the generated mix.exs does NOT list the non-existent directory in files:.
    // Instead, it should list lib (the directory where the actual Rust source lives).
    let api = ApiSurface {
        crate_name: "demo".into(),
        version: "0.1.0".into(),
        types: vec![],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: Default::default(),
        excluded_trait_names: Default::default(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: vec![],
    };

    let config = ResolvedCrateConfig {
        name: "demo".to_string(),
        languages: vec![Language::Elixir],
        workspace_root: Some(PathBuf::from("/workspace")),
        package_metadata: Some(PackageMetadataConfig {
            license: Some("MIT".to_string()),
            ..PackageMetadataConfig::default()
        }),
        // Default output: packages/elixir/lib/
        explicit_output: Default::default(),
        ..ResolvedCrateConfig::default()
    };

    let result = scaffold(&api, &config, &[Language::Elixir]).expect("scaffold failed");
    let mix_exs_file = result
        .iter()
        .find(|file| file.path.to_string_lossy().ends_with("mix.exs"))
        .expect("Elixir scaffold should generate mix.exs");

    let content = &mix_exs_file.content;

    // The test runs in an environment where native/demo_nif/src doesn't exist,
    // so the generated mix.exs files: list should NOT reference native/demo_nif/src.
    // Instead, it should reference lib (where the wrapper module and Rust lib.rs co-exist).
    assert!(
        !content.contains("native/demo_nif/src"),
        "mix.exs files: list must not include non-existent native/<nif>/src directory"
    );

    // Verify that lib is listed (since wrapper modules are generated by alef)
    assert!(
        content.contains("lib"),
        "mix.exs files: list must include lib directory when native/<nif>/src does not exist"
    );

    // Verify the files: keyword is well-formed
    assert!(
        content.contains("files:"),
        "mix.exs package() must include files: keyword"
    );
}
