axonml-cli 0.5.0

Command-line interface for the Axonml ML framework
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
//! Analyze - Comprehensive Model and Dataset Analysis
//!
//! # File
//! `crates/axonml-cli/src/commands/analyze.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

use axonml_serialize::load_state_dict;
use serde::{Deserialize, Serialize};

use super::load::load_workspace;
use super::utils::{path_exists, print_header, print_info, print_kv, print_success, print_warning};
use crate::cli::{
    AnalyzeArgs, AnalyzeBothArgs, AnalyzeDataArgs, AnalyzeModelArgs, AnalyzeReportArgs,
    AnalyzeSubcommand,
};
use crate::error::{CliError, CliResult};

// =============================================================================
// Analysis Results
// =============================================================================

/// Model analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelAnalysis {
    pub name: String,
    pub path: String,
    pub format: String,
    pub num_parameters: usize,
    pub num_layers: usize,
    pub file_size: u64,
    pub layer_analysis: Vec<LayerInfo>,
    pub architecture_type: String,
    pub estimated_memory: u64,
    pub recommendations: Vec<String>,
}

/// Layer information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayerInfo {
    pub name: String,
    pub layer_type: String,
    pub shape: Vec<usize>,
    pub num_parameters: usize,
    pub percentage: f64,
}

/// Dataset analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataAnalysis {
    pub name: String,
    pub path: String,
    pub data_type: String,
    pub num_samples: usize,
    pub num_classes: Option<usize>,
    pub class_distribution: Option<HashMap<String, usize>>,
    pub total_size: u64,
    pub file_stats: FileStats,
    pub quality_score: f64,
    pub issues: Vec<String>,
    pub recommendations: Vec<String>,
}

/// File statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FileStats {
    pub total_files: usize,
    pub file_types: HashMap<String, usize>,
    pub avg_file_size: u64,
    pub min_file_size: u64,
    pub max_file_size: u64,
}

/// Combined analysis for model + dataset
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CombinedAnalysis {
    pub model: ModelAnalysis,
    pub dataset: DataAnalysis,
    pub compatibility: CompatibilityAnalysis,
}

/// Compatibility analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityAnalysis {
    pub compatible: bool,
    pub input_match: bool,
    pub output_match: bool,
    pub issues: Vec<String>,
    pub suggestions: Vec<String>,
}

// =============================================================================
// Execute Command
// =============================================================================

/// Execute the `analyze` command
pub fn execute(args: AnalyzeArgs) -> CliResult<()> {
    match args.action {
        AnalyzeSubcommand::Model(model_args) => execute_analyze_model(model_args),
        AnalyzeSubcommand::Data(data_args) => execute_analyze_data(data_args),
        AnalyzeSubcommand::Both(both_args) => execute_analyze_both(both_args),
        AnalyzeSubcommand::Report(report_args) => execute_report(report_args),
    }
}

// =============================================================================
// Analyze Model Subcommand
// =============================================================================

fn execute_analyze_model(args: AnalyzeModelArgs) -> CliResult<()> {
    print_header("Model Analysis");

    // Get model path (from args or workspace)
    let (path, name) = if let Some(p) = &args.path {
        let path = PathBuf::from(p);
        if !path_exists(&path) {
            return Err(CliError::Model(format!("Model not found: {p}")));
        }
        let name = path
            .file_stem()
            .map_or_else(|| "model".to_string(), |s| s.to_string_lossy().to_string());
        (path, name)
    } else {
        // Use workspace
        let workspace = load_workspace()?;
        if let Some(model) = &workspace.model {
            (PathBuf::from(&model.path), model.name.clone())
        } else {
            return Err(CliError::Model(
                "No model specified. Use --path or load a model first with 'axonml load model'"
                    .to_string(),
            ));
        }
    };

    print_kv("Model", &name);
    print_kv("Path", &path.display().to_string());

    println!();
    print_info("Analyzing model...");

    let analysis = analyze_model(&path, &name)?;

    // Print results
    print_model_analysis(&analysis, args.detailed);

    // Save report if requested
    if let Some(output) = &args.output {
        save_analysis_report(&analysis, output, &args.format)?;
        print_success(&format!("Report saved to: {output}"));
    }

    Ok(())
}

fn analyze_model(path: &PathBuf, name: &str) -> CliResult<ModelAnalysis> {
    let state_dict =
        load_state_dict(path).map_err(|e| CliError::Model(format!("Failed to load model: {e}")))?;

    let file_size = fs::metadata(path)?.len();
    let format = detect_format(path);

    let mut layer_analysis = Vec::new();
    let total_params: usize = state_dict
        .entries()
        .map(|(_, entry)| entry.data.shape.iter().product::<usize>())
        .sum();

    for (layer_name, entry) in state_dict.entries() {
        let shape = entry.data.shape.clone();
        let num_params: usize = shape.iter().product();
        let percentage = if total_params > 0 {
            (num_params as f64 / total_params as f64) * 100.0
        } else {
            0.0
        };

        let layer_type = infer_layer_type(layer_name, &shape);

        layer_analysis.push(LayerInfo {
            name: layer_name.clone(),
            layer_type,
            shape,
            num_parameters: num_params,
            percentage,
        });
    }

    // Sort by parameter count
    layer_analysis.sort_by(|a, b| b.num_parameters.cmp(&a.num_parameters));

    let architecture_type = infer_architecture(&layer_analysis);
    let estimated_memory = estimate_memory(total_params);
    let recommendations = generate_model_recommendations(&layer_analysis, total_params);

    Ok(ModelAnalysis {
        name: name.to_string(),
        path: path.display().to_string(),
        format,
        num_parameters: total_params,
        num_layers: layer_analysis.len(),
        file_size,
        layer_analysis,
        architecture_type,
        estimated_memory,
        recommendations,
    })
}

fn print_model_analysis(analysis: &ModelAnalysis, detailed: bool) {
    println!();
    print_header("Overview");
    print_kv("Architecture", &analysis.architecture_type);
    print_kv("Format", &analysis.format);
    print_kv("Total parameters", &format_number(analysis.num_parameters));
    print_kv("Number of layers", &analysis.num_layers.to_string());
    print_kv("File size", &format_size(analysis.file_size));
    print_kv(
        "Estimated memory (inference)",
        &format_size(analysis.estimated_memory),
    );

    // Parameter breakdown
    println!();
    print_header("Parameter Distribution");

    let top_layers: Vec<_> = analysis.layer_analysis.iter().take(10).collect();
    for layer in top_layers {
        println!(
            "  {:40} {:>12} ({:>5.1}%)",
            truncate(&layer.name, 40),
            format_number(layer.num_parameters),
            layer.percentage
        );
    }

    if analysis.layer_analysis.len() > 10 {
        println!(
            "  ... and {} more layers",
            analysis.layer_analysis.len() - 10
        );
    }

    if detailed {
        println!();
        print_header("All Layers");
        for layer in &analysis.layer_analysis {
            let shape_str = layer
                .shape
                .iter()
                .map(std::string::ToString::to_string)
                .collect::<Vec<_>>()
                .join("x");
            println!(
                "  {} [{}] - {} ({})",
                layer.name,
                shape_str,
                layer.layer_type,
                format_number(layer.num_parameters)
            );
        }
    }

    if !analysis.recommendations.is_empty() {
        println!();
        print_header("Recommendations");
        for rec in &analysis.recommendations {
            println!("  - {rec}");
        }
    }
}

// =============================================================================
// Analyze Data Subcommand
// =============================================================================

fn execute_analyze_data(args: AnalyzeDataArgs) -> CliResult<()> {
    print_header("Dataset Analysis");

    // Get data path (from args or workspace)
    let (path, name) = if let Some(p) = &args.path {
        let path = PathBuf::from(p);
        if !path_exists(&path) {
            return Err(CliError::Data(format!("Dataset not found: {p}")));
        }
        let name = path.file_name().map_or_else(
            || "dataset".to_string(),
            |s| s.to_string_lossy().to_string(),
        );
        (path, name)
    } else {
        let workspace = load_workspace()?;
        if let Some(dataset) = &workspace.dataset {
            (PathBuf::from(&dataset.path), dataset.name.clone())
        } else {
            return Err(CliError::Data(
                "No dataset specified. Use --path or load a dataset first with 'axonml load data'"
                    .to_string(),
            ));
        }
    };

    print_kv("Dataset", &name);
    print_kv("Path", &path.display().to_string());

    println!();
    print_info("Analyzing dataset...");

    let analysis = analyze_data(&path, &name, args.max_samples)?;

    // Print results
    print_data_analysis(&analysis, args.detailed);

    // Save report if requested
    if let Some(output) = &args.output {
        save_data_report(&analysis, output, &args.format)?;
        print_success(&format!("Report saved to: {output}"));
    }

    Ok(())
}

fn analyze_data(path: &PathBuf, name: &str, max_samples: usize) -> CliResult<DataAnalysis> {
    use walkdir::WalkDir;

    let data_type = detect_data_type(path);
    let mut total_size = 0u64;
    let mut file_stats = FileStats::default();
    let mut file_sizes: Vec<u64> = Vec::new();
    let mut class_distribution: HashMap<String, usize> = HashMap::new();

    // Scan files
    for entry in WalkDir::new(path)
        .into_iter()
        .filter_map(std::result::Result::ok)
    {
        if entry.file_type().is_file() {
            file_stats.total_files += 1;
            if let Ok(meta) = entry.metadata() {
                let size = meta.len();
                total_size += size;
                file_sizes.push(size);
            }
            if let Some(ext) = entry.path().extension() {
                let ext_str = ext.to_string_lossy().to_lowercase();
                *file_stats.file_types.entry(ext_str).or_insert(0) += 1;
            }
        }
    }

    // Calculate file size stats
    if !file_sizes.is_empty() {
        file_stats.avg_file_size = total_size / file_sizes.len() as u64;
        file_stats.min_file_size = *file_sizes.iter().min().unwrap_or(&0);
        file_stats.max_file_size = *file_sizes.iter().max().unwrap_or(&0);
    }

    // Detect classes and count samples
    let (num_samples, num_classes) = match data_type.as_str() {
        "image" => count_image_samples(path, &mut class_distribution),
        "tabular" => count_tabular_samples(path, &mut class_distribution, max_samples),
        _ => (file_stats.total_files, None),
    };

    // Calculate quality score
    let quality_score =
        calculate_quality_score(&data_type, num_samples, &class_distribution, &file_stats);

    // Find issues
    let issues = find_data_issues(&data_type, num_samples, &class_distribution, &file_stats);

    // Generate recommendations
    let recommendations =
        generate_data_recommendations(&data_type, num_samples, &class_distribution, &issues);

    Ok(DataAnalysis {
        name: name.to_string(),
        path: path.display().to_string(),
        data_type,
        num_samples,
        num_classes,
        class_distribution: if class_distribution.is_empty() {
            None
        } else {
            Some(class_distribution)
        },
        total_size,
        file_stats,
        quality_score,
        issues,
        recommendations,
    })
}

fn print_data_analysis(analysis: &DataAnalysis, detailed: bool) {
    println!();
    print_header("Overview");
    print_kv("Type", &analysis.data_type);
    print_kv("Total samples", &analysis.num_samples.to_string());
    if let Some(n) = analysis.num_classes {
        print_kv("Number of classes", &n.to_string());
    }
    print_kv("Total size", &format_size(analysis.total_size));
    print_kv("Total files", &analysis.file_stats.total_files.to_string());
    print_kv(
        "Quality score",
        &format!("{:.1}/10", analysis.quality_score),
    );

    // File statistics
    println!();
    print_header("File Statistics");
    print_kv(
        "Average file size",
        &format_size(analysis.file_stats.avg_file_size),
    );
    print_kv(
        "Min file size",
        &format_size(analysis.file_stats.min_file_size),
    );
    print_kv(
        "Max file size",
        &format_size(analysis.file_stats.max_file_size),
    );

    if detailed {
        println!();
        println!("File types:");
        for (ext, count) in &analysis.file_stats.file_types {
            println!("  .{ext}: {count} files");
        }
    }

    // Class distribution
    if let Some(dist) = &analysis.class_distribution {
        println!();
        print_header("Class Distribution");

        let mut sorted: Vec<_> = dist.iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(a.1));

        let total: usize = dist.values().sum();
        for (class, count) in sorted.iter().take(20) {
            let pct = (**count as f64 / total as f64) * 100.0;
            println!("  {:30} {:>8} ({:>5.1}%)", truncate(class, 30), count, pct);
        }

        if sorted.len() > 20 {
            println!("  ... and {} more classes", sorted.len() - 20);
        }

        // Check for imbalance
        let counts: Vec<usize> = dist.values().copied().collect();
        if !counts.is_empty() {
            let max = *counts.iter().max().unwrap() as f64;
            let min = *counts.iter().min().unwrap() as f64;
            if max / min > 5.0 {
                println!();
                print_warning(&format!(
                    "Class imbalance detected: {:.1}x ratio",
                    max / min
                ));
            }
        }
    }

    // Issues
    if !analysis.issues.is_empty() {
        println!();
        print_header("Issues Found");
        for issue in &analysis.issues {
            println!("  ! {issue}");
        }
    }

    // Recommendations
    if !analysis.recommendations.is_empty() {
        println!();
        print_header("Recommendations");
        for rec in &analysis.recommendations {
            println!("  - {rec}");
        }
    }
}

// =============================================================================
// Analyze Both Subcommand
// =============================================================================

fn execute_analyze_both(args: AnalyzeBothArgs) -> CliResult<()> {
    print_header("Combined Model & Dataset Analysis");

    let workspace = load_workspace()?;

    let model = workspace.model.ok_or_else(|| {
        CliError::Model("No model loaded. Use 'axonml load model' first".to_string())
    })?;

    let dataset = workspace.dataset.ok_or_else(|| {
        CliError::Data("No dataset loaded. Use 'axonml load data' first".to_string())
    })?;

    print_kv("Model", &model.name);
    print_kv("Dataset", &dataset.name);

    println!();
    print_info("Analyzing compatibility...");

    // Analyze both
    let model_analysis = analyze_model(&PathBuf::from(&model.path), &model.name)?;
    let data_analysis = analyze_data(&PathBuf::from(&dataset.path), &dataset.name, 1000)?;

    // Check compatibility
    let compatibility = check_compatibility(&model_analysis, &data_analysis);

    // Print results
    println!();
    print_header("Compatibility Analysis");

    if compatibility.compatible {
        print_success("Model and dataset appear compatible!");
    } else {
        print_warning("Potential compatibility issues found");
    }

    print_kv(
        "Input shape match",
        if compatibility.input_match {
            "Yes"
        } else {
            "No"
        },
    );
    print_kv(
        "Output shape match",
        if compatibility.output_match {
            "Yes"
        } else {
            "No"
        },
    );

    if !compatibility.issues.is_empty() {
        println!();
        println!("Issues:");
        for issue in &compatibility.issues {
            println!("  ! {issue}");
        }
    }

    if !compatibility.suggestions.is_empty() {
        println!();
        println!("Suggestions:");
        for sug in &compatibility.suggestions {
            println!("  - {sug}");
        }
    }

    // Summary
    println!();
    print_header("Summary");
    print_kv(
        "Model parameters",
        &format_number(model_analysis.num_parameters),
    );
    print_kv("Dataset samples", &data_analysis.num_samples.to_string());

    if let Some(n) = data_analysis.num_classes {
        print_kv("Classes", &n.to_string());

        // Check if model output matches
        let output_size = infer_output_size(&model_analysis);
        if let Some(out) = output_size {
            if out != n {
                print_warning(&format!(
                    "Model output size ({out}) doesn't match number of classes ({n})"
                ));
            }
        }
    }

    // Save report if requested
    if let Some(output) = &args.output {
        let combined = CombinedAnalysis {
            model: model_analysis,
            dataset: data_analysis,
            compatibility,
        };
        let json = serde_json::to_string_pretty(&combined)?;
        fs::write(output, json)?;
        print_success(&format!("Report saved to: {output}"));
    }

    Ok(())
}

fn check_compatibility(model: &ModelAnalysis, data: &DataAnalysis) -> CompatibilityAnalysis {
    let mut issues = Vec::new();
    let mut suggestions = Vec::new();

    // Check output size vs classes
    let output_size = infer_output_size(model);
    let output_match = if let (Some(out), Some(classes)) = (output_size, data.num_classes) {
        if out == classes {
            true
        } else {
            issues.push(format!(
                "Model output size ({out}) doesn't match dataset classes ({classes})"
            ));
            suggestions
                .push("Consider adjusting the final layer or relabeling the dataset".to_string());
            false
        }
    } else {
        true // Can't determine, assume OK
    };

    // Check input size (heuristic based on first layer)
    let input_match = true; // Would need more info to check properly

    // Check dataset size vs model complexity
    if data.num_samples < model.num_parameters / 10 {
        issues.push(format!(
            "Dataset may be too small ({} samples) for model size ({} parameters)",
            data.num_samples,
            format_number(model.num_parameters)
        ));
        suggestions.push("Consider using data augmentation or a smaller model".to_string());
    }

    // Check for class imbalance
    if let Some(dist) = &data.class_distribution {
        let counts: Vec<usize> = dist.values().copied().collect();
        if !counts.is_empty() {
            let max = *counts.iter().max().unwrap() as f64;
            let min = *counts.iter().min().unwrap() as f64;
            if max / min > 10.0 {
                issues.push("Severe class imbalance may affect training".to_string());
                suggestions
                    .push("Consider class weighting or oversampling minority classes".to_string());
            }
        }
    }

    let compatible = issues.is_empty();

    CompatibilityAnalysis {
        compatible,
        input_match,
        output_match,
        issues,
        suggestions,
    }
}

// =============================================================================
// Report Subcommand
// =============================================================================

fn execute_report(args: AnalyzeReportArgs) -> CliResult<()> {
    print_header("Generate Comprehensive Report");

    let workspace = load_workspace()?;

    if workspace.model.is_none() && workspace.dataset.is_none() {
        return Err(CliError::Other(
            "No model or dataset loaded. Use 'axonml load' first".to_string(),
        ));
    }

    print_info("Generating report...");

    let output_path = PathBuf::from(&args.output);
    if let Some(parent) = output_path.parent() {
        if !parent.exists() {
            fs::create_dir_all(parent)?;
        }
    }

    match args.format.as_str() {
        "html" => generate_html_report(&workspace, &output_path)?,
        "json" => generate_json_report(&workspace, &output_path)?,
        "md" | "markdown" => generate_markdown_report(&workspace, &output_path)?,
        _ => generate_text_report(&workspace, &output_path)?,
    }

    print_success(&format!("Report saved to: {}", args.output));

    Ok(())
}

fn generate_html_report(workspace: &super::load::WorkspaceState, path: &PathBuf) -> CliResult<()> {
    let mut html = String::new();
    html.push_str("<!DOCTYPE html>\n<html>\n<head>\n");
    html.push_str("<title>Axonml Analysis Report</title>\n");
    html.push_str("<style>\n");
    html.push_str("body { font-family: system-ui, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }\n");
    html.push_str("h1, h2, h3 { color: #1e3a5f; }\n");
    html.push_str("table { border-collapse: collapse; width: 100%; margin: 20px 0; }\n");
    html.push_str("th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }\n");
    html.push_str("th { background-color: #1e3a5f; color: white; }\n");
    html.push_str("tr:nth-child(even) { background-color: #f9f9f9; }\n");
    html.push_str(".metric { font-size: 24px; font-weight: bold; color: #1e3a5f; }\n");
    html.push_str(
        ".card { background: #f5f5f5; padding: 20px; border-radius: 8px; margin: 10px 0; }\n",
    );
    html.push_str("</style>\n</head>\n<body>\n");

    html.push_str("<h1>Axonml Analysis Report</h1>\n");
    html.push_str(&format!(
        "<p>Generated: {}</p>\n",
        chrono::Utc::now().to_rfc3339()
    ));

    if let Some(model) = &workspace.model {
        html.push_str("<h2>Model Analysis</h2>\n");
        html.push_str("<div class=\"card\">\n");
        html.push_str(&format!("<p><strong>Name:</strong> {}</p>\n", model.name));
        html.push_str(&format!("<p><strong>Path:</strong> {}</p>\n", model.path));
        html.push_str(&format!(
            "<p><strong>Format:</strong> {}</p>\n",
            model.format
        ));
        html.push_str(&format!(
            "<p><strong>Parameters:</strong> <span class=\"metric\">{}</span></p>\n",
            format_number(model.num_parameters)
        ));
        html.push_str(&format!(
            "<p><strong>Size:</strong> {}</p>\n",
            format_size(model.file_size)
        ));
        html.push_str("</div>\n");
    }

    if let Some(dataset) = &workspace.dataset {
        html.push_str("<h2>Dataset Analysis</h2>\n");
        html.push_str("<div class=\"card\">\n");
        html.push_str(&format!("<p><strong>Name:</strong> {}</p>\n", dataset.name));
        html.push_str(&format!("<p><strong>Path:</strong> {}</p>\n", dataset.path));
        html.push_str(&format!(
            "<p><strong>Type:</strong> {}</p>\n",
            dataset.data_type
        ));
        html.push_str(&format!(
            "<p><strong>Samples:</strong> <span class=\"metric\">{}</span></p>\n",
            dataset.num_samples
        ));
        if let Some(n) = dataset.num_classes {
            html.push_str(&format!("<p><strong>Classes:</strong> {n}</p>\n"));
        }
        html.push_str("</div>\n");
    }

    html.push_str("</body>\n</html>");

    fs::write(path, html)?;
    Ok(())
}

fn generate_json_report(workspace: &super::load::WorkspaceState, path: &PathBuf) -> CliResult<()> {
    let json = serde_json::to_string_pretty(workspace)?;
    fs::write(path, json)?;
    Ok(())
}

fn generate_markdown_report(
    workspace: &super::load::WorkspaceState,
    path: &PathBuf,
) -> CliResult<()> {
    let mut md = String::new();
    md.push_str("# Axonml Analysis Report\n\n");
    md.push_str(&format!(
        "Generated: {}\n\n",
        chrono::Utc::now().to_rfc3339()
    ));

    if let Some(model) = &workspace.model {
        md.push_str("## Model Analysis\n\n");
        md.push_str(&format!("- **Name:** {}\n", model.name));
        md.push_str(&format!("- **Path:** {}\n", model.path));
        md.push_str(&format!("- **Format:** {}\n", model.format));
        md.push_str(&format!(
            "- **Parameters:** {}\n",
            format_number(model.num_parameters)
        ));
        md.push_str(&format!("- **Size:** {}\n\n", format_size(model.file_size)));
    }

    if let Some(dataset) = &workspace.dataset {
        md.push_str("## Dataset Analysis\n\n");
        md.push_str(&format!("- **Name:** {}\n", dataset.name));
        md.push_str(&format!("- **Path:** {}\n", dataset.path));
        md.push_str(&format!("- **Type:** {}\n", dataset.data_type));
        md.push_str(&format!("- **Samples:** {}\n", dataset.num_samples));
        if let Some(n) = dataset.num_classes {
            md.push_str(&format!("- **Classes:** {n}\n"));
        }
        md.push('\n');
    }

    fs::write(path, md)?;
    Ok(())
}

fn generate_text_report(workspace: &super::load::WorkspaceState, path: &PathBuf) -> CliResult<()> {
    let mut text = String::new();
    text.push_str("FERRITE ANALYSIS REPORT\n");
    text.push_str(&"=".repeat(50));
    text.push_str(&format!(
        "\nGenerated: {}\n\n",
        chrono::Utc::now().to_rfc3339()
    ));

    if let Some(model) = &workspace.model {
        text.push_str("MODEL ANALYSIS\n");
        text.push_str(&"-".repeat(50));
        text.push_str(&format!("\nName: {}\n", model.name));
        text.push_str(&format!("Path: {}\n", model.path));
        text.push_str(&format!("Format: {}\n", model.format));
        text.push_str(&format!(
            "Parameters: {}\n",
            format_number(model.num_parameters)
        ));
        text.push_str(&format!("Size: {}\n\n", format_size(model.file_size)));
    }

    if let Some(dataset) = &workspace.dataset {
        text.push_str("DATASET ANALYSIS\n");
        text.push_str(&"-".repeat(50));
        text.push_str(&format!("\nName: {}\n", dataset.name));
        text.push_str(&format!("Path: {}\n", dataset.path));
        text.push_str(&format!("Type: {}\n", dataset.data_type));
        text.push_str(&format!("Samples: {}\n", dataset.num_samples));
        if let Some(n) = dataset.num_classes {
            text.push_str(&format!("Classes: {n}\n"));
        }
        text.push('\n');
    }

    fs::write(path, text)?;
    Ok(())
}

// =============================================================================
// Helper Functions
// =============================================================================

fn detect_format(path: &PathBuf) -> String {
    if let Some(ext) = path.extension() {
        match ext.to_string_lossy().to_lowercase().as_str() {
            "pt" | "pth" => "pytorch".to_string(),
            "safetensors" => "safetensors".to_string(),
            "onnx" => "onnx".to_string(),
            "axonml" => "axonml".to_string(),
            _ => "unknown".to_string(),
        }
    } else {
        "unknown".to_string()
    }
}

fn detect_data_type(path: &PathBuf) -> String {
    use walkdir::WalkDir;

    let mut counts: HashMap<&str, usize> = HashMap::new();

    for entry in WalkDir::new(path)
        .max_depth(3)
        .into_iter()
        .filter_map(std::result::Result::ok)
        .take(100)
    {
        if let Some(ext) = entry.path().extension() {
            match ext.to_string_lossy().to_lowercase().as_str() {
                "jpg" | "jpeg" | "png" | "bmp" => *counts.entry("image").or_insert(0) += 1,
                "csv" | "tsv" | "parquet" => *counts.entry("tabular").or_insert(0) += 1,
                "txt" | "json" | "jsonl" => *counts.entry("text").or_insert(0) += 1,
                "wav" | "mp3" | "flac" => *counts.entry("audio").or_insert(0) += 1,
                _ => {}
            }
        }
    }

    counts
        .into_iter()
        .max_by_key(|(_, count)| *count)
        .map_or_else(|| "unknown".to_string(), |(t, _)| t.to_string())
}

fn infer_layer_type(name: &str, shape: &[usize]) -> String {
    let name_lower = name.to_lowercase();

    if name_lower.contains("embed") {
        "Embedding".to_string()
    } else if name_lower.contains("conv") {
        if shape.len() == 4 {
            "Conv2d".to_string()
        } else if shape.len() == 3 {
            "Conv1d".to_string()
        } else {
            "Conv".to_string()
        }
    } else if name_lower.contains("bn") || name_lower.contains("batch_norm") {
        "BatchNorm".to_string()
    } else if name_lower.contains("ln") || name_lower.contains("layer_norm") {
        "LayerNorm".to_string()
    } else if name_lower.contains("attention") || name_lower.contains("attn") {
        "Attention".to_string()
    } else if name_lower.contains("fc")
        || name_lower.contains("linear")
        || name_lower.contains("dense")
    {
        "Linear".to_string()
    } else if name_lower.contains("bias") {
        "Bias".to_string()
    } else if name_lower.contains("weight") {
        if shape.len() == 2 {
            "Linear".to_string()
        } else if shape.len() == 4 {
            "Conv2d".to_string()
        } else {
            "Weight".to_string()
        }
    } else {
        "Unknown".to_string()
    }
}

fn infer_architecture(layers: &[LayerInfo]) -> String {
    let has_conv = layers.iter().any(|l| l.layer_type.contains("Conv"));
    let has_attention = layers.iter().any(|l| l.layer_type.contains("Attention"));
    let has_embed = layers.iter().any(|l| l.layer_type.contains("Embed"));

    if has_attention {
        "Transformer".to_string()
    } else if has_conv {
        "CNN".to_string()
    } else if has_embed {
        "Embedding-based".to_string()
    } else {
        "MLP".to_string()
    }
}

fn infer_output_size(model: &ModelAnalysis) -> Option<usize> {
    // Look for the last linear/dense layer
    for layer in model.layer_analysis.iter().rev() {
        if layer.layer_type == "Linear" && layer.shape.len() == 2 {
            return Some(layer.shape[0]); // Output dimension
        }
    }
    None
}

fn estimate_memory(num_params: usize) -> u64 {
    // Estimate: params * 4 bytes (f32) * 2 (gradients) + overhead
    (num_params as u64 * 4 * 2) + (1024 * 1024) // 1MB overhead
}

fn generate_model_recommendations(layers: &[LayerInfo], total_params: usize) -> Vec<String> {
    let mut recs = Vec::new();

    if total_params > 100_000_000 {
        recs.push("Large model - consider gradient checkpointing to reduce memory".to_string());
        recs.push("Use mixed precision (F16) training for faster training".to_string());
    }

    if total_params < 10_000 {
        recs.push("Small model - may underfit on complex tasks".to_string());
    }

    // Check for potential optimizations
    let linear_params: usize = layers
        .iter()
        .filter(|l| l.layer_type == "Linear")
        .map(|l| l.num_parameters)
        .sum();

    if linear_params as f64 / total_params as f64 > 0.8 {
        recs.push(
            "Model is mostly linear layers - consider using LoRA for fine-tuning".to_string(),
        );
    }

    recs
}

fn count_image_samples(
    path: &PathBuf,
    class_dist: &mut HashMap<String, usize>,
) -> (usize, Option<usize>) {
    use walkdir::WalkDir;

    let mut total = 0;

    if let Ok(entries) = fs::read_dir(path) {
        for entry in entries.filter_map(std::result::Result::ok) {
            if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
                let class_name = entry.file_name().to_string_lossy().to_string();
                if !class_name.starts_with('.') {
                    let count = WalkDir::new(entry.path())
                        .into_iter()
                        .filter_map(std::result::Result::ok)
                        .filter(|e| {
                            e.file_type().is_file()
                                && e.path().extension().is_some_and(|ext| {
                                    matches!(
                                        ext.to_string_lossy().to_lowercase().as_str(),
                                        "jpg" | "jpeg" | "png" | "bmp" | "gif"
                                    )
                                })
                        })
                        .count();

                    total += count;
                    class_dist.insert(class_name, count);
                }
            }
        }
    }

    let num_classes = if class_dist.len() > 1 {
        Some(class_dist.len())
    } else {
        None
    };
    (total, num_classes)
}

fn count_tabular_samples(
    path: &PathBuf,
    class_dist: &mut HashMap<String, usize>,
    max_samples: usize,
) -> (usize, Option<usize>) {
    use walkdir::WalkDir;

    for entry in WalkDir::new(path)
        .max_depth(2)
        .into_iter()
        .filter_map(std::result::Result::ok)
    {
        if entry.path().extension().is_some_and(|e| e == "csv") {
            if let Ok(content) = fs::read_to_string(entry.path()) {
                let lines: Vec<&str> = content.lines().collect();
                let num_samples = lines.len().saturating_sub(1);

                // Try to detect classes from last column
                for line in lines.iter().skip(1).take(max_samples) {
                    if let Some(label) = line.split(',').next_back() {
                        *class_dist.entry(label.trim().to_string()).or_insert(0) += 1;
                    }
                }

                let num_classes = if class_dist.len() > 1 && class_dist.len() < 100 {
                    Some(class_dist.len())
                } else {
                    None
                };

                return (num_samples, num_classes);
            }
        }
    }

    (0, None)
}

fn calculate_quality_score(
    _data_type: &str,
    num_samples: usize,
    class_dist: &HashMap<String, usize>,
    _file_stats: &FileStats,
) -> f64 {
    let mut score: f64 = 5.0; // Base score

    // Sample count bonus
    if num_samples > 10000 {
        score += 2.0;
    } else if num_samples > 1000 {
        score += 1.0;
    } else if num_samples < 100 {
        score -= 2.0;
    }

    // Class balance bonus
    if !class_dist.is_empty() {
        let counts: Vec<usize> = class_dist.values().copied().collect();
        let max = *counts.iter().max().unwrap_or(&1) as f64;
        let min = *counts.iter().min().unwrap_or(&1) as f64;
        let ratio = max / min.max(1.0);

        if ratio < 2.0 {
            score += 2.0;
        } else if ratio < 5.0 {
            score += 1.0;
        } else if ratio > 10.0 {
            score -= 1.0;
        }
    }

    score.clamp(0.0, 10.0)
}

fn find_data_issues(
    _data_type: &str,
    num_samples: usize,
    class_dist: &HashMap<String, usize>,
    _file_stats: &FileStats,
) -> Vec<String> {
    let mut issues = Vec::new();

    if num_samples < 100 {
        issues.push("Very small dataset - may lead to overfitting".to_string());
    }

    if !class_dist.is_empty() {
        let counts: Vec<usize> = class_dist.values().copied().collect();
        let max = *counts.iter().max().unwrap_or(&1) as f64;
        let min = *counts.iter().min().unwrap_or(&1) as f64;

        if max / min.max(1.0) > 10.0 {
            issues.push("Severe class imbalance detected".to_string());
        }

        // Check for very small classes
        for (class, count) in class_dist {
            if *count < 10 {
                issues.push(format!(
                    "Class '{}' has only {} samples",
                    truncate(class, 20),
                    count
                ));
            }
        }
    }

    issues
}

fn generate_data_recommendations(
    data_type: &str,
    num_samples: usize,
    class_dist: &HashMap<String, usize>,
    _issues: &[String],
) -> Vec<String> {
    let mut recs = Vec::new();

    if num_samples < 1000 {
        recs.push("Consider data augmentation to increase effective dataset size".to_string());
    }

    if !class_dist.is_empty() {
        let counts: Vec<usize> = class_dist.values().copied().collect();
        let max = *counts.iter().max().unwrap_or(&1) as f64;
        let min = *counts.iter().min().unwrap_or(&1) as f64;

        if max / min.max(1.0) > 5.0 {
            recs.push("Use class weighting or oversampling for imbalanced classes".to_string());
        }
    }

    if data_type == "image" {
        recs.push("Consider using pretrained models with transfer learning".to_string());
    }

    recs
}

fn save_analysis_report<T: Serialize>(analysis: &T, path: &str, format: &str) -> CliResult<()> {
    let content = match format {
        "json" => serde_json::to_string_pretty(analysis)?,
        _ => serde_json::to_string_pretty(analysis)?, // Default to JSON
    };
    fs::write(path, content)?;
    Ok(())
}

fn save_data_report(analysis: &DataAnalysis, path: &str, format: &str) -> CliResult<()> {
    save_analysis_report(analysis, path, format)
}

fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{bytes} bytes")
    }
}

fn format_number(n: usize) -> String {
    if n >= 1_000_000_000 {
        format!("{:.2}B", n as f64 / 1_000_000_000.0)
    } else if n >= 1_000_000 {
        format!("{:.2}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.2}K", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

fn truncate(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_infer_layer_type() {
        assert_eq!(infer_layer_type("conv1.weight", &[64, 3, 3, 3]), "Conv2d");
        assert_eq!(infer_layer_type("fc1.weight", &[128, 64]), "Linear");
        assert_eq!(infer_layer_type("embed.weight", &[1000, 256]), "Embedding");
    }

    #[test]
    fn test_format_size() {
        assert_eq!(format_size(500), "500 bytes");
        assert_eq!(format_size(1024), "1.00 KB");
    }

    #[test]
    fn test_calculate_quality_score() {
        let mut dist = HashMap::new();
        dist.insert("class1".to_string(), 100);
        dist.insert("class2".to_string(), 100);

        let score = calculate_quality_score("image", 10000, &dist, &FileStats::default());
        assert!(score > 5.0); // Good score for balanced dataset
    }
}