eulumdat 0.7.0

Eulumdat (LDT) and IES photometric file parser, writer, and validator for Rust
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
//! Conversion between ATLA and Eulumdat formats
//!
//! Provides bidirectional conversion between ATLA S001 / TM-33 / UNI 11733
//! and the traditional EULUMDAT (LDT) format.

use crate::atla::types::*;

use crate::{Eulumdat, LampSet, Symmetry as EulumdatSymmetry, TypeIndicator};

impl From<&Eulumdat> for LuminaireOpticalData {
    fn from(ldt: &Eulumdat) -> Self {
        let mut doc = LuminaireOpticalData::new();

        // Header
        doc.header = Header {
            manufacturer: if ldt.identification.is_empty() {
                None
            } else {
                Some(ldt.identification.clone())
            },
            catalog_number: if ldt.luminaire_number.is_empty() {
                None
            } else {
                Some(ldt.luminaire_number.clone())
            },
            description: if ldt.luminaire_name.is_empty() {
                None
            } else {
                Some(ldt.luminaire_name.clone())
            },
            laboratory: if ldt.identification.is_empty() {
                None
            } else {
                Some(ldt.identification.clone())
            },
            report_number: if ldt.measurement_report_number.is_empty() {
                None
            } else {
                Some(ldt.measurement_report_number.clone())
            },
            test_date: if ldt.date_user.is_empty() {
                None
            } else {
                Some(ldt.date_user.clone())
            },
            report_date: if ldt.date_user.is_empty() {
                None
            } else {
                Some(ldt.date_user.clone())
            },
            ..Default::default()
        };

        // Luminaire dimensions
        if ldt.length > 0.0 || ldt.width > 0.0 || ldt.height > 0.0 {
            let shape = if ldt.width == 0.0 {
                LuminousOpeningShape::Circular
            } else {
                LuminousOpeningShape::Rectangular
            };

            doc.luminaire = Some(Luminaire {
                dimensions: Some(Dimensions {
                    length: ldt.length,
                    width: ldt.width,
                    height: ldt.height,
                }),
                luminous_openings: vec![LuminousOpening {
                    shape,
                    dimensions: OpeningDimensions {
                        length: ldt.luminous_area_length,
                        width: if ldt.luminous_area_width > 0.0 {
                            Some(ldt.luminous_area_width)
                        } else {
                            None
                        },
                    },
                    position: None,
                }],
                mounting: None,
                num_emitters: Some(ldt.lamp_sets.iter().map(|ls| ls.num_lamps as u32).sum()),
            });
        }

        // Create emitters — one per lamp set (preserving multi-lamp-set data)
        if ldt.lamp_sets.len() <= 1 {
            // Single lamp set: create one emitter with intensity data
            let emitter = create_emitter_from_ldt(ldt);
            doc.emitters.push(emitter);
        } else {
            // Multiple lamp sets: each gets its own emitter
            // The first emitter carries the intensity distribution
            // (it represents the combined output)
            for (i, ls) in ldt.lamp_sets.iter().enumerate() {
                let emitter =
                    create_emitter_from_lamp_set(ls, if i == 0 { Some(ldt) } else { None });
                doc.emitters.push(emitter);
            }
        }

        doc
    }
}

/// Create an emitter from a single LampSet.
/// If `ldt_for_intensity` is Some, includes the intensity distribution.
fn create_emitter_from_lamp_set(ls: &LampSet, ldt_for_intensity: Option<&Eulumdat>) -> Emitter {
    let cct = parse_cct(&ls.color_appearance);
    let color_rendering = parse_cri(&ls.color_rendering_group).map(|ra| ColorRendering {
        ra: Some(ra),
        r9: None,
        rf: None,
        rg: None,
    });

    let intensity_distribution = ldt_for_intensity.and_then(|ldt| {
        if ldt.intensities.is_empty() {
            None
        } else {
            let num_intensity_rows = ldt.intensities.len();
            let horizontal_angles = if ldt.c_angles.len() == num_intensity_rows {
                ldt.c_angles.clone()
            } else {
                ldt.c_angles[..num_intensity_rows.min(ldt.c_angles.len())].to_vec()
            };
            Some(IntensityDistribution {
                photometry_type: PhotometryType::TypeC,
                metric: IntensityMetric::Luminous,
                units: IntensityUnits::CandelaPerKilolumen,
                horizontal_angles,
                vertical_angles: ldt.g_angles.clone(),
                intensities: ldt.intensities.clone(),
                ..Default::default()
            })
        }
    });

    Emitter {
        description: if ls.lamp_type.is_empty() {
            None
        } else {
            Some(ls.lamp_type.clone())
        },
        quantity: ls.num_lamps as u32,
        rated_lumens: Some(ls.total_luminous_flux),
        measured_lumens: Some(ls.total_luminous_flux),
        input_watts: Some(ls.wattage_with_ballast),
        cct,
        color_rendering,
        intensity_distribution,
        ..Default::default()
    }
}

fn create_emitter_from_ldt(ldt: &Eulumdat) -> Emitter {
    // Calculate total flux and power from lamp sets
    let total_lumens: f64 = ldt.lamp_sets.iter().map(|ls| ls.total_luminous_flux).sum();
    let total_watts: f64 = ldt.lamp_sets.iter().map(|ls| ls.wattage_with_ballast).sum();

    // Get CCT from first lamp set if available
    let cct = ldt
        .lamp_sets
        .first()
        .and_then(|ls| parse_cct(&ls.color_appearance));

    // Get CRI from first lamp set if available
    let color_rendering = ldt.lamp_sets.first().and_then(|ls| {
        parse_cri(&ls.color_rendering_group).map(|ra| ColorRendering {
            ra: Some(ra),
            r9: None,
            rf: None,
            rg: None,
        })
    });

    // Build intensity distribution
    // Note: LDT files with symmetry store all C-angles but only a subset of intensity rows
    // (e.g., PlaneC90C270 stores 27 rows for 52 angles). We must match angles to intensity rows.
    let intensity_distribution = if !ldt.intensities.is_empty() {
        let num_intensity_rows = ldt.intensities.len();
        let horizontal_angles = if ldt.c_angles.len() == num_intensity_rows {
            ldt.c_angles.clone()
        } else {
            // Only take the C-angles that correspond to stored intensity data
            ldt.c_angles[..num_intensity_rows.min(ldt.c_angles.len())].to_vec()
        };
        Some(IntensityDistribution {
            photometry_type: PhotometryType::TypeC,
            metric: IntensityMetric::Luminous,
            units: IntensityUnits::CandelaPerKilolumen,
            horizontal_angles,
            vertical_angles: ldt.g_angles.clone(),
            intensities: ldt.intensities.clone(),
            ..Default::default()
        })
    } else {
        None
    };

    // Combine lamp descriptions
    let description = if ldt.lamp_sets.is_empty() {
        None
    } else {
        let lamp_desc: Vec<String> = ldt
            .lamp_sets
            .iter()
            .filter(|ls| !ls.lamp_type.is_empty())
            .map(|ls| format!("{}x {}", ls.num_lamps, ls.lamp_type))
            .collect();
        if lamp_desc.is_empty() {
            None
        } else {
            Some(lamp_desc.join(", "))
        }
    };

    Emitter {
        id: None,
        description,
        quantity: ldt
            .lamp_sets
            .iter()
            .map(|ls| ls.num_lamps as u32)
            .sum::<u32>()
            .max(1),
        rated_lumens: if total_lumens > 0.0 {
            Some(total_lumens)
        } else {
            None
        },
        measured_lumens: None,
        input_watts: if total_watts > 0.0 {
            Some(total_watts)
        } else {
            None
        },
        power_factor: None,
        cct,
        color_rendering,
        sp_ratio: None,
        data_generation: Some(DataGeneration {
            source: DataSource::Measured,
            scaled: false,
            interpolated: false,
            software: None,
            uncertainty: None,
        }),
        intensity_distribution,
        spectral_distribution: None,
        ..Default::default()
    }
}

impl From<&LuminaireOpticalData> for Eulumdat {
    fn from(doc: &LuminaireOpticalData) -> Self {
        let mut ldt = Eulumdat::default();

        // Header -> identification fields
        if let Some(ref mfr) = doc.header.manufacturer {
            ldt.identification = mfr.clone();
        }
        if let Some(ref cat) = doc.header.catalog_number {
            ldt.luminaire_number = cat.clone();
        }
        if let Some(ref desc) = doc.header.description {
            ldt.luminaire_name = desc.clone();
        }
        if let Some(ref report) = doc.header.report_number {
            ldt.measurement_report_number = report.clone();
        }
        if let Some(ref date) = doc.header.test_date {
            ldt.date_user = date.clone();
        }

        // Luminaire dimensions
        if let Some(ref luminaire) = doc.luminaire {
            if let Some(ref dims) = luminaire.dimensions {
                ldt.length = dims.length;
                ldt.width = dims.width;
                ldt.height = dims.height;
            }

            // Luminous opening -> luminous area
            if let Some(opening) = luminaire.luminous_openings.first() {
                ldt.luminous_area_length = opening.dimensions.length;
                ldt.luminous_area_width = opening.dimensions.width.unwrap_or(0.0);
            }
        }

        // Emitters -> lamp sets and intensity data
        // Each emitter becomes one lamp set (preserving multi-lamp-set data)
        for emitter in &doc.emitters {
            let lamp_set = LampSet {
                num_lamps: emitter.quantity as i32,
                lamp_type: emitter.description.clone().unwrap_or_default(),
                total_luminous_flux: emitter
                    .measured_lumens
                    .or(emitter.rated_lumens)
                    .unwrap_or(0.0),
                color_appearance: emitter
                    .cct
                    .map(|cct| format!("{}K", cct as i32))
                    .unwrap_or_default(),
                color_rendering_group: emitter
                    .color_rendering
                    .as_ref()
                    .and_then(|cr| cr.ra)
                    .map(cri_to_group)
                    .unwrap_or_default(),
                wattage_with_ballast: emitter.input_watts.unwrap_or(0.0),
            };
            ldt.lamp_sets.push(lamp_set);
        }

        // Intensity distribution — from first emitter that has it
        if let Some(emitter) = doc
            .emitters
            .iter()
            .find(|e| e.intensity_distribution.is_some())
        {
            if let Some(ref dist) = emitter.intensity_distribution {
                ldt.c_angles = dist.horizontal_angles.clone();
                ldt.g_angles = dist.vertical_angles.clone();
                ldt.intensities = dist.intensities.clone();

                // Calculate grid parameters
                ldt.num_c_planes = if dist.horizontal_angles.len() > 1 {
                    dist.horizontal_angles.len()
                } else {
                    1
                };
                ldt.num_g_planes = dist.vertical_angles.len();

                if dist.horizontal_angles.len() > 1 {
                    ldt.c_plane_distance = dist.horizontal_angles[1] - dist.horizontal_angles[0];
                }
                if dist.vertical_angles.len() > 1 {
                    ldt.g_plane_distance = dist.vertical_angles[1] - dist.vertical_angles[0];
                }

                // Determine symmetry from data and expand c_angles to full range
                ldt.symmetry = determine_symmetry(&dist.horizontal_angles);
                let step = ldt.c_plane_distance;
                if step > 0.0 {
                    let full_count = match ldt.symmetry {
                        EulumdatSymmetry::BothPlanes => (360.0 / step) as usize,
                        EulumdatSymmetry::PlaneC0C180 | EulumdatSymmetry::PlaneC90C270 => {
                            (360.0 / step) as usize
                        }
                        _ => dist.horizontal_angles.len(),
                    };
                    if full_count > dist.horizontal_angles.len() {
                        ldt.c_angles = (0..full_count).map(|i| i as f64 * step).collect();
                        ldt.num_c_planes = full_count;
                    }
                }
            }
        }

        // Calculate light output ratio and downward flux fraction
        if !ldt.intensities.is_empty() && !ldt.g_angles.is_empty() {
            let (dff, lor) = calculate_flux_fractions(&ldt);
            ldt.downward_flux_fraction = dff;
            ldt.light_output_ratio = lor;

            // Recalculate direct ratios from intensity data (SHR 1.25 is standard)
            ldt.direct_ratios =
                crate::PhotometricCalculations::calculate_direct_ratios(&ldt, "1.25");
        }

        // Set type indicator based on dimensions
        ldt.type_indicator = if ldt.width == 0.0 {
            TypeIndicator::PointSourceSymmetric
        } else if ldt.length > ldt.width * 2.0 {
            TypeIndicator::Linear
        } else {
            TypeIndicator::PointSourceOther
        };

        ldt
    }
}

/// Parse CCT from color appearance string
///
/// Supported formats:
/// - "3000K", "4000", "3000.0" - direct numeric
/// - "tw/6500", "TW-6500" - tunable white with CCT
/// - "ww/3000", "cw/5000" - warm/cool white with CCT
/// - "CT3000", "CCT:3000" - prefixed CCT
/// - "warm white", "daylight", "neutral" - named temperatures
/// - "LED 3000K", "3000K LED" - mixed with other text
fn parse_cct(color_appearance: &str) -> Option<f64> {
    if color_appearance.is_empty() {
        return None;
    }

    let s = color_appearance.trim();

    // Skip "n/a", "none", "unknown" etc.
    let lower = s.to_lowercase();
    if lower == "n/a" || lower == "na" || lower == "none" || lower == "unknown" || lower == "-" {
        return None;
    }

    // Extract all numeric sequences from the string
    let numbers: Vec<f64> = extract_numbers(s);

    // Find a number that looks like a CCT (1000-20000)
    for &num in &numbers {
        if (1000.0..=20000.0).contains(&num) {
            return Some(num);
        }
    }

    // Common color temperature names (only if no valid number found)
    if lower.contains("warm") || lower.contains("ww") || lower.starts_with("ww") {
        return Some(2700.0);
    }
    if lower.contains("neutral") || lower.contains("nw") || lower.starts_with("nw") {
        return Some(4000.0);
    }
    if lower.contains("cool")
        || lower.contains("cold")
        || lower.contains("cw")
        || lower.starts_with("cw")
    {
        return Some(5000.0);
    }
    if lower.contains("daylight") || lower.contains("tw") || lower.starts_with("tw") {
        return Some(6500.0);
    }

    None
}

/// Extract all numeric values from a string
fn extract_numbers(s: &str) -> Vec<f64> {
    let mut numbers = Vec::new();
    let mut current = String::new();
    let mut has_dot = false;

    for c in s.chars() {
        if c.is_ascii_digit() {
            current.push(c);
        } else if c == '.' && !has_dot && !current.is_empty() {
            current.push(c);
            has_dot = true;
        } else if !current.is_empty() {
            if let Ok(num) = current.trim_end_matches('.').parse::<f64>() {
                numbers.push(num);
            }
            current.clear();
            has_dot = false;
        }
    }

    // Don't forget the last number
    if !current.is_empty() {
        if let Ok(num) = current.trim_end_matches('.').parse::<f64>() {
            numbers.push(num);
        }
    }

    numbers
}

/// Parse CRI from color rendering group string
///
/// Supported formats:
/// - "1A", "1B", "2A", "2B", "3", "4" - CIE groups
/// - "1B/86", "1A-95" - CIE group with numeric Ra value
/// - "80", "Ra>90", "CRI 85", "Ra80", "R80" - direct numeric
/// - "80%", ">80", ">=90" - with symbols
fn parse_cri(color_rendering_group: &str) -> Option<f64> {
    if color_rendering_group.is_empty() {
        return None;
    }

    let s = color_rendering_group.trim();

    // Skip "n/a", "none", "unknown" etc.
    let lower = s.to_lowercase();
    if lower == "n/a" || lower == "na" || lower == "none" || lower == "unknown" || lower == "-" {
        return None;
    }

    let upper = s.to_uppercase();

    // Extract all numbers from the string
    let numbers = extract_numbers(s);

    // Find a number that looks like a CRI (20-100)
    // Prefer larger numbers (more precise measurement over group approximation)
    let cri_candidates: Vec<f64> = numbers
        .iter()
        .copied()
        .filter(|&n| (20.0..=100.0).contains(&n))
        .collect();

    if let Some(&cri) = cri_candidates
        .iter()
        .max_by(|a, b| a.partial_cmp(b).unwrap())
    {
        return Some(cri);
    }

    // Fall back to CIE color rendering groups
    // Check each part for group codes
    for part in upper.split(&['/', '-', ' ', ':', '(', ')'][..]) {
        let part = part.trim();
        match part {
            "1A" => return Some(90.0),
            "1B" => return Some(80.0),
            "2A" | "2" => return Some(70.0),
            "2B" => return Some(60.0),
            "3" => return Some(50.0),
            "4" => return Some(40.0),
            _ => {}
        }
    }

    // Try matching group at start of string (e.g., "1Bxxx")
    if upper.starts_with("1A") {
        return Some(90.0);
    }
    if upper.starts_with("1B") {
        return Some(80.0);
    }
    if upper.starts_with("2A") {
        return Some(70.0);
    }
    if upper.starts_with("2B") {
        return Some(60.0);
    }

    None
}

/// Convert CRI value to color rendering group
fn cri_to_group(cri: f64) -> String {
    match cri as i32 {
        90..=100 => "1A".to_string(),
        80..=89 => "1B".to_string(),
        70..=79 => "2A".to_string(),
        60..=69 => "2B".to_string(),
        40..=59 => "3".to_string(),
        _ => "4".to_string(),
    }
}

/// Determine symmetry from horizontal angles
fn determine_symmetry(horizontal_angles: &[f64]) -> EulumdatSymmetry {
    if horizontal_angles.len() <= 1 {
        return EulumdatSymmetry::VerticalAxis;
    }

    let max_angle = horizontal_angles.iter().copied().fold(0.0_f64, f64::max);
    let min_angle = horizontal_angles.iter().copied().fold(360.0_f64, f64::min);

    // Check range of angles
    if (max_angle - min_angle) < 1.0 {
        EulumdatSymmetry::VerticalAxis
    } else if max_angle <= 90.5 {
        EulumdatSymmetry::BothPlanes
    } else if max_angle <= 180.5 {
        if min_angle < 0.5 {
            EulumdatSymmetry::PlaneC0C180
        } else {
            EulumdatSymmetry::PlaneC90C270
        }
    } else {
        EulumdatSymmetry::None
    }
}

/// Calculate downward flux fraction and light output ratio
fn calculate_flux_fractions(ldt: &Eulumdat) -> (f64, f64) {
    // Simple approximation based on intensity data
    // Real calculation would require proper integration

    let mut downward_flux = 0.0;
    let mut total_flux = 0.0;

    for (g_idx, &g_angle) in ldt.g_angles.iter().enumerate() {
        let sin_g = g_angle.to_radians().sin();

        for c_plane in &ldt.intensities {
            if let Some(&intensity) = c_plane.get(g_idx) {
                let flux_contribution = intensity * sin_g;
                total_flux += flux_contribution;

                // Downward: 0-90 degrees
                if g_angle <= 90.0 {
                    downward_flux += flux_contribution;
                }
            }
        }
    }

    let dff = if total_flux > 0.0 {
        (downward_flux / total_flux * 100.0).min(100.0)
    } else {
        0.0
    };

    // LOR is typically provided separately, estimate from data
    let lor = 100.0; // Default to 100% if not calculable

    (dff, lor)
}

impl LuminaireOpticalData {
    /// Convert from Eulumdat format
    pub fn from_eulumdat(ldt: &Eulumdat) -> Self {
        ldt.into()
    }

    /// Convert to Eulumdat format
    pub fn to_eulumdat(&self) -> Eulumdat {
        self.into()
    }
}

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

    #[test]
    fn test_cct_parsing() {
        // Direct numeric
        assert_eq!(parse_cct("3000K"), Some(3000.0));
        assert_eq!(parse_cct("4000"), Some(4000.0));
        assert_eq!(parse_cct("3000.0"), Some(3000.0));
        // With prefix (tw=tunable white, ww=warm white, cw=cool white)
        assert_eq!(parse_cct("tw/6500"), Some(6500.0));
        assert_eq!(parse_cct("ww/2700"), Some(2700.0));
        assert_eq!(parse_cct("cw/5000"), Some(5000.0));
        assert_eq!(parse_cct("TW-6500"), Some(6500.0));
        // Named temperatures
        assert_eq!(parse_cct("warm white"), Some(2700.0));
        assert_eq!(parse_cct("daylight"), Some(6500.0));
        assert_eq!(parse_cct("neutral"), Some(4000.0));
        // Mixed with other text
        assert_eq!(parse_cct("LED 3000K"), Some(3000.0));
        assert_eq!(parse_cct("3000K LED"), Some(3000.0));
        assert_eq!(parse_cct("CCT:4000"), Some(4000.0));
        assert_eq!(parse_cct("CT3000"), Some(3000.0));
        // Empty/invalid
        assert_eq!(parse_cct(""), None);
        assert_eq!(parse_cct("n/a"), None);
        assert_eq!(parse_cct("none"), None);
        assert_eq!(parse_cct("-"), None);
    }

    #[test]
    fn test_cri_parsing() {
        // CIE groups
        assert_eq!(parse_cri("1A"), Some(90.0));
        assert_eq!(parse_cri("1B"), Some(80.0));
        assert_eq!(parse_cri("2A"), Some(70.0));
        // With numeric value (should prefer numeric)
        assert_eq!(parse_cri("1B/86"), Some(86.0));
        assert_eq!(parse_cri("1A/95"), Some(95.0));
        assert_eq!(parse_cri("1A-95"), Some(95.0));
        // Direct numeric
        assert_eq!(parse_cri("80"), Some(80.0));
        assert_eq!(parse_cri("Ra>90"), Some(90.0));
        assert_eq!(parse_cri("CRI 85"), Some(85.0));
        assert_eq!(parse_cri("Ra80"), Some(80.0));
        assert_eq!(parse_cri("R80"), Some(80.0));
        assert_eq!(parse_cri(">80"), Some(80.0));
        assert_eq!(parse_cri(">=90"), Some(90.0));
        // Group at start
        assert_eq!(parse_cri("1Bxyz"), Some(80.0));
        // Empty/invalid
        assert_eq!(parse_cri(""), None);
        assert_eq!(parse_cri("n/a"), None);
        assert_eq!(parse_cri("none"), None);
    }

    #[test]
    fn test_cri_to_group() {
        assert_eq!(cri_to_group(95.0), "1A");
        assert_eq!(cri_to_group(85.0), "1B");
        assert_eq!(cri_to_group(75.0), "2A");
    }
}

// ============================================================================
// Schema-to-Schema Conversion (ATLA S001 <-> TM-33-23)
// ============================================================================

use crate::atla::error::{AtlaError, Result};

/// Conversion policy for S001 -> TM-33-23 migration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConversionPolicy {
    /// Strict: fail if required fields are missing
    Strict,
    /// Compatible: use defaults for missing required fields (with warnings)
    #[default]
    Compatible,
}

/// Action taken during conversion
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConversionAction {
    /// Value preserved as-is
    Preserved,
    /// Default value applied for missing required field
    DefaultApplied,
    /// Field renamed (e.g., InputWatts -> InputWattage)
    Renamed,
    /// Type converted (e.g., string GTIN -> integer)
    TypeConverted,
    /// Data dropped (not supported in target schema)
    Dropped,
    /// Warning issued but data preserved
    Warning,
}

/// Log entry for conversion actions
#[derive(Debug, Clone)]
pub struct ConversionLogEntry {
    /// Field path (e.g., "Header.Description", "Emitter\[0\].InputWatts")
    pub field: String,
    /// Action taken
    pub action: ConversionAction,
    /// Original value (if applicable)
    pub original_value: Option<String>,
    /// New value (if applicable)
    pub new_value: Option<String>,
    /// Human-readable message
    pub message: String,
}

impl ConversionLogEntry {
    fn new(field: &str, action: ConversionAction, message: &str) -> Self {
        Self {
            field: field.to_string(),
            action,
            original_value: None,
            new_value: None,
            message: message.to_string(),
        }
    }

    fn with_values(mut self, original: Option<&str>, new: Option<&str>) -> Self {
        self.original_value = original.map(|s| s.to_string());
        self.new_value = new.map(|s| s.to_string());
        self
    }
}

/// Convert ATLA S001 document to TM-33-23 format
///
/// # Arguments
/// * `doc` - Source document (S001 format)
/// * `policy` - How to handle missing required fields
///
/// # Returns
/// * Converted document and log of changes
/// * Error if strict mode and required fields are missing
///
/// # Required Fields in TM-33-23
/// - Header: Description, Laboratory, ReportNumber, ReportDate (xs:date)
/// - Emitter: Quantity, Description, InputWattage
pub fn atla_to_tm33(
    doc: &LuminaireOpticalData,
    policy: ConversionPolicy,
) -> Result<(LuminaireOpticalData, Vec<ConversionLogEntry>)> {
    let mut converted = doc.clone();
    let mut log = Vec::new();

    // Set target schema version
    converted.schema_version = SchemaVersion::Tm3323;
    converted.version = "1.1".to_string();

    // === Header Required Fields ===

    // Description (required in TM-33-23)
    if converted.header.description.is_none() {
        match policy {
            ConversionPolicy::Strict => {
                return Err(AtlaError::MissingElement(
                    "Header.Description is required in TM-33-23".to_string(),
                ));
            }
            ConversionPolicy::Compatible => {
                converted.header.description = Some("Not specified".to_string());
                log.push(
                    ConversionLogEntry::new(
                        "Header.Description",
                        ConversionAction::DefaultApplied,
                        "Applied default value for required field",
                    )
                    .with_values(None, Some("Not specified")),
                );
            }
        }
    }

    // Laboratory (required in TM-33-23)
    if converted.header.laboratory.is_none() {
        match policy {
            ConversionPolicy::Strict => {
                return Err(AtlaError::MissingElement(
                    "Header.Laboratory is required in TM-33-23".to_string(),
                ));
            }
            ConversionPolicy::Compatible => {
                converted.header.laboratory = Some("Not specified".to_string());
                log.push(
                    ConversionLogEntry::new(
                        "Header.Laboratory",
                        ConversionAction::DefaultApplied,
                        "Applied default value for required field",
                    )
                    .with_values(None, Some("Not specified")),
                );
            }
        }
    }

    // ReportNumber (required in TM-33-23)
    if converted.header.report_number.is_none() {
        match policy {
            ConversionPolicy::Strict => {
                return Err(AtlaError::MissingElement(
                    "Header.ReportNumber is required in TM-33-23".to_string(),
                ));
            }
            ConversionPolicy::Compatible => {
                converted.header.report_number = Some("UNKNOWN".to_string());
                log.push(
                    ConversionLogEntry::new(
                        "Header.ReportNumber",
                        ConversionAction::DefaultApplied,
                        "Applied default value for required field",
                    )
                    .with_values(None, Some("UNKNOWN")),
                );
            }
        }
    }

    // ReportDate (required in TM-33-23, xs:date format)
    if converted.header.report_date.is_none() {
        // Try to use test_date if available
        if let Some(ref test_date) = converted.header.test_date {
            // Try to parse and convert to xs:date format (YYYY-MM-DD)
            if let Some(date) = try_parse_date(test_date) {
                converted.header.report_date = Some(date.clone());
                log.push(
                    ConversionLogEntry::new(
                        "Header.ReportDate",
                        ConversionAction::TypeConverted,
                        "Converted from TestDate",
                    )
                    .with_values(Some(test_date), Some(&date)),
                );
            }
        }

        // If still none, apply default or error
        if converted.header.report_date.is_none() {
            match policy {
                ConversionPolicy::Strict => {
                    return Err(AtlaError::MissingElement(
                        "Header.ReportDate is required in TM-33-23".to_string(),
                    ));
                }
                ConversionPolicy::Compatible => {
                    // Use current date as default
                    let today = current_date_string();
                    converted.header.report_date = Some(today.clone());
                    log.push(
                        ConversionLogEntry::new(
                            "Header.ReportDate",
                            ConversionAction::DefaultApplied,
                            "Applied current date as default",
                        )
                        .with_values(None, Some(&today)),
                    );
                }
            }
        }
    }

    // GTIN: string to integer conversion
    if let Some(ref gtin_str) = converted.header.gtin {
        if converted.header.gtin_int.is_none() {
            if let Ok(gtin_int) = gtin_str.parse::<i64>() {
                converted.header.gtin_int = Some(gtin_int);
                log.push(
                    ConversionLogEntry::new(
                        "Header.GTIN",
                        ConversionAction::TypeConverted,
                        "Converted string GTIN to integer",
                    )
                    .with_values(Some(gtin_str), Some(&gtin_int.to_string())),
                );
            } else {
                log.push(ConversionLogEntry::new(
                    "Header.GTIN",
                    ConversionAction::Warning,
                    "GTIN could not be parsed as integer, will use string value",
                ));
            }
        }
    }

    // === Emitter Required Fields ===
    for (i, emitter) in converted.emitters.iter_mut().enumerate() {
        // Description (required in TM-33-23)
        if emitter.description.is_none() {
            match policy {
                ConversionPolicy::Strict => {
                    return Err(AtlaError::MissingElement(format!(
                        "Emitter[{}].Description is required in TM-33-23",
                        i
                    )));
                }
                ConversionPolicy::Compatible => {
                    emitter.description = Some("Emitter".to_string());
                    log.push(
                        ConversionLogEntry::new(
                            &format!("Emitter[{}].Description", i),
                            ConversionAction::DefaultApplied,
                            "Applied default value for required field",
                        )
                        .with_values(None, Some("Emitter")),
                    );
                }
            }
        }

        // InputWattage (required in TM-33-23) - NO sensible default
        if emitter.input_watts.is_none() {
            // This is always an error - no sensible default for power consumption
            return Err(AtlaError::MissingElement(format!(
                "Emitter[{}].InputWattage is required in TM-33-23 (no sensible default)",
                i
            )));
        } else {
            // Log the rename from InputWatts to InputWattage
            log.push(ConversionLogEntry::new(
                &format!("Emitter[{}].InputWatts", i),
                ConversionAction::Renamed,
                "Renamed to InputWattage for TM-33-23",
            ));
        }
    }

    // === Migrate CustomData ===
    // Convert single S001 CustomData to TM-33-23 CustomDataItem
    if let Some(ref cd) = converted.custom_data {
        if converted.custom_data_items.is_empty() {
            converted.custom_data_items.push(CustomDataItem {
                name: cd
                    .namespace
                    .clone()
                    .unwrap_or_else(|| "migrated".to_string()),
                unique_identifier: format!("migrated-{}", generate_uuid_stub()),
                raw_content: cd.data.clone(),
            });
            log.push(ConversionLogEntry::new(
                "CustomData",
                ConversionAction::TypeConverted,
                "Migrated S001 CustomData to TM-33-23 CustomDataItem",
            ));
        }
    }

    Ok((converted, log))
}

/// Convert TM-33-23 document to ATLA S001 format
///
/// Note: This conversion may be lossy as TM-33-23 has features not in S001:
/// - AngularSpectral data will be dropped
/// - AngularColor data will be dropped
/// - Multiple CustomData items will be merged/first-only
///
/// # Returns
/// * Converted document and log of changes (including warnings about data loss)
pub fn tm33_to_atla(doc: &LuminaireOpticalData) -> (LuminaireOpticalData, Vec<ConversionLogEntry>) {
    let mut converted = doc.clone();
    let mut log = Vec::new();

    // Set target schema version
    converted.schema_version = SchemaVersion::AtlaS001;
    converted.version = "1.0".to_string();

    // === GTIN: integer to string ===
    if let Some(gtin_int) = converted.header.gtin_int {
        if converted.header.gtin.is_none() {
            converted.header.gtin = Some(gtin_int.to_string());
            log.push(
                ConversionLogEntry::new(
                    "Header.GTIN",
                    ConversionAction::TypeConverted,
                    "Converted integer GTIN to string",
                )
                .with_values(Some(&gtin_int.to_string()), Some(&gtin_int.to_string())),
            );
        }
    }

    // === ReportDate to TestDate ===
    if converted.header.test_date.is_none() {
        if let Some(ref report_date) = converted.header.report_date {
            converted.header.test_date = Some(report_date.clone());
            log.push(ConversionLogEntry::new(
                "Header.ReportDate",
                ConversionAction::Renamed,
                "Copied to TestDate for S001 compatibility",
            ));
        }
    }

    // === Emitter Data Loss ===
    for (i, emitter) in converted.emitters.iter_mut().enumerate() {
        // AngularSpectral - not supported in S001
        if emitter.angular_spectral.is_some() {
            emitter.angular_spectral = None;
            log.push(ConversionLogEntry::new(
                &format!("Emitter[{}].AngularSpectral", i),
                ConversionAction::Dropped,
                "AngularSpectral data not supported in S001 - DATA LOSS",
            ));
        }

        // AngularColor - not supported in S001
        if emitter.angular_color.is_some() {
            emitter.angular_color = None;
            log.push(ConversionLogEntry::new(
                &format!("Emitter[{}].AngularColor", i),
                ConversionAction::Dropped,
                "AngularColor data not supported in S001 - DATA LOSS",
            ));
        }

        // Apply multiplier to intensity values (S001 doesn't have Multiplier)
        if let Some(ref mut dist) = emitter.intensity_distribution {
            if let Some(multiplier) = dist.multiplier {
                if (multiplier - 1.0).abs() > 0.0001 {
                    // Apply multiplier to all intensity values
                    for row in dist.intensities.iter_mut() {
                        for value in row.iter_mut() {
                            *value *= multiplier as f64;
                        }
                    }
                    log.push(
                        ConversionLogEntry::new(
                            &format!("Emitter[{}].IntensityDistribution.Multiplier", i),
                            ConversionAction::TypeConverted,
                            "Applied Multiplier to intensity values (S001 doesn't support Multiplier field)",
                        )
                        .with_values(Some(&multiplier.to_string()), None),
                    );
                }
                dist.multiplier = None;
            }

            // Remove symmetry field (S001 infers from angle data)
            if dist.symmetry.is_some() {
                log.push(ConversionLogEntry::new(
                    &format!("Emitter[{}].IntensityDistribution.Symm", i),
                    ConversionAction::Dropped,
                    "Symmetry field not used in S001 (inferred from angles)",
                ));
                dist.symmetry = None;
            }

            // Remove absolute_photometry field
            if dist.absolute_photometry.is_some() {
                dist.absolute_photometry = None;
            }
        }
    }

    // === CustomData: multiple to single ===
    if !converted.custom_data_items.is_empty() && converted.custom_data.is_none() {
        // Take only the first CustomData item
        let first = &converted.custom_data_items[0];
        converted.custom_data = Some(CustomData {
            namespace: Some(first.name.clone()),
            data: first.raw_content.clone(),
        });

        if converted.custom_data_items.len() > 1 {
            log.push(ConversionLogEntry::new(
                "CustomData",
                ConversionAction::Dropped,
                &format!(
                    "S001 only supports single CustomData - {} additional items dropped",
                    converted.custom_data_items.len() - 1
                ),
            ));
        } else {
            log.push(ConversionLogEntry::new(
                "CustomData",
                ConversionAction::TypeConverted,
                "Converted TM-33-23 CustomDataItem to S001 CustomData",
            ));
        }

        converted.custom_data_items.clear();
    }

    (converted, log)
}

/// Try to parse a date string and convert to xs:date format (YYYY-MM-DD)
fn try_parse_date(s: &str) -> Option<String> {
    let s = s.trim();

    // Already in YYYY-MM-DD format
    if s.len() == 10 && s.chars().nth(4) == Some('-') && s.chars().nth(7) == Some('-') {
        return Some(s.to_string());
    }

    // Try common formats: DD.MM.YYYY, DD/MM/YYYY, MM/DD/YYYY, YYYYMMDD
    // For simplicity, just check if it looks like a date
    let digits: String = s.chars().filter(|c| c.is_ascii_digit()).collect();
    if digits.len() == 8 {
        // Assume YYYYMMDD or similar
        let year = &digits[0..4];
        let month = &digits[4..6];
        let day = &digits[6..8];
        if year.parse::<u32>().is_ok()
            && month
                .parse::<u32>()
                .map(|m| (1..=12).contains(&m))
                .unwrap_or(false)
            && day
                .parse::<u32>()
                .map(|d| (1..=31).contains(&d))
                .unwrap_or(false)
        {
            return Some(format!("{}-{}-{}", year, month, day));
        }
    }

    None
}

/// Get current date as YYYY-MM-DD string
fn current_date_string() -> String {
    // Simple approach without external dependencies
    "2024-01-01".to_string() // Placeholder - in real code use chrono or similar
}

/// Generate a simple UUID-like stub
fn generate_uuid_stub() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    format!("{:x}", timestamp)
}