ym 0.3.57

Yummy - A modern Java build tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
use anyhow::{bail, Result};
use console::style;
use dialoguer::{Confirm, Input, MultiSelect, Select};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::config;
use crate::config::schema::{DependencyValue, YmConfig};
use crate::jdk_manager;

/// Entry point — aligned with spec 18-init.md
pub fn execute(
    name: Option<String>,
    interactive: bool,
    template: Option<String>,
    _yes: bool,
) -> Result<()> {
    let dir = resolve_dir(name.as_deref())?;

    // Pre-check: refuse if ym.json already exists
    let config_path = dir.join(config::CONFIG_FILE);
    if config_path.exists() {
        bail!("ym.json already exists in {}", dir.display());
    }

    if let Some(tpl) = template {
        return execute_from_template(&dir, &tpl);
    }

    if interactive {
        execute_interactive(&dir)
    } else {
        // Default: zero-question mode (like `bun init`)
        execute_defaults(&dir)
    }
}

/// Resolve the target directory path. Does NOT create the directory.
fn resolve_dir(name: Option<&str>) -> Result<PathBuf> {
    let cwd = std::env::current_dir()?;

    match name {
        None => Ok(cwd),
        Some(project_name) => Ok(cwd.join(project_name)),
    }
}

/// Zero-question mode — generate runnable project immediately
fn execute_defaults(dir: &Path) -> Result<()> {
    let dir_name = dir_name(dir);
    let pkg = default_package(&dir_name);

    let mut env = BTreeMap::new();
    env.insert("APP_PORT".to_string(), "8080".to_string());

    let config = YmConfig {
        name: dir_name.clone(),
        group_id: "com.example".to_string(),
        version: Some("0.1.0".to_string()),
        target: Some("21".to_string()),
        package: Some(pkg.clone()),
        main: Some(format!("{}.Application", pkg)),
        dependencies: Some(BTreeMap::new()),
        scripts: Some(default_scripts()),
        env: Some(env),
        ..Default::default()
    };

    write_project(dir, &config)?;

    // Run postinit hook if defined
    crate::scripts::run_script(&config, "postinit", dir)?;

    let main_class = config.main.as_deref().unwrap_or("Application");
    let main_path = main_class.replace('.', "/");
    println!();
    println!("  {} Created ym.json", style("").green());
    println!("  {} Created src/main/java/{}.java", style("").green(), main_path);
    println!();
    println!("  Done! Next steps:");
    if dir != std::env::current_dir().unwrap_or_default() {
        println!("    cd {}", dir_name);
    }
    println!("    ym dev");
    println!();

    Ok(())
}

/// Interactive mode (`-i`) — ask package name, Java version, template, JDK
fn execute_interactive(dir: &Path) -> Result<()> {
    let default_name = dir_name(dir);

    // 1. Package name
    let pkg_input: String = Input::new()
        .with_prompt("package name")
        .default(default_package(&default_name))
        .interact_text()?;

    // 2. Java version — scan installed JDKs
    println!();
    let term = console::Term::stderr();
    let _ = term.write_line(&format!("  {} scanning JDKs...", style("").green()));
    let jdks = jdk_manager::scan_jdks();
    let _ = term.clear_last_lines(1);

    let java_version = select_java_version(&jdks)?;

    // 3. Template selection
    let template_items = ["app", "lib", "spring-boot"];
    let template_idx = Select::new()
        .with_prompt("template")
        .items(&template_items)
        .default(0)
        .interact()?;
    let template = template_items[template_idx];

    // 4. DEV JDK selection (recommend JBR for hot reload)
    let dev_jdk_path = select_jdk("Select DEV JDK (for hot reload)", &jdks, true)?;

    // Build config
    let dir_name = dir_name(dir);
    let mut config = YmConfig {
        name: dir_name.clone(),
        group_id: "com.example".to_string(),
        version: Some("0.1.0".to_string()),
        target: Some(java_version),
        package: Some(pkg_input.clone()),
        dependencies: Some(BTreeMap::new()),
        scripts: Some(default_scripts()),
        ..Default::default()
    };

    // Set up env
    let env = config.env.get_or_insert_with(BTreeMap::new);
    env.insert("APP_PORT".to_string(), "8080".to_string());
    if let Some(ref path) = dev_jdk_path {
        env.insert("DEV_JAVA_HOME".to_string(), shorten_home(path));
        // Update dev script to use DEV_JAVA_HOME
        if let Some(ref mut scripts) = config.scripts {
            use config::schema::ScriptValue;
            scripts.insert("dev".to_string(), ScriptValue::Simple("JAVA_HOME=$DEV_JAVA_HOME ymc dev".to_string()));
        }
    }

    // Apply template-specific config
    match template {
        "spring-boot" => {
            let mut deps = BTreeMap::new();
            deps.insert(
                "org.springframework.boot:spring-boot-starter-web".to_string(),
                DependencyValue::Simple("3.4.0".to_string()),
            );
            config.dependencies = Some(deps);
            config.main = Some(format!("{}.Application", pkg_input));
        }
        "lib" => {
            let mut deps = BTreeMap::new();
            deps.insert(
                "org.junit.jupiter:junit-jupiter".to_string(),
                DependencyValue::Detailed(crate::config::schema::DependencySpec {
                    version: Some("5.11.0".to_string()),
                    scope: Some("test".to_string()),
                    ..Default::default()
                }),
            );
            deps.insert(
                "org.junit.platform:junit-platform-console-standalone".to_string(),
                DependencyValue::Detailed(crate::config::schema::DependencySpec {
                    version: Some("1.11.0".to_string()),
                    scope: Some("test".to_string()),
                    ..Default::default()
                }),
            );
            config.dependencies = Some(deps);
            config.main = None; // lib has no main
        }
        _ => {
            // app (default)
            config.main = Some(format!("{}.Application", pkg_input));
        }
    }

    // 5. Optional dependency selection (checkbox)
    let extra_deps = select_optional_deps(template)?;
    if !extra_deps.is_empty() {
        let deps = config.dependencies.get_or_insert_with(BTreeMap::new);
        for (coord, value) in extra_deps {
            deps.insert(coord, value);
        }
    }

    write_project_for_template(dir, &config, template)?;

    // Run postinit hook if defined
    crate::scripts::run_script(&config, "postinit", dir)?;

    println!();
    println!(
        "  {} Created {} project",
        style("").green(),
        style(template).cyan()
    );
    println!();
    println!("  Done! Next steps:");
    if dir != std::env::current_dir().unwrap_or_default() {
        println!("    cd {}", dir_name);
    }
    println!("    ym dev");
    println!();

    Ok(())
}

fn select_java_version(_jdks: &[jdk_manager::DetectedJdk]) -> Result<String> {
    let version_items = ["25 (latest)", "21 (LTS)", "17 (LTS)"];
    let version_idx = Select::new()
        .with_prompt("Java version")
        .items(&version_items)
        .default(0)
        .interact()?;
    Ok(match version_idx {
        0 => "25".to_string(),
        1 => "21".to_string(),
        2 => "17".to_string(),
        _ => "21".to_string(),
    })
}

/// Interactive JDK selection with arrow keys.
fn select_jdk(label: &str, jdks: &[jdk_manager::DetectedJdk], prefer_dcevm: bool) -> Result<Option<PathBuf>> {
    println!();
    println!("  {}", style(label).bold());

    if jdks.is_empty() {
        println!("  No JDKs found locally.");
        let download = Confirm::new()
            .with_prompt("  Download a JDK?")
            .default(true)
            .interact()?;

        if download {
            return download_jdk_interactive();
        }
        return Ok(None);
    }

    // Sort: when prefer_dcevm, put JBR (has_dcevm) entries first
    let sorted_jdks: Vec<&jdk_manager::DetectedJdk> = if prefer_dcevm {
        let mut dcevm: Vec<_> = jdks.iter().filter(|j| j.has_dcevm).collect();
        let mut rest: Vec<_> = jdks.iter().filter(|j| !j.has_dcevm).collect();
        dcevm.append(&mut rest);
        dcevm
    } else {
        jdks.iter().collect()
    };

    let mut items: Vec<String> = Vec::new();
    // Add "Skip" as first option
    items.push(format!("{}", style("Skip").dim()));

    for jdk in &sorted_jdks {
        let mut label = format!(
            "{:<20} {}  ({})",
            jdk.display_name(),
            style(jdk.path.display()).dim(),
            jdk.source,
        );
        if prefer_dcevm && jdk.has_dcevm {
            label = format!("{} {}", label, style("★ hot reload").yellow());
        }
        items.push(label);
    }
    items.push(format!("{}", style("Download other...").cyan()));

    let default = if prefer_dcevm && sorted_jdks.first().map(|j| j.has_dcevm).unwrap_or(false) {
        1 // First JBR
    } else {
        0 // Skip
    };

    let selection = Select::new()
        .items(&items)
        .default(default)
        .interact()?;

    if selection == 0 {
        return Ok(None); // Skip
    }

    // "Download other..." is the last item
    if selection == items.len() - 1 {
        return download_jdk_interactive();
    }

    let selected = sorted_jdks[selection - 1];
    println!(
        "  {} {}",
        style("").green(),
        selected.display_name()
    );

    Ok(Some(selected.path.clone()))
}

/// Interactive JDK download: select vendor then version.
fn download_jdk_interactive() -> Result<Option<PathBuf>> {
    // Step 1: Select provider (JBR recommended)
    let mut vendor_labels: Vec<String> = vec![
        format!("{} {}", "JetBrains Runtime (JBR)", style("★ recommended").yellow()),
    ];
    for (label, _) in jdk_manager::DOWNLOAD_VENDORS.iter() {
        if !label.to_lowercase().contains("jetbrains") && !label.to_lowercase().contains("jbr") {
            vendor_labels.push(label.to_string());
        }
    }
    vendor_labels.push(format!("{}", style("Skip").dim()));

    println!();
    let vendor_idx = Select::new()
        .with_prompt("  Provider")
        .items(&vendor_labels)
        .default(0)
        .interact()?;

    // Last item = Skip
    if vendor_idx == vendor_labels.len() - 1 {
        return Ok(None);
    }

    // Step 2: Select version (25 default)
    let version_labels = ["25 (latest)", "21 (LTS)", "17 (LTS)"];
    let version_idx = Select::new()
        .with_prompt("  Version")
        .items(&version_labels)
        .default(0)
        .interact()?;

    let version_key = match version_idx {
        0 => "25",
        1 => "21",
        2 => "17",
        _ => "25",
    };

    // Map vendor selection to vendor key
    let vendor_key = if vendor_idx == 0 {
        "jbr"
    } else {
        // Find the matching vendor key from DOWNLOAD_VENDORS
        jdk_manager::DOWNLOAD_VENDORS
            .iter()
            .find(|(label, _)| vendor_labels.get(vendor_idx).map(|l| l == *label).unwrap_or(false))
            .map(|(_, key)| *key)
            .unwrap_or("temurin")
    };

    let path = jdk_manager::download_jdk(vendor_key, version_key)?;
    Ok(Some(path))
}

/// Replace $HOME prefix with ~ for shorter, portable paths.
fn shorten_home(path: &Path) -> String {
    let home = crate::home_dir_string();
    let s = path.display().to_string();
    if let Some(rest) = s.strip_prefix(&home) {
        return format!("~{}", rest);
    }
    s
}

/// Default scripts for ym.json
fn default_scripts() -> BTreeMap<String, config::schema::ScriptValue> {
    use config::schema::ScriptValue;
    let mut s = BTreeMap::new();
    // Build
    s.insert("build".into(), ScriptValue::Simple("ymc build".into()));
    s.insert("build:clean".into(), ScriptValue::Simple("ymc build --clean".into()));
    // Dev
    s.insert("dev".into(), ScriptValue::Simple("ymc dev".into()));
    s.insert("dev:debug".into(), ScriptValue::Simple("ymc dev --debug".into()));
    // Test
    s.insert("test".into(), ScriptValue::Simple("ymc test".into()));
    s.insert("test:watch".into(), ScriptValue::Simple("ymc test --watch".into()));
    s.insert("test:coverage".into(), ScriptValue::Simple("ymc test --coverage".into()));
    // Clean
    s.insert("clean".into(), ScriptValue::Simple("ymc clean -y".into()));
    s.insert("clean:all".into(), ScriptValue::Simple("ymc clean --all -y".into()));
    // IDE
    s.insert("idea".into(), ScriptValue::Simple("ymc idea --sources".into()));
    // Native
    s.insert("native".into(), ScriptValue::Simple("ymc native".into()));
    s.insert("native:docker".into(), ScriptValue::Simple("ymc native --docker".into()));
    s.insert("native:install".into(), ScriptValue::Simple("ymc native --install".into()));
    // Docker — jar
    s.insert("docker:jar".into(), ScriptValue::Simple("ymc build && docker build -f docker/jar/Dockerfile --build-arg APP_NAME=${project.name} --build-arg APP_VERSION=${project.version} --build-arg APP_PORT=$APP_PORT -t ${project.name}:${project.version} .".into()));
    s.insert("docker:jar:publish".into(), ScriptValue::Simple("ymc build && docker build -f docker/jar/Dockerfile --build-arg APP_NAME=${project.name} --build-arg APP_VERSION=${project.version} --build-arg APP_PORT=$APP_PORT -t ${project.name}:${project.version} -t ${project.name}:latest . && docker push ${project.name}:${project.version} && docker push ${project.name}:latest".into()));
    // Docker — native
    s.insert("docker:native".into(), ScriptValue::Simple("ymc native && docker build -f docker/native/Dockerfile --build-arg APP_NAME=${project.name} --build-arg APP_VERSION=${project.version} --build-arg APP_PORT=$APP_PORT -t ${project.name}:${project.version} .".into()));
    s.insert("docker:native:publish".into(), ScriptValue::Simple("ymc native && docker build -f docker/native/Dockerfile --build-arg APP_NAME=${project.name} --build-arg APP_VERSION=${project.version} --build-arg APP_PORT=$APP_PORT -t ${project.name}:${project.version} -t ${project.name}:latest . && docker push ${project.name}:${project.version} && docker push ${project.name}:latest".into()));
    s
}

/// Non-interactive init from template
fn execute_from_template(dir: &Path, template: &str) -> Result<()> {
    // Check if template is a Git URL or local directory path
    if is_custom_template(template) {
        return execute_from_custom_template(dir, template);
    }

    let dir_name = dir_name(dir);
    let pkg = default_package(&dir_name);

    let mut env = BTreeMap::new();
    env.insert("APP_PORT".to_string(), "8080".to_string());

    let mut config = YmConfig {
        name: dir_name.clone(),
        group_id: "com.example".to_string(),
        version: Some("0.1.0".to_string()),
        target: Some("21".to_string()),
        package: Some(pkg.clone()),
        dependencies: Some(BTreeMap::new()),
        scripts: Some(default_scripts()),
        env: Some(env),
        ..Default::default()
    };

    match template {
        "spring-boot" | "spring" => {
            let mut deps = BTreeMap::new();
            deps.insert(
                "org.springframework.boot:spring-boot-starter-web".to_string(),
                DependencyValue::Simple("3.4.0".to_string()),
            );
            config.dependencies = Some(deps);
            config.main = Some(format!("{}.Application", pkg));
        }
        "lib" | "library" => {
            let mut deps = BTreeMap::new();
            deps.insert(
                "org.junit.jupiter:junit-jupiter".to_string(),
                DependencyValue::Detailed(crate::config::schema::DependencySpec {
                    version: Some("5.11.0".to_string()),
                    scope: Some("test".to_string()),
                    ..Default::default()
                }),
            );
            deps.insert(
                "org.junit.platform:junit-platform-console-standalone".to_string(),
                DependencyValue::Detailed(crate::config::schema::DependencySpec {
                    version: Some("1.11.0".to_string()),
                    scope: Some("test".to_string()),
                    ..Default::default()
                }),
            );
            config.dependencies = Some(deps);
            config.main = None;
        }
        _ => {
            // "app" default
            config.main = Some(format!("{}.Application", pkg));
        }
    }

    write_project_for_template(dir, &config, template)?;

    // Run postinit hook if defined
    crate::scripts::run_script(&config, "postinit", dir)?;

    println!();
    println!(
        "  {} Created {} project from '{}' template",
        style("").green(),
        style(&dir_name).bold(),
        style(template).cyan()
    );
    println!();
    println!("  Done! Next steps:");
    if dir != std::env::current_dir().unwrap_or_default() {
        println!("    cd {}", dir_name);
    }
    println!("    ym dev");
    println!();

    Ok(())
}

/// Check if template string is a custom template (Git URL or local path)
fn is_custom_template(template: &str) -> bool {
    template.starts_with("https://")
        || template.starts_with("http://")
        || template.starts_with("git@")
        || template.starts_with("./")
        || template.starts_with("../")
        || template.starts_with('/')
}

/// Init from a custom template: Git URL or local directory
fn execute_from_custom_template(dir: &Path, template: &str) -> Result<()> {
    std::fs::create_dir_all(dir)?;

    if template.starts_with("https://") || template.starts_with("http://") || template.starts_with("git@") {
        // Clone Git repo to temp dir, then copy contents
        println!(
            "  {} cloning template from {}",
            style("").green(),
            style(template).dim()
        );

        let tmp = tempfile::tempdir()?;
        let status = std::process::Command::new("git")
            .args(["clone", "--depth", "1", template, tmp.path().to_str().unwrap()])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::piped())
            .status()?;

        if !status.success() {
            bail!("Failed to clone template from {}", template);
        }

        copy_dir_contents(tmp.path(), dir)?;
    } else {
        // Local directory template
        let src = Path::new(template);
        if !src.is_dir() {
            bail!("Template directory not found: {}", template);
        }

        println!(
            "  {} copying template from {}",
            style("").green(),
            style(template).dim()
        );

        copy_dir_contents(src, dir)?;
    }

    // Load and display the generated config if it exists
    let config_path = dir.join(config::CONFIG_FILE);
    if config_path.exists() {
        // Run postinit hook if defined in the template's package.toml
        if let Ok(cfg) = config::load_config(&config_path) {
            crate::scripts::run_script(&cfg, "postinit", dir)?;
        }
    }

    let dn = dir_name(dir);
    println!();
    println!(
        "  {} Created project from custom template",
        style("").green(),
    );
    println!();
    println!("  Done! Next steps:");
    if dir != std::env::current_dir().unwrap_or_default() {
        println!("    cd {}", dn);
    }
    println!("    ym dev");
    println!();

    Ok(())
}

/// Recursively copy directory contents, skipping .git
fn copy_dir_contents(src: &Path, dst: &Path) -> Result<()> {
    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let name = entry.file_name();
        let name_str = name.to_string_lossy();

        // Skip .git directory
        if name_str == ".git" {
            continue;
        }

        let src_path = entry.path();
        let dst_path = dst.join(&name);

        if src_path.is_dir() {
            std::fs::create_dir_all(&dst_path)?;
            copy_dir_contents(&src_path, &dst_path)?;
        } else {
            std::fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

/// Write ym.json + create directories/files (default app template)
fn write_project(dir: &Path, config: &YmConfig) -> Result<()> {
    std::fs::create_dir_all(dir)?;

    let config_path = dir.join(config::CONFIG_FILE);
    config::save_config(&config_path, config)?;

    let src_dir = dir.join("src").join("main").join("java");
    std::fs::create_dir_all(&src_dir)?;
    create_sample_main(&src_dir, config)?;

    let resources_dir = dir.join("src").join("main").join("resources");
    std::fs::create_dir_all(&resources_dir)?;

    let test_dir = dir.join("src").join("test").join("java");
    std::fs::create_dir_all(&test_dir)?;

    let target = config.target.as_deref().unwrap_or("21");
    write_dockerfiles(dir, target)?;
    write_gitignore(dir)?;

    Ok(())
}

/// Write project with template-specific source files
fn write_project_for_template(dir: &Path, config: &YmConfig, template: &str) -> Result<()> {
    std::fs::create_dir_all(dir)?;

    let config_path = dir.join(config::CONFIG_FILE);
    config::save_config(&config_path, config)?;

    let src_dir = dir.join("src").join("main").join("java");
    std::fs::create_dir_all(&src_dir)?;

    let resources_dir = dir.join("src").join("main").join("resources");
    std::fs::create_dir_all(&resources_dir)?;

    let test_java_dir = dir.join("src").join("test").join("java");
    std::fs::create_dir_all(&test_java_dir)?;

    let pkg = config.package.as_deref().unwrap_or("com.example");
    let pkg_dir = src_dir.join(pkg.replace('.', "/"));
    std::fs::create_dir_all(&pkg_dir)?;

    match template {
        "spring-boot" | "spring" => {
            let main_content = format!(
                r#"package {};

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {{
    public static void main(String[] args) {{
        SpringApplication.run(Application.class, args);
    }}
}}
"#,
                pkg
            );
            std::fs::write(pkg_dir.join("Application.java"), main_content)?;

            // Create application.yml
            std::fs::write(resources_dir.join("application.yml"), "server:\n  port: 8080\n")?;
        }
        "lib" | "library" => {
            let class_name = to_class_name(&config.name);
            let lib_content = format!(
                r#"package {};

public class {} {{
    public String greet(String name) {{
        return "Hello, " + name + "!";
    }}
}}
"#,
                pkg, class_name
            );
            std::fs::write(pkg_dir.join(format!("{}.java", class_name)), lib_content)?;

            // Create test file
            let test_pkg_dir = test_java_dir.join(pkg.replace('.', "/"));
            std::fs::create_dir_all(&test_pkg_dir)?;
            let test_content = format!(
                r#"package {};

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class {}Test {{
    @Test
    void testGreet() {{
        {} lib = new {}();
        assertEquals("Hello, World!", lib.greet("World"));
    }}
}}
"#,
                pkg, class_name, class_name, class_name
            );
            std::fs::write(
                test_pkg_dir.join(format!("{}Test.java", class_name)),
                test_content,
            )?;
        }
        _ => {
            // app (default)
            create_sample_main(&src_dir, config)?;
        }
    }

    let target = config.target.as_deref().unwrap_or("21");
    write_dockerfiles(dir, target)?;
    write_gitignore(dir)?;

    Ok(())
}

/// Create docker/jar/Dockerfile and docker/native/Dockerfile
fn write_dockerfiles(dir: &Path, target: &str) -> Result<()> {
    let jar_dir = dir.join("docker").join("jar");
    std::fs::create_dir_all(&jar_dir)?;
    std::fs::write(
        jar_dir.join("Dockerfile"),
        format!(
            r#"FROM eclipse-temurin:{}-jre-alpine
ARG APP_NAME
ARG APP_VERSION
ARG APP_PORT=8080
COPY out/release/${{APP_NAME}}-${{APP_VERSION}}.jar /app.jar
EXPOSE ${{APP_PORT}}
ENTRYPOINT ["java", "-jar", "/app.jar"]
"#,
            target
        ),
    )?;

    let native_dir = dir.join("docker").join("native");
    std::fs::create_dir_all(&native_dir)?;
    std::fs::write(
        native_dir.join("Dockerfile"),
        r#"FROM alpine:3
ARG APP_NAME
ARG APP_VERSION
ARG APP_PORT=8080
COPY out/release/${APP_NAME} /app
EXPOSE ${APP_PORT}
ENTRYPOINT ["/app"]
"#,
    )?;

    Ok(())
}

fn write_gitignore(dir: &Path) -> Result<()> {
    let gitignore_path = dir.join(".gitignore");
    if !gitignore_path.exists() {
        std::fs::write(&gitignore_path, "out/\n.ym/\n.ym-sources.txt\n*.class\n")?;
    }
    Ok(())
}

/// my-app → myapp
pub fn sanitize_package_name(name: &str) -> String {
    name.chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_lowercase()
}

/// my-app → com.example.myapp
pub fn default_package(name: &str) -> String {
    format!("com.example.{}", sanitize_package_name(name))
}

fn dir_name(dir: &Path) -> String {
    dir.file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("my-app")
        .to_string()
}

fn to_class_name(name: &str) -> String {
    name.split(['-', '_'])
        .map(|part| {
            let mut c = part.chars();
            match c.next() {
                None => String::new(),
                Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
            }
        })
        .collect()
}

fn create_sample_main(src_dir: &Path, config: &YmConfig) -> Result<()> {
    let main_class = config.main.as_deref().unwrap_or("Application");

    let (pkg, class_name) = if let Some(idx) = main_class.rfind('.') {
        (Some(&main_class[..idx]), &main_class[idx + 1..])
    } else {
        (config.package.as_deref(), main_class)
    };

    let pkg_dir = if let Some(p) = pkg {
        src_dir.join(p.replace('.', "/"))
    } else {
        src_dir.to_path_buf()
    };

    std::fs::create_dir_all(&pkg_dir)?;
    let main_file = pkg_dir.join(format!("{}.java", class_name));
    if main_file.exists() {
        return Ok(());
    }

    let package_decl = if let Some(p) = pkg {
        format!("package {};\n\n", p)
    } else {
        String::new()
    };

    let content = format!(
        r#"{}public class {} {{
    public static void main(String[] args) {{
        System.out.println("Hello, World!");
    }}
}}
"#,
        package_decl, class_name
    );

    std::fs::write(&main_file, content)?;
    Ok(())
}

/// Common dependency catalog for interactive selection
const COMMON_DEPS: &[(&str, &str, &str, &str)] = &[
    // (display_label, coordinate, version, scope)
    ("Jackson (JSON)",                     "com.fasterxml.jackson.core:jackson-databind", "2.18.2", "compile"),
    ("Lombok",                             "org.projectlombok:lombok",                    "1.18.36", "provided"),
    ("SLF4J + Logback",                    "ch.qos.logback:logback-classic",              "1.5.16", "compile"),
    ("Google Guava",                       "com.google.guava:guava",                      "33.4.0-jre", "compile"),
    ("Apache Commons Lang",                "org.apache.commons:commons-lang3",            "3.17.0", "compile"),
    ("JUnit Jupiter (test)",               "org.junit.jupiter:junit-jupiter",             "5.11.4", "test"),
    ("Mockito (test)",                     "org.mockito:mockito-core",                    "5.14.2", "test"),
    ("AssertJ (test)",                     "org.assertj:assertj-core",                    "3.27.3", "test"),
];

/// Interactive dependency selection via checkbox
fn select_optional_deps(template: &str) -> Result<BTreeMap<String, DependencyValue>> {
    // Skip for spring-boot (already has its own deps) and lib (already has test deps)
    if template == "spring-boot" || template == "lib" {
        return Ok(BTreeMap::new());
    }

    println!();
    let labels: Vec<&str> = COMMON_DEPS.iter().map(|(l, _, _, _)| *l).collect();
    let selections = MultiSelect::new()
        .with_prompt("Add dependencies (space to select, enter to confirm)")
        .items(&labels)
        .interact()?;

    let mut deps = BTreeMap::new();
    for idx in selections {
        let (_, coord, version, scope) = COMMON_DEPS[idx];
        let value = if scope == "compile" {
            DependencyValue::Simple(version.to_string())
        } else {
            DependencyValue::Detailed(crate::config::schema::DependencySpec {
                version: Some(version.to_string()),
                scope: Some(scope.to_string()),
                ..Default::default()
            })
        };
        deps.insert(coord.to_string(), value);
    }

    Ok(deps)
}