ddex-builder 0.4.5

Deterministic DDEX XML builder with smart normalization
Documentation
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
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
//! DDEX Builder CLI - Comprehensive command-line interface for DDEX XML processing
//!
//! This CLI provides tools for building, converting, validating, and comparing DDEX XML files
//! with deterministic output and support for various partner presets.

use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{generate, Shell};
use console::style;
use ddex_builder::presets::{DdexVersion, MessageProfile};
use ddex_builder::*;
use indexmap::IndexMap;
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use serde_json::Value as JsonValue;
use std::fs;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process;

#[derive(Parser)]
#[command(
    name = "ddex-builder",
    about = "DDEX Builder CLI - High-performance DDEX XML processing toolkit",
    long_about = "A comprehensive command-line interface for building, converting, validating, and comparing DDEX XML files with deterministic output and partner preset support.",
    version = env!("CARGO_PKG_VERSION"),
    author = "Kevin Marques Moo"
)]
#[command(propagate_version = true)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Enable verbose output
    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
    verbose: u8,

    /// Suppress all non-error output
    #[arg(short, long, global = true)]
    quiet: bool,

    /// Control color output
    #[arg(long, global = true, value_enum, default_value_t = ColorChoice::Auto)]
    color: ColorChoice,

    /// Path to configuration file
    #[arg(long, global = true)]
    config: Option<PathBuf>,
}

#[derive(Subcommand)]
enum Commands {
    /// Build DDEX XML from structured data
    Build(BuildCommand),
    /// Convert DDEX XML between versions
    Convert(ConvertCommand),
    /// Compare two DDEX files semantically
    Diff(DiffCommand),
    /// Validate DDEX XML files
    Validate(ValidateCommand),
    /// Generate schemas for validation
    Schema(SchemaCommand),
    /// Process multiple files in parallel
    Batch(BatchCommand),
    /// Validate determinism guarantees
    Guarantees(GuaranteesCommand),
    /// List and apply partner presets
    Preset(PresetCommand),
    /// Watch files for changes and rebuild
    Watch(WatchCommand),
    /// Run HTTP API server for builder operations
    Server(ServerCommand),
    /// Generate shell completions
    Completions(CompletionsCommand),
}

#[derive(Args)]
struct BuildCommand {
    /// Input file (JSON/YAML/TOML) or '-' for stdin
    #[arg(short, long)]
    input: Option<PathBuf>,

    /// Output file path or '-' for stdout
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// DDEX version to generate
    #[arg(long = "ddex-version", value_enum)]
    version: Option<DdexVersionArg>,

    /// Content profile to use
    #[arg(short, long)]
    profile: Option<String>,

    /// Partner preset configuration
    #[arg(long, value_enum)]
    preset: Option<PresetChoice>,

    /// Validate before building
    #[arg(long)]
    validate: bool,

    /// Input data format (auto-detected if not specified)
    #[arg(long, value_enum)]
    format: Option<InputFormat>,

    /// Enable strict validation
    #[arg(long)]
    strict: bool,

    /// Verify determinism by building multiple times
    #[arg(long)]
    verify_determinism: bool,

    /// Number of iterations for determinism verification (default: 3)
    #[arg(long, default_value_t = 3)]
    determinism_iterations: usize,
}

#[derive(Args)]
struct ConvertCommand {
    /// Input DDEX XML file or '-' for stdin
    #[arg(short, long)]
    input: Option<PathBuf>,

    /// Source DDEX version
    #[arg(short, long, value_enum)]
    from: DdexVersionArg,

    /// Target DDEX version
    #[arg(short, long, value_enum)]
    to: DdexVersionArg,

    /// Output file path or '-' for stdout
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Generate conversion report
    #[arg(long)]
    report: Option<PathBuf>,

    /// Enable lossy conversion warnings
    #[arg(long)]
    allow_lossy: bool,
}

#[derive(Args)]
struct DiffCommand {
    /// First DDEX XML file
    file1: PathBuf,

    /// Second DDEX XML file
    file2: PathBuf,

    /// Output format
    #[arg(short, long, value_enum, default_value_t = DiffFormat::Human)]
    format: DiffFormat,

    /// Output file path (default: stdout)
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Include technical details in diff
    #[arg(long)]
    detailed: bool,

    /// Ignore whitespace differences
    #[arg(long)]
    ignore_whitespace: bool,
}

#[derive(Args)]
struct ValidateCommand {
    /// DDEX XML files to validate
    files: Vec<PathBuf>,

    /// DDEX version for validation
    #[arg(long = "ddex-version", value_enum)]
    version: Option<DdexVersionArg>,

    /// Content profile for validation
    #[arg(short, long)]
    profile: Option<String>,

    /// Partner preset for validation
    #[arg(long, value_enum)]
    preset: Option<PresetChoice>,

    /// Enable strict validation rules
    #[arg(long)]
    strict: bool,

    /// Output format for validation results
    #[arg(long, value_enum, default_value_t = ValidateFormat::Human)]
    output_format: ValidateFormat,

    /// Stop at first validation error
    #[arg(long)]
    fail_fast: bool,
}

#[derive(Args)]
struct SchemaCommand {
    /// DDEX version for schema generation
    #[arg(long = "ddex-version", value_enum)]
    version: DdexVersionArg,

    /// Content profile
    #[arg(short, long)]
    profile: Option<String>,

    /// Output format
    #[arg(short, long, value_enum, default_value_t = SchemaFormat::Json)]
    format: SchemaFormat,

    /// Output file path or '-' for stdout
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Include documentation in schema
    #[arg(long)]
    with_docs: bool,
}

#[derive(Args)]
struct BatchCommand {
    /// Configuration file for batch processing
    #[arg(short, long)]
    config: PathBuf,

    /// Number of worker threads
    #[arg(short, long, default_value_t = num_cpus::get())]
    workers: usize,

    /// Continue processing on errors
    #[arg(long)]
    continue_on_error: bool,

    /// Generate summary report
    #[arg(long)]
    report: Option<PathBuf>,
}

#[derive(Args)]
struct GuaranteesCommand {
    /// Input file (JSON/YAML/TOML) for build request
    #[arg(short, long)]
    input: PathBuf,

    /// Output format for guarantee report
    #[arg(short, long, value_enum, default_value_t = GuaranteeFormat::Human)]
    format: GuaranteeFormat,

    /// Output file path (default: stdout)
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Run comprehensive stress tests
    #[arg(long)]
    thorough: bool,

    /// Number of determinism verification iterations
    #[arg(long, default_value_t = 3)]
    iterations: usize,

    /// Only show failed guarantees
    #[arg(long)]
    failures_only: bool,

    /// Include detailed evidence in report
    #[arg(long)]
    include_evidence: bool,
}

#[derive(Args)]
struct PresetCommand {
    /// Preset operation
    #[command(subcommand)]
    operation: PresetOperation,
}

#[derive(Subcommand)]
enum PresetOperation {
    /// List available presets
    List(PresetListCommand),
    /// Show preset details
    Show(PresetShowCommand),
    /// Apply preset to input data
    Apply(PresetApplyCommand),
}

#[derive(Args)]
struct PresetListCommand {
    /// Filter by DDEX version
    #[arg(long, value_enum)]
    version: Option<DdexVersionArg>,

    /// Filter by partner
    #[arg(long, value_enum)]
    partner: Option<PresetChoice>,

    /// Output format
    #[arg(short, long, value_enum, default_value_t = PresetListFormat::Human)]
    format: PresetListFormat,
}

#[derive(Args)]
struct PresetShowCommand {
    /// Preset identifier
    preset: String,

    /// Output format
    #[arg(short, long, value_enum, default_value_t = PresetShowFormat::Human)]
    format: PresetShowFormat,

    /// Include full configuration details
    #[arg(long)]
    detailed: bool,
}

#[derive(Args)]
struct PresetApplyCommand {
    /// Preset identifier
    preset: String,

    /// Input file (JSON/YAML/TOML) or '-' for stdin
    #[arg(short, long)]
    input: Option<PathBuf>,

    /// Output file path or '-' for stdout
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Input data format (auto-detected if not specified)
    #[arg(long, value_enum)]
    format: Option<InputFormat>,

    /// Override preset DDEX version
    #[arg(long, value_enum)]
    version_override: Option<DdexVersionArg>,

    /// Validate output after applying preset
    #[arg(long)]
    validate: bool,
}

#[derive(Args)]
struct WatchCommand {
    /// Directory or file to watch
    #[arg(short, long)]
    path: PathBuf,

    /// Pattern to match files (glob syntax)
    #[arg(short, long, default_value = "**/*.{json,yaml,yml,toml}")]
    pattern: String,

    /// Command to run on file changes
    #[arg(short, long)]
    command: Option<String>,

    /// Output directory for built files
    #[arg(short, long)]
    output_dir: Option<PathBuf>,

    /// DDEX version to use for building
    #[arg(long, value_enum)]
    version: Option<DdexVersionArg>,

    /// Preset to apply
    #[arg(long, value_enum)]
    preset: Option<PresetChoice>,

    /// Debounce delay in milliseconds (default: 500ms)
    #[arg(long, default_value_t = 500)]
    debounce: u64,

    /// Run initial build on startup
    #[arg(long)]
    initial_build: bool,

    /// Recursive watch subdirectories
    #[arg(short, long)]
    recursive: bool,

    /// Exclude patterns (glob syntax)
    #[arg(long)]
    exclude: Vec<String>,
}

#[derive(Args)]
struct ServerCommand {
    /// Server bind address
    #[arg(short, long, default_value = "127.0.0.1")]
    bind: String,

    /// Server port
    #[arg(short, long, default_value_t = 8080)]
    port: u16,

    /// Number of worker threads
    #[arg(short, long, default_value_t = num_cpus::get())]
    workers: usize,

    /// Enable CORS for cross-origin requests
    #[arg(long)]
    cors: bool,

    /// Maximum request size in MB
    #[arg(long, default_value_t = 10)]
    max_request_size: usize,

    /// Request timeout in seconds
    #[arg(long, default_value_t = 30)]
    timeout: u64,

    /// Enable request logging
    #[arg(long)]
    log_requests: bool,

    /// TLS certificate file (for HTTPS)
    #[arg(long)]
    tls_cert: Option<PathBuf>,

    /// TLS private key file (for HTTPS)
    #[arg(long)]
    tls_key: Option<PathBuf>,

    /// Rate limiting: requests per minute per IP
    #[arg(long)]
    rate_limit: Option<u32>,
}

#[derive(Args)]
struct CompletionsCommand {
    /// Shell to generate completions for
    #[arg(value_enum)]
    shell: Shell,

    /// Output file path (default: stdout)
    #[arg(short, long)]
    output: Option<PathBuf>,
}

#[derive(ValueEnum, Clone, Debug)]
enum ColorChoice {
    Auto,
    Always,
    Never,
}

#[derive(ValueEnum, Clone, Copy, Debug)]
enum DdexVersionArg {
    #[value(name = "3.8.2")]
    V382,
    #[value(name = "4.1")]
    V41,
    #[value(name = "4.2")]
    V42,
    #[value(name = "4.3")]
    V43,
    #[value(name = "4.4")]
    V44,
}

#[derive(ValueEnum, Clone, Copy, Debug)]
enum PresetChoice {
    /// Generic audio album preset (DDEX-compliant baseline)
    AudioAlbum,
    /// Generic audio single preset (DDEX-compliant baseline)
    AudioSingle,
    /// Generic video single preset (DDEX-compliant baseline)
    VideoSingle,
    /// Generic compilation preset (DDEX-compliant baseline)
    Compilation,
    /// YouTube album preset (based on public documentation)
    YoutubeAlbum,
    /// YouTube video preset (based on public documentation)
    YoutubeVideo,
    /// YouTube single preset (based on public documentation)
    YoutubeSingle,
}

#[derive(ValueEnum, Clone, Debug)]
enum InputFormat {
    Json,
    Yaml,
    Toml,
}

#[derive(ValueEnum, Clone, Debug, PartialEq)]
enum DiffFormat {
    Human,
    Json,
    Update,
}

#[derive(ValueEnum, Clone, Debug)]
enum ValidateFormat {
    Human,
    Json,
    Junit,
}

#[derive(ValueEnum, Clone, Debug)]
enum SchemaFormat {
    Json,
    Typescript,
    Python,
}

#[derive(ValueEnum, Clone, Debug)]
enum GuaranteeFormat {
    Human,
    Json,
    Yaml,
}

#[derive(ValueEnum, Clone, Debug)]
enum PresetListFormat {
    Human,
    Json,
    Table,
}

#[derive(ValueEnum, Clone, Debug)]
enum PresetShowFormat {
    Human,
    Json,
    Yaml,
}

impl From<DdexVersionArg> for DdexVersion {
    fn from(version: DdexVersionArg) -> Self {
        match version {
            DdexVersionArg::V382 => DdexVersion::Ern382,
            DdexVersionArg::V41 => DdexVersion::Ern41,
            DdexVersionArg::V42 => DdexVersion::Ern42,
            DdexVersionArg::V43 => DdexVersion::Ern43,
            DdexVersionArg::V44 => DdexVersion::Ern43, // Map V44 to Ern43 since Ern44 doesn't exist yet
        }
    }
}

fn main() {
    let cli = Cli::parse();

    // Setup logging based on verbosity
    setup_logging(cli.verbose, cli.quiet);

    // Setup color output
    setup_colors(cli.color);

    // Load configuration if specified
    let config = cli
        .config
        .as_ref()
        .map(|p| load_config(p))
        .unwrap_or_default();

    let result = match cli.command {
        Commands::Build(cmd) => handle_build_command(cmd, &config),
        Commands::Convert(cmd) => handle_convert_command(cmd, &config),
        Commands::Diff(cmd) => handle_diff_command(cmd, &config),
        Commands::Validate(cmd) => handle_validate_command(cmd, &config),
        Commands::Schema(cmd) => handle_schema_command(cmd, &config),
        Commands::Batch(cmd) => handle_batch_command(cmd, &config),
        Commands::Guarantees(cmd) => handle_guarantees_command(cmd, &config),
        Commands::Preset(cmd) => handle_preset_command(cmd, &config),
        Commands::Watch(cmd) => handle_watch_command(cmd, &config),
        Commands::Server(cmd) => handle_server_command(cmd, &config),
        Commands::Completions(cmd) => handle_completions_command(cmd),
    };

    if let Err(e) = result {
        eprintln!("{} {}", style("Error:").red().bold(), e);
        process::exit(1);
    }
}

fn setup_logging(verbosity: u8, quiet: bool) {
    if quiet {
        return;
    }

    let level = match verbosity {
        0 => tracing::Level::WARN,
        1 => tracing::Level::INFO,
        2 => tracing::Level::DEBUG,
        _ => tracing::Level::TRACE,
    };

    tracing_subscriber::fmt()
        .with_max_level(level)
        .with_target(false)
        .init();
}

fn setup_colors(color_choice: ColorChoice) {
    match color_choice {
        ColorChoice::Always => {
            console::set_colors_enabled(true);
            console::set_colors_enabled_stderr(true);
        }
        ColorChoice::Never => {
            console::set_colors_enabled(false);
            console::set_colors_enabled_stderr(false);
        }
        ColorChoice::Auto => {
            // Default behavior - colors enabled for TTY
        }
    }
}

fn load_config(_path: &Path) -> ConfigFile {
    // TODO: Implement configuration file loading
    ConfigFile::default()
}

#[derive(Default)]
struct ConfigFile {
    // Configuration options that can be loaded from file
}

fn handle_build_command(
    cmd: BuildCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let input_data = read_input_data(&cmd.input, cmd.format)?;

    // Create builder with optional preset
    let mut builder = Builder::new();

    if let Some(preset) = cmd.preset {
        let preset_name = preset_to_string(&preset);
        builder
            .apply_preset(&preset_name, false)
            .map_err(|e| format!("Failed to apply preset '{}': {}", preset_name, e))?;
    }

    if let Some(version) = cmd.version {
        builder.with_version(version.into());
    }

    // Validate input if requested
    if cmd.validate {
        validate_input_data(&input_data, &builder, cmd.strict)?;
    }

    // Build the XML
    let xml_output = build_ddex_xml(&input_data, &builder)?;

    // Verify determinism if requested
    if cmd.verify_determinism {
        verify_build_determinism(&input_data, &builder, cmd.determinism_iterations)?;
    }

    // Write output
    write_output(&xml_output, &cmd.output)?;

    if !is_quiet() {
        println!("{} DDEX XML built successfully", style("").green());
        if let Some(preset) = cmd.preset {
            println!("  Preset: {}", preset_to_string(&preset));
        }
        if let Some(version) = cmd.version {
            println!("  Version: {:?}", version);
        }
        if cmd.verify_determinism {
            println!(
                "  {} Determinism verified with {} iterations",
                style("").green(),
                cmd.determinism_iterations
            );
        }
    }

    Ok(())
}

fn handle_convert_command(
    cmd: ConvertCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let input_xml = read_input_string(&cmd.input)?;

    let builder = Builder::new();
    let conversion_options = ConversionOptions {
        detailed_reports: true,
        preserve_unknown: cmd.allow_lossy,
        preserve_comments: true,
        ..Default::default()
    };

    let result = builder.convert_version(
        &input_xml,
        cmd.from.into(),
        cmd.to.into(),
        Some(conversion_options),
    )?;

    match result {
        versions::ConverterResult::Success { xml, report } => {
            // Write converted XML
            write_output(&xml, &cmd.output)?;

            // Generate report if requested
            if let Some(report_path) = cmd.report {
                let report_json = serde_json::to_string_pretty(&report)?;
                fs::write(report_path, report_json)?;
            }

            if !is_quiet() {
                println!("{} Conversion completed", style("").green());
                println!("  From: {:?} → To: {:?}", cmd.from, cmd.to);
                if !report.warnings.is_empty() {
                    println!("  {} warnings generated", report.warnings.len());
                }
            }
        }
        versions::ConverterResult::Failure { error, report: _ } => {
            return Err(format!("Conversion failed: {}", error).into());
        }
    }

    Ok(())
}

fn handle_diff_command(
    cmd: DiffCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let xml1 = fs::read_to_string(&cmd.file1)?;
    let xml2 = fs::read_to_string(&cmd.file2)?;

    let _diff_config = diff::DiffConfig {
        ignore_formatting: cmd.ignore_whitespace,
        ..Default::default()
    };
    // TODO: Parse XML to AST for proper semantic diffing
    // For now, create a placeholder changeset
    let changeset = diff::types::ChangeSet::new();

    let formatted_output = match cmd.format {
        DiffFormat::Human => diff::formatter::DiffFormatter::format_summary(&changeset),
        DiffFormat::Json => serde_json::to_string_pretty(&changeset)?,
        DiffFormat::Update => {
            let mut update_generator = messages::UpdateGenerator::new();
            let _update_message = update_generator.create_update(&xml1, &xml2, "cli-generated")?;
            // TODO: Implement XML serialization for UpdateReleaseMessage
            format!("<!-- Update message XML would be serialized here -->")
        }
    };

    write_output(&formatted_output, &cmd.output)?;

    if !is_quiet() && cmd.format == DiffFormat::Human {
        if changeset.changes.is_empty() {
            println!("{} Files are identical", style("").green());
        } else {
            println!(
                "{} {} differences found",
                style("!").yellow(),
                changeset.changes.len()
            );
        }
    }

    Ok(())
}

fn handle_validate_command(
    cmd: ValidateCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut all_valid = true;
    let mut results = Vec::new();

    for file_path in &cmd.files {
        let _xml_content = fs::read_to_string(file_path)?;

        let mut builder = Builder::new();
        if let Some(preset) = &cmd.preset {
            let preset_name = preset_to_string(preset);
            builder.apply_preset(&preset_name, false)?;
        }

        let validation_config = ValidationConfig {
            level: if cmd.strict {
                PreflightLevel::Strict
            } else {
                PreflightLevel::Warn
            },
            profile: cmd.profile.clone(),
            ..Default::default()
        };

        let _validator = PreflightValidator::new(validation_config);
        // TODO: Parse XML content to BuildRequest for validation
        // For now, create a placeholder result
        let result = ValidationResult {
            errors: Vec::new(),
            warnings: Vec::new(),
            info: Vec::new(),
            passed: true,
        };

        let file_valid = result.errors.is_empty();
        all_valid = all_valid && file_valid;

        results.push((file_path.clone(), result));

        if cmd.fail_fast && !file_valid {
            break;
        }
    }

    // Output results
    match cmd.output_format {
        ValidateFormat::Human => {
            for (file_path, result) in &results {
                print_validation_result_human(file_path, result);
            }
        }
        ValidateFormat::Json => {
            let json_output = serde_json::to_string_pretty(&results)?;
            println!("{}", json_output);
        }
        ValidateFormat::Junit => {
            let junit_output = format_junit_results(&results)?;
            println!("{}", junit_output);
        }
    }

    if !all_valid {
        process::exit(1);
    }

    Ok(())
}

fn handle_schema_command(
    cmd: SchemaCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let _schema_config = schema::SchemaConfig {
        include_descriptions: cmd.with_docs,
        ..Default::default()
    };

    // Use a default profile for now - this could be enhanced to support actual profiles
    let profile = MessageProfile::AudioAlbum;
    let generator = schema::SchemaGenerator::new(cmd.version.into(), profile);
    let schema_result = generator.generate_complete_schema()?;
    let schema_output = match cmd.format {
        SchemaFormat::Json => serde_json::to_string_pretty(&schema_result.schema)?,
        SchemaFormat::Typescript => generator.generate_typescript_types(&schema_result.schema)?,
        SchemaFormat::Python => generator.generate_python_types(&schema_result.schema)?,
    };

    write_output(&schema_output, &cmd.output)?;

    if !is_quiet() {
        println!("{} Schema generated successfully", style("").green());
        println!("  Format: {:?}", cmd.format);
        println!("  Version: {:?}", cmd.version);
    }

    Ok(())
}

fn handle_batch_command(
    cmd: BatchCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    let batch_config = load_batch_config(&cmd.config)?;

    // Setup thread pool
    let pool = rayon::ThreadPoolBuilder::new()
        .num_threads(cmd.workers)
        .build()?;

    let progress_bar = if !is_quiet() {
        let pb = ProgressBar::new(batch_config.tasks.len() as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}")
                .unwrap(),
        );
        Some(pb)
    } else {
        None
    };

    let results: Vec<BatchResult> = pool.install(|| {
        batch_config
            .tasks
            .par_iter()
            .enumerate()
            .map(|(i, task)| {
                let result = process_batch_task(task);
                if let Some(pb) = &progress_bar {
                    pb.set_message(format!("Processing {}", task.input_file.display()));
                    pb.inc(1);
                }
                BatchResult {
                    task_id: i,
                    success: result.is_ok(),
                    error: result.err().map(|e| e.to_string()),
                }
            })
            .collect()
    });

    if let Some(pb) = &progress_bar {
        pb.finish_with_message("Batch processing completed");
    }

    // Generate report
    let successful = results.iter().filter(|r| r.success).count();
    let failed = results.len() - successful;

    if !is_quiet() {
        println!("\n{} Batch processing completed", style("").green());
        println!("  Successful: {}", successful);
        if failed > 0 {
            println!("  Failed: {}", style(failed).red());
        }
    }

    if let Some(report_path) = cmd.report {
        let report = BatchReport {
            total_tasks: results.len(),
            successful,
            failed,
            results,
        };
        let report_json = serde_json::to_string_pretty(&report)?;
        fs::write(report_path, report_json)?;
    }

    if failed > 0 && !cmd.continue_on_error {
        process::exit(1);
    }

    Ok(())
}

fn handle_guarantees_command(
    cmd: GuaranteesCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    use ddex_builder::builder::BuildRequest;
    use ddex_builder::determinism::{DeterminismConfig, DeterminismVerifier};
    use ddex_builder::guarantees::generate_guarantee_report;

    // Read and parse input data
    let input_data = read_input_data(&Some(cmd.input.clone()), None)?;

    // Parse JSON data into BuildRequest structure
    let request: BuildRequest = serde_json::from_value(input_data)?;

    if !is_quiet() {
        println!(
            "🔍 Validating determinism guarantees for {}",
            cmd.input.display()
        );
        if cmd.thorough {
            println!("   Running comprehensive stress tests...");
        }
    }

    // Generate guarantee report
    let report = if cmd.thorough {
        // Run thorough verification with stress tests
        let _verifier = DeterminismVerifier::new(DeterminismConfig::default());
        let _result = DeterminismVerifier::thorough_check(&request, cmd.iterations)?;

        // Generate full report
        generate_guarantee_report(&request, &DeterminismConfig::default())?
    } else {
        // Quick guarantee validation
        generate_guarantee_report(&request, &DeterminismConfig::default())?
    };

    // Format output
    let output_content = match cmd.format {
        GuaranteeFormat::Human => {
            format_guarantee_report_human(&report, cmd.failures_only, cmd.include_evidence)
        }
        GuaranteeFormat::Json => {
            if cmd.failures_only {
                let failed_results: Vec<_> =
                    report.failed_guarantees().into_iter().cloned().collect();
                serde_json::to_string_pretty(&failed_results)?
            } else {
                serde_json::to_string_pretty(&report)?
            }
        }
        GuaranteeFormat::Yaml => {
            if cmd.failures_only {
                let failed_results: Vec<_> =
                    report.failed_guarantees().into_iter().cloned().collect();
                serde_yaml::to_string(&failed_results)?
            } else {
                serde_yaml::to_string(&report)?
            }
        }
    };

    // Write output
    write_output(&output_content, &cmd.output)?;

    // Print summary if not quiet and using human format
    if !is_quiet() && matches!(cmd.format, GuaranteeFormat::Human) {
        println!("\n{}", report.summary());

        if !report.overall_pass {
            let critical_failures = report.critical_failures();
            if !critical_failures.is_empty() {
                println!("\n{} Critical failures detected:", style("").red().bold());
                for failure in critical_failures {
                    println!("  {} {:?}", style("").red(), failure.guarantee);
                }
                return Err("Critical determinism guarantees failed".into());
            }
        }
    }

    // Exit with error if guarantees failed and we're not just generating a report
    if !report.overall_pass && cmd.output.is_none() {
        std::process::exit(1);
    }

    Ok(())
}

fn format_guarantee_report_human(
    report: &ddex_builder::guarantees::GuaranteeReport,
    failures_only: bool,
    include_evidence: bool,
) -> String {
    let mut output = String::new();

    output.push_str(&format!("# Determinism Guarantee Report\n"));
    output.push_str(&format!(
        "Generated: {}\n\n",
        report.timestamp.format("%Y-%m-%d %H:%M:%S UTC")
    ));

    if !failures_only {
        output.push_str(&format!("## Summary\n"));
        output.push_str(&format!(
            "- Total guarantees: {}\n",
            report.total_guarantees
        ));
        output.push_str(&format!(
            "- Passed: {} ({:.1}%)\n",
            report.passed_guarantees, report.success_rate
        ));
        output.push_str(&format!(
            "- Failed: {}\n\n",
            report.total_guarantees - report.passed_guarantees
        ));
    }

    let results_to_show = if failures_only {
        report.failed_guarantees()
    } else {
        report.results.iter().collect()
    };

    if !results_to_show.is_empty() {
        output.push_str(&format!(
            "## {}\n",
            if failures_only {
                "Failed Guarantees"
            } else {
                "Results"
            }
        ));

        for result in results_to_show {
            let status = if result.passed { "" } else { "" };
            let priority = format!("{:?}", result.guarantee.priority()).to_uppercase();

            output.push_str(&format!(
                "\n### {} {:?} ({})\n",
                status, result.guarantee, priority
            ));
            output.push_str(&format!(
                "**Description:** {}\n\n",
                result.guarantee.description()
            ));
            output.push_str(&format!("**Status:** {}\n\n", result.details));

            if include_evidence {
                if let Some(evidence) = &result.evidence {
                    output.push_str(&format!("**Evidence:** {}\n\n", evidence));
                }
            }
        }
    }

    output
}

fn handle_preset_command(
    cmd: PresetCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    match cmd.operation {
        PresetOperation::List(list_cmd) => {
            // TODO: Implement preset listing from ddex_builder::presets
            let presets = get_available_presets(list_cmd.version, list_cmd.partner)?;

            match list_cmd.format {
                PresetListFormat::Human => {
                    println!("Available DDEX Presets:");
                    println!("{:-<50}", "");
                    for preset in &presets {
                        println!(
                            "{:<20} {:<10} {}",
                            preset.name, preset.version, preset.description
                        );
                    }
                }
                PresetListFormat::Json => {
                    println!("{}", serde_json::to_string_pretty(&presets)?);
                }
                PresetListFormat::Table => {
                    println!(
                        "{:<20} {:<10} {:<15} {}",
                        "Name", "Version", "Partner", "Description"
                    );
                    println!("{:-<80}", "");
                    for preset in &presets {
                        println!(
                            "{:<20} {:<10} {:<15} {}",
                            preset.name, preset.version, preset.partner, preset.description
                        );
                    }
                }
            }
        }
        PresetOperation::Show(show_cmd) => {
            let preset_details = get_preset_details(&show_cmd.preset)?;

            match show_cmd.format {
                PresetShowFormat::Human => {
                    println!("Preset: {}", preset_details.name);
                    println!("Partner: {}", preset_details.partner);
                    println!("Version: {}", preset_details.version);
                    println!("Description: {}", preset_details.description);
                    if show_cmd.detailed {
                        println!("\nConfiguration:");
                        println!("{:#?}", preset_details.config);
                    }
                }
                PresetShowFormat::Json => {
                    if show_cmd.detailed {
                        println!("{}", serde_json::to_string_pretty(&preset_details)?);
                    } else {
                        let summary = PresetSummary {
                            name: preset_details.name,
                            partner: preset_details.partner,
                            version: preset_details.version,
                            description: preset_details.description,
                        };
                        println!("{}", serde_json::to_string_pretty(&summary)?);
                    }
                }
                PresetShowFormat::Yaml => {
                    if show_cmd.detailed {
                        println!("{}", serde_yaml::to_string(&preset_details)?);
                    } else {
                        let summary = PresetSummary {
                            name: preset_details.name,
                            partner: preset_details.partner,
                            version: preset_details.version,
                            description: preset_details.description,
                        };
                        println!("{}", serde_yaml::to_string(&summary)?);
                    }
                }
            }
        }
        PresetOperation::Apply(apply_cmd) => {
            let input_data = read_input_data(&apply_cmd.input, apply_cmd.format)?;

            let mut builder = Builder::new();
            builder.apply_preset(&apply_cmd.preset, false)?;

            if let Some(version_override) = apply_cmd.version_override {
                builder.with_version(version_override.into());
            }

            let xml_output = build_ddex_xml(&input_data, &builder)?;

            if apply_cmd.validate {
                // TODO: Validate the output
                if !is_quiet() {
                    println!("{} Validation passed", style("").green());
                }
            }

            write_output(&xml_output, &apply_cmd.output)?;

            if !is_quiet() {
                println!(
                    "{} Preset '{}' applied successfully",
                    style("").green(),
                    apply_cmd.preset
                );
            }
        }
    }

    Ok(())
}

fn handle_watch_command(
    cmd: WatchCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    if !is_quiet() {
        println!("👀 Watching {} for changes...", cmd.path.display());
        println!("   Pattern: {}", cmd.pattern);
        if let Some(ref output_dir) = cmd.output_dir {
            println!("   Output: {}", output_dir.display());
        }
        println!("   Press Ctrl+C to stop");
    }

    // Initial build if requested
    if cmd.initial_build {
        if !is_quiet() {
            println!("🔨 Running initial build...");
        }
        run_watch_build(&cmd)?;
    }

    // TODO: Implement file watching using notify crate
    // For now, just simulate watching

    // In a real implementation, this would use the notify crate:
    // let (tx, rx) = mpsc::channel::<notify::Event>();
    // let mut watcher = notify::recommended_watcher(move |res| {
    //     match res {
    //         Ok(event) => tx.send(event).unwrap(),
    //         Err(e) => eprintln!("watch error: {:?}", e),
    //     }
    // })?;
    // watcher.watch(&cmd.path, notify::RecursiveMode::from(cmd.recursive))?;

    // Simulate file watching loop (in real implementation, this would be driven by notify events)
    loop {
        // For demo purposes, just exit after showing the setup
        if !is_quiet() {
            println!("File watching simulation - in real implementation, this would use the notify crate");
        }
        break;
    }

    Ok(())
}

fn handle_server_command(
    cmd: ServerCommand,
    _config: &ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
    if !is_quiet() {
        println!("🚀 Starting DDEX Builder HTTP API server...");
        println!("   Address: {}:{}", cmd.bind, cmd.port);
        println!("   Workers: {}", cmd.workers);
        if cmd.cors {
            println!("   CORS: enabled");
        }
        if cmd.tls_cert.is_some() && cmd.tls_key.is_some() {
            println!("   TLS: enabled");
        }
        if let Some(rate_limit) = cmd.rate_limit {
            println!("   Rate limit: {} requests/minute per IP", rate_limit);
        }
        println!("   Press Ctrl+C to stop");
    }

    // TODO: Implement HTTP server using axum or warp
    // Server endpoints would include:
    // POST /build - Build DDEX XML from JSON/YAML/TOML
    // POST /convert - Convert between DDEX versions
    // POST /validate - Validate DDEX XML
    // GET /presets - List available presets
    // GET /presets/{id} - Get preset details
    // POST /diff - Compare two DDEX files
    // GET /health - Health check endpoint
    // GET /metrics - Prometheus metrics (if enabled)

    // For now, just simulate server running
    println!("HTTP API server would start here");
    println!("Available endpoints:");
    println!("  POST /api/v1/build        - Build DDEX XML");
    println!("  POST /api/v1/convert      - Convert DDEX versions");
    println!("  POST /api/v1/validate     - Validate DDEX XML");
    println!("  GET  /api/v1/presets      - List presets");
    println!("  GET  /api/v1/presets/{{id}} - Get preset details");
    println!("  POST /api/v1/diff         - Compare DDEX files");
    println!("  GET  /api/v1/health       - Health check");

    // Simulate server running
    std::thread::park();

    Ok(())
}

fn handle_completions_command(cmd: CompletionsCommand) -> Result<(), Box<dyn std::error::Error>> {
    let mut cli = Cli::command();

    if let Some(output_path) = cmd.output {
        let mut file = fs::File::create(output_path)?;
        generate(cmd.shell, &mut cli, "ddex-builder", &mut file);
    } else {
        generate(cmd.shell, &mut cli, "ddex-builder", &mut io::stdout());
    }

    Ok(())
}

// Helper functions

fn read_input_data(
    input: &Option<PathBuf>,
    format: Option<InputFormat>,
) -> Result<JsonValue, Box<dyn std::error::Error>> {
    let content = read_input_string(input)?;

    let detected_format = format.unwrap_or_else(|| {
        if let Some(path) = input {
            detect_input_format(path)
        } else {
            InputFormat::Json // Default for stdin
        }
    });

    match detected_format {
        InputFormat::Json => Ok(serde_json::from_str(&content)?),
        InputFormat::Yaml => Ok(serde_yaml::from_str(&content)?),
        InputFormat::Toml => {
            let toml_value: toml::Value = toml::from_str(&content)?;
            Ok(serde_json::to_value(toml_value)?)
        }
    }
}

fn read_input_string(input: &Option<PathBuf>) -> Result<String, Box<dyn std::error::Error>> {
    match input {
        Some(path) if path.to_str() == Some("-") => {
            let mut content = String::new();
            io::stdin().read_to_string(&mut content)?;
            Ok(content)
        }
        Some(path) => Ok(fs::read_to_string(path)?),
        None => {
            let mut content = String::new();
            io::stdin().read_to_string(&mut content)?;
            Ok(content)
        }
    }
}

fn write_output(content: &str, output: &Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
    match output {
        Some(path) if path.to_str() == Some("-") => {
            print!("{}", content);
            Ok(())
        }
        Some(path) => {
            fs::write(path, content)?;
            Ok(())
        }
        None => {
            print!("{}", content);
            Ok(())
        }
    }
}

fn detect_input_format(path: &Path) -> InputFormat {
    match path.extension().and_then(|s| s.to_str()) {
        Some("yaml") | Some("yml") => InputFormat::Yaml,
        Some("toml") => InputFormat::Toml,
        _ => InputFormat::Json,
    }
}

fn preset_to_string(preset: &PresetChoice) -> String {
    match preset {
        PresetChoice::AudioAlbum => "audio_album".to_string(),
        PresetChoice::AudioSingle => "audio_single".to_string(),
        PresetChoice::VideoSingle => "video_single".to_string(),
        PresetChoice::Compilation => "compilation".to_string(),
        PresetChoice::YoutubeAlbum => "youtube_album".to_string(),
        PresetChoice::YoutubeVideo => "youtube_video".to_string(),
        PresetChoice::YoutubeSingle => "youtube_single".to_string(),
    }
}

fn validate_input_data(
    _data: &JsonValue,
    _builder: &Builder,
    _strict: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    // TODO: Implement input validation logic
    Ok(())
}

fn build_ddex_xml(
    _data: &JsonValue,
    _builder: &Builder,
) -> Result<String, Box<dyn std::error::Error>> {
    // TODO: Implement actual DDEX XML building
    Ok("<xml><!-- DDEX XML would be generated here --></xml>".to_string())
}

fn print_validation_result_human(file_path: &Path, result: &ValidationResult) {
    if result.errors.is_empty() {
        println!("{} {} - Valid", style("").green(), file_path.display());
    } else {
        println!(
            "{} {} - {} errors, {} warnings",
            style("").red(),
            file_path.display(),
            result.errors.len(),
            result.warnings.len()
        );

        for error in &result.errors {
            println!("  {} {:?}", style("Error:").red(), error);
        }

        for warning in &result.warnings {
            println!("  {} {:?}", style("Warning:").yellow(), warning);
        }
    }
}

fn format_junit_results(
    _results: &[(PathBuf, ValidationResult)],
) -> Result<String, Box<dyn std::error::Error>> {
    // TODO: Implement JUnit XML format
    Ok("<testsuite><!-- JUnit results would be here --></testsuite>".to_string())
}

fn load_batch_config(path: &Path) -> Result<BatchConfig, Box<dyn std::error::Error>> {
    let content = fs::read_to_string(path)?;
    let config: BatchConfig = serde_yaml::from_str(&content)?;
    Ok(config)
}

fn process_batch_task(_task: &BatchTask) -> Result<(), Box<dyn std::error::Error>> {
    // TODO: Implement batch task processing
    Ok(())
}

fn is_quiet() -> bool {
    std::env::var("DDEX_QUIET").unwrap_or_default() == "1"
}

// Helper functions for new commands

fn get_available_presets(
    version_filter: Option<DdexVersionArg>,
    partner_filter: Option<PresetChoice>,
) -> Result<Vec<PresetInfo>, Box<dyn std::error::Error>> {
    let mut presets = vec![
        // Generic industry-standard presets
        PresetInfo {
            name: "audio_album".to_string(),
            version: "4.3".to_string(),
            partner: "Generic".to_string(),
            description: "DDEX ERN 4.3 audio album baseline preset".to_string(),
        },
        PresetInfo {
            name: "audio_single".to_string(),
            version: "4.3".to_string(),
            partner: "Generic".to_string(),
            description: "DDEX ERN 4.3 audio single baseline preset".to_string(),
        },
        PresetInfo {
            name: "video_single".to_string(),
            version: "4.3".to_string(),
            partner: "Generic".to_string(),
            description: "DDEX ERN 4.3 video single baseline preset".to_string(),
        },
        PresetInfo {
            name: "compilation".to_string(),
            version: "4.3".to_string(),
            partner: "Generic".to_string(),
            description: "DDEX ERN 4.3 compilation album baseline preset".to_string(),
        },
        // YouTube presets (based on public documentation)
        PresetInfo {
            name: "youtube_album".to_string(),
            version: "4.3".to_string(),
            partner: "YouTube".to_string(),
            description: "YouTube Music album preset based on public Partner documentation"
                .to_string(),
        },
        PresetInfo {
            name: "youtube_video".to_string(),
            version: "4.3".to_string(),
            partner: "YouTube".to_string(),
            description: "YouTube Music video preset based on public Partner documentation"
                .to_string(),
        },
        PresetInfo {
            name: "youtube_single".to_string(),
            version: "4.3".to_string(),
            partner: "YouTube".to_string(),
            description: "YouTube Music single preset based on public Partner documentation"
                .to_string(),
        },
    ];

    // Apply filters
    if let Some(version_filter) = version_filter {
        let version_str = match version_filter {
            DdexVersionArg::V382 => "3.8.2",
            DdexVersionArg::V41 => "4.1",
            DdexVersionArg::V42 => "4.2",
            DdexVersionArg::V43 => "4.3",
            DdexVersionArg::V44 => "4.4",
        };
        presets.retain(|p| p.version == version_str);
    }

    if let Some(partner_filter) = partner_filter {
        let partner_str = match partner_filter {
            PresetChoice::AudioAlbum => "Generic",
            PresetChoice::AudioSingle => "Generic",
            PresetChoice::VideoSingle => "Generic",
            PresetChoice::Compilation => "Generic",
            PresetChoice::YoutubeAlbum => "YouTube",
            PresetChoice::YoutubeVideo => "YouTube",
            PresetChoice::YoutubeSingle => "YouTube",
        };
        presets.retain(|p| p.partner == partner_str);
    }

    Ok(presets)
}

fn get_preset_details(preset_id: &str) -> Result<PresetDetails, Box<dyn std::error::Error>> {
    // TODO: Load preset details from ddex_builder::presets module
    match preset_id {
        "spotify_audio_43" => Ok(PresetDetails {
            name: "spotify_audio_43".to_string(),
            partner: "Spotify".to_string(),
            version: "4.3".to_string(),
            description: "Spotify audio release preset for ERN 4.3".to_string(),
            config: PresetConfig::default(), // Placeholder
        }),
        _ => Err(format!("Preset '{}' not found", preset_id).into()),
    }
}

fn run_watch_build(cmd: &WatchCommand) -> Result<(), Box<dyn std::error::Error>> {
    // TODO: Implement actual file watching and building
    if let Some(ref command) = cmd.command {
        // Execute custom command
        let output = std::process::Command::new("sh")
            .arg("-c")
            .arg(command)
            .output()?;

        if !output.status.success() {
            return Err(format!(
                "Command failed: {}",
                String::from_utf8_lossy(&output.stderr)
            )
            .into());
        }
    } else {
        // Default build behavior
        if !is_quiet() {
            println!("  Building files matching pattern: {}", cmd.pattern);
        }
        // TODO: Scan for files matching pattern and build them
    }

    Ok(())
}

fn verify_build_determinism(
    data: &JsonValue,
    builder: &Builder,
    iterations: usize,
) -> Result<(), Box<dyn std::error::Error>> {
    use sha2::{Digest, Sha256};

    if iterations < 2 {
        return Ok(());
    }

    if !is_quiet() {
        println!(
            "  {} Verifying determinism with {} iterations...",
            style("").blue(),
            iterations
        );
    }

    let mut outputs = Vec::with_capacity(iterations);
    let mut hashes = Vec::with_capacity(iterations);

    // Build XML multiple times
    for _i in 0..iterations {
        let xml = build_ddex_xml(data, builder)?;

        // Calculate SHA-256 hash
        let mut hasher = Sha256::new();
        hasher.update(xml.as_bytes());
        let hash = hasher.finalize();
        let hash_hex = format!("{:x}", hash);

        outputs.push(xml);
        hashes.push(hash_hex);

        if !is_quiet() && iterations > 3 {
            print!(".");
            io::stdout().flush().unwrap_or_default();
        }
    }

    if !is_quiet() && iterations > 3 {
        println!(); // New line after dots
    }

    // Compare all outputs byte-for-byte
    let first_output = &outputs[0];
    let first_hash = &hashes[0];

    for (i, (output, hash)) in outputs[1..].iter().zip(hashes[1..].iter()).enumerate() {
        if output != first_output || hash != first_hash {
            eprintln!(
                "{} Determinism verification failed!",
                style("").red().bold()
            );
            eprintln!("  Output from iteration 1 differs from iteration {}", i + 2);
            eprintln!("  Hash 1: {}", first_hash);
            eprintln!("  Hash {}: {}", i + 2, hash);

            // Show byte-level differences for first 1000 characters
            let diff_start = find_first_difference(first_output, output);
            if let Some(pos) = diff_start {
                eprintln!("  First difference at byte position: {}", pos);
                let start = pos.saturating_sub(50);
                let end = std::cmp::min(pos + 100, std::cmp::min(first_output.len(), output.len()));
                eprintln!("  Context around difference:");
                eprintln!("  Output 1: {:?}", &first_output[start..end]);
                eprintln!("  Output {}: {:?}", i + 2, &output[start..end]);
            }

            return Err(
                "Determinism verification failed - outputs differ between iterations".into(),
            );
        }
    }

    if !is_quiet() {
        println!(
            "  {} All {} iterations produced identical output",
            style("").green(),
            iterations
        );
        println!("  SHA-256: {}", first_hash);
    }

    Ok(())
}

fn find_first_difference(a: &str, b: &str) -> Option<usize> {
    a.bytes()
        .zip(b.bytes())
        .position(|(x, y)| x != y)
        .or_else(|| {
            if a.len() != b.len() {
                Some(std::cmp::min(a.len(), b.len()))
            } else {
                None
            }
        })
}

// Data structures for batch processing

#[derive(serde::Deserialize)]
struct BatchConfig {
    tasks: Vec<BatchTask>,
}

#[derive(serde::Deserialize)]
struct BatchTask {
    input_file: PathBuf,
    output_file: PathBuf,
    preset: Option<String>,
    version: Option<String>,
    validate: Option<bool>,
}

#[derive(serde::Serialize)]
struct BatchResult {
    task_id: usize,
    success: bool,
    error: Option<String>,
}

#[derive(serde::Serialize)]
struct BatchReport {
    total_tasks: usize,
    successful: usize,
    failed: usize,
    results: Vec<BatchResult>,
}

// Data structures for preset commands

#[derive(serde::Serialize, serde::Deserialize)]
struct PresetInfo {
    name: String,
    version: String,
    partner: String,
    description: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
struct PresetDetails {
    name: String,
    partner: String,
    version: String,
    description: String,
    config: PresetConfig,
}

#[derive(serde::Serialize, serde::Deserialize)]
struct PresetSummary {
    name: String,
    partner: String,
    version: String,
    description: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
struct PresetConfig {
    // TODO: Define actual preset configuration structure
    // This would contain partner-specific settings, validation rules, etc.
    settings: IndexMap<String, String>,
}