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
//! # JSON Schema Generation for DDEX Models
//!
//! This module provides comprehensive JSON Schema generation from DDEX structures
//! for validation, documentation, and cross-language type definitions. It supports
//! version-specific variations, profile-based constraints, partner preset rules, and
//! can generate TypeScript and Python type definitions.
//!
//! ## Key Features
//!
//! - **JSON Schema Draft 2020-12** and Draft-07 support for broad compatibility
//! - **DDEX Version Aware**: Generates schemas for ERN 3.8.2, 4.2, and 4.3
//! - **Message Profile Support**: Audio, Video, and Mixed content profiles
//! - **Partner Preset Integration**: Incorporates partner-specific validation rules
//! - **Multi-Language Export**: TypeScript `.d.ts` and Python `TypedDict` generation
//! - **Advanced Validation**: Pattern matching, conditional schemas, enum constraints
//!
//! ## Architecture Overview
//!
//! ```text
//! Schema Generation Pipeline
//! ┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
//! │ DDEX Structures │───▶│ SchemaGenerator  │───▶│  JSON Schema    │
//! │ (Rust types)    │    │                  │    │ (Draft 2020-12) │
//! └─────────────────┘    └──────────────────┘    └─────────────────┘
//!           │                       │                       │
//!           ▼                       ▼                       ▼
//!    ┌─────────────┐      ┌─────────────────┐    ┌─────────────────┐
//!    │ • BuildReq  │      │ • Version Rules │    │ • Validation    │
//!    │ • Releases  │      │ • Profile Cnstr │    │ • Documentation │
//!    │ • Tracks    │      │ • Partner Rules │    │ • Type Export   │
//!    │ • Metadata  │      │ • Type Mapping  │    │ • References    │
//!    └─────────────┘      └─────────────────┘    └─────────────────┘
//! ```
//!
//! ## Generation Capabilities
//!
//! ### Core Schema Types
//! - **BuildRequest**: Complete DDEX build request structure
//! - **FlatRelease**: Simplified release representation
//! - **Complete Schema**: All DDEX types with cross-references
//!
//! ### Output Formats
//! - **JSON Schema**: Standards-compliant validation schemas
//! - **TypeScript**: `.d.ts` type definition files
//! - **Python**: `TypedDict` class definitions
//!
//! ## Usage Examples
//!
//! ### Basic Schema Generation
//!
//! ```rust
//! use ddex_builder::schema::{SchemaGenerator, SchemaConfig};
//! use ddex_builder::presets::{DdexVersion, MessageProfile};
//!
//! let generator = SchemaGenerator::new(
//!     DdexVersion::Ern43,
//!     MessageProfile::AudioAlbum
//! );
//!
//! let result = generator.generate_build_request_schema()?;
//! let schema_json = serde_json::to_string_pretty(&result.schema)?;
//! println!("Generated schema:\n{}", schema_json);
//! ```
//!
//! ### Advanced Configuration
//!
//! ```rust
//! use ddex_builder::schema::*;
//! use ddex_builder::presets::*;
//!
//! let config = SchemaConfig {
//!     draft_version: SchemaDraft::Draft202012,
//!     include_examples: true,
//!     include_descriptions: true,
//!     strict_validation: true,
//!     version_conditionals: true,
//!     ..Default::default()
//! };
//!
//! let spotify_preset = spotify_audio_43();
//! let generator = SchemaGenerator::with_preset(
//!     DdexVersion::Ern43,
//!     MessageProfile::AudioAlbum,
//!     spotify_preset
//! ).with_config(config);
//!
//! let result = generator.generate_complete_schema()?;
//! ```
//!
//! ### Type Definition Export
//!
//! ```rust
//! // Generate TypeScript definitions
//! let typescript = generator.generate_typescript_types(&result.schema)?;
//! std::fs::write("ddex-types.d.ts", typescript)?;
//!
//! // Generate Python definitions
//! let python = generator.generate_python_types(&result.schema)?;
//! std::fs::write("ddex_types.py", python)?;
//! ```
//!
//! ## Schema Features
//!
//! ### Validation Rules
//! - **Required Fields**: Platform-specific mandatory fields
//! - **Format Validation**: ISRC, UPC, date format validation
//! - **Pattern Matching**: Regex patterns for code validation
//! - **Enum Constraints**: Allowed values for controlled vocabularies
//! - **Conditional Logic**: Version-specific field requirements
//!
//! ### Documentation Integration
//! - **DDEX Specification**: Field descriptions from official docs
//! - **Examples**: Real-world usage examples for each field
//! - **Cross-References**: Links between related schema definitions
//! - **Version Notes**: Migration guidance between DDEX versions
//!
//! ## Performance Characteristics
//!
//! - **Schema Generation**: 1-5ms for complete schema generation
//! - **Type Export**: 5-15ms for TypeScript/Python generation
//! - **Memory Usage**: ~2MB peak for complete schema with examples
//! - **Cache Support**: Generated schemas are reusable across builds
//!
//! ## Command Line Interface
//!
//! The schema generator includes CLI support:
//!
//! ```bash
//! # Generate complete schema
//! ddex-builder schema --version 4.3 --profile AudioAlbum --output schema.json
//!
//! # Include TypeScript and Python types
//! ddex-builder schema --version 4.3 --profile AudioAlbum \
//!   --typescript --python --examples --strict
//! ```

// All necessary imports are via super::* in submodules
use crate::error::BuildError;
use crate::presets::{DdexVersion, MessageProfile, PartnerPreset};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};

mod generators;
mod types;
mod validation;

// Re-export public items from submodules - only what we need publicly
pub use validation::{
    SchemaValidator, ValidationConfig as SchemaValidationConfig,
    ValidationResult as SchemaValidationResult,
};

/// Main JSON Schema generator for DDEX models
#[derive(Debug, Clone)]
pub struct SchemaGenerator {
    /// DDEX version to generate schema for
    version: DdexVersion,
    /// Message profile for constraints
    profile: MessageProfile,
    /// Partner preset for additional validation rules
    #[allow(dead_code)]
    preset: Option<PartnerPreset>,
    /// Schema configuration
    config: SchemaConfig,
}

/// Configuration for schema generation
#[derive(Debug, Clone)]
pub struct SchemaConfig {
    /// JSON Schema draft version
    pub draft_version: SchemaDraft,
    /// Include examples in schema
    pub include_examples: bool,
    /// Include descriptions from DDEX spec
    pub include_descriptions: bool,
    /// Generate strict validation rules
    pub strict_validation: bool,
    /// Include deprecated fields with warnings
    pub include_deprecated: bool,
    /// Generate conditional schemas for version differences
    pub version_conditionals: bool,
}

/// Supported JSON Schema draft versions
#[derive(Debug, Clone, Copy)]
pub enum SchemaDraft {
    /// JSON Schema Draft 2020-12
    Draft202012,
    /// JSON Schema Draft-07 (for broader compatibility)
    Draft07,
}

/// Complete JSON Schema representation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonSchema {
    /// Schema metadata
    #[serde(rename = "$schema")]
    pub schema: String,
    /// Schema ID/URI
    #[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Schema title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Schema description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Schema type
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub schema_type: Option<String>,
    /// Object properties
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<IndexMap<String, JsonSchema>>,
    /// Required properties
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,
    /// Additional properties allowed
    #[serde(
        rename = "additionalProperties",
        skip_serializing_if = "Option::is_none"
    )]
    pub additional_properties: Option<bool>,
    /// Array items schema
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<JsonSchema>>,
    /// Enum values
    #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
    pub enum_values: Option<Vec<JsonValue>>,
    /// String pattern validation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,
    /// String format
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// Minimum length
    #[serde(rename = "minLength", skip_serializing_if = "Option::is_none")]
    pub min_length: Option<usize>,
    /// Maximum length
    #[serde(rename = "maxLength", skip_serializing_if = "Option::is_none")]
    pub max_length: Option<usize>,
    /// Examples
    #[serde(skip_serializing_if = "Option::is_none")]
    pub examples: Option<Vec<JsonValue>>,
    /// Schema definitions
    #[serde(rename = "$defs", skip_serializing_if = "Option::is_none")]
    pub definitions: Option<IndexMap<String, JsonSchema>>,
    /// Reference to another schema
    #[serde(rename = "$ref", skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
    /// All of (intersection)
    #[serde(rename = "allOf", skip_serializing_if = "Option::is_none")]
    pub all_of: Option<Vec<JsonSchema>>,
    /// Any of (union)
    #[serde(rename = "anyOf", skip_serializing_if = "Option::is_none")]
    pub any_of: Option<Vec<JsonSchema>>,
    /// One of (exclusive union)
    #[serde(rename = "oneOf", skip_serializing_if = "Option::is_none")]
    pub one_of: Option<Vec<JsonSchema>>,
    /// Conditional schema
    #[serde(rename = "if", skip_serializing_if = "Option::is_none")]
    pub if_schema: Option<Box<JsonSchema>>,
    /// Then schema (if condition is true)
    #[serde(rename = "then", skip_serializing_if = "Option::is_none")]
    pub then_schema: Option<Box<JsonSchema>>,
    /// Else schema (if condition is false)
    #[serde(rename = "else", skip_serializing_if = "Option::is_none")]
    pub else_schema: Option<Box<JsonSchema>>,
    /// Custom annotations
    #[serde(flatten)]
    pub annotations: IndexMap<String, JsonValue>,
}

/// Schema generation result with metadata
#[derive(Debug, Clone)]
pub struct SchemaGenerationResult {
    /// Generated JSON Schema
    pub schema: JsonSchema,
    /// Schema metadata
    pub metadata: SchemaMetadata,
    /// Generation warnings
    pub warnings: Vec<SchemaWarning>,
}

/// Metadata about generated schema
#[derive(Debug, Clone)]
pub struct SchemaMetadata {
    /// DDEX version
    pub ddex_version: DdexVersion,
    /// Message profile
    pub profile: MessageProfile,
    /// Schema draft version
    pub draft_version: SchemaDraft,
    /// Generation timestamp
    pub generated_at: chrono::DateTime<chrono::Utc>,
    /// Number of properties
    pub property_count: usize,
    /// Number of required fields
    pub required_count: usize,
    /// Schema complexity score
    pub complexity_score: f64,
}

/// Warning during schema generation
#[derive(Debug, Clone)]
pub struct SchemaWarning {
    /// Warning code
    pub code: String,
    /// Warning message
    pub message: String,
    /// Field path where warning occurred
    pub field_path: Option<String>,
    /// Suggestion for resolution
    pub suggestion: Option<String>,
}

impl Default for SchemaConfig {
    fn default() -> Self {
        Self {
            draft_version: SchemaDraft::Draft202012,
            include_examples: true,
            include_descriptions: true,
            strict_validation: true,
            include_deprecated: false,
            version_conditionals: true,
        }
    }
}

impl SchemaGenerator {
    /// Create a new schema generator
    pub fn new(version: DdexVersion, profile: MessageProfile) -> Self {
        Self {
            version,
            profile,
            preset: None,
            config: SchemaConfig::default(),
        }
    }

    /// Create schema generator with preset
    pub fn with_preset(
        version: DdexVersion,
        profile: MessageProfile,
        preset: PartnerPreset,
    ) -> Self {
        Self {
            version,
            profile,
            preset: Some(preset),
            config: SchemaConfig::default(),
        }
    }

    /// Set schema configuration
    pub fn with_config(mut self, config: SchemaConfig) -> Self {
        self.config = config;
        self
    }

    /// Generate complete JSON Schema for DDEX BuildRequest
    pub fn generate_build_request_schema(&self) -> Result<SchemaGenerationResult, BuildError> {
        let mut warnings = Vec::new();

        let schema = self.build_request_schema(&mut warnings)?;

        let metadata = SchemaMetadata {
            ddex_version: self.version,
            profile: self.profile,
            draft_version: self.config.draft_version,
            generated_at: chrono::Utc::now(),
            property_count: self.count_properties(&schema),
            required_count: schema.required.as_ref().map(|r| r.len()).unwrap_or(0),
            complexity_score: self.calculate_complexity(&schema),
        };

        Ok(SchemaGenerationResult {
            schema,
            metadata,
            warnings,
        })
    }

    /// Generate schema for FlatRelease model
    pub fn generate_flat_release_schema(&self) -> Result<SchemaGenerationResult, BuildError> {
        let mut warnings = Vec::new();

        let schema = self.flat_release_schema(&mut warnings)?;

        let metadata = SchemaMetadata {
            ddex_version: self.version,
            profile: self.profile,
            draft_version: self.config.draft_version,
            generated_at: chrono::Utc::now(),
            property_count: self.count_properties(&schema),
            required_count: schema.required.as_ref().map(|r| r.len()).unwrap_or(0),
            complexity_score: self.calculate_complexity(&schema),
        };

        Ok(SchemaGenerationResult {
            schema,
            metadata,
            warnings,
        })
    }

    /// Generate schema for all DDEX element types
    pub fn generate_complete_schema(&self) -> Result<SchemaGenerationResult, BuildError> {
        let mut warnings = Vec::new();
        let mut definitions = IndexMap::new();

        // Generate schemas for all major DDEX types
        definitions.insert(
            "BuildRequest".to_string(),
            self.build_request_schema(&mut warnings)?,
        );
        definitions.insert(
            "ReleaseRequest".to_string(),
            self.release_request_schema(&mut warnings)?,
        );
        definitions.insert(
            "TrackRequest".to_string(),
            self.track_request_schema(&mut warnings)?,
        );
        definitions.insert(
            "DealRequest".to_string(),
            self.deal_request_schema(&mut warnings)?,
        );
        definitions.insert(
            "MessageHeader".to_string(),
            self.message_header_schema(&mut warnings)?,
        );

        // Add common type definitions
        definitions.extend(self.common_type_definitions(&mut warnings)?);

        let schema = JsonSchema {
            schema: self.schema_draft_url(),
            id: Some("https://ddex.net/schema/ern/builder".to_string()),
            title: Some(format!(
                "DDEX Builder Schema - ERN {} {}",
                self.version_string(),
                self.profile_string()
            )),
            description: Some(format!(
                "Complete JSON Schema for DDEX Builder structures targeting ERN {} with {} profile",
                self.version_string(),
                self.profile_string()
            )),
            schema_type: Some("object".to_string()),
            definitions: Some(definitions),
            additional_properties: Some(false),
            ..Default::default()
        };

        let metadata = SchemaMetadata {
            ddex_version: self.version,
            profile: self.profile,
            draft_version: self.config.draft_version,
            generated_at: chrono::Utc::now(),
            property_count: self.count_properties(&schema),
            required_count: 0, // Root schema doesn't have required fields
            complexity_score: self.calculate_complexity(&schema),
        };

        Ok(SchemaGenerationResult {
            schema,
            metadata,
            warnings,
        })
    }

    /// Generate TypeScript type definitions from schema
    pub fn generate_typescript_types(&self, schema: &JsonSchema) -> Result<String, BuildError> {
        let mut typescript = String::new();

        typescript.push_str(&format!(
            "// Generated TypeScript types for DDEX Builder - ERN {} {}\n",
            self.version_string(),
            self.profile_string()
        ));
        typescript.push_str("// Generated at: ");
        typescript.push_str(&chrono::Utc::now().to_rfc3339());
        typescript.push_str("\n\n");

        if let Some(ref definitions) = schema.definitions {
            for (name, def_schema) in definitions {
                typescript.push_str(&self.schema_to_typescript(name, def_schema)?);
                typescript.push_str("\n\n");
            }
        }

        Ok(typescript)
    }

    /// Generate Python TypedDict definitions from schema
    pub fn generate_python_types(&self, schema: &JsonSchema) -> Result<String, BuildError> {
        let mut python = String::new();

        python.push_str(&format!(
            "# Generated Python TypedDict types for DDEX Builder - ERN {} {}\n",
            self.version_string(),
            self.profile_string()
        ));
        python.push_str("# Generated at: ");
        python.push_str(&chrono::Utc::now().to_rfc3339());
        python.push_str("\n\n");
        python.push_str("from typing import TypedDict, Optional, List, Union, Literal\nfrom datetime import datetime\n\n");

        if let Some(ref definitions) = schema.definitions {
            for (name, def_schema) in definitions {
                python.push_str(&self.schema_to_python(name, def_schema)?);
                python.push_str("\n\n");
            }
        }

        Ok(python)
    }

    // Private helper methods

    fn schema_draft_url(&self) -> String {
        match self.config.draft_version {
            SchemaDraft::Draft202012 => "https://json-schema.org/draft/2020-12/schema".to_string(),
            SchemaDraft::Draft07 => "https://json-schema.org/draft-07/schema".to_string(),
        }
    }

    fn version_string(&self) -> &str {
        match self.version {
            DdexVersion::Ern43 => "4.3",
            DdexVersion::Ern42 => "4.2",
            DdexVersion::Ern41 => "4.1",
            DdexVersion::Ern382 => "3.8.2",
        }
    }

    fn profile_string(&self) -> &str {
        match self.profile {
            MessageProfile::AudioAlbum => "AudioAlbum",
            MessageProfile::AudioSingle => "AudioSingle",
            MessageProfile::VideoAlbum => "VideoAlbum",
            MessageProfile::VideoSingle => "VideoSingle",
            MessageProfile::Mixed => "Mixed",
        }
    }

    fn count_properties(&self, schema: &JsonSchema) -> usize {
        let mut count = 0;

        if let Some(ref properties) = schema.properties {
            count += properties.len();
            for (_, prop_schema) in properties {
                count += self.count_properties(prop_schema);
            }
        }

        if let Some(ref definitions) = schema.definitions {
            for (_, def_schema) in definitions {
                count += self.count_properties(def_schema);
            }
        }

        count
    }

    fn calculate_complexity(&self, schema: &JsonSchema) -> f64 {
        let mut complexity = 0.0;

        // Base complexity for each property
        if let Some(ref properties) = schema.properties {
            complexity += properties.len() as f64;

            for (_, prop_schema) in properties {
                complexity += self.calculate_complexity(prop_schema) * 0.5;
            }
        }

        // Add complexity for advanced features
        if schema.all_of.is_some() {
            complexity += 2.0;
        }
        if schema.any_of.is_some() {
            complexity += 3.0;
        }
        if schema.one_of.is_some() {
            complexity += 4.0;
        }
        if schema.if_schema.is_some() {
            complexity += 5.0;
        }
        if schema.pattern.is_some() {
            complexity += 1.0;
        }
        if schema.enum_values.is_some() {
            complexity += 0.5;
        }

        complexity
    }
}

impl Default for JsonSchema {
    fn default() -> Self {
        Self {
            schema: String::new(),
            id: None,
            title: None,
            description: None,
            schema_type: None,
            properties: None,
            required: None,
            additional_properties: None,
            items: None,
            enum_values: None,
            pattern: None,
            format: None,
            min_length: None,
            max_length: None,
            examples: None,
            definitions: None,
            reference: None,
            all_of: None,
            any_of: None,
            one_of: None,
            if_schema: None,
            then_schema: None,
            else_schema: None,
            annotations: IndexMap::new(),
        }
    }
}

impl std::fmt::Display for SchemaDraft {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SchemaDraft::Draft202012 => write!(f, "2020-12"),
            SchemaDraft::Draft07 => write!(f, "draft-07"),
        }
    }
}

/// Generate schema command-line arguments
#[derive(Debug, Clone)]
pub struct SchemaCommand {
    /// DDEX version
    pub version: String,
    /// Message profile
    pub profile: String,
    /// Output file path
    pub output: Option<String>,
    /// Generate TypeScript types
    pub typescript: bool,
    /// Generate Python types
    pub python: bool,
    /// Include examples
    pub examples: bool,
    /// Strict validation mode
    pub strict: bool,
}

impl SchemaCommand {
    /// Execute schema generation command
    pub fn execute(&self) -> Result<(), BuildError> {
        let version = self.parse_version()?;
        let profile = self.parse_profile()?;

        let config = SchemaConfig {
            include_examples: self.examples,
            strict_validation: self.strict,
            ..Default::default()
        };

        let generator = SchemaGenerator::new(version, profile).with_config(config);
        let result = generator.generate_complete_schema()?;

        // Output JSON Schema
        let schema_json = serde_json::to_string_pretty(&result.schema).map_err(|e| {
            BuildError::InvalidFormat {
                field: "schema".to_string(),
                message: format!("Failed to serialize schema: {}", e),
            }
        })?;

        if let Some(ref output_path) = self.output {
            std::fs::write(output_path, &schema_json).map_err(|e| BuildError::InvalidFormat {
                field: "output".to_string(),
                message: format!("Failed to write schema: {}", e),
            })?;
            // Schema file written successfully
        } else {
            println!("{}", schema_json);
        }

        // Generate TypeScript types if requested
        if self.typescript {
            let ts_types = generator.generate_typescript_types(&result.schema)?;
            let ts_path = self
                .output
                .as_ref()
                .map(|p| p.replace(".json", ".d.ts"))
                .unwrap_or_else(|| "ddex-types.d.ts".to_string());

            std::fs::write(&ts_path, ts_types).map_err(|e| BuildError::InvalidFormat {
                field: "typescript".to_string(),
                message: format!("Failed to write TypeScript types: {}", e),
            })?;
            // TypeScript types written successfully
        }

        // Generate Python types if requested
        if self.python {
            let py_types = generator.generate_python_types(&result.schema)?;
            let py_path = self
                .output
                .as_ref()
                .map(|p| p.replace(".json", ".py"))
                .unwrap_or_else(|| "ddex_types.py".to_string());

            std::fs::write(&py_path, py_types).map_err(|e| BuildError::InvalidFormat {
                field: "python".to_string(),
                message: format!("Failed to write Python types: {}", e),
            })?;
            // Python types written successfully
        }

        // Schema generation completed successfully

        // Warnings are captured in result.warnings for caller to handle

        Ok(())
    }

    fn parse_version(&self) -> Result<DdexVersion, BuildError> {
        match self.version.as_str() {
            "4.3" | "43" => Ok(DdexVersion::Ern43),
            "4.2" | "42" => Ok(DdexVersion::Ern42),
            "4.1" | "41" => Ok(DdexVersion::Ern41),
            "3.8.2" | "382" => Ok(DdexVersion::Ern382),
            _ => Err(BuildError::InvalidFormat {
                field: "version".to_string(),
                message: format!("Unsupported DDEX version: {}", self.version),
            }),
        }
    }

    fn parse_profile(&self) -> Result<MessageProfile, BuildError> {
        match self.profile.to_lowercase().as_str() {
            "audioalbum" | "audio-album" => Ok(MessageProfile::AudioAlbum),
            "audiosingle" | "audio-single" => Ok(MessageProfile::AudioSingle),
            "videoalbum" | "video-album" => Ok(MessageProfile::VideoAlbum),
            "videosingle" | "video-single" => Ok(MessageProfile::VideoSingle),
            "mixed" => Ok(MessageProfile::Mixed),
            _ => Err(BuildError::InvalidFormat {
                field: "profile".to_string(),
                message: format!("Unsupported message profile: {}", self.profile),
            }),
        }
    }
}

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

    #[test]
    fn test_schema_generator_creation() {
        let generator = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioAlbum);
        assert_eq!(generator.version, DdexVersion::Ern43);
        assert_eq!(generator.profile, MessageProfile::AudioAlbum);
        assert!(generator.preset.is_none());
    }

    #[test]
    fn test_schema_config_defaults() {
        let config = SchemaConfig::default();

        assert!(matches!(config.draft_version, SchemaDraft::Draft202012));
        assert!(config.include_examples);
        assert!(config.include_descriptions);
        assert!(config.strict_validation);
        assert!(!config.include_deprecated);
        assert!(config.version_conditionals);
    }

    #[test]
    fn test_build_request_schema_generation() {
        let generator = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioAlbum);
        let result = generator.generate_build_request_schema().unwrap();

        assert!(result.schema.title.is_some());
        assert!(result.schema.schema_type == Some("object".to_string()));
        assert!(result.schema.properties.is_some());
        assert!(result.schema.required.is_some());

        let properties = result.schema.properties.unwrap();
        assert!(properties.contains_key("header"));
        assert!(properties.contains_key("releases"));

        let required = result.schema.required.unwrap();
        assert!(required.contains(&"header".to_string()));
        assert!(required.contains(&"releases".to_string()));

        // Check metadata
        assert!(result.metadata.property_count > 0);
        assert!(result.metadata.complexity_score > 0.0);
    }

    #[test]
    fn test_complete_schema_generation() {
        let generator = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioAlbum);
        let result = generator.generate_complete_schema().unwrap();

        assert!(result.schema.definitions.is_some());

        let definitions = result.schema.definitions.unwrap();
        assert!(definitions.contains_key("BuildRequest"));
        assert!(definitions.contains_key("ReleaseRequest"));
        assert!(definitions.contains_key("TrackRequest"));
        assert!(definitions.contains_key("DealRequest"));
        assert!(definitions.contains_key("MessageHeader"));
        assert!(definitions.contains_key("LocalizedString"));
        assert!(definitions.contains_key("Party"));
        assert!(definitions.contains_key("DealTerms"));

        // Verify schema structure
        assert_eq!(
            result.schema.schema,
            "https://json-schema.org/draft/2020-12/schema"
        );
        assert!(result.schema.id.is_some());
        assert!(result.schema.title.is_some());
        assert!(result.schema.description.is_some());
    }

    #[test]
    fn test_version_strings() {
        let generator_43 = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioAlbum);
        let generator_42 = SchemaGenerator::new(DdexVersion::Ern42, MessageProfile::AudioAlbum);
        let generator_41 = SchemaGenerator::new(DdexVersion::Ern41, MessageProfile::AudioAlbum);

        assert_eq!(generator_43.version_string(), "4.3");
        assert_eq!(generator_42.version_string(), "4.2");
        assert_eq!(generator_41.version_string(), "4.1");
    }

    #[test]
    fn test_profile_strings() {
        let audio_album = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioAlbum);
        let audio_single = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::AudioSingle);
        let video_album = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::VideoAlbum);
        let video_single = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::VideoSingle);
        let mixed = SchemaGenerator::new(DdexVersion::Ern43, MessageProfile::Mixed);

        assert_eq!(audio_album.profile_string(), "AudioAlbum");
        assert_eq!(audio_single.profile_string(), "AudioSingle");
        assert_eq!(video_album.profile_string(), "VideoAlbum");
        assert_eq!(video_single.profile_string(), "VideoSingle");
        assert_eq!(mixed.profile_string(), "Mixed");
    }

    #[test]
    fn test_schema_command_parsing() {
        let command = SchemaCommand {
            version: "4.3".to_string(),
            profile: "AudioAlbum".to_string(),
            output: Some("schema.json".to_string()),
            typescript: true,
            python: true,
            examples: true,
            strict: true,
        };

        let parsed_version = command.parse_version().unwrap();
        let parsed_profile = command.parse_profile().unwrap();

        assert!(matches!(parsed_version, DdexVersion::Ern43));
        assert!(matches!(parsed_profile, MessageProfile::AudioAlbum));
    }
}