cobre-io 0.15.0

Case directory loading and validation for the Cobre power systems ecosystem
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
//! Layer 2 — Schema validation.
//!
//! Runs all individual file parsers and collects every parse/schema error across
//! the entire case directory before failing. This layer does **not** check
//! cross-references, dimensional consistency, or semantic constraints — those are
//! handled by later layers.
//!
//! The primary entry point is `validate_schema`, which returns a `ParsedData`
//! bundle holding every parsed output when all files parse cleanly, or `None`
//! when any parse or schema error is encountered.

use std::path::Path;

use cobre_core::{
    EntityId, GenericConstraint, PostStudyStages, ScalarParameter,
    entities::{Bus, EnergyContract, Hydro, Line, NonControllableSource, PumpingStation, Thermal},
    initial_conditions::InitialConditions,
    penalty::GlobalPenaltyDefaults,
    scenario::{CorrelationModel, NcsModel},
};

use crate::{
    LoadError,
    config::{Config, parse_config},
    constraints::{
        BusPenaltyOverrideRow, ContractBoundsRow, GenericConstraintBoundsRow, HydroBoundsRow,
        HydroPenaltyOverrideRow, HydroUnitGroupBoundsRow, LineBoundsRow, LinePenaltyOverrideRow,
        NcsBoundsRow, NcsPenaltyOverrideRow, PumpingBoundsRow, ThermalBoundsRow,
        build_line_bus_pair_index, load_contract_bounds, load_generic_constraint_bounds,
        load_generic_constraints, load_hydro_bounds, load_hydro_unit_group_bounds,
        load_line_bounds, load_ncs_bounds, load_penalty_overrides_bus,
        load_penalty_overrides_hydro, load_penalty_overrides_line, load_penalty_overrides_ncs,
        load_pumping_bounds, load_thermal_bounds,
    },
    extensions::{
        FphaHyperplaneRow, HydroEnergyProductivityRow, HydroGeometryRow, PlaneReductionConfig,
        ProductionModelConfig, ProductionModelFile, load_fpha_hyperplanes,
        load_hydro_energy_productivity, load_production_models, parse_hydro_geometry,
        parse_scalar_parameters_json,
    },
    initial_conditions::parse_initial_conditions,
    penalties::parse_penalties,
    post_study_stages::parse_post_study_stages,
    scenarios::{
        ExternalLoadRow, ExternalNcsRow, ExternalScenarioRow, InflowAnnualComponentRow,
        InflowArCoefficientRow, InflowHistoryRow, InflowSeasonalStatsRow, LoadFactorEntry,
        LoadSeasonalStatsRow, NcsFactorEntry, load_correlation, load_external_inflow_scenarios,
        load_external_load_scenarios, load_external_ncs_scenarios, load_inflow_annual_component,
        load_inflow_ar_coefficients, load_inflow_history, load_inflow_seasonal_stats,
        load_load_factors, load_load_seasonal_stats, load_ncs_stats, load_non_controllable_factors,
    },
    stages::{StagesData, parse_stages},
    system::{
        load_energy_contracts, load_non_controllable_sources, load_pumping_stations, parse_buses,
        parse_hydros, parse_lines, parse_thermals,
    },
    validation::{ErrorKind, ValidationContext, structural::FileManifest},
};

// ── ParsedData ────────────────────────────────────────────────────────────────

/// All parsed file outputs produced by Layer 2 schema validation.
///
/// One field per input file (the field doc names the file). Required files use
/// direct types; optional files use `Vec<T>` (Parquet rows) or `Option<T>`
/// (structured JSON) and are empty or `None` when the file is absent.
pub(crate) struct ParsedData {
    /// `config.json`.
    pub(crate) config: Config,
    /// `penalties.json`.
    // Rationale: the per-entity parsers embed these defaults for resolution, but the
    // Layer 5 penalty-ordering check needs the original global values; retaining the
    // parsed field here avoids re-reading `penalties.json` when that check runs.
    #[allow(dead_code)]
    pub(crate) penalties: GlobalPenaltyDefaults,
    /// `stages.json`.
    pub(crate) stages: StagesData,
    /// `initial_conditions.json`.
    pub(crate) initial_conditions: InitialConditions,
    /// `post_study_stages.json`. `None` when absent.
    pub(crate) post_study_stages: Option<PostStudyStages>,

    /// `system/buses.json`.
    pub(crate) buses: Vec<Bus>,
    /// `system/thermals.json`.
    pub(crate) thermals: Vec<Thermal>,
    /// `system/hydros.json`.
    pub(crate) hydros: Vec<Hydro>,
    /// `system/lines.json`.
    pub(crate) lines: Vec<Line>,

    /// `system/non_controllable_sources.json`.
    pub(crate) non_controllable_sources: Vec<NonControllableSource>,
    /// `system/pumping_stations.json`.
    pub(crate) pumping_stations: Vec<PumpingStation>,
    /// `system/energy_contracts.json`.
    pub(crate) energy_contracts: Vec<EnergyContract>,
    /// `system/hydro_geometry.parquet`.
    pub(crate) hydro_geometry: Vec<HydroGeometryRow>,
    /// `system/hydro_production_models.json`.
    pub(crate) production_models: Vec<ProductionModelConfig>,
    /// File-level FPHA plane-reduction block from
    /// `system/hydro_production_models.json`. `None` when absent or unset.
    pub(crate) plane_reduction: Option<PlaneReductionConfig>,
    /// `system/hydro_energy_productivity.parquet`.
    pub(crate) hydro_energy_productivity_rows: Vec<HydroEnergyProductivityRow>,
    /// `system/fpha_hyperplanes.parquet`.
    pub(crate) fpha_hyperplanes: Vec<FphaHyperplaneRow>,
    /// `constraints/generic_parameters.json`.
    // Rationale: the field is populated by the schema-validation layer for the expression-resolution
    // step that substitutes `@name` sigils in constraint expressions; removing it would discard
    // the parsed data and require re-parsing from disk at that step.
    #[allow(dead_code)]
    pub(crate) scalar_parameters: Vec<ScalarParameter>,

    /// `scenarios/inflow_history.parquet`.
    pub(crate) inflow_history: Vec<InflowHistoryRow>,
    /// `scenarios/inflow_seasonal_stats.parquet`.
    pub(crate) inflow_seasonal_stats: Vec<InflowSeasonalStatsRow>,
    /// `scenarios/inflow_ar_coefficients.parquet`.
    pub(crate) inflow_ar_coefficients: Vec<InflowArCoefficientRow>,
    /// `scenarios/inflow_annual_component.parquet`.
    pub(crate) inflow_annual_components: Vec<InflowAnnualComponentRow>,
    /// `scenarios/external_inflow_scenarios.parquet`.
    pub(crate) external_scenarios: Vec<ExternalScenarioRow>,
    /// `scenarios/external_load_scenarios.parquet`.
    pub(crate) external_load_scenarios: Vec<ExternalLoadRow>,
    /// `scenarios/external_ncs_scenarios.parquet`.
    pub(crate) external_ncs_scenarios: Vec<ExternalNcsRow>,
    /// `scenarios/load_seasonal_stats.parquet`.
    pub(crate) load_seasonal_stats: Vec<LoadSeasonalStatsRow>,
    /// `scenarios/load_factors.json`.
    pub(crate) load_factors: Vec<LoadFactorEntry>,
    /// `scenarios/correlation.json`. `None` when absent.
    pub(crate) correlation: Option<CorrelationModel>,
    /// `scenarios/non_controllable_factors.json`.
    pub(crate) non_controllable_factors: Vec<NcsFactorEntry>,
    /// `scenarios/non_controllable_stats.parquet`.
    pub(crate) ncs_models: Vec<NcsModel>,

    /// `constraints/thermal_bounds.parquet`.
    pub(crate) thermal_bounds: Vec<ThermalBoundsRow>,
    /// `constraints/hydro_bounds.parquet`.
    pub(crate) hydro_bounds: Vec<HydroBoundsRow>,
    /// `constraints/line_bounds.parquet`.
    pub(crate) line_bounds: Vec<LineBoundsRow>,
    /// `constraints/pumping_bounds.parquet`.
    pub(crate) pumping_bounds: Vec<PumpingBoundsRow>,
    /// `constraints/contract_bounds.parquet`.
    pub(crate) contract_bounds: Vec<ContractBoundsRow>,
    /// `constraints/generic_constraints.json`.
    pub(crate) generic_constraints: Vec<GenericConstraint>,
    /// `constraints/generic_constraint_bounds.parquet`.
    pub(crate) generic_constraint_bounds: Vec<GenericConstraintBoundsRow>,
    /// `constraints/penalty_overrides_bus.parquet`.
    pub(crate) penalty_overrides_bus: Vec<BusPenaltyOverrideRow>,
    /// `constraints/penalty_overrides_line.parquet`.
    pub(crate) penalty_overrides_line: Vec<LinePenaltyOverrideRow>,
    /// `constraints/penalty_overrides_hydro.parquet`.
    pub(crate) penalty_overrides_hydro: Vec<HydroPenaltyOverrideRow>,
    /// `constraints/penalty_overrides_ncs.parquet`.
    pub(crate) penalty_overrides_ncs: Vec<NcsPenaltyOverrideRow>,
    /// `constraints/ncs_bounds.parquet`.
    pub(crate) ncs_bounds: Vec<NcsBoundsRow>,
    /// `constraints/hydro_unit_group_bounds.parquet`.
    pub(crate) hydro_unit_group_bounds: Vec<HydroUnitGroupBoundsRow>,
}

// ── Error mapping helper ──────────────────────────────────────────────────────

/// Maps a [`LoadError`] from a parse call into a [`ValidationContext`] entry.
///
/// The entry `message` carries only the path-free detail of each variant: the
/// `ValidationEntry.file` field already holds `relative_path`, and the report
/// renderer prefixes that path. Embedding the variant's full `Display` (which
/// repeats the file path) would surface the same path twice in the combined
/// message — relative as the prefix, absolute inside the `Display`.
fn map_load_error(err: &LoadError, relative_path: &str, ctx: &mut ValidationContext) {
    match err {
        LoadError::IoError { source, .. } => {
            ctx.add_error(
                ErrorKind::FileNotFound,
                relative_path,
                None::<&str>,
                source.to_string(),
            );
        }
        LoadError::ParseError { message, .. } => {
            ctx.add_error(
                ErrorKind::ParseError,
                relative_path,
                None::<&str>,
                format!("parse error: {message}"),
            );
        }
        LoadError::SchemaError { field, message, .. } => {
            ctx.add_error(
                ErrorKind::SchemaViolation,
                relative_path,
                None::<&str>,
                format!("field {field}: {message}"),
            );
        }
        _ => {
            // Layer-2 parsers should not produce these variants; map conservatively.
            // Unlike the arms above, these do not embed `relative_path`, so their full
            // Display is safe (no path duplication).
            ctx.add_error(
                ErrorKind::SchemaViolation,
                relative_path,
                None::<&str>,
                err.to_string(),
            );
        }
    }
}

// ── validate_schema ───────────────────────────────────────────────────────────

/// Performs Layer 2 schema validation on the case directory at `case_root`.
///
/// Errors are collected across all files — the function never short-circuits on
/// the first failure. Returns `Some(ParsedData)` only when no parse/schema errors
/// were added to `ctx` during this call; optional files absent from `manifest`
/// produce empty `Vec`s or `None` fields without adding any error.
// Rationale: every manifest file is attempted in one sequential pass so that all
// errors are collected before returning; splitting into helpers would thread `ctx`
// through many boundaries or short-circuit, violating the all-errors-collected contract.
#[must_use]
#[allow(clippy::too_many_lines)]
pub(crate) fn validate_schema(
    case_root: &Path,
    manifest: &FileManifest,
    ctx: &mut ValidationContext,
) -> Option<ParsedData> {
    let error_count_before = ctx.error_count();

    let config = parse_or_error(
        parse_config(&case_root.join("config.json")),
        "config.json",
        ctx,
    );

    let penalties = parse_or_error(
        parse_penalties(&case_root.join("penalties.json")),
        "penalties.json",
        ctx,
    );

    let stages = parse_or_error(
        parse_stages(&case_root.join("stages.json")),
        "stages.json",
        ctx,
    );

    let initial_conditions = parse_or_error(
        parse_initial_conditions(&case_root.join("initial_conditions.json")),
        "initial_conditions.json",
        ctx,
    );

    // `Option` (not the aggregate): callers distinguish "no file" from an empty deck.
    let post_study_stages: Option<PostStudyStages> = optional_or_error(
        manifest.post_study_stages_json,
        || parse_post_study_stages(&case_root.join("post_study_stages.json")).map(Some),
        || None,
        "post_study_stages.json",
        ctx,
    );

    // Fall back to a sentinel when penalties failed to parse, so penalty-dependent
    // parsers still run and their own errors are collected in this pass.
    let sentinel = penalties.clone().unwrap_or_else(sentinel_penalties);

    let buses = parse_or_error(
        parse_buses(&case_root.join("system/buses.json"), &sentinel),
        "system/buses.json",
        ctx,
    );

    let lines = parse_or_error(
        parse_lines(&case_root.join("system/lines.json"), &sentinel),
        "system/lines.json",
        ctx,
    );

    let hydros = parse_or_error(
        parse_hydros(&case_root.join("system/hydros.json"), &sentinel),
        "system/hydros.json",
        ctx,
    );

    let thermals = parse_or_error(
        parse_thermals(&case_root.join("system/thermals.json")),
        "system/thermals.json",
        ctx,
    );

    let non_controllable_sources = optional_or_error(
        manifest.system_non_controllable_sources_json,
        || {
            load_non_controllable_sources(
                Some(&case_root.join("system/non_controllable_sources.json")),
                &sentinel,
            )
        },
        Vec::new,
        "system/non_controllable_sources.json",
        ctx,
    );

    let pumping_stations = optional_or_error(
        manifest.system_pumping_stations_json,
        || load_pumping_stations(Some(&case_root.join("system/pumping_stations.json"))),
        Vec::new,
        "system/pumping_stations.json",
        ctx,
    );

    let energy_contracts = optional_or_error(
        manifest.system_energy_contracts_json,
        || load_energy_contracts(Some(&case_root.join("system/energy_contracts.json"))),
        Vec::new,
        "system/energy_contracts.json",
        ctx,
    );

    let hydro_geometry = optional_or_error(
        manifest.system_hydro_geometry_parquet,
        || parse_hydro_geometry(&case_root.join("system/hydro_geometry.parquet")),
        Vec::new,
        "system/hydro_geometry.parquet",
        ctx,
    );

    let ProductionModelFile {
        configs: production_models,
        plane_reduction,
    } = optional_or_error(
        manifest.system_hydro_production_models_json,
        || load_production_models(Some(&case_root.join("system/hydro_production_models.json"))),
        ProductionModelFile::default,
        "system/hydro_production_models.json",
        ctx,
    );

    let hydro_energy_productivity_rows = optional_or_error(
        manifest.system_hydro_energy_productivity_parquet,
        || {
            load_hydro_energy_productivity(Some(
                &case_root.join("system/hydro_energy_productivity.parquet"),
            ))
        },
        Vec::new,
        "system/hydro_energy_productivity.parquet",
        ctx,
    );

    let fpha_hyperplanes = optional_or_error(
        manifest.system_fpha_hyperplanes_parquet,
        || load_fpha_hyperplanes(Some(&case_root.join("system/fpha_hyperplanes.parquet"))),
        Vec::new,
        "system/fpha_hyperplanes.parquet",
        ctx,
    );

    let inflow_history = optional_or_error(
        manifest.scenarios_inflow_history_parquet,
        || load_inflow_history(Some(&case_root.join("scenarios/inflow_history.parquet"))),
        Vec::new,
        "scenarios/inflow_history.parquet",
        ctx,
    );

    let inflow_seasonal_stats = optional_or_error(
        manifest.scenarios_inflow_seasonal_stats_parquet,
        || {
            load_inflow_seasonal_stats(Some(
                &case_root.join("scenarios/inflow_seasonal_stats.parquet"),
            ))
        },
        Vec::new,
        "scenarios/inflow_seasonal_stats.parquet",
        ctx,
    );

    let inflow_ar_coefficients = optional_or_error(
        manifest.scenarios_inflow_ar_coefficients_parquet,
        || {
            load_inflow_ar_coefficients(Some(
                &case_root.join("scenarios/inflow_ar_coefficients.parquet"),
            ))
        },
        Vec::new,
        "scenarios/inflow_ar_coefficients.parquet",
        ctx,
    );

    let inflow_annual_components = optional_or_error(
        manifest.scenarios_inflow_annual_component_parquet,
        || {
            load_inflow_annual_component(Some(
                &case_root.join("scenarios/inflow_annual_component.parquet"),
            ))
        },
        Vec::new,
        "scenarios/inflow_annual_component.parquet",
        ctx,
    );

    let external_scenarios = optional_or_error(
        manifest.scenarios_external_inflow_scenarios_parquet,
        || {
            load_external_inflow_scenarios(Some(
                &case_root.join("scenarios/external_inflow_scenarios.parquet"),
            ))
        },
        Vec::new,
        "scenarios/external_inflow_scenarios.parquet",
        ctx,
    );

    let external_load_scenarios = optional_or_error(
        manifest.scenarios_external_load_scenarios_parquet,
        || {
            load_external_load_scenarios(Some(
                &case_root.join("scenarios/external_load_scenarios.parquet"),
            ))
        },
        Vec::new,
        "scenarios/external_load_scenarios.parquet",
        ctx,
    );

    let external_ncs_scenarios = optional_or_error(
        manifest.scenarios_external_ncs_scenarios_parquet,
        || {
            load_external_ncs_scenarios(Some(
                &case_root.join("scenarios/external_ncs_scenarios.parquet"),
            ))
        },
        Vec::new,
        "scenarios/external_ncs_scenarios.parquet",
        ctx,
    );

    let load_seasonal_stats = optional_or_error(
        manifest.scenarios_load_seasonal_stats_parquet,
        || {
            load_load_seasonal_stats(Some(
                &case_root.join("scenarios/load_seasonal_stats.parquet"),
            ))
        },
        Vec::new,
        "scenarios/load_seasonal_stats.parquet",
        ctx,
    );

    let load_factors = optional_or_error(
        manifest.scenarios_load_factors_json,
        || load_load_factors(Some(&case_root.join("scenarios/load_factors.json"))),
        Vec::new,
        "scenarios/load_factors.json",
        ctx,
    );

    // `Option` (not `Vec`) so callers distinguish "no file" from "empty model".
    let correlation: Option<CorrelationModel> = optional_or_error(
        manifest.scenarios_correlation_json,
        || load_correlation(Some(&case_root.join("scenarios/correlation.json"))).map(Some),
        || None,
        "scenarios/correlation.json",
        ctx,
    );

    let non_controllable_factors = optional_or_error(
        manifest.scenarios_non_controllable_factors_json,
        || {
            load_non_controllable_factors(Some(
                &case_root.join("scenarios/non_controllable_factors.json"),
            ))
        },
        Vec::new,
        "scenarios/non_controllable_factors.json",
        ctx,
    );

    let ncs_models = optional_or_error(
        manifest.scenarios_non_controllable_stats_parquet,
        || {
            load_ncs_stats(Some(
                &case_root.join("scenarios/non_controllable_stats.parquet"),
            ))
        },
        Vec::new,
        "scenarios/non_controllable_stats.parquet",
        ctx,
    );

    let thermal_bounds = optional_or_error(
        manifest.constraints_thermal_bounds_parquet,
        || load_thermal_bounds(Some(&case_root.join("constraints/thermal_bounds.parquet"))),
        Vec::new,
        "constraints/thermal_bounds.parquet",
        ctx,
    );

    let hydro_bounds = optional_or_error(
        manifest.constraints_hydro_bounds_parquet,
        || load_hydro_bounds(Some(&case_root.join("constraints/hydro_bounds.parquet"))),
        Vec::new,
        "constraints/hydro_bounds.parquet",
        ctx,
    );

    let line_bounds = optional_or_error(
        manifest.constraints_line_bounds_parquet,
        || load_line_bounds(Some(&case_root.join("constraints/line_bounds.parquet"))),
        Vec::new,
        "constraints/line_bounds.parquet",
        ctx,
    );

    let pumping_bounds = optional_or_error(
        manifest.constraints_pumping_bounds_parquet,
        || load_pumping_bounds(Some(&case_root.join("constraints/pumping_bounds.parquet"))),
        Vec::new,
        "constraints/pumping_bounds.parquet",
        ctx,
    );

    let contract_bounds = optional_or_error(
        manifest.constraints_contract_bounds_parquet,
        || load_contract_bounds(Some(&case_root.join("constraints/contract_bounds.parquet"))),
        Vec::new,
        "constraints/contract_bounds.parquet",
        ctx,
    );

    // Must load BEFORE generic_constraints below: it resolves the `@name` sigils
    // in constraint expressions. Reordering breaks that resolution.
    let scalar_parameters: Vec<ScalarParameter> = optional_or_error(
        manifest.constraints_generic_parameters_json,
        || parse_scalar_parameters_json(&case_root.join("constraints/generic_parameters.json")),
        Vec::new,
        "constraints/generic_parameters.json",
        ctx,
    );

    // An empty map (file absent or failed to parse) is fine: any `@name` reference
    // then produces a descriptive schema error downstream.
    let scalar_name_to_id: std::collections::HashMap<String, EntityId> = scalar_parameters
        .iter()
        .map(|p| (p.name.clone(), p.id))
        .collect();

    let generic_constraints = optional_or_error(
        manifest.constraints_generic_constraints_json,
        || {
            let line_pair_index = build_line_bus_pair_index(lines.as_deref().unwrap_or(&[]))?;
            load_generic_constraints(
                Some(&case_root.join("constraints/generic_constraints.json")),
                &scalar_name_to_id,
                &line_pair_index,
            )
        },
        Vec::new,
        "constraints/generic_constraints.json",
        ctx,
    );

    let generic_constraint_bounds = optional_or_error(
        manifest.constraints_generic_constraint_bounds_parquet,
        || {
            load_generic_constraint_bounds(Some(
                &case_root.join("constraints/generic_constraint_bounds.parquet"),
            ))
        },
        Vec::new,
        "constraints/generic_constraint_bounds.parquet",
        ctx,
    );

    let penalty_overrides_bus = optional_or_error(
        manifest.constraints_penalty_overrides_bus_parquet,
        || {
            load_penalty_overrides_bus(Some(
                &case_root.join("constraints/penalty_overrides_bus.parquet"),
            ))
        },
        Vec::new,
        "constraints/penalty_overrides_bus.parquet",
        ctx,
    );

    let penalty_overrides_line = optional_or_error(
        manifest.constraints_penalty_overrides_line_parquet,
        || {
            load_penalty_overrides_line(Some(
                &case_root.join("constraints/penalty_overrides_line.parquet"),
            ))
        },
        Vec::new,
        "constraints/penalty_overrides_line.parquet",
        ctx,
    );

    let penalty_overrides_hydro = optional_or_error(
        manifest.constraints_penalty_overrides_hydro_parquet,
        || {
            load_penalty_overrides_hydro(Some(
                &case_root.join("constraints/penalty_overrides_hydro.parquet"),
            ))
        },
        Vec::new,
        "constraints/penalty_overrides_hydro.parquet",
        ctx,
    );

    let penalty_overrides_ncs = optional_or_error(
        manifest.constraints_penalty_overrides_ncs_parquet,
        || {
            load_penalty_overrides_ncs(Some(
                &case_root.join("constraints/penalty_overrides_ncs.parquet"),
            ))
        },
        Vec::new,
        "constraints/penalty_overrides_ncs.parquet",
        ctx,
    );

    let ncs_bounds = optional_or_error(
        manifest.constraints_ncs_bounds_parquet,
        || load_ncs_bounds(Some(&case_root.join("constraints/ncs_bounds.parquet"))),
        Vec::new,
        "constraints/ncs_bounds.parquet",
        ctx,
    );

    let hydro_unit_group_bounds = optional_or_error(
        manifest.constraints_hydro_unit_group_bounds_parquet,
        || {
            load_hydro_unit_group_bounds(Some(
                &case_root.join("constraints/hydro_unit_group_bounds.parquet"),
            ))
        },
        Vec::new,
        "constraints/hydro_unit_group_bounds.parquet",
        ctx,
    );

    if ctx.error_count() > error_count_before {
        return None;
    }

    // The guard above already ensures every required file parsed; these `?`s only
    // narrow the Options to satisfy the type checker.
    let config = config?;
    let penalties = penalties?;
    let stages = stages?;
    let initial_conditions = initial_conditions?;
    let buses = buses?;
    let lines = lines?;
    let hydros = hydros?;
    let thermals = thermals?;

    Some(ParsedData {
        config,
        penalties,
        stages,
        initial_conditions,
        post_study_stages,
        buses,
        thermals,
        hydros,
        lines,
        non_controllable_sources,
        pumping_stations,
        energy_contracts,
        hydro_geometry,
        production_models,
        plane_reduction,
        hydro_energy_productivity_rows,
        fpha_hyperplanes,
        scalar_parameters,
        inflow_history,
        inflow_seasonal_stats,
        inflow_ar_coefficients,
        inflow_annual_components,
        external_scenarios,
        external_load_scenarios,
        external_ncs_scenarios,
        load_seasonal_stats,
        load_factors,
        correlation,
        non_controllable_factors,
        ncs_models,
        thermal_bounds,
        hydro_bounds,
        line_bounds,
        pumping_bounds,
        contract_bounds,
        generic_constraints,
        generic_constraint_bounds,
        penalty_overrides_bus,
        penalty_overrides_line,
        penalty_overrides_hydro,
        penalty_overrides_ncs,
        ncs_bounds,
        hydro_unit_group_bounds,
    })
}

// ── Private helpers ───────────────────────────────────────────────────────────

fn parse_or_error<T>(
    result: Result<T, LoadError>,
    relative_path: &str,
    ctx: &mut ValidationContext,
) -> Option<T> {
    match result {
        Ok(value) => Some(value),
        Err(ref err) => {
            map_load_error(err, relative_path, ctx);
            None
        }
    }
}

/// Like `parse_or_error`, but skips the parser (returning `default_fn()`) when
/// `present` is `false`; also returns the default on parse failure.
fn optional_or_error<T, F, D>(
    present: bool,
    parse_fn: F,
    default_fn: D,
    relative_path: &str,
    ctx: &mut ValidationContext,
) -> T
where
    F: FnOnce() -> Result<T, LoadError>,
    D: FnOnce() -> T,
{
    if present {
        match parse_fn() {
            Ok(value) => value,
            Err(ref err) => {
                map_load_error(err, relative_path, ctx);
                default_fn()
            }
        }
    } else {
        default_fn()
    }
}

/// Build a minimal valid [`GlobalPenaltyDefaults`] sentinel used when
/// `penalties.json` failed to parse.
fn sentinel_penalties() -> GlobalPenaltyDefaults {
    use cobre_core::entities::{DeficitSegment, HydroPenalties};
    GlobalPenaltyDefaults {
        bus_deficit_segments: vec![DeficitSegment {
            depth_mw: None,
            cost_per_mwh: 1.0,
        }],
        bus_excess_cost: 1.0,
        line_exchange_cost: 1.0,
        hydro: HydroPenalties {
            spillage_cost: 1.0,
            turbined_cost: 1.0,
            diversion_cost: 1.0,
            storage_violation_below_cost: 1.0,
            filling_target_violation_cost: 1.0,
            turbined_violation_below_cost: 1.0,
            outflow_violation_below_cost: 1.0,
            outflow_violation_above_cost: 1.0,
            generation_violation_below_cost: 1.0,
            evaporation_violation_cost: 1.0,
            water_withdrawal_violation_cost: 1.0,
            water_withdrawal_violation_pos_cost: 1.0,
            water_withdrawal_violation_neg_cost: 1.0,
            evaporation_violation_pos_cost: 1.0,
            evaporation_violation_neg_cost: 1.0,
            inflow_nonnegativity_cost: 1000.0,
        },
        ncs_curtailment_cost: 1.0,
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::panic,
    clippy::too_many_lines,
    clippy::doc_markdown,
    clippy::expect_used
)]
mod tests {
    use super::*;
    use crate::validation::{ErrorKind, ValidationContext, structural::validate_structure};
    use std::fs;
    use tempfile::TempDir;

    // config.json: `training.stopping_rules[].type` = "iteration_limit",
    // field name is "limit" (not "max_iterations").
    const VALID_CONFIG_JSON: &str = r#"{
        "training": {
            "selection": {"method": "sampled", "forward_passes": 10},
            "stopping_rules": [
                { "type": "iteration_limit", "limit": 100 }
            ]
        }
    }"#;

    // penalties.json: top-level keys are "bus", "line", "hydro",
    // "non_controllable_source". Under "bus": "deficit_segments" (not
    // "segments") and "excess_cost". Under each segment: "cost" (not
    // "cost_per_mwh"). Under "line": "exchange_cost". Under "hydro": plain
    // field names without unit suffixes. Under "non_controllable_source":
    // "curtailment_cost". See src/penalties.rs for the raw serde types.
    const VALID_PENALTIES_JSON: &str = r#"{
        "bus": {
            "deficit_segments": [
                { "depth_mw": 500.0, "cost": 1000.0 },
                { "depth_mw": null,  "cost": 5000.0 }
            ],
            "excess_cost": 100.0
        },
        "line": { "exchange_cost": 2.0 },
        "hydro": {
            "spillage_cost": 0.01,
            "turbined_cost": 0.05,
            "diversion_cost": 0.1,
            "storage_violation_below_cost": 10000.0,
            "filling_target_violation_cost": 50000.0,
            "turbined_violation_below_cost": 500.0,
            "outflow_violation_below_cost": 500.0,
            "outflow_violation_above_cost": 500.0,
            "generation_violation_below_cost": 1000.0,
            "evaporation_violation_cost": 5000.0,
            "water_withdrawal_violation_cost": 1000.0
        },
        "non_controllable_source": { "curtailment_cost": 0.005 }
    }"#;

    // stages.json: `target_id` in transitions must be an integer (not null).
    // For a single-stage finite horizon we omit transitions entirely.
    // Only mandatory per-stage fields: id, start_date, end_date, blocks,
    // num_openings. season_id, block_mode, state_variables, risk_measure,
    // sampling_method all have serde defaults and are optional.
    const VALID_STAGES_JSON: &str = r#"{
        "policy_graph": {
            "type": "finite_horizon",
            "annual_discount_rate": 0.06,
            "transitions": []
        },
        "stages": [
            {
                "id": 0,
                "start_date": "2024-01-01",
                "end_date": "2024-02-01",
                "blocks": [{ "id": 0, "name": "FLAT", "hours": 744.0 }],
                "num_openings": 50
            }
        ]
    }"#;

    const VALID_INITIAL_CONDITIONS_JSON: &str = r#"{
        "storage": [],
        "filling_storage": []
    }"#;

    // buses.json: mandatory fields are "id" and "name" only.
    // "base_kv" does not exist in the actual Bus raw type.
    const VALID_BUSES_JSON: &str =
        r#"{ "buses": [{ "id": 1, "name": "BUS_1", "operational_start_date": "2024-01-01" }] }"#;

    const VALID_LINES_JSON: &str = r#"{ "lines": [] }"#;

    const VALID_HYDROS_JSON: &str = r#"{ "hydros": [] }"#;

    const VALID_THERMALS_JSON: &str = r#"{ "thermals": [] }"#;

    /// Write a string to a path relative to `root`, creating all intermediate
    /// directories.
    fn write_file(root: &Path, relative: &str, content: &str) {
        let full = root.join(relative);
        if let Some(parent) = full.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(&full, content).unwrap();
    }

    /// Populate a `TempDir` with all 8 required files using valid JSON content.
    fn make_valid_case(dir: &TempDir) {
        let root = dir.path();
        write_file(root, "config.json", VALID_CONFIG_JSON);
        write_file(root, "penalties.json", VALID_PENALTIES_JSON);
        write_file(root, "stages.json", VALID_STAGES_JSON);
        write_file(
            root,
            "initial_conditions.json",
            VALID_INITIAL_CONDITIONS_JSON,
        );
        write_file(root, "system/buses.json", VALID_BUSES_JSON);
        write_file(root, "system/lines.json", VALID_LINES_JSON);
        write_file(root, "system/hydros.json", VALID_HYDROS_JSON);
        write_file(root, "system/thermals.json", VALID_THERMALS_JSON);
    }

    /// Given a case directory with all 8 required files containing valid JSON,
    /// `validate_schema` returns `Some(ParsedData)` with all required fields
    /// populated and `ctx.has_errors()` is `false`.
    #[test]
    fn test_valid_case_returns_some_and_no_errors() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_some(),
            "validate_schema should return Some(ParsedData) for valid case"
        );
        assert!(
            !ctx.has_errors(),
            "ctx should have no errors for a valid case, got: {:?}",
            ctx.errors()
        );

        let data = data.unwrap();
        assert_eq!(
            data.buses.len(),
            1,
            "expected 1 bus parsed from valid buses.json"
        );
        assert!(
            data.non_controllable_sources.is_empty(),
            "non_controllable_sources should be empty when file absent"
        );
        assert!(
            data.correlation.is_none(),
            "correlation should be None when file absent"
        );
    }

    /// Given a case directory where `system/hydros.json` contains invalid JSON
    /// syntax, `validate_schema` returns `None` and `ctx` contains at least one
    /// `ParseError` entry with `file` containing `"system/hydros.json"`.
    #[test]
    fn test_invalid_json_returns_none_and_parse_error() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);
        write_file(dir.path(), "system/hydros.json", "{ invalid json !!!");

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_none(),
            "validate_schema should return None when a file has invalid JSON"
        );
        assert!(ctx.has_errors(), "ctx should have at least one error");

        let parse_errors: Vec<_> = ctx
            .errors()
            .into_iter()
            .filter(|e| e.kind == ErrorKind::ParseError)
            .collect();
        assert!(
            !parse_errors.is_empty(),
            "ctx should have at least one ParseError entry"
        );
        assert!(
            parse_errors
                .iter()
                .any(|e| e.file.to_string_lossy().contains("system/hydros.json")),
            "ParseError entry should reference system/hydros.json, got: {:?}",
            parse_errors
                .iter()
                .map(|e| e.file.display().to_string())
                .collect::<Vec<_>>()
        );
    }

    /// Given a case where `system/buses.json` has a schema violation (duplicate
    /// bus IDs) AND `penalties.json` has a parse error (invalid JSON),
    /// `validate_schema` returns `None` and `ctx` contains at least 2 error
    /// entries — both errors are collected, not just the first.
    #[test]
    fn test_two_invalid_files_both_errors_collected() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);

        // buses.json: duplicate bus IDs — produces a SchemaError
        write_file(
            dir.path(),
            "system/buses.json",
            r#"{ "buses": [{ "id": 1, "name": "A", "operational_start_date": "2024-01-01" }, { "id": 1, "name": "B", "operational_start_date": "2024-01-01" }] }"#,
        );

        // penalties.json: invalid JSON syntax — produces a parse error
        write_file(dir.path(), "penalties.json", "{ not valid json");

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(data.is_none(), "validate_schema should return None");
        assert!(
            ctx.errors().len() >= 2,
            "ctx should have at least 2 errors (one per invalid file), got {} errors: {:?}",
            ctx.errors().len(),
            ctx.errors()
                .iter()
                .map(|e| format!("{:?} @ {}", e.kind, e.file.display()))
                .collect::<Vec<_>>()
        );

        assert!(
            ctx.errors()
                .iter()
                .any(|e| e.file.to_string_lossy().contains("penalties.json")),
            "expected an error referencing penalties.json"
        );
        assert!(
            ctx.errors()
                .iter()
                .any(|e| e.file.to_string_lossy().contains("system/buses.json")),
            "expected an error referencing system/buses.json"
        );
    }

    /// Given a manifest where `scenarios_correlation_json` is `false`,
    /// `validate_schema` returns `Some(ParsedData)` with `correlation == None`
    /// and no error is added for the missing file.
    #[test]
    fn test_absent_optional_file_yields_none_no_error() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);
        // Do NOT write scenarios/correlation.json.

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");
        assert!(
            !manifest.scenarios_correlation_json,
            "manifest should show correlation.json absent"
        );

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_some(),
            "validate_schema should return Some(ParsedData)"
        );
        assert!(
            !ctx.has_errors(),
            "no error should be added for an absent optional file"
        );
        assert!(
            data.unwrap().correlation.is_none(),
            "correlation should be None when file absent"
        );
    }

    /// `map_load_error` maps `IoError` to `ErrorKind::FileNotFound`.
    #[test]
    fn test_map_load_error_io_error() {
        let mut ctx = ValidationContext::new();
        let err = LoadError::io(
            "system/hydros.json",
            std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
        );
        map_load_error(&err, "system/hydros.json", &mut ctx);
        assert_eq!(ctx.errors().len(), 1);
        assert_eq!(ctx.errors()[0].kind, ErrorKind::FileNotFound);
        assert!(
            ctx.errors()[0]
                .file
                .to_string_lossy()
                .contains("system/hydros.json")
        );
    }

    /// `map_load_error` maps `ParseError` to `ErrorKind::ParseError`.
    #[test]
    fn test_map_load_error_parse_error() {
        let mut ctx = ValidationContext::new();
        let err = LoadError::parse("stages.json", "unexpected token");
        map_load_error(&err, "stages.json", &mut ctx);
        assert_eq!(ctx.errors().len(), 1);
        assert_eq!(ctx.errors()[0].kind, ErrorKind::ParseError);
        assert!(
            ctx.errors()[0]
                .file
                .to_string_lossy()
                .contains("stages.json")
        );
    }

    /// `map_load_error` maps `SchemaError` to `ErrorKind::SchemaViolation`.
    #[test]
    fn test_map_load_error_schema_error() {
        let mut ctx = ValidationContext::new();
        let err = LoadError::SchemaError {
            path: std::path::PathBuf::from("system/buses.json"),
            field: "id".to_string(),
            message: "duplicate id".to_string(),
        };
        map_load_error(&err, "system/buses.json", &mut ctx);
        assert_eq!(ctx.errors().len(), 1);
        assert_eq!(ctx.errors()[0].kind, ErrorKind::SchemaViolation);
        assert!(
            ctx.errors()[0]
                .file
                .to_string_lossy()
                .contains("system/buses.json")
        );
    }

    // ── hydro_energy_productivity.parquet in Layer 2 ──────────────────────────

    /// Write a minimal valid `system/hydro_energy_productivity.parquet` with the
    /// given rows to a path inside `root`.
    fn write_hydro_energy_productivity_parquet(
        root: &Path,
        rows: &[(i32, Option<i32>, Option<f64>)],
    ) {
        use std::sync::Arc;

        use arrow::array::{Float64Array, Int32Array};
        use arrow::datatypes::{DataType, Field, Schema};
        use arrow::record_batch::RecordBatch;
        use parquet::arrow::ArrowWriter;

        let schema = Arc::new(Schema::new(vec![
            Field::new("hydro_id", DataType::Int32, false),
            Field::new("stage_id", DataType::Int32, true),
            Field::new(
                "equivalent_productivity_mw_per_m3s",
                DataType::Float64,
                true,
            ),
            Field::new("reference_volume_hm3", DataType::Float64, true),
            Field::new("reference_outflow_m3s", DataType::Float64, true),
            Field::new(
                "specific_productivity_mw_per_m3s_per_m",
                DataType::Float64,
                true,
            ),
        ]));

        let hydro_ids: Vec<i32> = rows.iter().map(|(h, _, _)| *h).collect();
        let stage_ids: Vec<Option<i32>> = rows.iter().map(|(_, s, _)| *s).collect();
        let rho_eqs: Vec<Option<f64>> = rows.iter().map(|(_, _, r)| *r).collect();
        let nulls: Vec<Option<f64>> = rows.iter().map(|_| None).collect();

        let batch = RecordBatch::try_new(
            Arc::clone(&schema),
            vec![
                Arc::new(Int32Array::from(hydro_ids)),
                Arc::new(Int32Array::from(stage_ids)),
                Arc::new(Float64Array::from(rho_eqs)),
                Arc::new(Float64Array::from(nulls.clone())),
                Arc::new(Float64Array::from(nulls.clone())),
                Arc::new(Float64Array::from(nulls)),
            ],
        )
        .expect("valid batch");

        let dest = root.join("system/hydro_energy_productivity.parquet");
        let file = fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&dest)
            .expect("create parquet file");
        let mut writer =
            ArrowWriter::try_new(file, batch.schema(), None).expect("ArrowWriter::try_new");
        writer.write(&batch).expect("write batch");
        writer.close().expect("close writer");
    }

    /// Write a `system/hydro_energy_productivity.parquet` with a NULL `hydro_id`
    /// column to trigger a `SchemaError` during parsing.
    fn write_hydro_energy_productivity_null_hydro_id(root: &Path) {
        use std::sync::Arc;

        use arrow::array::{Float64Array, Int32Array};
        use arrow::datatypes::{DataType, Field, Schema};
        use arrow::record_batch::RecordBatch;
        use parquet::arrow::ArrowWriter;

        // Use a nullable hydro_id column so we can insert a NULL value.
        let schema = Arc::new(Schema::new(vec![
            Field::new("hydro_id", DataType::Int32, true),
            Field::new("stage_id", DataType::Int32, true),
            Field::new(
                "equivalent_productivity_mw_per_m3s",
                DataType::Float64,
                true,
            ),
            Field::new("reference_volume_hm3", DataType::Float64, true),
            Field::new("reference_outflow_m3s", DataType::Float64, true),
            Field::new(
                "specific_productivity_mw_per_m3s_per_m",
                DataType::Float64,
                true,
            ),
        ]));

        let batch = RecordBatch::try_new(
            Arc::clone(&schema),
            vec![
                Arc::new(Int32Array::from(vec![None::<i32>])),
                Arc::new(Int32Array::from(vec![None::<i32>])),
                Arc::new(Float64Array::from(vec![None::<f64>])),
                Arc::new(Float64Array::from(vec![None::<f64>])),
                Arc::new(Float64Array::from(vec![None::<f64>])),
                Arc::new(Float64Array::from(vec![None::<f64>])),
            ],
        )
        .expect("valid batch");

        let dest = root.join("system/hydro_energy_productivity.parquet");
        let file = fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&dest)
            .expect("create parquet file");
        let mut writer =
            ArrowWriter::try_new(file, batch.schema(), None).expect("ArrowWriter::try_new");
        writer.write(&batch).expect("write batch");
        writer.close().expect("close writer");
    }

    /// Given a case directory with a valid 2-row `system/hydro_energy_productivity.parquet`,
    /// `validate_schema` returns `Some(ParsedData)` whose
    /// `hydro_energy_productivity_rows` has length 2 and both rows carry the
    /// parsed values.
    #[test]
    fn test_validate_schema_loads_hydro_energy_productivity_when_present() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);

        // Write a valid 2-row parquet: hydro_id=0 at stage_id=0 and stage_id=1.
        write_hydro_energy_productivity_parquet(
            dir.path(),
            &[(0, Some(0), Some(3.6)), (0, Some(1), Some(4.0))],
        );

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");
        assert!(
            manifest.system_hydro_energy_productivity_parquet,
            "manifest should detect the parquet"
        );

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_some(),
            "validate_schema should return Some(ParsedData) for a valid case"
        );
        assert!(
            !ctx.has_errors(),
            "ctx should have no errors, got: {:?}",
            ctx.errors()
        );

        let rows = &data.unwrap().hydro_energy_productivity_rows;
        assert_eq!(rows.len(), 2, "expected 2 rows parsed from the parquet");
        assert!(
            rows.iter().all(|r| r.hydro_id.0 == 0),
            "both rows should have hydro_id=0"
        );
    }

    /// Given a case directory without `system/hydro_energy_productivity.parquet`,
    /// `validate_schema` returns `Some(ParsedData)` with
    /// `hydro_energy_productivity_rows` equal to `Vec::new()` and no error is
    /// added for the absent file.
    #[test]
    fn test_validate_schema_hydro_energy_productivity_absent_is_empty() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);
        // Do NOT write system/hydro_energy_productivity.parquet.

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");
        assert!(
            !manifest.system_hydro_energy_productivity_parquet,
            "manifest should show parquet absent"
        );

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_some(),
            "validate_schema should return Some(ParsedData)"
        );
        assert!(
            !ctx.has_errors(),
            "no error should be added for an absent optional file"
        );
        assert!(
            data.unwrap().hydro_energy_productivity_rows.is_empty(),
            "hydro_energy_productivity_rows should be empty when the file is absent"
        );
    }

    /// Given a case directory whose `system/hydro_energy_productivity.parquet`
    /// contains a row with a NULL `hydro_id`, `validate_schema` returns `None`
    /// and the `ValidationContext` contains a `SchemaViolation` error whose
    /// file path references the parquet.
    #[test]
    fn test_validate_schema_malformed_hydro_energy_productivity_collects_error() {
        let dir = TempDir::new().unwrap();
        make_valid_case(&dir);
        write_hydro_energy_productivity_null_hydro_id(dir.path());

        let mut ctx = ValidationContext::new();
        let manifest = validate_structure(dir.path(), &mut ctx);
        assert!(!ctx.has_errors(), "structural validation should pass");
        assert!(
            manifest.system_hydro_energy_productivity_parquet,
            "manifest should detect the parquet"
        );

        let data = validate_schema(dir.path(), &manifest, &mut ctx);

        assert!(
            data.is_none(),
            "validate_schema should return None when the parquet has a schema violation"
        );
        assert!(ctx.has_errors(), "ctx should have at least one error");

        let schema_errors: Vec<_> = ctx
            .errors()
            .into_iter()
            .filter(|e| e.kind == ErrorKind::SchemaViolation)
            .collect();
        assert!(
            !schema_errors.is_empty(),
            "ctx should have at least one SchemaViolation entry"
        );
        assert!(
            schema_errors.iter().any(|e| e
                .file
                .to_string_lossy()
                .contains("hydro_energy_productivity")),
            "SchemaViolation should reference the parquet path, got: {:?}",
            schema_errors
                .iter()
                .map(|e| e.file.display().to_string())
                .collect::<Vec<_>>()
        );
    }
}