eulumdat 0.6.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
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
//! IES (IESNA LM-63) file format support.
//!
//! This module provides parsing and export of IES photometric files according to
//! ANSI/IES LM-63-2019 and compatible older formats (LM-63-2002, LM-63-1995, LM-63-1991).
//!
//! ## IES File Format Overview
//!
//! The IES format is the North American standard for photometric data exchange,
//! developed by the Illuminating Engineering Society (IES).
//!
//! ### File Structure
//!
//! 1. **Version header**: `IES:LM-63-2019`, `IESNA:LM-63-2002`, or `IESNA91` (older)
//! 2. **Keywords**: `[KEYWORD] value` format (TEST, MANUFAC, LUMINAIRE, etc.)
//!    - Required: `[TEST]`, `[TESTLAB]`, `[ISSUEDATE]`, `[MANUFAC]`
//!    - Optional: `[LUMCAT]`, `[LUMINAIRE]`, `[LAMP]`, `[LAMPCAT]`, `[BALLAST]`, etc.
//! 3. **TILT specification**: `TILT=NONE` or `TILT=INCLUDE`
//! 4. **Photometric data**:
//!    - Line 1: num_lamps, lumens_per_lamp, multiplier, n_vert, n_horiz, photo_type, units, width, length, height
//!    - Line 2: ballast_factor, file_generation_type, input_watts
//!    - Vertical angles (n_vert values)
//!    - Horizontal angles (n_horiz values)
//!    - Candela values (n_horiz sets of n_vert values each)
//!
//! ### Photometric Types
//!
//! - **Type A**: Automotive (horizontal angles in horizontal plane)
//! - **Type B**: Adjustable luminaires (horizontal angles in vertical plane)
//! - **Type C**: Most common - architectural (vertical angles from nadir)
//!
//! ### File Generation Types (LM-63-2019)
//!
//! - 1.00001: Undefined
//! - 1.00010: Computer simulation
//! - 1.00000: Test at unaccredited lab
//! - 1.10000: Test at accredited lab
//! - See [`FileGenerationType`] for full list
//!
//! ## Example
//!
//! ```rust,no_run
//! use eulumdat::{Eulumdat, IesParser, IesExporter};
//!
//! // Import from IES
//! let ldt = IesParser::parse_file("luminaire.ies")?;
//! println!("Luminaire: {}", ldt.luminaire_name);
//!
//! // Export to IES
//! let ies_content = IesExporter::export(&ldt);
//! std::fs::write("output.ies", ies_content)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use std::collections::HashMap;
use std::fs;
use std::path::Path;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::error::{anyhow, Result};
use crate::eulumdat::{Eulumdat, LampSet, Symmetry, TypeIndicator};
use crate::symmetry::SymmetryHandler;

/// IES file format parser.
///
/// Parses IES LM-63 format files (versions 1991, 1995, 2002, 2019).
///
/// Supports both UTF-8 and ISO-8859-1 (Latin-1) encoded files, with automatic
/// detection and conversion. This is necessary because many IES files from
/// Windows-based tools use ISO-8859-1 encoding.
pub struct IesParser;

/// Read file with encoding fallback.
///
/// Tries UTF-8 first, then falls back to ISO-8859-1 (Latin-1) which is common
/// for IES files from Windows tools.
fn read_with_encoding_fallback<P: AsRef<Path>>(path: P) -> Result<String> {
    let bytes = fs::read(path.as_ref()).map_err(|e| anyhow!("Failed to read file: {}", e))?;

    // Try UTF-8 first
    match String::from_utf8(bytes.clone()) {
        Ok(content) => Ok(content),
        Err(_) => {
            // Fall back to ISO-8859-1 (Latin-1)
            // Every byte is valid in ISO-8859-1, so this always succeeds
            Ok(bytes.iter().map(|&b| b as char).collect())
        }
    }
}

/// IES file format version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum IesVersion {
    /// LM-63-1991 (IESNA91)
    Lm63_1991,
    /// LM-63-1995 (IESNA:LM-63-1995)
    Lm63_1995,
    /// LM-63-2002 (IESNA:LM-63-2002)
    #[default]
    Lm63_2002,
    /// LM-63-2019 (IES:LM-63-2019)
    Lm63_2019,
}

impl IesVersion {
    /// Parse version from header string.
    pub fn from_header(header: &str) -> Self {
        let header_upper = header.to_uppercase();
        if header_upper.contains("LM-63-2019") || header_upper.starts_with("IES:LM-63") {
            Self::Lm63_2019
        } else if header_upper.contains("LM-63-2002") {
            Self::Lm63_2002
        } else if header_upper.contains("LM-63-1995") {
            Self::Lm63_1995
        } else {
            Self::Lm63_1991
        }
    }

    /// Get the header string for this version.
    pub fn header(&self) -> &'static str {
        match self {
            Self::Lm63_1991 => "IESNA91",
            Self::Lm63_1995 => "IESNA:LM-63-1995",
            Self::Lm63_2002 => "IESNA:LM-63-2002",
            Self::Lm63_2019 => "IES:LM-63-2019",
        }
    }
}

/// File generation type (LM-63-2019 Section 5.13, Table 2).
///
/// Describes how the IES file was generated.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum FileGenerationType {
    /// 1.00001 - Undefined or older file
    #[default]
    Undefined,
    /// 1.00010 - Computer simulation (raytracing)
    ComputerSimulation,
    /// 1.00000 - Test at unaccredited lab
    UnaccreditedLab,
    /// 1.00100 - Test at unaccredited lab, lumen scaled
    UnaccreditedLabScaled,
    /// 1.01000 - Test at unaccredited lab, interpolated angles
    UnaccreditedLabInterpolated,
    /// 1.01100 - Test at unaccredited lab, interpolated and scaled
    UnaccreditedLabInterpolatedScaled,
    /// 1.10000 - Test at accredited lab
    AccreditedLab,
    /// 1.10100 - Test at accredited lab, lumen scaled
    AccreditedLabScaled,
    /// 1.11000 - Test at accredited lab, interpolated angles
    AccreditedLabInterpolated,
    /// 1.11100 - Test at accredited lab, interpolated and scaled
    AccreditedLabInterpolatedScaled,
}

impl FileGenerationType {
    /// Parse from decimal value.
    pub fn from_value(value: f64) -> Self {
        // Round to 5 decimal places to handle floating point precision
        let rounded = (value * 100000.0).round() / 100000.0;
        match rounded {
            v if (v - 1.00001).abs() < 0.000001 => Self::Undefined,
            v if (v - 1.00010).abs() < 0.000001 => Self::ComputerSimulation,
            v if (v - 1.00000).abs() < 0.000001 => Self::UnaccreditedLab,
            v if (v - 1.00100).abs() < 0.000001 => Self::UnaccreditedLabScaled,
            v if (v - 1.01000).abs() < 0.000001 => Self::UnaccreditedLabInterpolated,
            v if (v - 1.01100).abs() < 0.000001 => Self::UnaccreditedLabInterpolatedScaled,
            v if (v - 1.10000).abs() < 0.000001 => Self::AccreditedLab,
            v if (v - 1.10100).abs() < 0.000001 => Self::AccreditedLabScaled,
            v if (v - 1.11000).abs() < 0.000001 => Self::AccreditedLabInterpolated,
            v if (v - 1.11100).abs() < 0.000001 => Self::AccreditedLabInterpolatedScaled,
            // For legacy files, treat ballast-lamp factor as undefined
            _ => Self::Undefined,
        }
    }

    /// Get decimal value for this type.
    pub fn value(&self) -> f64 {
        match self {
            Self::Undefined => 1.00001,
            Self::ComputerSimulation => 1.00010,
            Self::UnaccreditedLab => 1.00000,
            Self::UnaccreditedLabScaled => 1.00100,
            Self::UnaccreditedLabInterpolated => 1.01000,
            Self::UnaccreditedLabInterpolatedScaled => 1.01100,
            Self::AccreditedLab => 1.10000,
            Self::AccreditedLabScaled => 1.10100,
            Self::AccreditedLabInterpolated => 1.11000,
            Self::AccreditedLabInterpolatedScaled => 1.11100,
        }
    }

    /// Get title for this type (per LM-63-2019 Table 2).
    pub fn title(&self) -> &'static str {
        match self {
            Self::Undefined => "Undefined",
            Self::ComputerSimulation => "Computer Simulation",
            Self::UnaccreditedLab => "Test at an unaccredited lab",
            Self::UnaccreditedLabScaled => "Test at an unaccredited lab that has been lumen scaled",
            Self::UnaccreditedLabInterpolated => {
                "Test at an unaccredited lab with interpolated angle set"
            }
            Self::UnaccreditedLabInterpolatedScaled => {
                "Test at an unaccredited lab with interpolated angle set that has been lumen scaled"
            }
            Self::AccreditedLab => "Test at an accredited lab",
            Self::AccreditedLabScaled => "Test at an accredited lab that has been lumen scaled",
            Self::AccreditedLabInterpolated => {
                "Test at an accredited lab with interpolated angle set"
            }
            Self::AccreditedLabInterpolatedScaled => {
                "Test at an accredited lab with interpolated angle set that has been lumen scaled"
            }
        }
    }

    /// Check if this is from an accredited lab.
    pub fn is_accredited(&self) -> bool {
        matches!(
            self,
            Self::AccreditedLab
                | Self::AccreditedLabScaled
                | Self::AccreditedLabInterpolated
                | Self::AccreditedLabInterpolatedScaled
        )
    }

    /// Check if lumen values were scaled.
    pub fn is_scaled(&self) -> bool {
        matches!(
            self,
            Self::UnaccreditedLabScaled
                | Self::UnaccreditedLabInterpolatedScaled
                | Self::AccreditedLabScaled
                | Self::AccreditedLabInterpolatedScaled
        )
    }

    /// Check if angles were interpolated.
    pub fn is_interpolated(&self) -> bool {
        matches!(
            self,
            Self::UnaccreditedLabInterpolated
                | Self::UnaccreditedLabInterpolatedScaled
                | Self::AccreditedLabInterpolated
                | Self::AccreditedLabInterpolatedScaled
        )
    }
}

/// Luminous opening shape (LM-63-2019 Section 5.11, Table 1).
///
/// Determined by the signs of width, length, and height dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LuminousShape {
    /// Point source (all dimensions = 0)
    #[default]
    Point,
    /// Rectangular opening (width > 0, length > 0, height = 0)
    Rectangular,
    /// Rectangular with luminous sides (all > 0)
    RectangularWithSides,
    /// Circular (width = length < 0, height = 0)
    Circular,
    /// Ellipse (width < 0, length < 0, height = 0)
    Ellipse,
    /// Vertical cylinder (width = length < 0, height > 0)
    VerticalCylinder,
    /// Vertical ellipsoidal cylinder
    VerticalEllipsoidalCylinder,
    /// Sphere (all negative, equal)
    Sphere,
    /// Ellipsoidal spheroid (all negative, not equal)
    EllipsoidalSpheroid,
    /// Horizontal cylinder along photometric horizontal
    HorizontalCylinderAlong,
    /// Horizontal ellipsoidal cylinder along photometric horizontal
    HorizontalEllipsoidalCylinderAlong,
    /// Horizontal cylinder perpendicular to photometric horizontal
    HorizontalCylinderPerpendicular,
    /// Horizontal ellipsoidal cylinder perpendicular to photometric horizontal
    HorizontalEllipsoidalCylinderPerpendicular,
    /// Vertical circle facing photometric horizontal
    VerticalCircle,
    /// Vertical ellipse facing photometric horizontal
    VerticalEllipse,
}

impl LuminousShape {
    /// Determine shape from width, length, height values.
    pub fn from_dimensions(width: f64, length: f64, height: f64) -> Self {
        let w_zero = width.abs() < 0.0001;
        let l_zero = length.abs() < 0.0001;
        let h_zero = height.abs() < 0.0001;
        let w_neg = width < 0.0;
        let l_neg = length < 0.0;
        let h_neg = height < 0.0;
        let w_pos = width > 0.0;
        let l_pos = length > 0.0;
        let h_pos = height > 0.0;

        // Check for equal negative values (circular shapes)
        let wl_equal = (width - length).abs() < 0.0001;
        let all_equal = wl_equal && (width - height).abs() < 0.0001;

        match (
            w_zero, l_zero, h_zero, w_neg, l_neg, h_neg, w_pos, l_pos, h_pos,
        ) {
            // Point: all zero
            (true, true, true, _, _, _, _, _, _) => Self::Point,
            // Rectangular: width > 0, length > 0, height = 0
            (_, _, true, _, _, _, true, true, _) => Self::Rectangular,
            // Rectangular with sides: all positive
            (_, _, _, _, _, _, true, true, true) => Self::RectangularWithSides,
            // Circular: width = length < 0, height = 0
            (_, _, true, true, true, _, _, _, _) if wl_equal => Self::Circular,
            // Ellipse: width < 0, length < 0, height = 0
            (_, _, true, true, true, _, _, _, _) => Self::Ellipse,
            // Sphere: all negative and equal
            (_, _, _, true, true, true, _, _, _) if all_equal => Self::Sphere,
            // Ellipsoidal spheroid: all negative
            (_, _, _, true, true, true, _, _, _) => Self::EllipsoidalSpheroid,
            // Vertical cylinder: width = length < 0, height > 0
            (_, _, _, true, true, _, _, _, true) if wl_equal => Self::VerticalCylinder,
            // Vertical ellipsoidal cylinder: width < 0, length < 0, height > 0
            (_, _, _, true, true, _, _, _, true) => Self::VerticalEllipsoidalCylinder,
            // Horizontal cylinder along: width < 0, length > 0, height < 0
            (_, _, _, true, _, true, _, true, _) if (width - height).abs() < 0.0001 => {
                Self::HorizontalCylinderAlong
            }
            // Horizontal ellipsoidal cylinder along
            (_, _, _, true, _, true, _, true, _) => Self::HorizontalEllipsoidalCylinderAlong,
            // Horizontal cylinder perpendicular: width > 0, length < 0, height < 0
            (_, _, _, _, true, true, true, _, _) if (length - height).abs() < 0.0001 => {
                Self::HorizontalCylinderPerpendicular
            }
            // Horizontal ellipsoidal cylinder perpendicular
            (_, _, _, _, true, true, true, _, _) => {
                Self::HorizontalEllipsoidalCylinderPerpendicular
            }
            // Vertical circle: width < 0, length = 0, height < 0
            (_, true, _, true, _, true, _, _, _) if (width - height).abs() < 0.0001 => {
                Self::VerticalCircle
            }
            // Vertical ellipse: width < 0, length = 0, height < 0
            (_, true, _, true, _, true, _, _, _) => Self::VerticalEllipse,
            // Default to point for any unrecognized combination
            _ => Self::Point,
        }
    }

    /// Get description of the shape.
    pub fn description(&self) -> &'static str {
        match self {
            Self::Point => "Point source",
            Self::Rectangular => "Rectangular luminous opening",
            Self::RectangularWithSides => "Rectangular with luminous sides",
            Self::Circular => "Circular luminous opening",
            Self::Ellipse => "Elliptical luminous opening",
            Self::VerticalCylinder => "Vertical cylinder",
            Self::VerticalEllipsoidalCylinder => "Vertical ellipsoidal cylinder",
            Self::Sphere => "Spherical luminous opening",
            Self::EllipsoidalSpheroid => "Ellipsoidal spheroid",
            Self::HorizontalCylinderAlong => "Horizontal cylinder along photometric horizontal",
            Self::HorizontalEllipsoidalCylinderAlong => {
                "Horizontal ellipsoidal cylinder along photometric horizontal"
            }
            Self::HorizontalCylinderPerpendicular => {
                "Horizontal cylinder perpendicular to photometric horizontal"
            }
            Self::HorizontalEllipsoidalCylinderPerpendicular => {
                "Horizontal ellipsoidal cylinder perpendicular to photometric horizontal"
            }
            Self::VerticalCircle => "Vertical circle facing photometric horizontal",
            Self::VerticalEllipse => "Vertical ellipse facing photometric horizontal",
        }
    }
}

/// TILT data for luminaires with position-dependent output.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TiltData {
    /// Lamp to luminaire geometry (1-3)
    /// 1 = vertical base-up or base-down
    /// 2 = horizontal, stays horizontal when tilted
    /// 3 = horizontal, tilts with luminaire
    pub lamp_geometry: i32,
    /// Tilt angles in degrees
    pub angles: Vec<f64>,
    /// Multiplying factors corresponding to angles
    pub factors: Vec<f64>,
}

/// Lamp position within luminaire (LM-63-2019 Annex E).
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LampPosition {
    /// Horizontal position angle (0-360°)
    pub horizontal: f64,
    /// Vertical position angle (0-180°)
    pub vertical: f64,
}

/// Photometric measurement type.
///
/// ## Coordinate System Differences
///
/// - **Type C**: Vertical polar axis (0° = nadir, 180° = zenith). Standard for downlights, streetlights.
/// - **Type B**: Horizontal polar axis (0H 0V = beam center). Used for floodlights, sports lighting.
///   - ⚠️ **TODO**: Implement 90° coordinate rotation for Type B → Type C conversion
///   - Required transformation matrix: R_x(90°) to align horizontal axis to vertical
/// - **Type A**: Automotive coordinates. Rare in architectural lighting.
///   - Currently parsed but may render incorrectly without coordinate mapping
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum PhotometricType {
    /// Type A - Automotive photometry (rare)
    TypeA = 3,
    /// Type B - Adjustable luminaires (floodlights, theatrical)
    TypeB = 2,
    /// Type C - Architectural (most common)
    #[default]
    TypeC = 1,
}

impl PhotometricType {
    /// Create from integer value.
    pub fn from_int(value: i32) -> Result<Self> {
        match value {
            1 => Ok(Self::TypeC),
            2 => Ok(Self::TypeB),
            3 => Ok(Self::TypeA),
            _ => Err(anyhow!("Invalid photometric type: {}", value)),
        }
    }
}

/// Unit type for dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnitType {
    /// Dimensions in feet
    Feet = 1,
    /// Dimensions in meters
    #[default]
    Meters = 2,
}

impl UnitType {
    /// Create from integer value.
    pub fn from_int(value: i32) -> Result<Self> {
        match value {
            1 => Ok(Self::Feet),
            2 => Ok(Self::Meters),
            _ => Err(anyhow!("Invalid unit type: {}", value)),
        }
    }

    /// Conversion factor to millimeters.
    pub fn to_mm_factor(&self) -> f64 {
        match self {
            UnitType::Feet => 304.8,    // 1 foot = 304.8 mm
            UnitType::Meters => 1000.0, // 1 meter = 1000 mm
        }
    }
}

/// Parsed IES data before conversion to Eulumdat.
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IesData {
    /// Version (parsed from header)
    pub version: IesVersion,
    /// Version string as found in file
    pub version_string: String,
    /// All keyword metadata
    pub keywords: HashMap<String, String>,

    // === Required Keywords (LM-63-2019) ===
    /// `[TEST]` Test report number
    pub test: String,
    /// `[TESTLAB]` Photometric testing laboratory
    pub test_lab: String,
    /// `[ISSUEDATE]` Date manufacturer issued the file
    pub issue_date: String,
    /// `[MANUFAC]` Manufacturer of luminaire
    pub manufacturer: String,

    // === Common Optional Keywords ===
    /// `[LUMCAT]` Luminaire catalog number
    pub luminaire_catalog: String,
    /// `[LUMINAIRE]` Luminaire description
    pub luminaire: String,
    /// `[LAMPCAT]` Lamp catalog number
    pub lamp_catalog: String,
    /// `[LAMP]` Lamp description
    pub lamp: String,
    /// `[BALLAST]` Ballast description
    pub ballast: String,
    /// `[BALLASTCAT]` Ballast catalog number
    pub ballast_catalog: String,
    /// `[TESTDATE]` Date of photometric test
    pub test_date: String,
    /// `[MAINTCAT]` IES maintenance category (1-6)
    pub maintenance_category: Option<i32>,
    /// `[DISTRIBUTION]` Distribution description
    pub distribution: String,
    /// `[FLASHAREA]` Flash area in m²
    pub flash_area: Option<f64>,
    /// `[COLORCONSTANT]` Color constant for glare
    pub color_constant: Option<f64>,
    /// `[LAMPPOSITION]` Lamp position angles
    pub lamp_position: Option<LampPosition>,
    /// `[NEARFIELD]` Near field distances (D1, D2, D3)
    pub near_field: Option<(f64, f64, f64)>,
    /// `[FILEGENINFO]` Additional file generation info
    pub file_gen_info: String,
    /// `[SEARCH]` User search string
    pub search: String,
    /// `[OTHER]` lines (can appear multiple times)
    pub other: Vec<String>,

    // === Photometric Parameters ===
    /// Number of lamps
    pub num_lamps: i32,
    /// Lumens per lamp (-1 = absolute photometry)
    pub lumens_per_lamp: f64,
    /// Candela multiplier
    pub multiplier: f64,
    /// Number of vertical angles
    pub n_vertical: usize,
    /// Number of horizontal angles
    pub n_horizontal: usize,
    /// Photometric type (1=C, 2=B, 3=A)
    pub photometric_type: PhotometricType,
    /// Unit type (1=feet, 2=meters)
    pub unit_type: UnitType,
    /// Luminous opening width (negative = rounded shape)
    pub width: f64,
    /// Luminous opening length (negative = rounded shape)
    pub length: f64,
    /// Luminous opening height (negative = rounded shape)
    pub height: f64,
    /// Derived luminous shape
    pub luminous_shape: LuminousShape,
    /// Ballast factor
    pub ballast_factor: f64,
    /// File generation type (LM-63-2019) or ballast-lamp factor (older)
    pub file_generation_type: FileGenerationType,
    /// Raw file generation type value (for preservation)
    pub file_generation_value: f64,
    /// Input watts
    pub input_watts: f64,

    // === TILT Data ===
    /// TILT mode (NONE or INCLUDE)
    pub tilt_mode: String,
    /// TILT data if INCLUDE
    pub tilt_data: Option<TiltData>,

    // === Angle Data ===
    /// Vertical angles (gamma)
    pub vertical_angles: Vec<f64>,
    /// Horizontal angles (C-planes)
    pub horizontal_angles: Vec<f64>,
    /// Candela values `[horizontal_index][vertical_index]`
    pub candela_values: Vec<Vec<f64>>,
}

impl IesParser {
    /// Parse an IES file from a file path.
    ///
    /// Automatically handles both UTF-8 and ISO-8859-1 (Latin-1) encoded files.
    pub fn parse_file<P: AsRef<Path>>(path: P) -> Result<Eulumdat> {
        let content = read_with_encoding_fallback(path)?;
        Self::parse(&content)
    }

    /// Parse an IES file with import options (e.g. C-plane rotation).
    pub fn parse_file_with_options<P: AsRef<Path>>(
        path: P,
        options: &IesImportOptions,
    ) -> Result<Eulumdat> {
        let content = read_with_encoding_fallback(path)?;
        Self::parse_with_options(&content, options)
    }

    /// Parse IES content from a string.
    pub fn parse(content: &str) -> Result<Eulumdat> {
        let ies_data = Self::parse_ies_data(content)?;
        Self::convert_to_eulumdat(ies_data)
    }

    /// Parse IES content with import options (e.g. C-plane rotation).
    pub fn parse_with_options(content: &str, options: &IesImportOptions) -> Result<Eulumdat> {
        let ies_data = Self::parse_ies_data(content)?;
        let mut ldt = Self::convert_to_eulumdat(ies_data)?;
        if options.rotate_c_planes.abs() > 0.001 {
            ldt.rotate_c_planes(options.rotate_c_planes);
        }
        Ok(ldt)
    }

    /// Parse IES content and return raw IES data structure.
    ///
    /// This is useful for accessing IES-specific fields that don't map to Eulumdat.
    pub fn parse_to_ies_data(content: &str) -> Result<IesData> {
        Self::parse_ies_data(content)
    }

    /// Parse IES format into intermediate structure.
    fn parse_ies_data(content: &str) -> Result<IesData> {
        let mut data = IesData::default();
        let lines: Vec<&str> = content.lines().collect();

        if lines.is_empty() {
            return Err(anyhow!("Empty IES file"));
        }

        let mut line_idx = 0;

        // Parse version header
        let first_line = lines[line_idx].trim();
        // LM-63-2019: IES:LM-63-2019
        // LM-63-2002: IESNA:LM-63-2002
        // LM-63-1991: IESNA91 or IESNA:LM-63-1991
        if first_line.to_uppercase().starts_with("IES")
            || first_line.to_uppercase().starts_with("IESNA")
        {
            data.version_string = first_line.to_string();
            data.version = IesVersion::from_header(first_line);
            line_idx += 1;
        } else {
            // Older format without explicit version
            data.version_string = "IESNA91".to_string();
            data.version = IesVersion::Lm63_1991;
        }

        // Parse keywords until TILT, handling [MORE] continuation
        let mut current_keyword = String::new();
        let mut current_value = String::new();
        let mut last_stored_keyword = String::new(); // Track last keyword for [MORE]

        while line_idx < lines.len() {
            let line = lines[line_idx].trim();

            if line.to_uppercase().starts_with("TILT=") || line.to_uppercase().starts_with("TILT ")
            {
                // Save last keyword if any
                if !current_keyword.is_empty() {
                    Self::store_keyword(&mut data, &current_keyword, &current_value);
                }
                break;
            }

            // Parse [KEYWORD] value format
            if line.starts_with('[') {
                // Save previous keyword
                if !current_keyword.is_empty() {
                    Self::store_keyword(&mut data, &current_keyword, &current_value);
                    last_stored_keyword = current_keyword.clone();
                }

                if let Some(end_bracket) = line.find(']') {
                    current_keyword = line[1..end_bracket].to_uppercase();
                    current_value = line[end_bracket + 1..].trim().to_string();

                    // Handle [MORE] continuation (Annex A)
                    if current_keyword == "MORE" && !last_stored_keyword.is_empty() {
                        // Append to previous keyword's value
                        if let Some(existing) = data.keywords.get_mut(&last_stored_keyword) {
                            existing.push('\n');
                            existing.push_str(&current_value);
                        }
                        current_keyword.clear();
                        current_value.clear();
                    }
                }
            }

            line_idx += 1;
        }

        // Handle TILT
        if line_idx < lines.len() {
            let tilt_line = lines[line_idx].trim().to_uppercase();
            data.tilt_mode = if tilt_line.contains("INCLUDE") {
                "INCLUDE".to_string()
            } else {
                "NONE".to_string()
            };

            line_idx += 1;

            if tilt_line.contains("INCLUDE") {
                // Parse TILT data (Annex F)
                let mut tilt = TiltData::default();

                // Lamp to luminaire geometry
                if line_idx < lines.len() {
                    if let Ok(geom) = lines[line_idx].trim().parse::<i32>() {
                        tilt.lamp_geometry = geom;
                    }
                    line_idx += 1;
                }

                // Number of angle-factor pairs
                if line_idx < lines.len() {
                    if let Ok(n_pairs) = lines[line_idx].trim().parse::<usize>() {
                        line_idx += 1;

                        // Collect angle values
                        let mut angle_values: Vec<f64> = Vec::new();
                        while angle_values.len() < n_pairs && line_idx < lines.len() {
                            for token in lines[line_idx].split_whitespace() {
                                if let Ok(val) = token.replace(',', ".").parse::<f64>() {
                                    angle_values.push(val);
                                }
                            }
                            line_idx += 1;
                        }
                        tilt.angles = angle_values;

                        // Collect factor values
                        let mut factor_values: Vec<f64> = Vec::new();
                        while factor_values.len() < n_pairs && line_idx < lines.len() {
                            for token in lines[line_idx].split_whitespace() {
                                if let Ok(val) = token.replace(',', ".").parse::<f64>() {
                                    factor_values.push(val);
                                }
                            }
                            line_idx += 1;
                        }
                        tilt.factors = factor_values;
                    }
                }

                data.tilt_data = Some(tilt);
            }
        }

        // Collect remaining numeric data
        let mut numeric_values: Vec<f64> = Vec::new();
        while line_idx < lines.len() {
            let line = lines[line_idx].trim();
            for token in line.split_whitespace() {
                if let Ok(val) = token.replace(',', ".").parse::<f64>() {
                    numeric_values.push(val);
                }
            }
            line_idx += 1;
        }

        // Parse photometric data
        if numeric_values.len() < 13 {
            return Err(anyhow!(
                "Insufficient photometric data: expected at least 13 values, found {}",
                numeric_values.len()
            ));
        }

        let mut idx = 0;

        // Line 1: num_lamps, lumens_per_lamp, multiplier, n_vert, n_horiz, photo_type, units, width, length, height
        data.num_lamps = numeric_values[idx] as i32;
        idx += 1;
        data.lumens_per_lamp = numeric_values[idx];
        idx += 1;
        data.multiplier = numeric_values[idx];
        idx += 1;
        data.n_vertical = numeric_values[idx] as usize;
        idx += 1;
        data.n_horizontal = numeric_values[idx] as usize;
        idx += 1;
        data.photometric_type = PhotometricType::from_int(numeric_values[idx] as i32)?;
        idx += 1;
        data.unit_type = UnitType::from_int(numeric_values[idx] as i32)?;
        idx += 1;
        data.width = numeric_values[idx];
        idx += 1;
        data.length = numeric_values[idx];
        idx += 1;
        data.height = numeric_values[idx];
        idx += 1;

        // Determine luminous shape from dimensions
        data.luminous_shape = LuminousShape::from_dimensions(data.width, data.length, data.height);

        // Line 2: ballast_factor, file_generation_type, input_watts
        data.ballast_factor = numeric_values[idx];
        idx += 1;
        data.file_generation_value = numeric_values[idx];
        data.file_generation_type = FileGenerationType::from_value(data.file_generation_value);
        idx += 1;
        data.input_watts = numeric_values[idx];
        idx += 1;

        // Vertical angles
        if idx + data.n_vertical > numeric_values.len() {
            return Err(anyhow!("Insufficient vertical angle data"));
        }
        data.vertical_angles = numeric_values[idx..idx + data.n_vertical].to_vec();
        idx += data.n_vertical;

        // Horizontal angles
        if idx + data.n_horizontal > numeric_values.len() {
            return Err(anyhow!("Insufficient horizontal angle data"));
        }
        data.horizontal_angles = numeric_values[idx..idx + data.n_horizontal].to_vec();
        idx += data.n_horizontal;

        // Candela values: n_horizontal sets of n_vertical values
        let expected_candela = data.n_horizontal * data.n_vertical;
        if idx + expected_candela > numeric_values.len() {
            return Err(anyhow!(
                "Insufficient candela data: expected {}, remaining {}",
                expected_candela,
                numeric_values.len() - idx
            ));
        }

        for _ in 0..data.n_horizontal {
            let row: Vec<f64> = numeric_values[idx..idx + data.n_vertical].to_vec();
            data.candela_values.push(row);
            idx += data.n_vertical;
        }

        Ok(data)
    }

    /// Store a keyword value in the IesData structure.
    fn store_keyword(data: &mut IesData, keyword: &str, value: &str) {
        // Store in generic keywords map
        data.keywords.insert(keyword.to_string(), value.to_string());

        // Also store in specific fields for easy access
        match keyword {
            "TEST" => data.test = value.to_string(),
            "TESTLAB" => data.test_lab = value.to_string(),
            "ISSUEDATE" => data.issue_date = value.to_string(),
            "MANUFAC" => data.manufacturer = value.to_string(),
            "LUMCAT" => data.luminaire_catalog = value.to_string(),
            "LUMINAIRE" => data.luminaire = value.to_string(),
            "LAMPCAT" => data.lamp_catalog = value.to_string(),
            "LAMP" => data.lamp = value.to_string(),
            "BALLAST" => data.ballast = value.to_string(),
            "BALLASTCAT" => data.ballast_catalog = value.to_string(),
            "TESTDATE" => data.test_date = value.to_string(),
            "MAINTCAT" => data.maintenance_category = value.trim().parse().ok(),
            "DISTRIBUTION" => data.distribution = value.to_string(),
            "FLASHAREA" => data.flash_area = value.trim().parse().ok(),
            "COLORCONSTANT" => data.color_constant = value.trim().parse().ok(),
            "LAMPPOSITION" => {
                let parts: Vec<f64> = value
                    .split([' ', ','])
                    .filter_map(|s| s.trim().parse().ok())
                    .collect();
                if parts.len() >= 2 {
                    data.lamp_position = Some(LampPosition {
                        horizontal: parts[0],
                        vertical: parts[1],
                    });
                }
            }
            "NEARFIELD" => {
                let parts: Vec<f64> = value
                    .split([' ', ','])
                    .filter_map(|s| s.trim().parse().ok())
                    .collect();
                if parts.len() >= 3 {
                    data.near_field = Some((parts[0], parts[1], parts[2]));
                }
            }
            "FILEGENINFO" => {
                if data.file_gen_info.is_empty() {
                    data.file_gen_info = value.to_string();
                } else {
                    data.file_gen_info.push('\n');
                    data.file_gen_info.push_str(value);
                }
            }
            "SEARCH" => data.search = value.to_string(),
            "OTHER" => data.other.push(value.to_string()),
            _ => {
                // User-defined keywords (starting with _) are stored in generic map only
            }
        }
    }

    /// Convert parsed IES data to Eulumdat structure.
    fn convert_to_eulumdat(ies: IesData) -> Result<Eulumdat> {
        let mut ldt = Eulumdat::new();

        // Convert keywords to Eulumdat fields (use parsed fields, not raw keywords)
        ldt.identification = ies.manufacturer.clone();
        ldt.luminaire_name = ies.luminaire.clone();
        ldt.luminaire_number = ies.luminaire_catalog.clone();
        ldt.measurement_report_number = ies.test.clone();
        ldt.file_name = ies.test_lab.clone();
        // Store issue date in date_user field
        ldt.date_user = ies.issue_date.clone();

        // Determine symmetry from horizontal angles
        ldt.symmetry = Self::detect_symmetry(&ies.horizontal_angles);

        // Type indicator based on dimensions and symmetry
        ldt.type_indicator = if ies.length > ies.width * 2.0 {
            TypeIndicator::Linear
        } else if ldt.symmetry == Symmetry::VerticalAxis {
            TypeIndicator::PointSourceSymmetric
        } else {
            TypeIndicator::PointSourceOther
        };

        // Store angles
        ldt.c_angles = ies.horizontal_angles.clone();
        ldt.g_angles = ies.vertical_angles.clone();
        ldt.num_c_planes = ies.n_horizontal;
        ldt.num_g_planes = ies.n_vertical;

        // Calculate angle spacing
        if ldt.c_angles.len() >= 2 {
            ldt.c_plane_distance = ldt.c_angles[1] - ldt.c_angles[0];
        }
        if ldt.g_angles.len() >= 2 {
            ldt.g_plane_distance = ldt.g_angles[1] - ldt.g_angles[0];
        }

        // Convert dimensions to mm
        let mm_factor = ies.unit_type.to_mm_factor();
        ldt.length = ies.length * mm_factor;
        ldt.width = ies.width * mm_factor;
        ldt.height = ies.height * mm_factor;

        // Luminous area (assume same as luminaire for now)
        ldt.luminous_area_length = ldt.length;
        ldt.luminous_area_width = ldt.width;

        // Lamp set
        // CRITICAL: Handle absolute photometry (LED fixtures)
        // IES Standard: lumens_per_lamp = -1 signals absolute photometry
        // Eulumdat Convention: num_lamps must be NEGATIVE to signal absolute photometry
        // This is the "single most important fix for LED compatibility"
        let (num_lamps, total_flux) = if ies.lumens_per_lamp < 0.0 {
            // Absolute photometry: calculate flux from intensity distribution
            // The candela values * multiplier give absolute candelas
            // We integrate these to get total lumens
            let calculated_flux =
                Self::calculate_flux_from_intensities(&ies.candela_values, &ies.vertical_angles)
                    * ies.multiplier;
            (-1, calculated_flux)
        } else {
            // Relative photometry: positive lamp count
            (ies.num_lamps, ies.lumens_per_lamp * ies.num_lamps as f64)
        };

        ldt.lamp_sets.push(LampSet {
            num_lamps,
            lamp_type: if ies.lamp.is_empty() {
                "Unknown".to_string()
            } else {
                ies.lamp.clone()
            },
            total_luminous_flux: total_flux,
            color_appearance: ies.keywords.get("COLORTEMP").cloned().unwrap_or_default(),
            color_rendering_group: ies.keywords.get("CRI").cloned().unwrap_or_default(),
            wattage_with_ballast: ies.input_watts,
        });

        // Store intensities (IES candela values are absolute, convert to cd/klm)
        // Eulumdat uses cd/1000lm, IES uses absolute candela
        let cd_to_cdklm = if total_flux > 0.0 {
            1000.0 / total_flux
        } else {
            1.0
        };

        ldt.intensities = ies
            .candela_values
            .iter()
            .map(|row| {
                row.iter()
                    .map(|&v| v * cd_to_cdklm * ies.multiplier)
                    .collect()
            })
            .collect();

        // Photometric parameters
        ldt.conversion_factor = ies.multiplier;
        ldt.downward_flux_fraction =
            crate::calculations::PhotometricCalculations::downward_flux(&ldt, 90.0);
        ldt.light_output_ratio = 100.0; // Default

        Ok(ldt)
    }

    /// Detect symmetry type from horizontal angles.
    fn detect_symmetry(h_angles: &[f64]) -> Symmetry {
        if h_angles.is_empty() {
            return Symmetry::None;
        }

        let min_angle = h_angles.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_angle = h_angles.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

        if h_angles.len() == 1 {
            // Single horizontal angle = rotationally symmetric
            Symmetry::VerticalAxis
        } else if (max_angle - 90.0).abs() < 0.1 && min_angle.abs() < 0.1 {
            // 0° to 90° = quadrant symmetry
            Symmetry::BothPlanes
        } else if (max_angle - 180.0).abs() < 0.1 && min_angle.abs() < 0.1 {
            // 0° to 180° = bilateral symmetry
            Symmetry::PlaneC0C180
        } else if (min_angle - 90.0).abs() < 0.1 && (max_angle - 270.0).abs() < 0.1 {
            // 90° to 270°
            Symmetry::PlaneC90C270
        } else {
            // Full 360° or other
            Symmetry::None
        }
    }

    /// Calculate luminous flux from intensity distribution (for absolute photometry).
    ///
    /// Integrates candela values over solid angle to get total lumens.
    /// Formula: Φ = ∫∫ I(θ,φ) sin(θ) dθ dφ
    ///
    /// For rotationally symmetric (single C-plane), this simplifies to:
    /// Φ = 2π ∫ I(γ) sin(γ) dγ
    fn calculate_flux_from_intensities(
        candela_values: &[Vec<f64>],
        vertical_angles: &[f64],
    ) -> f64 {
        if candela_values.is_empty() || vertical_angles.len() < 2 {
            return 0.0;
        }

        let n_h = candela_values.len();
        let n_v = vertical_angles.len();

        // Average intensities across all horizontal angles for each vertical angle
        let avg_intensities: Vec<f64> = (0..n_v)
            .map(|v| {
                let sum: f64 = candela_values.iter().filter_map(|row| row.get(v)).sum();
                sum / n_h as f64
            })
            .collect();

        // Integrate using trapezoidal rule over solid angle
        // Φ = 2π ∫ I(γ) sin(γ) dγ
        let mut flux = 0.0;
        for i in 0..n_v - 1 {
            let gamma1 = vertical_angles[i].to_radians();
            let gamma2 = vertical_angles[i + 1].to_radians();
            let i1 = avg_intensities[i];
            let i2 = avg_intensities[i + 1];

            // Trapezoidal rule with sin(γ) weighting
            // ∫ I(γ) sin(γ) dγ ≈ (I1*sin(γ1) + I2*sin(γ2)) / 2 * Δγ
            let dg = gamma2 - gamma1;
            flux += (i1 * gamma1.sin() + i2 * gamma2.sin()) / 2.0 * dg;
        }

        // Multiply by 2π for full revolution (rotationally symmetric)
        // For non-symmetric, we would need to integrate over horizontal angles too
        flux * 2.0 * std::f64::consts::PI
    }
}

/// IES file format exporter.
///
/// Exports Eulumdat data to IES LM-63 format (2002 or 2019).
pub struct IesExporter;

/// Import options for IES files.
///
/// Use `rotate_c_planes` to correct the C-plane orientation difference between
/// EULUMDAT (C0 along luminaire length) and IES (C0 perpendicular to length).
/// Set to `90.0` when importing IES files that need EU-axis alignment.
#[derive(Debug, Clone)]
pub struct IesImportOptions {
    /// Rotate C-planes by this many degrees after import (default: 0.0).
    /// Set to 90.0 to convert IES C0 orientation → EULUMDAT C0 orientation.
    pub rotate_c_planes: f64,
}

impl Default for IesImportOptions {
    fn default() -> Self {
        Self {
            rotate_c_planes: 0.0,
        }
    }
}

/// Export options for IES files.
#[derive(Debug, Clone)]
pub struct IesExportOptions {
    /// IES version to export (default: LM-63-2019)
    pub version: IesVersion,
    /// File generation type (default: Undefined)
    pub file_generation_type: FileGenerationType,
    /// Issue date (required for LM-63-2019)
    pub issue_date: Option<String>,
    /// Additional file generation info
    pub file_gen_info: Option<String>,
    /// Test lab name
    pub test_lab: Option<String>,
    /// Rotate C-planes by this many degrees before export (default: 0.0).
    /// Set to -90.0 to convert EULUMDAT C0 orientation → IES C0 orientation.
    pub rotate_c_planes: f64,
}

impl Default for IesExportOptions {
    fn default() -> Self {
        Self {
            version: IesVersion::Lm63_2019,
            file_generation_type: FileGenerationType::Undefined,
            issue_date: None,
            file_gen_info: None,
            test_lab: None,
            rotate_c_planes: 0.0,
        }
    }
}

impl IesExporter {
    /// Export Eulumdat data to IES LM-63-2019 format (default).
    pub fn export(ldt: &Eulumdat) -> String {
        Self::export_with_options(ldt, &IesExportOptions::default())
    }

    /// Export Eulumdat data to IES LM-63-2002 format (legacy).
    pub fn export_2002(ldt: &Eulumdat) -> String {
        Self::export_with_options(
            ldt,
            &IesExportOptions {
                version: IesVersion::Lm63_2002,
                ..Default::default()
            },
        )
    }

    /// Export with custom options.
    pub fn export_with_options(ldt: &Eulumdat, options: &IesExportOptions) -> String {
        // Apply C-plane rotation if requested
        let rotated;
        let ldt = if options.rotate_c_planes.abs() > 0.001 {
            rotated = {
                let mut copy = ldt.clone();
                copy.rotate_c_planes(options.rotate_c_planes);
                copy
            };
            &rotated
        } else {
            ldt
        };

        let mut output = String::new();

        // Header based on version
        output.push_str(options.version.header());
        output.push('\n');

        // Required keywords
        Self::write_keyword(&mut output, "TEST", &ldt.measurement_report_number);

        // TESTLAB - required in LM-63-2019
        let test_lab = options.test_lab.as_deref().unwrap_or(&ldt.file_name);
        if !test_lab.is_empty() {
            Self::write_keyword(&mut output, "TESTLAB", test_lab);
        }

        // ISSUEDATE - required in LM-63-2019
        if options.version == IesVersion::Lm63_2019 {
            let issue_date = options
                .issue_date
                .as_deref()
                .filter(|s| !s.is_empty())
                .unwrap_or_else(|| {
                    if !ldt.date_user.is_empty() {
                        &ldt.date_user
                    } else {
                        // Default to current date if not provided
                        "01-JAN-2025"
                    }
                });
            Self::write_keyword(&mut output, "ISSUEDATE", issue_date);
        }

        // MANUFAC - required
        if !ldt.identification.is_empty() {
            Self::write_keyword(&mut output, "MANUFAC", &ldt.identification);
        }

        // Optional but recommended keywords
        Self::write_keyword(&mut output, "LUMCAT", &ldt.luminaire_number);
        Self::write_keyword(&mut output, "LUMINAIRE", &ldt.luminaire_name);

        if !ldt.lamp_sets.is_empty() {
            Self::write_keyword(&mut output, "LAMP", &ldt.lamp_sets[0].lamp_type);
            if ldt.lamp_sets[0].total_luminous_flux > 0.0 {
                Self::write_keyword(
                    &mut output,
                    "LAMPCAT",
                    &format!("{:.0} lm", ldt.lamp_sets[0].total_luminous_flux),
                );
            }
        }

        // FILEGENINFO - new in LM-63-2019
        if options.version == IesVersion::Lm63_2019 {
            if let Some(ref info) = options.file_gen_info {
                Self::write_keyword(&mut output, "FILEGENINFO", info);
            }
        }

        // TILT=NONE (most common)
        output.push_str("TILT=NONE\n");

        // Line 1: Number of lamps, lumens per lamp, multiplier, number of vertical angles,
        //         number of horizontal angles, photometric type, units type, width, length, height
        let num_lamps = ldt.lamp_sets.iter().map(|ls| ls.num_lamps).sum::<i32>();
        let total_flux = ldt.total_luminous_flux();

        // CRITICAL: Absolute photometry handling for LED fixtures
        // LDT Convention: negative num_lamps signals absolute photometry
        // IES Standard: lumens_per_lamp = -1 signals absolute photometry
        let lumens_per_lamp = if num_lamps < 0 {
            // Absolute photometry: output -1 to signal absolute mode
            -1.0
        } else if num_lamps > 0 {
            // Relative photometry: divide total flux by lamp count
            total_flux / num_lamps as f64
        } else {
            // Fallback: treat as absolute
            total_flux
        };

        // Expand to full distribution for IES
        let (h_angles, v_angles, intensities) = Self::prepare_photometric_data(ldt);

        // Photometric type: 1 = Type C (vertical angles from 0 at nadir)
        let photometric_type = 1;
        // Units: 1 = feet, 2 = meters
        let units_type = 2;

        // Dimensions in meters (convert from mm)
        let width = ldt.width / 1000.0;
        let length = ldt.length / 1000.0;
        let height = ldt.height / 1000.0;

        // For IES output, num_lamps should always be positive (1 for absolute mode)
        let ies_num_lamps = num_lamps.abs().max(1);

        output.push_str(&format!(
            "{} {:.1} {:.6} {} {} {} {} {:.4} {:.4} {:.4}\n",
            ies_num_lamps,
            lumens_per_lamp,
            ldt.conversion_factor.max(1.0),
            v_angles.len(),
            h_angles.len(),
            photometric_type,
            units_type,
            width,
            length,
            height
        ));

        // Line 2: Ballast factor, file generation type (LM-63-2019) or ballast-lamp factor, input watts
        let total_watts = ldt.total_wattage();
        let file_gen_value = if options.version == IesVersion::Lm63_2019 {
            options.file_generation_type.value()
        } else {
            1.0 // Legacy ballast-lamp photometric factor
        };
        output.push_str(&format!("1.0 {:.5} {:.1}\n", file_gen_value, total_watts));

        // Vertical angles
        output.push_str(&Self::format_values_multiline(&v_angles, 10));
        output.push('\n');

        // Horizontal angles
        output.push_str(&Self::format_values_multiline(&h_angles, 10));
        output.push('\n');

        // Candela values for each horizontal angle
        // Convert from cd/klm back to absolute candela
        let cdklm_to_cd = total_flux / 1000.0;
        for row in &intensities {
            let absolute_candela: Vec<f64> = row.iter().map(|&v| v * cdklm_to_cd).collect();
            output.push_str(&Self::format_values_multiline(&absolute_candela, 10));
            output.push('\n');
        }

        output
    }

    /// Write a keyword line.
    fn write_keyword(output: &mut String, keyword: &str, value: &str) {
        if !value.is_empty() {
            output.push_str(&format!("[{}] {}\n", keyword, value));
        }
    }

    /// Prepare photometric data for IES export.
    ///
    /// Returns (horizontal_angles, vertical_angles, intensities).
    fn prepare_photometric_data(ldt: &Eulumdat) -> (Vec<f64>, Vec<f64>, Vec<Vec<f64>>) {
        // IES uses vertical angles (0 = down, 90 = horizontal, 180 = up)
        // Same as Eulumdat G-angles
        let v_angles = ldt.g_angles.clone();

        // Horizontal angles depend on symmetry
        let (h_angles, intensities) = match ldt.symmetry {
            Symmetry::VerticalAxis => {
                // Single horizontal angle (0°)
                (
                    vec![0.0],
                    vec![ldt.intensities.first().cloned().unwrap_or_default()],
                )
            }
            Symmetry::PlaneC0C180 => {
                // 0° to 180°
                let expanded = SymmetryHandler::expand_to_full(ldt);
                let h = SymmetryHandler::expand_c_angles(ldt);
                // Select only the angles and intensities from 0° to 180°
                let mut h_filtered = Vec::new();
                let mut i_filtered = Vec::new();
                for (i, &angle) in h.iter().enumerate() {
                    if angle <= 180.0 && i < expanded.len() {
                        h_filtered.push(angle);
                        i_filtered.push(expanded[i].clone());
                    }
                }
                (h_filtered, i_filtered)
            }
            Symmetry::PlaneC90C270 => {
                // Full 0° to 360° for C90-C270 symmetry
                // IES format needs the complete distribution
                let expanded = SymmetryHandler::expand_to_full(ldt);
                let h = SymmetryHandler::expand_c_angles(ldt);
                (h, expanded)
            }
            Symmetry::BothPlanes => {
                // 0° to 90°
                let h: Vec<f64> = ldt
                    .c_angles
                    .iter()
                    .filter(|&&a| a <= 90.0)
                    .copied()
                    .collect();
                let i: Vec<Vec<f64>> = ldt.intensities.iter().take(h.len()).cloned().collect();
                (h, i)
            }
            Symmetry::None => {
                // Full 0° to 360°
                (ldt.c_angles.clone(), ldt.intensities.clone())
            }
        };

        (h_angles, v_angles, intensities)
    }

    /// Format values with line wrapping.
    fn format_values_multiline(values: &[f64], per_line: usize) -> String {
        values
            .chunks(per_line)
            .map(|chunk| {
                chunk
                    .iter()
                    .map(|&v| format!("{:.2}", v))
                    .collect::<Vec<_>>()
                    .join(" ")
            })
            .collect::<Vec<_>>()
            .join("\n")
    }
}

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

    #[test]
    fn test_ies_export() {
        let mut ldt = Eulumdat::new();
        ldt.identification = "Test Manufacturer".to_string();
        ldt.luminaire_name = "Test Luminaire".to_string();
        ldt.luminaire_number = "LUM-001".to_string();
        ldt.measurement_report_number = "TEST-001".to_string();
        ldt.symmetry = Symmetry::VerticalAxis;
        ldt.num_c_planes = 1;
        ldt.num_g_planes = 5;
        ldt.c_angles = vec![0.0];
        ldt.g_angles = vec![0.0, 22.5, 45.0, 67.5, 90.0];
        ldt.intensities = vec![vec![1000.0, 900.0, 700.0, 400.0, 100.0]];
        ldt.lamp_sets.push(LampSet {
            num_lamps: 1,
            lamp_type: "LED".to_string(),
            total_luminous_flux: 1000.0,
            color_appearance: "3000K".to_string(),
            color_rendering_group: "80".to_string(),
            wattage_with_ballast: 10.0,
        });
        ldt.conversion_factor = 1.0;
        ldt.length = 100.0;
        ldt.width = 100.0;
        ldt.height = 50.0;

        let ies = IesExporter::export(&ldt);

        // Default export is now LM-63-2019
        assert!(ies.contains("IES:LM-63-2019"));
        assert!(ies.contains("[LUMINAIRE] Test Luminaire"));
        assert!(ies.contains("[MANUFAC] Test Manufacturer"));
        assert!(ies.contains("[ISSUEDATE]")); // Required in 2019
        assert!(ies.contains("TILT=NONE"));

        // Test legacy 2002 export
        let ies_2002 = IesExporter::export_2002(&ldt);
        assert!(ies_2002.contains("IESNA:LM-63-2002"));
        assert!(!ies_2002.contains("[ISSUEDATE]")); // Not required in 2002
    }

    #[test]
    fn test_ies_parse() {
        let ies_content = r#"IESNA:LM-63-2002
[TEST] TEST-001
[MANUFAC] Test Company
[LUMINAIRE] Test Fixture
[LAMP] LED Module
TILT=NONE
1 1000.0 1.0 5 1 1 2 0.1 0.1 0.05
1.0 1.0 10.0
0.0 22.5 45.0 67.5 90.0
0.0
1000.0 900.0 700.0 400.0 100.0
"#;

        let ldt = IesParser::parse(ies_content).expect("Failed to parse IES");

        assert_eq!(ldt.luminaire_name, "Test Fixture");
        assert_eq!(ldt.identification, "Test Company");
        assert_eq!(ldt.measurement_report_number, "TEST-001");
        assert_eq!(ldt.g_angles.len(), 5);
        assert_eq!(ldt.c_angles.len(), 1);
        assert_eq!(ldt.symmetry, Symmetry::VerticalAxis);
        assert!(!ldt.intensities.is_empty());
    }

    #[test]
    fn test_ies_roundtrip() {
        let mut ldt = Eulumdat::new();
        ldt.identification = "Roundtrip Test".to_string();
        ldt.luminaire_name = "Test Luminaire".to_string();
        ldt.symmetry = Symmetry::VerticalAxis;
        ldt.c_angles = vec![0.0];
        ldt.g_angles = vec![0.0, 45.0, 90.0];
        ldt.intensities = vec![vec![500.0, 400.0, 200.0]];
        ldt.lamp_sets.push(LampSet {
            num_lamps: 1,
            lamp_type: "LED".to_string(),
            total_luminous_flux: 1000.0,
            ..Default::default()
        });
        ldt.length = 100.0;
        ldt.width = 100.0;
        ldt.height = 50.0;

        // Export to IES
        let ies = IesExporter::export(&ldt);

        // Parse back
        let parsed = IesParser::parse(&ies).expect("Failed to parse exported IES");

        // Verify key fields
        assert_eq!(parsed.luminaire_name, ldt.luminaire_name);
        assert_eq!(parsed.g_angles.len(), ldt.g_angles.len());
        assert_eq!(parsed.symmetry, Symmetry::VerticalAxis);
    }

    #[test]
    fn test_detect_symmetry() {
        assert_eq!(IesParser::detect_symmetry(&[0.0]), Symmetry::VerticalAxis);
        assert_eq!(
            IesParser::detect_symmetry(&[0.0, 45.0, 90.0]),
            Symmetry::BothPlanes
        );
        assert_eq!(
            IesParser::detect_symmetry(&[0.0, 45.0, 90.0, 135.0, 180.0]),
            Symmetry::PlaneC0C180
        );
        assert_eq!(
            IesParser::detect_symmetry(&[0.0, 90.0, 180.0, 270.0, 360.0]),
            Symmetry::None
        );
    }

    #[test]
    fn test_photometric_type() {
        assert_eq!(
            PhotometricType::from_int(1).unwrap(),
            PhotometricType::TypeC
        );
        assert_eq!(
            PhotometricType::from_int(2).unwrap(),
            PhotometricType::TypeB
        );
        assert_eq!(
            PhotometricType::from_int(3).unwrap(),
            PhotometricType::TypeA
        );
        assert!(PhotometricType::from_int(0).is_err());
    }

    #[test]
    fn test_unit_conversion() {
        assert!((UnitType::Feet.to_mm_factor() - 304.8).abs() < 0.01);
        assert!((UnitType::Meters.to_mm_factor() - 1000.0).abs() < 0.01);
    }

    #[test]
    fn test_ies_version_parsing() {
        assert_eq!(
            IesVersion::from_header("IES:LM-63-2019"),
            IesVersion::Lm63_2019
        );
        assert_eq!(
            IesVersion::from_header("IESNA:LM-63-2002"),
            IesVersion::Lm63_2002
        );
        assert_eq!(
            IesVersion::from_header("IESNA:LM-63-1995"),
            IesVersion::Lm63_1995
        );
        assert_eq!(IesVersion::from_header("IESNA91"), IesVersion::Lm63_1991);
    }

    #[test]
    fn test_file_generation_type() {
        assert_eq!(
            FileGenerationType::from_value(1.00001),
            FileGenerationType::Undefined
        );
        assert_eq!(
            FileGenerationType::from_value(1.00010),
            FileGenerationType::ComputerSimulation
        );
        assert_eq!(
            FileGenerationType::from_value(1.10000),
            FileGenerationType::AccreditedLab
        );
        assert_eq!(
            FileGenerationType::from_value(1.10100),
            FileGenerationType::AccreditedLabScaled
        );

        // Test accredited lab check
        assert!(FileGenerationType::AccreditedLab.is_accredited());
        assert!(!FileGenerationType::UnaccreditedLab.is_accredited());

        // Test scaled check
        assert!(FileGenerationType::AccreditedLabScaled.is_scaled());
        assert!(!FileGenerationType::AccreditedLab.is_scaled());
    }

    #[test]
    fn test_luminous_shape() {
        // Point source
        assert_eq!(
            LuminousShape::from_dimensions(0.0, 0.0, 0.0),
            LuminousShape::Point
        );

        // Rectangular
        assert_eq!(
            LuminousShape::from_dimensions(0.5, 0.6, 0.0),
            LuminousShape::Rectangular
        );

        // Circular (negative equal width/length)
        assert_eq!(
            LuminousShape::from_dimensions(-0.3, -0.3, 0.0),
            LuminousShape::Circular
        );

        // Sphere (all negative equal)
        assert_eq!(
            LuminousShape::from_dimensions(-0.2, -0.2, -0.2),
            LuminousShape::Sphere
        );
    }

    #[test]
    fn test_ies_2019_parse() {
        let ies_content = r#"IES:LM-63-2019
[TEST] ABC1234
[TESTLAB] ABC Laboratories
[ISSUEDATE] 28-FEB-2019
[MANUFAC] Test Company
[LUMCAT] SKYVIEW-123
[LUMINAIRE] LED Wide beam flood
[LAMP] LED Module
[FILEGENINFO] This file was generated from test data
TILT=NONE
1 -1 1.0 5 1 1 2 0.1 0.1 0.0
1.0 1.10000 50.0
0.0 22.5 45.0 67.5 90.0
0.0
1000.0 900.0 700.0 400.0 100.0
"#;

        let ies_data = IesParser::parse_to_ies_data(ies_content).expect("Failed to parse IES");

        assert_eq!(ies_data.version, IesVersion::Lm63_2019);
        assert_eq!(ies_data.test, "ABC1234");
        assert_eq!(ies_data.test_lab, "ABC Laboratories");
        assert_eq!(ies_data.issue_date, "28-FEB-2019");
        assert_eq!(ies_data.manufacturer, "Test Company");
        assert_eq!(
            ies_data.file_generation_type,
            FileGenerationType::AccreditedLab
        );
        assert_eq!(
            ies_data.file_gen_info,
            "This file was generated from test data"
        );
        assert_eq!(ies_data.lumens_per_lamp, -1.0); // Absolute photometry
    }

    #[test]
    fn test_ies_tilt_include() {
        let ies_content = r#"IES:LM-63-2019
[TEST] TILT-TEST
[TESTLAB] Test Lab
[ISSUEDATE] 01-JAN-2020
[MANUFAC] Test Mfg
TILT=INCLUDE
1
7
0 15 30 45 60 75 90
1.0 0.95 0.94 0.90 0.88 0.87 0.94
1 1000.0 1.0 3 1 1 2 0.1 0.1 0.0
1.0 1.00001 10.0
0.0 45.0 90.0
0.0
100.0 80.0 50.0
"#;

        let ies_data = IesParser::parse_to_ies_data(ies_content).expect("Failed to parse");

        assert_eq!(ies_data.tilt_mode, "INCLUDE");
        assert!(ies_data.tilt_data.is_some());

        let tilt = ies_data.tilt_data.as_ref().unwrap();
        assert_eq!(tilt.lamp_geometry, 1);
        assert_eq!(tilt.angles.len(), 7);
        assert_eq!(tilt.factors.len(), 7);
        assert!((tilt.angles[0] - 0.0).abs() < 0.001);
        assert!((tilt.factors[0] - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_more_continuation() {
        let ies_content = r#"IES:LM-63-2019
[TEST] MORE-TEST
[TESTLAB] Test Lab
[ISSUEDATE] 01-JAN-2020
[MANUFAC] Test Manufacturer
[OTHER] This is the first line of other info
[MORE] This is the second line of other info
TILT=NONE
1 1000.0 1.0 3 1 1 2 0.1 0.1 0.0
1.0 1.00001 10.0
0.0 45.0 90.0
0.0
100.0 80.0 50.0
"#;

        let ies_data = IesParser::parse_to_ies_data(ies_content).expect("Failed to parse");

        // Check that OTHER contains multi-line content via MORE
        let other_value = ies_data.keywords.get("OTHER").expect("OTHER not found");
        assert!(other_value.contains("first line"));
        assert!(other_value.contains("second line"));
    }
}

// === IES Validation ===

/// IES-specific validation warning.
#[derive(Debug, Clone, PartialEq)]
pub struct IesValidationWarning {
    /// Warning code
    pub code: &'static str,
    /// Human-readable message
    pub message: String,
    /// Severity level
    pub severity: IesValidationSeverity,
}

/// Severity level for IES validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IesValidationSeverity {
    /// Required by LM-63-2019 specification
    Required,
    /// Recommended but not strictly required
    Recommended,
    /// Informational warning
    Info,
}

impl std::fmt::Display for IesValidationWarning {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let severity = match self.severity {
            IesValidationSeverity::Required => "ERROR",
            IesValidationSeverity::Recommended => "WARNING",
            IesValidationSeverity::Info => "INFO",
        };
        write!(f, "[{}:{}] {}", self.code, severity, self.message)
    }
}

/// Validate IES data according to LM-63-2019 specification.
pub fn validate_ies(data: &IesData) -> Vec<IesValidationWarning> {
    let mut warnings = Vec::new();

    // === Required Keywords (Section 5.2) ===

    // IES001: [TEST] is required
    if data.test.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES001",
            message: "Missing required keyword [TEST]".to_string(),
            severity: IesValidationSeverity::Required,
        });
    }

    // IES002: [TESTLAB] is required
    if data.test_lab.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES002",
            message: "Missing required keyword [TESTLAB]".to_string(),
            severity: IesValidationSeverity::Required,
        });
    }

    // IES003: [ISSUEDATE] is required (LM-63-2019)
    if data.version == IesVersion::Lm63_2019 && data.issue_date.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES003",
            message: "Missing required keyword [ISSUEDATE] for LM-63-2019".to_string(),
            severity: IesValidationSeverity::Required,
        });
    }

    // IES004: [MANUFAC] is required
    if data.manufacturer.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES004",
            message: "Missing required keyword [MANUFAC]".to_string(),
            severity: IesValidationSeverity::Required,
        });
    }

    // === Recommended Keywords ===

    // IES005: [LUMCAT] recommended
    if data.luminaire_catalog.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES005",
            message: "Missing recommended keyword [LUMCAT]".to_string(),
            severity: IesValidationSeverity::Recommended,
        });
    }

    // IES006: [LUMINAIRE] recommended
    if data.luminaire.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES006",
            message: "Missing recommended keyword [LUMINAIRE]".to_string(),
            severity: IesValidationSeverity::Recommended,
        });
    }

    // IES007: [LAMP] recommended
    if data.lamp.is_empty() {
        warnings.push(IesValidationWarning {
            code: "IES007",
            message: "Missing recommended keyword [LAMP]".to_string(),
            severity: IesValidationSeverity::Recommended,
        });
    }

    // === Photometric Type Validation (Section 5.9) ===

    // IES010: Valid photometric type
    let photo_type = data.photometric_type as i32;
    if !(1..=3).contains(&photo_type) {
        warnings.push(IesValidationWarning {
            code: "IES010",
            message: format!(
                "Invalid photometric type: {} (must be 1, 2, or 3)",
                photo_type
            ),
            severity: IesValidationSeverity::Required,
        });
    }

    // === Unit Type Validation (Section 5.10.1) ===

    let unit_type = data.unit_type as i32;
    if !(1..=2).contains(&unit_type) {
        warnings.push(IesValidationWarning {
            code: "IES011",
            message: format!(
                "Invalid unit type: {} (must be 1=feet or 2=meters)",
                unit_type
            ),
            severity: IesValidationSeverity::Required,
        });
    }

    // === Vertical Angle Validation (Section 5.15) ===

    if !data.vertical_angles.is_empty() {
        let first_v = data.vertical_angles[0];
        let last_v = *data.vertical_angles.last().unwrap();

        // Type C photometry
        if data.photometric_type == PhotometricType::TypeC {
            // First angle must be 0 or 90
            if (first_v - 0.0).abs() > 0.01 && (first_v - 90.0).abs() > 0.01 {
                warnings.push(IesValidationWarning {
                    code: "IES020",
                    message: format!(
                        "Type C: First vertical angle ({}) must be 0 or 90 degrees",
                        first_v
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }

            // Last angle must be 90 or 180
            if (last_v - 90.0).abs() > 0.01 && (last_v - 180.0).abs() > 0.01 {
                warnings.push(IesValidationWarning {
                    code: "IES021",
                    message: format!(
                        "Type C: Last vertical angle ({}) must be 90 or 180 degrees",
                        last_v
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }
        }

        // Type A or B photometry
        if data.photometric_type == PhotometricType::TypeA
            || data.photometric_type == PhotometricType::TypeB
        {
            // First angle must be -90 or 0
            if (first_v - 0.0).abs() > 0.01 && (first_v + 90.0).abs() > 0.01 {
                warnings.push(IesValidationWarning {
                    code: "IES022",
                    message: format!(
                        "Type A/B: First vertical angle ({}) must be -90 or 0 degrees",
                        first_v
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }

            // Last angle must be 90
            if (last_v - 90.0).abs() > 0.01 {
                warnings.push(IesValidationWarning {
                    code: "IES023",
                    message: format!(
                        "Type A/B: Last vertical angle ({}) must be 90 degrees",
                        last_v
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }
        }

        // Check ascending order
        for i in 1..data.vertical_angles.len() {
            if data.vertical_angles[i] <= data.vertical_angles[i - 1] {
                warnings.push(IesValidationWarning {
                    code: "IES024",
                    message: format!("Vertical angles not in ascending order at index {}", i),
                    severity: IesValidationSeverity::Required,
                });
                break;
            }
        }
    }

    // === Horizontal Angle Validation (Section 5.16) ===

    if !data.horizontal_angles.is_empty() {
        let first_h = data.horizontal_angles[0];
        let last_h = *data.horizontal_angles.last().unwrap();

        // Type C: first must be 0
        if data.photometric_type == PhotometricType::TypeC {
            if (first_h - 0.0).abs() > 0.01 {
                warnings.push(IesValidationWarning {
                    code: "IES030",
                    message: format!(
                        "Type C: First horizontal angle ({}) must be 0 degrees",
                        first_h
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }

            // Last must be 0, 90, 180, or 360
            let valid_last = [
                (0.0, "laterally symmetric"),
                (90.0, "quadrant symmetric"),
                (180.0, "bilateral symmetric"),
                (360.0, "no lateral symmetry"),
            ];
            let mut found_valid = false;
            for (angle, _) in &valid_last {
                if (last_h - angle).abs() < 0.01 {
                    found_valid = true;
                    break;
                }
            }
            if !found_valid && data.horizontal_angles.len() > 1 {
                warnings.push(IesValidationWarning {
                    code: "IES031",
                    message: format!(
                        "Type C: Last horizontal angle ({}) must be 0, 90, 180, or 360 degrees",
                        last_h
                    ),
                    severity: IesValidationSeverity::Required,
                });
            }
        }

        // Check ascending order
        for i in 1..data.horizontal_angles.len() {
            if data.horizontal_angles[i] <= data.horizontal_angles[i - 1] {
                warnings.push(IesValidationWarning {
                    code: "IES032",
                    message: format!("Horizontal angles not in ascending order at index {}", i),
                    severity: IesValidationSeverity::Required,
                });
                break;
            }
        }
    }

    // === Data Dimension Validation ===

    // IES040: Verify candela data dimensions
    if data.candela_values.len() != data.n_horizontal {
        warnings.push(IesValidationWarning {
            code: "IES040",
            message: format!(
                "Candela data has {} horizontal planes, expected {}",
                data.candela_values.len(),
                data.n_horizontal
            ),
            severity: IesValidationSeverity::Required,
        });
    }

    for (i, row) in data.candela_values.iter().enumerate() {
        if row.len() != data.n_vertical {
            warnings.push(IesValidationWarning {
                code: "IES041",
                message: format!(
                    "Candela row {} has {} values, expected {}",
                    i,
                    row.len(),
                    data.n_vertical
                ),
                severity: IesValidationSeverity::Required,
            });
        }
    }

    // === File Generation Type Validation (LM-63-2019) ===

    if data.version == IesVersion::Lm63_2019 {
        // Check if file generation type is valid
        let valid_values = [
            1.00001, 1.00010, 1.00000, 1.00100, 1.01000, 1.01100, 1.10000, 1.10100, 1.11000,
            1.11100,
        ];
        let mut found = false;
        for &v in &valid_values {
            if (data.file_generation_value - v).abs() < 0.000001 {
                found = true;
                break;
            }
        }
        // Only warn if it looks like a non-legacy value
        if !found && data.file_generation_value > 1.0 {
            warnings.push(IesValidationWarning {
                code: "IES050",
                message: format!(
                    "File generation type value ({}) is not a standard LM-63-2019 value",
                    data.file_generation_value
                ),
                severity: IesValidationSeverity::Info,
            });
        }
    }

    // === Ballast Factor Validation ===

    if data.ballast_factor <= 0.0 || data.ballast_factor > 2.0 {
        warnings.push(IesValidationWarning {
            code: "IES060",
            message: format!(
                "Unusual ballast factor: {} (typically 0.5-1.5)",
                data.ballast_factor
            ),
            severity: IesValidationSeverity::Info,
        });
    }

    // === Candela Value Validation ===

    let mut has_negative = false;
    let mut max_cd = 0.0f64;
    for row in &data.candela_values {
        for &cd in row {
            if cd < 0.0 {
                has_negative = true;
            }
            max_cd = max_cd.max(cd);
        }
    }

    if has_negative {
        warnings.push(IesValidationWarning {
            code: "IES070",
            message: "Negative candela values found".to_string(),
            severity: IesValidationSeverity::Required,
        });
    }

    if max_cd > 1_000_000.0 {
        warnings.push(IesValidationWarning {
            code: "IES071",
            message: format!(
                "Very high candela value: {:.0} (verify data correctness)",
                max_cd
            ),
            severity: IesValidationSeverity::Info,
        });
    }

    warnings
}

/// Get required warnings only (errors).
pub fn validate_ies_strict(data: &IesData) -> Vec<IesValidationWarning> {
    validate_ies(data)
        .into_iter()
        .filter(|w| w.severity == IesValidationSeverity::Required)
        .collect()
}