converge-provider 3.7.2

LLM provider implementations for Converge
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
// Copyright 2024-2026 Reflective Labs
// SPDX-License-Identifier: MIT
// See LICENSE file in the project root for full license information.

//! YAML-based model registry loader.
//!
//! Loads model metadata from `config/models.yaml` and provides
//! a registry that can be used for model selection.
//!
//! # Example
//!
//! ```ignore
//! use converge_provider::registry_loader::{load_registry, RegistryConfig};
//!
//! // Load from default path
//! let registry = load_registry()?;
//!
//! // Check available providers
//! for provider in registry.providers() {
//!     println!("{}: {} (key: {})",
//!         provider.id,
//!         provider.api_url,
//!         if provider.is_available() { "set" } else { "missing" }
//!     );
//! }
//! ```

use crate::model_selection::{ModelMetadata, ModelSelector};
use converge_provider_api::selection::{ComplianceLevel, CostClass, DataSovereignty};
use schemars::JsonSchema;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;

/// Error type for registry loading.
#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
    /// Failed to read the YAML file.
    #[error("Failed to read registry file: {0}")]
    IoError(#[from] std::io::Error),

    /// Failed to parse the YAML.
    #[error("Failed to parse registry YAML: {0}")]
    ParseError(#[from] serde_yaml::Error),

    /// Validation error in the registry.
    #[error("Registry validation failed: {0}")]
    ValidationError(String),
}

// =============================================================================
// YAML SCHEMA TYPES (Type-safe with serde enums)
// =============================================================================

/// Root of the YAML file.
///
/// This is the schema for `config/models.yaml`.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RegistryYaml {
    /// All providers.
    pub providers: HashMap<String, ProviderYaml>,
}

/// Provider type classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProviderTypeYaml {
    /// Direct API access to model provider (default).
    #[default]
    Direct,
    /// Routes to multiple underlying providers (adds latency overhead).
    Aggregator,
}

/// A provider in the YAML.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ProviderYaml {
    /// Environment variable for API key.
    pub env_key: String,
    /// Optional secondary environment variable (e.g., Baidu secret key).
    #[serde(default)]
    pub env_key_secondary: Option<String>,
    /// URL to get an API key.
    pub key_url: String,
    /// API endpoint URL.
    pub api_url: String,
    /// ISO country code (2 letters) or "LOCAL".
    pub country: String,
    /// Region (US, EU, CN, LOCAL, etc.).
    pub region: RegionYaml,
    /// Compliance certifications.
    #[serde(default)]
    pub compliance: Vec<ComplianceYaml>,
    /// Provider type (direct or aggregator).
    #[serde(default)]
    pub provider_type: ProviderTypeYaml,
    /// Models provided.
    pub models: HashMap<String, ModelYaml>,
}

/// Region enum - type-safe parsing.
///
/// Represents the data residency region for a provider.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]
pub enum RegionYaml {
    /// United States
    US,
    /// European Union
    EU,
    /// European Economic Area
    EEA,
    /// Switzerland
    CH,
    /// China
    CN,
    /// Japan
    JP,
    /// United Kingdom
    UK,
    /// Local/on-premises (any jurisdiction)
    LOCAL,
}

impl RegionYaml {
    /// Converts to string for storage.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::US => "US",
            Self::EU => "EU",
            Self::EEA => "EEA",
            Self::CH => "CH",
            Self::CN => "CN",
            Self::JP => "JP",
            Self::UK => "UK",
            Self::LOCAL => "LOCAL",
        }
    }
}

/// Compliance certification enum.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]
pub enum ComplianceYaml {
    /// General Data Protection Regulation (EU)
    GDPR,
    /// Service Organization Control 2
    SOC2,
    /// Health Insurance Portability and Accountability Act
    HIPAA,
}

impl From<ComplianceYaml> for ComplianceLevel {
    fn from(c: ComplianceYaml) -> Self {
        match c {
            ComplianceYaml::GDPR => ComplianceLevel::GDPR,
            ComplianceYaml::SOC2 => ComplianceLevel::SOC2,
            ComplianceYaml::HIPAA => ComplianceLevel::HIPAA,
        }
    }
}

/// Cost class for model pricing tier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema)]
pub enum CostClassYaml {
    /// Very low cost (e.g., Haiku, GPT-3.5, local models)
    VeryLow,
    /// Low cost (e.g., Sonnet, GPT-4o)
    Low,
    /// Medium cost (e.g., GPT-4 Turbo)
    Medium,
    /// High cost (e.g., Opus, o1-mini)
    High,
    /// Very high cost (e.g., o1-preview)
    VeryHigh,
}

impl From<CostClassYaml> for CostClass {
    fn from(c: CostClassYaml) -> Self {
        match c {
            CostClassYaml::VeryLow => CostClass::VeryLow,
            CostClassYaml::Low => CostClass::Low,
            CostClassYaml::Medium => CostClass::Medium,
            CostClassYaml::High => CostClass::High,
            CostClassYaml::VeryHigh => CostClass::VeryHigh,
        }
    }
}

/// Model capability flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CapabilityYaml {
    /// Function/tool calling support
    ToolUse,
    /// Image understanding
    Vision,
    /// JSON mode / schema enforcement
    StructuredOutput,
    /// Code generation/understanding
    Code,
    /// Multi-step logical reasoning
    Reasoning,
    /// Good performance across languages
    Multilingual,
    /// Real-time web information retrieval
    WebSearch,
    /// Audio input/output support
    Audio,
    /// Image generation support
    ImageGeneration,
    /// Streaming responses
    Streaming,
    /// Logprobs support
    Logprobs,
    /// Deterministic seed support
    Seed,
    /// Tool choice (e.g., required/none/auto)
    ToolChoice,
    /// Parallel tool call support
    ParallelToolCalls,
    /// Prompt caching support
    PromptCaching,
    /// Built-in file search retrieval
    FileSearch,
    /// Built-in code interpreter / sandbox execution
    CodeInterpreter,
    /// Built-in browser automation / computer use
    ComputerUse,
    /// Tool-level web search (native search tool)
    ToolSearch,
    /// Model Context Protocol tool support
    Mcp,
    /// Hosted shell tool support
    HostedShell,
    /// Apply-patch tool support
    ApplyPatch,
    /// Native context compaction support
    NativeCompaction,
    /// Reasoning effort controls (e.g., low/medium/high)
    ReasoningEffort,
}

impl CapabilityYaml {
    /// Stable `snake_case` string representation used in API responses.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::ToolUse => "tool_use",
            Self::Vision => "vision",
            Self::StructuredOutput => "structured_output",
            Self::Code => "code",
            Self::Reasoning => "reasoning",
            Self::Multilingual => "multilingual",
            Self::WebSearch => "web_search",
            Self::Audio => "audio",
            Self::ImageGeneration => "image_generation",
            Self::Streaming => "streaming",
            Self::Logprobs => "logprobs",
            Self::Seed => "seed",
            Self::ToolChoice => "tool_choice",
            Self::ParallelToolCalls => "parallel_tool_calls",
            Self::PromptCaching => "prompt_caching",
            Self::FileSearch => "file_search",
            Self::CodeInterpreter => "code_interpreter",
            Self::ComputerUse => "computer_use",
            Self::ToolSearch => "tool_search",
            Self::Mcp => "mcp",
            Self::HostedShell => "hosted_shell",
            Self::ApplyPatch => "apply_patch",
            Self::NativeCompaction => "native_compaction",
            Self::ReasoningEffort => "reasoning_effort",
        }
    }
}

/// Supported reasoning effort level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ReasoningEffortYaml {
    /// Disable explicit chain-of-thought style effort controls.
    None,
    /// Minimal extra reasoning.
    Minimal,
    /// Low extra reasoning.
    Low,
    /// Medium extra reasoning.
    Medium,
    /// High extra reasoning.
    High,
    /// Extra-high reasoning.
    Xhigh,
}

impl ReasoningEffortYaml {
    /// Stable `snake_case` string representation used in API responses.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Minimal => "minimal",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::Xhigh => "xhigh",
        }
    }
}

/// Model type classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema, Default)]
#[serde(rename_all = "snake_case")]
pub enum ModelTypeYaml {
    /// LLM for chat/completion (default)
    #[default]
    Llm,
    /// Vector embedding model
    Embedding,
    /// Cross-encoder reranking model
    Reranker,
    /// OCR / Document AI model
    Ocr,
}

/// Model architecture type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, JsonSchema, Default)]
#[serde(rename_all = "snake_case")]
pub enum ArchitectureYaml {
    /// Traditional transformer (all parameters active).
    #[default]
    Dense,
    /// Mixture of Experts (only subset active per forward pass).
    Moe,
    /// Hybrid architecture (e.g., Jamba's Mamba-Transformer).
    Hybrid,
}

/// Input modality type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ModalityYaml {
    /// Text input/output.
    Text,
    /// Image input.
    Image,
    /// Video input.
    Video,
    /// Audio input.
    Audio,
}

/// Agentic capabilities configuration.
#[derive(Debug, Clone, Default, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct AgenticYaml {
    /// Maximum number of parallel agents this model can orchestrate.
    #[serde(default)]
    pub max_parallel_agents: Option<u32>,
    /// Whether the model supports agent orchestration/swarm.
    #[serde(default)]
    pub supports_orchestration: bool,
}

/// Pricing information (USD per million tokens).
#[derive(Debug, Clone, Default, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PricingYaml {
    /// Input price per million tokens (USD).
    #[serde(default)]
    pub input_per_m: Option<f64>,
    /// Output price per million tokens (USD).
    #[serde(default)]
    pub output_per_m: Option<f64>,
}

/// Rate limit information (provider- or model-specific).
#[derive(Debug, Clone, Default, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RateLimitsYaml {
    /// Requests per minute.
    #[serde(default)]
    pub requests_per_min: Option<u32>,
    /// Tokens per minute.
    #[serde(default)]
    pub tokens_per_min: Option<u32>,
    /// Requests per day.
    #[serde(default)]
    pub requests_per_day: Option<u32>,
    /// Maximum concurrent requests.
    #[serde(default)]
    pub concurrent_requests: Option<u32>,
}

/// A model entry in the registry.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ModelYaml {
    /// Cost class - validated at parse time.
    pub cost_class: CostClassYaml,
    /// Typical latency in milliseconds (must be > 0).
    pub typical_latency_ms: u32,
    /// Quality score (must be 0.0-1.0).
    pub quality: f64,
    /// Context window size in tokens.
    #[serde(default = "default_context_tokens")]
    pub context_tokens: usize,
    /// Capabilities list - validated at parse time.
    #[serde(default)]
    pub capabilities: Vec<CapabilityYaml>,
    /// Model type - validated at parse time.
    #[serde(default, rename = "type")]
    pub model_type: ModelTypeYaml,
    /// Embedding dimensions (required for embedding models).
    #[serde(default)]
    pub dimensions: Option<usize>,

    // === ENRICHED SCHEMA ===
    /// Model architecture (dense, moe, hybrid).
    #[serde(default)]
    pub architecture: ArchitectureYaml,
    /// Total parameters in billions.
    #[serde(default)]
    pub total_params_b: Option<f64>,
    /// Active parameters per forward pass in billions (for `MoE` models).
    #[serde(default)]
    pub active_params_b: Option<f64>,
    /// Maximum output tokens.
    #[serde(default)]
    pub max_output_tokens: Option<usize>,
    /// Whether the model is native multimodal (trained on mixed modalities).
    #[serde(default)]
    pub native_multimodal: bool,
    /// Supported input modalities.
    #[serde(default)]
    pub modalities: Vec<ModalityYaml>,
    /// Agentic/swarm capabilities.
    #[serde(default)]
    pub agentic: Option<AgenticYaml>,
    /// Whether the model supports extended thinking/reasoning mode.
    #[serde(default)]
    pub thinking_mode: bool,
    /// Supported reasoning effort levels (e.g., [low, medium, high]).
    #[serde(default)]
    pub reasoning_effort_levels: Vec<ReasoningEffortYaml>,
    /// Whether the model supports native context compaction.
    #[serde(default)]
    pub native_compaction: bool,
    /// Model ID of the thinking variant (if this is the base model).
    #[serde(default)]
    pub thinking_variant: Option<String>,
    /// Pricing information.
    #[serde(default)]
    pub pricing: Option<PricingYaml>,
    /// Model publisher or organization (e.g., `OpenAI`, Anthropic).
    #[serde(default)]
    pub publisher: Option<String>,
    /// Model family name (e.g., Claude, GPT, Llama).
    #[serde(default)]
    pub family: Option<String>,
    /// Release date (ISO-8601 format recommended).
    #[serde(default)]
    pub release_date: Option<String>,
    /// Training data cutoff date (ISO-8601 format recommended).
    #[serde(default)]
    pub training_cutoff: Option<String>,
    /// Whether model weights are openly available.
    #[serde(default)]
    pub open_weights: bool,
    /// License identifier or URL.
    #[serde(default)]
    pub license: Option<String>,
    /// Whether the model is deprecated.
    #[serde(default)]
    pub deprecated: bool,
    /// Whether the model is in beta/preview.
    #[serde(default)]
    pub beta: bool,
    /// Benchmark scores (keyed by benchmark name).
    #[serde(default)]
    pub benchmarks: HashMap<String, f64>,
    /// Free-form tags for routing or promotion.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Rate limit information (if published).
    #[serde(default)]
    pub rate_limits: Option<RateLimitsYaml>,
    /// Free-form notes.
    #[serde(default)]
    pub notes: Option<String>,
}

fn default_context_tokens() -> usize {
    8192
}

/// Generates JSON Schema for the model registry.
///
/// This can be used for:
/// - IDE autocompletion in YAML files
/// - Pre-runtime validation
/// - Documentation generation
///
/// # Example
///
/// ```
/// use converge_provider::registry_loader::generate_schema;
///
/// let schema = generate_schema();
/// println!("{}", serde_json::to_string_pretty(&schema).unwrap());
/// ```
#[must_use]
pub fn generate_schema() -> schemars::schema::RootSchema {
    schemars::schema_for!(RegistryYaml)
}

// =============================================================================
// LOADED REGISTRY
// =============================================================================

/// Provider type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProviderType {
    /// Direct API access to model provider.
    Direct,
    /// Routes to multiple underlying providers (adds latency overhead).
    Aggregator,
}

/// A loaded provider with its models.
#[derive(Debug, Clone)]
pub struct LoadedProvider {
    /// Provider ID (e.g., "anthropic").
    pub id: String,
    /// Environment variable name for API key.
    pub env_key: String,
    /// Optional secondary env key.
    pub env_key_secondary: Option<String>,
    /// URL to get an API key.
    pub key_url: String,
    /// API endpoint URL.
    pub api_url: String,
    /// ISO country code.
    pub country: String,
    /// Region.
    pub region: String,
    /// Compliance certifications.
    pub compliance: Vec<ComplianceLevel>,
    /// Provider type (direct or aggregator).
    pub provider_type: ProviderType,
    /// Models available.
    pub models: Vec<LoadedModel>,
}

impl LoadedProvider {
    /// Checks if this provider is available (env key is set).
    #[must_use]
    pub fn is_available(&self) -> bool {
        let primary_ok = std::env::var(&self.env_key).is_ok();
        let secondary_ok = self
            .env_key_secondary
            .as_ref()
            .map(|k| std::env::var(k).is_ok())
            .unwrap_or(true);
        primary_ok && secondary_ok
    }

    /// Returns the API key from environment (if available).
    #[must_use]
    pub fn api_key(&self) -> Option<String> {
        std::env::var(&self.env_key).ok()
    }

    /// Returns the secondary API key from environment (if available).
    #[must_use]
    pub fn secondary_api_key(&self) -> Option<String> {
        self.env_key_secondary
            .as_ref()
            .and_then(|k| std::env::var(k).ok())
    }
}

/// Model architecture.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Architecture {
    /// Traditional transformer (all parameters active).
    Dense,
    /// Mixture of Experts (only subset active per forward pass).
    Moe,
    /// Hybrid architecture (e.g., Jamba's Mamba-Transformer).
    Hybrid,
}

/// Input modality.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Modality {
    /// Text input/output.
    Text,
    /// Image input.
    Image,
    /// Video input.
    Video,
    /// Audio input.
    Audio,
}

/// Agentic capabilities.
#[derive(Debug, Clone, Default)]
pub struct AgenticCapabilities {
    /// Maximum number of parallel agents this model can orchestrate.
    pub max_parallel_agents: Option<u32>,
    /// Whether the model supports agent orchestration/swarm.
    pub supports_orchestration: bool,
}

/// Pricing information (USD per million tokens).
#[derive(Debug, Clone, Default)]
pub struct Pricing {
    /// Input price per million tokens (USD).
    pub input_per_m: Option<f64>,
    /// Output price per million tokens (USD).
    pub output_per_m: Option<f64>,
}

/// Rate limit information (provider- or model-specific).
#[derive(Debug, Clone, Default)]
pub struct RateLimits {
    /// Requests per minute.
    pub requests_per_min: Option<u32>,
    /// Tokens per minute.
    pub tokens_per_min: Option<u32>,
    /// Requests per day.
    pub requests_per_day: Option<u32>,
    /// Maximum concurrent requests.
    pub concurrent_requests: Option<u32>,
}

/// Reasoning effort level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReasoningEffort {
    /// Disable explicit chain-of-thought style effort controls.
    None,
    /// Minimal extra reasoning.
    Minimal,
    /// Low extra reasoning.
    Low,
    /// Medium extra reasoning.
    Medium,
    /// High extra reasoning.
    High,
    /// Extra-high reasoning.
    Xhigh,
}

impl ReasoningEffort {
    /// Stable `snake_case` string representation used in API responses.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Minimal => "minimal",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::Xhigh => "xhigh",
        }
    }
}

/// A loaded model.
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct LoadedModel {
    /// Model ID.
    pub id: String,
    /// Cost class.
    pub cost_class: CostClass,
    /// Typical latency in ms.
    pub typical_latency_ms: u32,
    /// Quality score.
    pub quality: f64,
    /// Context tokens.
    pub context_tokens: usize,
    /// Model type (llm, embedding, reranker).
    pub model_type: ModelType,
    /// Embedding dimensions (for embedding models).
    pub dimensions: Option<usize>,
    /// Full capability list (`snake_case` enum values from YAML).
    pub capabilities: Vec<CapabilityYaml>,
    // Capabilities
    /// Tool use support.
    pub supports_tool_use: bool,
    /// Vision support.
    pub supports_vision: bool,
    /// Structured output support.
    pub supports_structured_output: bool,
    /// Code support.
    pub supports_code: bool,
    /// Reasoning support.
    pub supports_reasoning: bool,
    /// Multilingual support.
    pub supports_multilingual: bool,
    /// Web search support.
    pub supports_web_search: bool,

    // === ENRICHED FIELDS ===
    /// Model architecture (dense, moe, hybrid).
    pub architecture: Architecture,
    /// Total parameters in billions.
    pub total_params_b: Option<f64>,
    /// Active parameters per forward pass in billions (for `MoE` models).
    pub active_params_b: Option<f64>,
    /// Maximum output tokens.
    pub max_output_tokens: Option<usize>,
    /// Whether the model is native multimodal (trained on mixed modalities).
    pub native_multimodal: bool,
    /// Supported input modalities.
    pub modalities: Vec<Modality>,
    /// Agentic/swarm capabilities.
    pub agentic: Option<AgenticCapabilities>,
    /// Whether the model supports extended thinking/reasoning mode.
    pub thinking_mode: bool,
    /// Supported reasoning effort levels.
    pub reasoning_effort_levels: Vec<ReasoningEffort>,
    /// Whether the model supports native context compaction.
    pub native_compaction: bool,
    /// Model ID of the thinking variant (if this is the base model).
    pub thinking_variant: Option<String>,
    /// Pricing information.
    pub pricing: Option<Pricing>,
    /// Model publisher or organization (e.g., `OpenAI`, Anthropic).
    pub publisher: Option<String>,
    /// Model family name (e.g., Claude, GPT, Llama).
    pub family: Option<String>,
    /// Release date (ISO-8601 format recommended).
    pub release_date: Option<String>,
    /// Training data cutoff date (ISO-8601 format recommended).
    pub training_cutoff: Option<String>,
    /// Whether model weights are openly available.
    pub open_weights: bool,
    /// License identifier or URL.
    pub license: Option<String>,
    /// Whether the model is deprecated.
    pub deprecated: bool,
    /// Whether the model is in beta/preview.
    pub beta: bool,
    /// Benchmark scores (keyed by benchmark name).
    pub benchmarks: HashMap<String, f64>,
    /// Free-form tags for routing or promotion.
    pub tags: Vec<String>,
    /// Rate limit information (if published).
    pub rate_limits: Option<RateLimits>,
    /// Free-form notes.
    pub notes: Option<String>,
}

/// Model type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModelType {
    /// LLM for chat/completion.
    Llm,
    /// Embedding model.
    Embedding,
    /// Reranker model.
    Reranker,
    /// OCR / Document AI model.
    Ocr,
}

/// The loaded model registry.
#[derive(Debug, Clone)]
pub struct LoadedRegistry {
    /// All providers.
    providers: Vec<LoadedProvider>,
}

impl LoadedRegistry {
    /// Returns all providers.
    #[must_use]
    pub fn providers(&self) -> &[LoadedProvider] {
        &self.providers
    }

    /// Returns available providers (with API keys set).
    #[must_use]
    pub fn available_providers(&self) -> Vec<&LoadedProvider> {
        self.providers.iter().filter(|p| p.is_available()).collect()
    }

    /// Finds a provider by ID.
    #[must_use]
    pub fn get_provider(&self, id: &str) -> Option<&LoadedProvider> {
        self.providers.iter().find(|p| p.id == id)
    }

    /// Returns all LLM models.
    #[must_use]
    pub fn llm_models(&self) -> Vec<(&LoadedProvider, &LoadedModel)> {
        self.providers
            .iter()
            .flat_map(|p| {
                p.models
                    .iter()
                    .filter(|m| m.model_type == ModelType::Llm)
                    .map(move |m| (p, m))
            })
            .collect()
    }

    /// Returns all embedding models.
    #[must_use]
    pub fn embedding_models(&self) -> Vec<(&LoadedProvider, &LoadedModel)> {
        self.providers
            .iter()
            .flat_map(|p| {
                p.models
                    .iter()
                    .filter(|m| m.model_type == ModelType::Embedding)
                    .map(move |m| (p, m))
            })
            .collect()
    }

    /// Returns all reranker models.
    #[must_use]
    pub fn reranker_models(&self) -> Vec<(&LoadedProvider, &LoadedModel)> {
        self.providers
            .iter()
            .flat_map(|p| {
                p.models
                    .iter()
                    .filter(|m| m.model_type == ModelType::Reranker)
                    .map(move |m| (p, m))
            })
            .collect()
    }

    /// Converts to a `ModelSelector` for use with the selection system.
    #[must_use]
    pub fn to_model_selector(&self) -> ModelSelector {
        let mut selector = ModelSelector::empty();

        for provider in &self.providers {
            for model in &provider.models {
                if model.model_type != ModelType::Llm {
                    continue; // ModelSelector is for LLMs only
                }

                let data_sovereignty = match provider.region.as_str() {
                    "EU" | "EEA" => DataSovereignty::EU,
                    "CH" => DataSovereignty::Switzerland,
                    "CN" => DataSovereignty::China,
                    "US" => DataSovereignty::US,
                    "LOCAL" => DataSovereignty::OnPremises,
                    _ => DataSovereignty::Any,
                };

                let compliance = provider
                    .compliance
                    .first()
                    .copied()
                    .unwrap_or(ComplianceLevel::None);

                let metadata = ModelMetadata::new(
                    &provider.id,
                    &model.id,
                    model.cost_class,
                    model.typical_latency_ms,
                    model.quality,
                )
                .with_reasoning(model.supports_reasoning)
                .with_web_search(model.supports_web_search)
                .with_data_sovereignty(data_sovereignty)
                .with_compliance(compliance)
                .with_multilingual(model.supports_multilingual)
                .with_context_tokens(model.context_tokens)
                .with_tool_use(model.supports_tool_use)
                .with_vision(model.supports_vision)
                .with_structured_output(model.supports_structured_output)
                .with_code(model.supports_code)
                .with_location(&provider.country, &provider.region);

                selector = selector.with_model(metadata);
            }
        }

        selector
    }

    /// Prints a summary of all providers.
    pub fn print_summary(&self) {
        println!("Model Registry Summary");
        println!("======================\n");

        for provider in &self.providers {
            let status = if provider.is_available() {
                "✓ available"
            } else {
                "✗ no key"
            };

            println!(
                "{} ({}) - {} models [{}]",
                provider.id,
                provider.region,
                provider.models.len(),
                status
            );
            println!("  Key URL: {}", provider.key_url);
            println!("  API URL: {}", provider.api_url);
            println!();
        }
    }
}

// =============================================================================
// LOADING FUNCTIONS
// =============================================================================

/// Default path for the model registry relative to crate root.
pub const DEFAULT_REGISTRY_PATH: &str = "converge-provider/config/models.yaml";

/// Loads the registry from the default path.
///
/// Tries these paths in order:
/// 1. `converge-provider/config/models.yaml` (when run from workspace root)
/// 2. `config/models.yaml` (when run from converge-provider directory)
/// 3. `CONVERGE_MODELS_PATH` environment variable
///
/// # Errors
///
/// Returns error if the file cannot be read or parsed.
pub fn load_registry() -> Result<LoadedRegistry, RegistryError> {
    // Check environment variable first
    if let Ok(path) = std::env::var("CONVERGE_MODELS_PATH") {
        return load_registry_from_path(&path);
    }

    // Try workspace-relative path
    if std::path::Path::new(DEFAULT_REGISTRY_PATH).exists() {
        return load_registry_from_path(DEFAULT_REGISTRY_PATH);
    }

    // Try crate-relative path
    let crate_path = "config/models.yaml";
    if std::path::Path::new(crate_path).exists() {
        return load_registry_from_path(crate_path);
    }

    // Fall back to compiled-in default
    load_registry_from_str(include_str!("../config/models.yaml"))
}

/// Loads the registry from a specific path.
///
/// # Errors
///
/// Returns error if the file cannot be read or parsed.
pub fn load_registry_from_path(path: impl AsRef<Path>) -> Result<LoadedRegistry, RegistryError> {
    let content = std::fs::read_to_string(path)?;
    load_registry_from_str(&content)
}

/// Loads the registry from a YAML string.
///
/// # Errors
///
/// Returns error if the YAML cannot be parsed or validation fails.
pub fn load_registry_from_str(yaml: &str) -> Result<LoadedRegistry, RegistryError> {
    let registry_yaml: RegistryYaml = serde_yaml::from_str(yaml)?;

    let mut providers = Vec::new();
    let mut errors = Vec::new();

    for (provider_id, provider_yaml) in registry_yaml.providers {
        // Validate provider
        if let Err(e) = validate_provider(&provider_id, &provider_yaml) {
            errors.push(e);
            continue;
        }

        let compliance = provider_yaml
            .compliance
            .iter()
            .map(|c| ComplianceLevel::from(*c))
            .collect();

        let mut models = Vec::new();

        for (model_id, model_yaml) in provider_yaml.models {
            // Validate model
            if let Err(e) = validate_model(&provider_id, &model_id, &model_yaml) {
                errors.push(e);
                continue;
            }

            let capabilities: std::collections::HashSet<_> =
                model_yaml.capabilities.iter().copied().collect();

            // Map modalities
            let modalities: Vec<Modality> = model_yaml
                .modalities
                .iter()
                .map(|m| match m {
                    ModalityYaml::Text => Modality::Text,
                    ModalityYaml::Image => Modality::Image,
                    ModalityYaml::Video => Modality::Video,
                    ModalityYaml::Audio => Modality::Audio,
                })
                .collect();

            // Map reasoning effort levels
            let reasoning_effort_levels = model_yaml
                .reasoning_effort_levels
                .iter()
                .copied()
                .map(ReasoningEffort::from)
                .collect();

            // Map agentic capabilities
            let agentic = model_yaml.agentic.as_ref().map(|a| AgenticCapabilities {
                max_parallel_agents: a.max_parallel_agents,
                supports_orchestration: a.supports_orchestration,
            });

            // Map pricing
            let pricing = model_yaml.pricing.as_ref().map(|p| Pricing {
                input_per_m: p.input_per_m,
                output_per_m: p.output_per_m,
            });

            // Map rate limits
            let rate_limits = model_yaml.rate_limits.as_ref().map(|r| RateLimits {
                requests_per_min: r.requests_per_min,
                tokens_per_min: r.tokens_per_min,
                requests_per_day: r.requests_per_day,
                concurrent_requests: r.concurrent_requests,
            });

            let model = LoadedModel {
                id: model_id,
                cost_class: model_yaml.cost_class.into(),
                typical_latency_ms: model_yaml.typical_latency_ms,
                quality: model_yaml.quality,
                context_tokens: model_yaml.context_tokens,
                model_type: model_yaml.model_type.into(),
                dimensions: model_yaml.dimensions,
                capabilities: model_yaml.capabilities.clone(),
                supports_tool_use: capabilities.contains(&CapabilityYaml::ToolUse),
                supports_vision: capabilities.contains(&CapabilityYaml::Vision),
                supports_structured_output: capabilities
                    .contains(&CapabilityYaml::StructuredOutput),
                supports_code: capabilities.contains(&CapabilityYaml::Code),
                supports_reasoning: capabilities.contains(&CapabilityYaml::Reasoning),
                supports_multilingual: capabilities.contains(&CapabilityYaml::Multilingual),
                supports_web_search: capabilities.contains(&CapabilityYaml::WebSearch),
                // Enriched fields
                architecture: model_yaml.architecture.into(),
                total_params_b: model_yaml.total_params_b,
                active_params_b: model_yaml.active_params_b,
                max_output_tokens: model_yaml.max_output_tokens,
                native_multimodal: model_yaml.native_multimodal,
                modalities,
                agentic,
                thinking_mode: model_yaml.thinking_mode,
                reasoning_effort_levels,
                native_compaction: model_yaml.native_compaction,
                thinking_variant: model_yaml.thinking_variant.clone(),
                pricing,
                publisher: model_yaml.publisher.clone(),
                family: model_yaml.family.clone(),
                release_date: model_yaml.release_date.clone(),
                training_cutoff: model_yaml.training_cutoff.clone(),
                open_weights: model_yaml.open_weights,
                license: model_yaml.license.clone(),
                deprecated: model_yaml.deprecated,
                beta: model_yaml.beta,
                benchmarks: model_yaml.benchmarks.clone(),
                tags: model_yaml.tags.clone(),
                rate_limits,
                notes: model_yaml.notes.clone(),
            };

            models.push(model);
        }

        // Sort models by id for consistent ordering
        models.sort_by(|a, b| a.id.cmp(&b.id));

        let provider = LoadedProvider {
            id: provider_id,
            env_key: provider_yaml.env_key,
            env_key_secondary: provider_yaml.env_key_secondary,
            key_url: provider_yaml.key_url,
            api_url: provider_yaml.api_url,
            country: provider_yaml.country,
            region: provider_yaml.region.as_str().to_string(),
            compliance,
            provider_type: provider_yaml.provider_type.into(),
            models,
        };

        providers.push(provider);
    }

    // Fail if there were any validation errors
    if !errors.is_empty() {
        return Err(RegistryError::ValidationError(errors.join("; ")));
    }

    // Sort providers alphabetically for consistent ordering
    providers.sort_by(|a, b| a.id.cmp(&b.id));

    Ok(LoadedRegistry { providers })
}

/// Validates a provider entry.
fn validate_provider(id: &str, provider: &ProviderYaml) -> Result<(), String> {
    // Validate env_key is not empty
    if provider.env_key.is_empty() {
        return Err(format!("Provider '{id}': env_key cannot be empty"));
    }

    // Validate URLs are valid
    if !provider.key_url.starts_with("http://") && !provider.key_url.starts_with("https://") {
        return Err(format!(
            "Provider '{id}': key_url must be a valid URL, got '{}'",
            provider.key_url
        ));
    }

    if !provider.api_url.starts_with("http://") && !provider.api_url.starts_with("https://") {
        return Err(format!(
            "Provider '{id}': api_url must be a valid URL, got '{}'",
            provider.api_url
        ));
    }

    // Validate country code (2 letters or LOCAL)
    if provider.country != "LOCAL" && provider.country.len() != 2 {
        return Err(format!(
            "Provider '{id}': country must be 2-letter ISO code or 'LOCAL', got '{}'",
            provider.country
        ));
    }

    // Validate has at least one model
    if provider.models.is_empty() {
        return Err(format!("Provider '{id}': must have at least one model"));
    }

    Ok(())
}

/// Validates a model entry.
fn validate_model(provider_id: &str, model_id: &str, model: &ModelYaml) -> Result<(), String> {
    // Validate quality is in range
    if !(0.0..=1.0).contains(&model.quality) {
        return Err(format!(
            "Model '{provider_id}/{model_id}': quality must be 0.0-1.0, got {}",
            model.quality
        ));
    }

    // Validate latency is reasonable
    if model.typical_latency_ms == 0 {
        return Err(format!(
            "Model '{provider_id}/{model_id}': typical_latency_ms must be > 0"
        ));
    }

    // Validate context_tokens is reasonable
    if model.context_tokens == 0 {
        return Err(format!(
            "Model '{provider_id}/{model_id}': context_tokens must be > 0"
        ));
    }

    // Validate embedding models have dimensions
    if model.model_type == ModelTypeYaml::Embedding && model.dimensions.is_none() {
        return Err(format!(
            "Model '{provider_id}/{model_id}': embedding models must specify dimensions"
        ));
    }

    Ok(())
}

impl From<ModelTypeYaml> for ModelType {
    fn from(t: ModelTypeYaml) -> Self {
        match t {
            ModelTypeYaml::Llm => ModelType::Llm,
            ModelTypeYaml::Embedding => ModelType::Embedding,
            ModelTypeYaml::Reranker => ModelType::Reranker,
            ModelTypeYaml::Ocr => ModelType::Ocr,
        }
    }
}

impl From<ArchitectureYaml> for Architecture {
    fn from(a: ArchitectureYaml) -> Self {
        match a {
            ArchitectureYaml::Dense => Architecture::Dense,
            ArchitectureYaml::Moe => Architecture::Moe,
            ArchitectureYaml::Hybrid => Architecture::Hybrid,
        }
    }
}

impl From<ReasoningEffortYaml> for ReasoningEffort {
    fn from(effort: ReasoningEffortYaml) -> Self {
        match effort {
            ReasoningEffortYaml::None => Self::None,
            ReasoningEffortYaml::Minimal => Self::Minimal,
            ReasoningEffortYaml::Low => Self::Low,
            ReasoningEffortYaml::Medium => Self::Medium,
            ReasoningEffortYaml::High => Self::High,
            ReasoningEffortYaml::Xhigh => Self::Xhigh,
        }
    }
}

impl From<ProviderTypeYaml> for ProviderType {
    fn from(p: ProviderTypeYaml) -> Self {
        match p {
            ProviderTypeYaml::Direct => ProviderType::Direct,
            ProviderTypeYaml::Aggregator => ProviderType::Aggregator,
        }
    }
}

// =============================================================================
// TESTS
// =============================================================================

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

    const TEST_YAML: &str = r"
providers:
  test-provider:
    env_key: TEST_API_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    models:
      test-model:
        cost_class: Low
        typical_latency_ms: 2000
        quality: 0.85
        context_tokens: 128000
        capabilities: [tool_use, reasoning, code]

      test-embedding:
        cost_class: VeryLow
        typical_latency_ms: 100
        quality: 0.80
        context_tokens: 8192
        capabilities: []
        type: embedding
        dimensions: 1024
";

    const INVALID_COST_CLASS_YAML: &str = r"
providers:
  bad-provider:
    env_key: TEST_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    models:
      bad-model:
        cost_class: SuperLow
        typical_latency_ms: 100
        quality: 0.5
";

    const INVALID_CAPABILITY_YAML: &str = r"
providers:
  bad-provider:
    env_key: TEST_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    models:
      bad-model:
        cost_class: Low
        typical_latency_ms: 100
        quality: 0.5
        capabilities: [tool_use, telepathy]
";

    const INVALID_QUALITY_YAML: &str = r"
providers:
  bad-provider:
    env_key: TEST_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    models:
      bad-model:
        cost_class: Low
        typical_latency_ms: 100
        quality: 1.5
";

    const MISSING_DIMENSIONS_YAML: &str = r"
providers:
  bad-provider:
    env_key: TEST_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    models:
      bad-embedding:
        cost_class: Low
        typical_latency_ms: 100
        quality: 0.5
        type: embedding
";

    const UNKNOWN_FIELD_YAML: &str = r"
providers:
  bad-provider:
    env_key: TEST_KEY
    key_url: https://test.com/keys
    api_url: https://api.test.com/v1
    country: US
    region: US
    unknown_field: oops
    models:
      model:
        cost_class: Low
        typical_latency_ms: 100
        quality: 0.5
";

    #[test]
    fn parse_yaml() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();
        assert_eq!(registry.providers.len(), 1);

        let provider = &registry.providers[0];
        assert_eq!(provider.id, "test-provider");
        assert_eq!(provider.key_url, "https://test.com/keys");
        assert_eq!(provider.api_url, "https://api.test.com/v1");
        assert_eq!(provider.models.len(), 2);
    }

    #[test]
    fn parse_model_capabilities() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();
        let provider = &registry.providers[0];

        let llm = provider
            .models
            .iter()
            .find(|m| m.id == "test-model")
            .unwrap();
        assert!(llm.supports_tool_use);
        assert!(llm.supports_reasoning);
        assert!(llm.supports_code);
        assert!(!llm.supports_vision);
        assert_eq!(llm.model_type, ModelType::Llm);
    }

    #[test]
    fn parse_embedding_model() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();
        let provider = &registry.providers[0];

        let embedding = provider
            .models
            .iter()
            .find(|m| m.id == "test-embedding")
            .unwrap();
        assert_eq!(embedding.model_type, ModelType::Embedding);
        assert_eq!(embedding.dimensions, Some(1024));
    }

    #[test]
    fn filter_by_model_type() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();

        let llms = registry.llm_models();
        assert_eq!(llms.len(), 1);
        assert_eq!(llms[0].1.id, "test-model");

        let embeddings = registry.embedding_models();
        assert_eq!(embeddings.len(), 1);
        assert_eq!(embeddings[0].1.id, "test-embedding");
    }

    #[test]
    fn to_model_selector() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();
        let selector = registry.to_model_selector();

        // Should have 1 LLM model (embedding is excluded)
        let reqs = converge_core::model_selection::AgentRequirements::balanced();
        let satisfying = selector.list_satisfying(&reqs);
        assert_eq!(satisfying.len(), 1);
    }

    #[test]
    fn provider_availability() {
        let registry = load_registry_from_str(TEST_YAML).unwrap();
        let provider = &registry.providers[0];

        // Should not be available (TEST_API_KEY not set by default)
        // Note: We don't test setting env vars as it requires unsafe in Rust 2024
        let _ = provider.is_available(); // Just verify method works
    }

    #[test]
    fn load_real_registry() {
        // This tests the compiled-in registry via include_str!
        let registry = load_registry().unwrap();

        // Should have multiple providers
        assert!(
            registry.providers.len() >= 10,
            "Expected at least 10 providers"
        );

        // Check some known providers exist
        let provider_ids: Vec<_> = registry.providers.iter().map(|p| p.id.as_str()).collect();
        assert!(provider_ids.contains(&"anthropic"), "Missing anthropic");
        assert!(provider_ids.contains(&"openai"), "Missing openai");
        assert!(provider_ids.contains(&"mistral"), "Missing mistral");
        assert!(provider_ids.contains(&"ollama"), "Missing ollama");

        // Check anthropic has correct URLs
        let anthropic = registry.get_provider("anthropic").unwrap();
        assert_eq!(
            anthropic.key_url,
            "https://console.anthropic.com/settings/keys"
        );
        assert_eq!(anthropic.api_url, "https://api.anthropic.com/v1");
        assert_eq!(anthropic.env_key, "ANTHROPIC_API_KEY");

        // Check ollama is marked as LOCAL
        let ollama = registry.get_provider("ollama").unwrap();
        assert_eq!(ollama.region, "LOCAL");

        // Check we have LLM models
        let llms = registry.llm_models();
        assert!(llms.len() >= 30, "Expected at least 30 LLM models");

        // Check we have embedding models
        let embeddings = registry.embedding_models();
        assert!(
            embeddings.len() >= 3,
            "Expected at least 3 embedding models"
        );

        println!(
            "Loaded {} providers with {} LLM models and {} embedding models",
            registry.providers.len(),
            llms.len(),
            embeddings.len()
        );
    }

    // =========================================================================
    // TYPE-SAFE VALIDATION TESTS
    // =========================================================================

    #[test]
    fn rejects_invalid_cost_class() {
        let result = load_registry_from_str(INVALID_COST_CLASS_YAML);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("SuperLow") || err.contains("unknown variant"),
            "Expected error about invalid cost class, got: {err}"
        );
    }

    #[test]
    fn rejects_invalid_capability() {
        let result = load_registry_from_str(INVALID_CAPABILITY_YAML);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("telepathy") || err.contains("unknown variant"),
            "Expected error about invalid capability, got: {err}"
        );
    }

    #[test]
    fn rejects_invalid_quality() {
        let result = load_registry_from_str(INVALID_QUALITY_YAML);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("quality") && err.contains("1.5"),
            "Expected error about quality out of range, got: {err}"
        );
    }

    #[test]
    fn rejects_embedding_without_dimensions() {
        let result = load_registry_from_str(MISSING_DIMENSIONS_YAML);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("dimensions"),
            "Expected error about missing dimensions, got: {err}"
        );
    }

    #[test]
    fn rejects_unknown_fields() {
        let result = load_registry_from_str(UNKNOWN_FIELD_YAML);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("unknown_field") || err.contains("unknown field"),
            "Expected error about unknown field, got: {err}"
        );
    }

    #[test]
    fn rejects_invalid_region() {
        let yaml = r"
providers:
  bad:
    env_key: KEY
    key_url: https://test.com
    api_url: https://api.test.com
    country: US
    region: INVALID
    models:
      m:
        cost_class: Low
        typical_latency_ms: 100
        quality: 0.5
";
        let result = load_registry_from_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("INVALID") || err.contains("unknown variant"),
            "Expected error about invalid region, got: {err}"
        );
    }

    #[test]
    fn rejects_invalid_url() {
        let yaml = r"
providers:
  bad:
    env_key: KEY
    key_url: not-a-url
    api_url: https://api.test.com
    country: US
    region: US
    models:
      m:
        cost_class: Low
        typical_latency_ms: 100
        quality: 0.5
";
        let result = load_registry_from_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("key_url") && err.contains("URL"),
            "Expected error about invalid URL, got: {err}"
        );
    }

    #[test]
    fn rejects_zero_latency() {
        let yaml = r"
providers:
  bad:
    env_key: KEY
    key_url: https://test.com
    api_url: https://api.test.com
    country: US
    region: US
    models:
      m:
        cost_class: Low
        typical_latency_ms: 0
        quality: 0.5
";
        let result = load_registry_from_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("latency") && err.contains("0"),
            "Expected error about zero latency, got: {err}"
        );
    }

    #[test]
    fn rejects_empty_provider() {
        let yaml = r"
providers:
  empty:
    env_key: KEY
    key_url: https://test.com
    api_url: https://api.test.com
    country: US
    region: US
    models: {}
";
        let result = load_registry_from_str(yaml);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("at least one model"),
            "Expected error about empty models, got: {err}"
        );
    }
}