ggen-core 26.5.19

Core graph-aware code generation engine
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
//! Project configuration types
//!
//! This module provides comprehensive configuration structures for ggen projects,
//! implementing the full ggen.toml schema with support for workspaces, dependencies,
//! ontology integration, templates, generators, lifecycle management, plugins, and profiles.
//!
//! ## Configuration Structure
//!
//! The `GgenConfig` structure represents the root configuration for a ggen project:
//!
//! - **Project**: Project metadata and settings
//! - **Workspace**: Mono-repo configuration
//! - **Graph**: Graph-based dependency resolution
//! - **Dependencies**: Full Cargo.toml-like dependencies
//! - **Ontology**: RDF/OWL integration
//! - **Templates**: Template composition and inheritance
//! - **Generators**: Code generation pipelines
//! - **Lifecycle**: Build lifecycle and hooks
//! - **Plugins**: Plugin system configuration
//! - **Profiles**: Environment-specific overrides
//! - **Metadata**: Package and build metadata
//! - **Validation**: Constraints and quality thresholds
//!
//! ## Type-First Design
//!
//! This implementation uses type-first thinking to encode invariants:
//! - Optional fields use `Option<T>` with proper defaults
//! - `BTreeMap` for deterministic ordering (not `HashMap`)
//! - `Result<T, Error>` for all fallible operations
//! - Zero unsafe blocks (memory safety guaranteed)
//!
//! ## Examples
//!
//! ### Loading Configuration
//!
//! ```rust,no_run
//! use crate::utils::project_config::GgenConfig;
//! use std::fs;
//!
//! # fn main() -> anyhow::Result<()> {
//! let content = fs::read_to_string("ggen.toml")?;
//! let config: GgenConfig = toml::from_str(&content)?;
//!
//! println!("Project: {} v{}", config.project.name, config.project.version);
//! # Ok(())
//! # }
//! ```
//!
//! ### Workspace Configuration
//!
//! ```rust,no_run
//! use crate::utils::project_config::{GgenConfig, Workspace};
//! use std::collections::BTreeMap;
//!
//! let workspace = Workspace {
//!     members: vec!["crates/*".to_string(), "packages/*".to_string()],
//!     exclude: Some(vec!["target".to_string()]),
//!     dependencies: Some(BTreeMap::new()),
//!     graph: None,
//! };
//! ```

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

// ============================================================================
// Root Configuration
// ============================================================================

/// Root configuration structure for ggen projects
///
/// Supports full ggen.toml schema with all features including workspaces,
/// dependencies, ontology, templates, generators, lifecycle, plugins, and profiles.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct GgenConfig {
    /// Project metadata and settings
    pub project: Project,

    /// Workspace configuration for mono-repos
    #[serde(skip_serializing_if = "Option::is_none")]
    pub workspace: Option<Workspace>,

    /// Graph-based dependency resolution
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph: Option<Graph>,

    /// Project dependencies (Cargo.toml-like)
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub dependencies: BTreeMap<String, Dependency>,

    /// Development dependencies
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub dev_dependencies: BTreeMap<String, Dependency>,

    /// Build dependencies
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub build_dependencies: BTreeMap<String, Dependency>,

    /// Target-specific dependencies
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub target: BTreeMap<String, TargetDependencies>,

    /// RDF/OWL ontology configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ontology: Option<Ontology>,

    /// Template configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub templates: Option<Templates>,

    /// Code generator configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub generators: Option<Generators>,

    /// Build lifecycle configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lifecycle: Option<Lifecycle>,

    /// Plugin system configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub plugins: Option<Plugins>,

    /// Environment profiles (dev, production, test, ci, bench)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub profiles: Option<Profiles>,

    /// Package and build metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,

    /// Validation rules and constraints
    #[serde(skip_serializing_if = "Option::is_none")]
    pub validation: Option<Validation>,

    /// RDF namespace prefixes (legacy support)
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub prefixes: BTreeMap<String, String>,

    /// Legacy RDF configuration (for backward compatibility)
    #[serde(rename = "rdf", skip_serializing_if = "Option::is_none")]
    pub rdf: Option<RdfConfig>,

    /// Template variables (legacy support)
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub vars: BTreeMap<String, String>,
}

// ============================================================================
// Project Section
// ============================================================================

/// Project metadata and settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Project {
    /// Project name (required)
    pub name: String,

    /// Project version (required)
    pub version: String,

    /// Project description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Project authors
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub authors: Vec<String>,

    /// License identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,

    /// Rust edition (2015, 2018, 2021, 2024)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub edition: Option<String>,

    /// Project type (auto, library, binary, workspace)
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub project_type: Option<String>,

    /// Primary language (auto, rust, typescript, python, multi)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,

    /// RDF URI for this project
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,

    /// Short namespace prefix for RDF queries
    #[serde(skip_serializing_if = "Option::is_none")]
    pub namespace: Option<String>,

    /// Template to inherit from
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extends: Option<String>,

    /// Output directory (legacy support)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_dir: Option<PathBuf>,
}

// ============================================================================
// Workspace Section
// ============================================================================

/// Workspace configuration for mono-repos
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Workspace {
    /// Workspace members (glob patterns supported)
    #[serde(default)]
    pub members: Vec<String>,

    /// Paths to exclude from workspace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclude: Option<Vec<String>>,

    /// Shared dependencies across workspace
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<BTreeMap<String, Dependency>>,

    /// Workspace-level graph queries
    #[serde(skip_serializing_if = "Option::is_none")]
    pub graph: Option<WorkspaceGraph>,
}

/// Workspace-level graph configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkspaceGraph {
    /// SPARQL query for workspace analysis
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
}

// ============================================================================
// Graph Section
// ============================================================================

/// Graph-based dependency resolution configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Graph {
    /// Resolution strategy (smart, conservative, aggressive)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strategy: Option<String>,

    /// Conflict resolution strategy (newest, oldest, semver-compatible)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conflict_resolution: Option<String>,

    /// Graph-based dependency queries
    #[serde(skip_serializing_if = "Option::is_none")]
    pub queries: Option<BTreeMap<String, String>>,

    /// Feature flags as graph nodes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub features: Option<BTreeMap<String, Vec<String>>>,
}

// ============================================================================
// Dependencies Section
// ============================================================================

/// Dependency specification (Cargo.toml-like)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Dependency {
    /// Simple version string
    Simple(String),
    /// Detailed dependency specification
    Detailed(DetailedDependency),
}

/// Detailed dependency specification
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DetailedDependency {
    /// Version requirement
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Feature flags to enable
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub features: Vec<String>,

    /// Whether dependency is optional
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optional: Option<bool>,

    /// Default features enabled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_features: Option<bool>,

    /// Git repository URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub git: Option<String>,

    /// Git branch
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,

    /// Git tag
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,

    /// Git revision
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rev: Option<String>,

    /// Local path dependency
    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<PathBuf>,

    /// Alternative registry
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registry: Option<String>,

    /// Package name (if different from dependency key)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub package: Option<String>,
}

/// Target-specific dependencies
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TargetDependencies {
    /// Dependencies for this target
    #[serde(default)]
    pub dependencies: BTreeMap<String, Dependency>,
}

// ============================================================================
// Ontology Section
// ============================================================================

/// RDF/OWL ontology integration configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Ontology {
    /// Ontology files (Turtle format)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub files: Vec<PathBuf>,

    /// Inline RDF (Turtle format)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inline: Option<String>,

    /// SHACL shape files for validation
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shapes: Vec<PathBuf>,

    /// Constitution (invariant checks)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub constitution: Option<Constitution>,
}

/// Constitution configuration (invariant checks)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Constitution {
    /// Built-in checks (NoRetrocausation, TypeSoundness, GuardSoundness)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub checks: Vec<String>,

    /// Custom invariants (SPARQL ASK queries)
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub custom: BTreeMap<String, String>,

    /// Enforce strict mode (production)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforce_strict: Option<bool>,

    /// Fail on warnings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fail_on_warning: Option<bool>,
}

// ============================================================================
// Templates Section
// ============================================================================

/// Template composition and inheritance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Templates {
    /// Template search paths
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub paths: Vec<String>,

    /// Default template variables
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vars: Option<BTreeMap<String, String>>,

    /// Template inheritance chain
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extends: Option<BTreeMap<String, String>>,

    /// Template composition (merge multiple templates)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compose: Option<BTreeMap<String, Vec<String>>>,

    /// Template guards (conditional rendering)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub guards: Option<BTreeMap<String, String>>,

    /// SPARQL-driven template queries
    #[serde(skip_serializing_if = "Option::is_none")]
    pub queries: Option<BTreeMap<String, String>>,
}

// ============================================================================
// Generators Section
// ============================================================================

/// Code generation pipeline configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Generators {
    /// Generator registry URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registry: Option<String>,

    /// Installed generators
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub installed: BTreeMap<String, InstalledGenerator>,

    /// Generator pipelines
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub pipeline: Vec<GeneratorPipeline>,

    /// Generator hooks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<GeneratorHooks>,
}

/// Installed generator specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledGenerator {
    /// Generator version
    pub version: String,

    /// Registry name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub registry: Option<String>,

    /// Git source
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
}

/// Generator pipeline (multi-step workflow)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratorPipeline {
    /// Pipeline name
    pub name: String,

    /// Pipeline description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Input files
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub inputs: Vec<String>,

    /// Pipeline steps
    #[serde(default)]
    pub steps: Vec<PipelineStep>,
}

/// Pipeline step specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineStep {
    /// Action type (template, parse, transform, exec)
    pub action: String,

    /// Template name (for template action)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template: Option<String>,

    /// Parser name (for parse action)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parser: Option<String>,

    /// SPARQL query file (for transform action)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,

    /// Shell command (for exec action)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
}

/// Generator lifecycle hooks
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneratorHooks {
    /// Before generation hook
    #[serde(skip_serializing_if = "Option::is_none")]
    pub before_generate: Option<String>,

    /// After generation hook
    #[serde(skip_serializing_if = "Option::is_none")]
    pub after_generate: Option<String>,

    /// Error handler hook
    #[serde(skip_serializing_if = "Option::is_none")]
    pub on_error: Option<String>,
}

// ============================================================================
// Lifecycle Section
// ============================================================================

/// Build lifecycle configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Lifecycle {
    /// Lifecycle phases
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub phases: Vec<String>,

    /// Phase hooks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<BTreeMap<String, Vec<String>>>,

    /// Task definitions
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub tasks: BTreeMap<String, LifecycleTask>,

    /// Parallel execution groups
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel: Option<BTreeMap<String, Vec<String>>>,
}

/// Lifecycle task definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LifecycleTask {
    /// Task description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Task dependencies
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<String>,

    /// Shell command
    #[serde(skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,

    /// Script to run
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script: Option<String>,
}

// ============================================================================
// Plugins Section
// ============================================================================

/// Plugin system configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Plugins {
    /// Plugin discovery paths
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub paths: Vec<String>,

    /// Installed plugins
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub installed: BTreeMap<String, InstalledPlugin>,

    /// Plugin-specific configuration
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub config: BTreeMap<String, BTreeMap<String, toml::Value>>,

    /// Plugin lifecycle hooks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<BTreeMap<String, Vec<String>>>,

    /// Plugin permissions (security sandboxing)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permissions: Option<BTreeMap<String, PluginPermissions>>,
}

/// Installed plugin specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InstalledPlugin {
    /// Plugin version
    pub version: String,

    /// Plugin source (path, git, registry)
    pub source: String,
}

/// Plugin security permissions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginPermissions {
    /// Filesystem access permissions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub filesystem: Vec<String>,

    /// Network access permissions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub network: Vec<String>,

    /// Executable permissions
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exec: Vec<String>,
}

// ============================================================================
// Profiles Section
// ============================================================================

/// Environment-specific profile configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Profiles {
    /// Default profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,

    /// Development profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dev: Option<Profile>,

    /// Production profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub production: Option<Profile>,

    /// Testing profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub test: Option<Profile>,

    /// CI profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ci: Option<Profile>,

    /// Benchmark profile
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bench: Option<Profile>,
}

/// Profile configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Profile {
    /// Profile to extend
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extends: Option<String>,

    /// Optimization level
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optimization: Option<String>,

    /// Debug assertions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub debug_assertions: Option<bool>,

    /// Overflow checks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub overflow_checks: Option<bool>,

    /// Link-time optimization
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lto: Option<String>,

    /// Strip symbols
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strip: Option<bool>,

    /// Codegen units
    #[serde(skip_serializing_if = "Option::is_none")]
    pub codegen_units: Option<u32>,

    /// Code coverage
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code_coverage: Option<bool>,

    /// Test threads
    #[serde(skip_serializing_if = "Option::is_none")]
    pub test_threads: Option<u32>,

    /// Profile-specific dependencies
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<BTreeMap<String, Dependency>>,

    /// Profile-specific template variables
    #[serde(skip_serializing_if = "Option::is_none")]
    pub templates: Option<ProfileTemplates>,

    /// Profile-specific ontology configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ontology: Option<ProfileOntology>,

    /// Profile-specific lifecycle hooks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lifecycle: Option<ProfileLifecycle>,
}

/// Profile template configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileTemplates {
    /// Template variables
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vars: Option<BTreeMap<String, String>>,
}

/// Profile ontology configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileOntology {
    /// Constitution configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub constitution: Option<Constitution>,
}

/// Profile lifecycle configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileLifecycle {
    /// Lifecycle hooks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hooks: Option<BTreeMap<String, Vec<String>>>,
}

// ============================================================================
// Metadata Section
// ============================================================================

/// Package and build metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Metadata {
    /// Project homepage
    #[serde(skip_serializing_if = "Option::is_none")]
    pub homepage: Option<String>,

    /// Documentation URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation: Option<String>,

    /// Repository URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,

    /// Changelog file
    #[serde(skip_serializing_if = "Option::is_none")]
    pub changelog: Option<String>,

    /// Build metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub build: Option<BuildMetadata>,

    /// Package metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub package: Option<PackageMetadata>,
}

/// Build metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BuildMetadata {
    /// Rust compiler version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rustc_version: Option<String>,

    /// LLVM version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub llvm_version: Option<String>,

    /// Target triple
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_triple: Option<String>,
}

/// Package metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageMetadata {
    /// Package categories
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub categories: Vec<String>,

    /// Package keywords
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub keywords: Vec<String>,

    /// README file
    #[serde(skip_serializing_if = "Option::is_none")]
    pub readme: Option<String>,

    /// Files to include
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub include: Vec<String>,

    /// Files to exclude
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exclude: Vec<String>,
}

// ============================================================================
// Validation Section
// ============================================================================

/// Validation rules and constraints
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Validation {
    /// Minimum Rust version
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_rust_version: Option<String>,

    /// Dependency constraints
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dependencies: Option<DependencyValidation>,

    /// Code quality thresholds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub quality: Option<QualityThresholds>,
}

/// Dependency validation rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyValidation {
    /// Maximum duplicate dependencies
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_duplicates: Option<u32>,

    /// Allow yanked crates
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_yanked: Option<bool>,

    /// Require checksums
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_checksums: Option<bool>,
}

/// Code quality thresholds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityThresholds {
    /// Minimum code coverage percentage
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_coverage: Option<u32>,

    /// Maximum cyclomatic complexity
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_complexity: Option<u32>,

    /// Maximum function lines
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_function_lines: Option<u32>,
}

// ============================================================================
// Legacy RDF Configuration (Backward Compatibility)
// ============================================================================

/// Legacy RDF configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RdfConfig {
    /// RDF files
    #[serde(default)]
    pub files: Vec<PathBuf>,

    /// Inline RDF data
    #[serde(default)]
    pub inline: Vec<String>,
}

// ============================================================================
// Implementation - Validation and Builder Methods
// ============================================================================

impl GgenConfig {
    /// Validate the configuration for consistency
    ///
    /// Checks:
    /// - Required fields are present
    /// - Version strings are valid
    /// - Paths exist (if validation enabled)
    /// - Dependencies are consistent
    ///
    /// # Errors
    ///
    /// Returns error if validation fails
    pub fn validate(&self) -> Result<(), String> {
        // Validate project name is not empty
        if self.project.name.is_empty() {
            return Err("Project name cannot be empty".to_string());
        }

        // Validate version format (basic semver check)
        if !self.is_valid_version(&self.project.version) {
            return Err(format!("Invalid version format: {}", self.project.version));
        }

        // Validate workspace members if present
        if let Some(ref workspace) = self.workspace {
            if workspace.members.is_empty() {
                return Err("Workspace members cannot be empty".to_string());
            }
        }

        // Validate minimum Rust version if specified
        if let Some(ref validation) = self.validation {
            if let Some(ref min_version) = validation.min_rust_version {
                if !self.is_valid_version(min_version) {
                    return Err(format!("Invalid min_rust_version: {}", min_version));
                }
            }
        }

        Ok(())
    }

    /// Check if version string is valid (basic semver check)
    fn is_valid_version(&self, version: &str) -> bool {
        let parts: Vec<&str> = version.split('.').collect();
        parts.len() >= 2 && parts.iter().all(|p| p.parse::<u32>().is_ok())
    }

    /// Get effective dependencies for a specific profile
    ///
    /// Merges base dependencies with profile-specific overrides
    pub fn get_profile_dependencies(&self, profile_name: &str) -> BTreeMap<String, Dependency> {
        let mut deps = self.dependencies.clone();

        if let Some(ref profiles) = self.profiles {
            let profile = match profile_name {
                "dev" => profiles.dev.as_ref(),
                "production" => profiles.production.as_ref(),
                "test" => profiles.test.as_ref(),
                "ci" => profiles.ci.as_ref(),
                "bench" => profiles.bench.as_ref(),
                _ => None,
            };

            if let Some(profile) = profile {
                if let Some(ref profile_deps) = profile.dependencies {
                    deps.extend(profile_deps.clone());
                }
            }
        }

        deps
    }

    /// Get template variables for a specific profile
    pub fn get_profile_template_vars(&self, profile_name: &str) -> BTreeMap<String, String> {
        let mut vars = self.vars.clone();

        // Add template vars if present
        if let Some(ref templates) = self.templates {
            if let Some(ref template_vars) = templates.vars {
                vars.extend(template_vars.clone());
            }
        }

        // Add profile-specific vars
        if let Some(ref profiles) = self.profiles {
            let profile = match profile_name {
                "dev" => profiles.dev.as_ref(),
                "production" => profiles.production.as_ref(),
                "test" => profiles.test.as_ref(),
                "ci" => profiles.ci.as_ref(),
                "bench" => profiles.bench.as_ref(),
                _ => None,
            };

            if let Some(profile) = profile {
                if let Some(ref templates) = profile.templates {
                    if let Some(ref profile_vars) = templates.vars {
                        vars.extend(profile_vars.clone());
                    }
                }
            }
        }

        vars
    }
}

impl Default for GgenConfig {
    fn default() -> Self {
        Self {
            project: Project {
                name: "untitled".to_string(),
                version: "0.1.0".to_string(),
                description: None,
                authors: Vec::new(),
                license: None,
                edition: Some("2021".to_string()),
                project_type: Some("auto".to_string()),
                language: Some("auto".to_string()),
                uri: None,
                namespace: None,
                extends: None,
                output_dir: None,
            },
            workspace: None,
            graph: None,
            dependencies: BTreeMap::new(),
            dev_dependencies: BTreeMap::new(),
            build_dependencies: BTreeMap::new(),
            target: BTreeMap::new(),
            ontology: None,
            templates: None,
            generators: None,
            lifecycle: None,
            plugins: None,
            profiles: None,
            metadata: None,
            validation: None,
            prefixes: BTreeMap::new(),
            rdf: None,
            vars: BTreeMap::new(),
        }
    }
}

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

    #[test]
    fn test_default_config_is_valid() {
        let config = GgenConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_version_validation() {
        let config = GgenConfig::default();
        assert!(config.is_valid_version("1.0.0"));
        assert!(config.is_valid_version("0.1.0"));
        assert!(config.is_valid_version("2.3.4"));
        assert!(!config.is_valid_version("invalid"));
        assert!(!config.is_valid_version("1"));
    }

    #[test]
    fn test_empty_project_name_fails_validation() {
        let mut config = GgenConfig::default();
        config.project.name = String::new();
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_invalid_version_fails_validation() {
        let mut config = GgenConfig::default();
        config.project.version = "invalid".to_string();
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_profile_dependencies_merge() {
        let mut config = GgenConfig::default();

        config
            .dependencies
            .insert("base".to_string(), Dependency::Simple("1.0".to_string()));

        let mut dev_deps = BTreeMap::new();
        dev_deps.insert("dev-dep".to_string(), Dependency::Simple("2.0".to_string()));

        config.profiles = Some(Profiles {
            default: Some("dev".to_string()),
            dev: Some(Profile {
                extends: None,
                optimization: None,
                debug_assertions: None,
                overflow_checks: None,
                lto: None,
                strip: None,
                codegen_units: None,
                code_coverage: None,
                test_threads: None,
                dependencies: Some(dev_deps),
                templates: None,
                ontology: None,
                lifecycle: None,
            }),
            production: None,
            test: None,
            ci: None,
            bench: None,
        });

        let deps = config.get_profile_dependencies("dev");
        assert_eq!(deps.len(), 2);
        assert!(deps.contains_key("base"));
        assert!(deps.contains_key("dev-dep"));
    }
}