gldf-rs 0.4.0

GLDF (General Lighting Data Format) parser and writer for Rust, specifically for the Rust/WASM target as such designed for JSON format
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
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
//! IFC STEP file writer
//!
//! Generates IFC files in STEP Physical File Format (ISO 10303-21)

use super::types::{
    DistributionSystemEnum, ElectricalDeviceProperties, EntityRef, FlowDirectionEnum,
    InsulationStandardClass, LightEmissionSourceEnum, LightFixtureCommonProperties,
    LightFixtureTypeEnum, ManufacturerTypeInfo, OptionalRef, ServiceLifeInfo, WarrantyInfo,
};
use std::collections::HashMap;

/// Triangulated mesh data for IFC export
///
/// Contains vertex positions and triangle indices in a format
/// suitable for IFC TRIANGULATEDFACESET representation.
#[derive(Debug, Clone, Default)]
pub struct MeshData {
    /// Vertex positions as (x, y, z) tuples in meters
    pub vertices: Vec<(f64, f64, f64)>,
    /// Triangle indices (1-based for IFC)
    /// Each tuple is (v1, v2, v3) referencing vertices
    pub triangles: Vec<(u32, u32, u32)>,
}

#[cfg(not(target_arch = "wasm32"))]
use chrono::{DateTime, Utc};

/// IFC STEP file writer
///
/// Builds a valid IFC file with the minimum structure needed for light fixtures.
pub struct StepWriter {
    schema: String,
    next_id: u64,
    entities: Vec<String>,
    // Track entity IDs for relationships
    entity_map: HashMap<String, EntityRef>,
    // Track light sources for connecting to fixtures
    light_source_ids: Vec<EntityRef>,
    // GUID counter for uniqueness
    guid_counter: u64,
}

impl StepWriter {
    /// Create a new STEP writer for the given IFC schema
    pub fn new(schema: &str) -> Self {
        Self {
            schema: schema.to_string(),
            next_id: 1,
            entities: Vec::new(),
            entity_map: HashMap::new(),
            light_source_ids: Vec::new(),
            guid_counter: 1,
        }
    }

    /// Get next entity ID and increment counter
    fn next_id(&mut self) -> EntityRef {
        let id = EntityRef::new(self.next_id);
        self.next_id += 1;
        id
    }

    /// Add an entity line
    fn add_entity(&mut self, entity: String) -> EntityRef {
        let id = self.next_id();
        self.entities.push(format!("{}={}", id, entity));
        id
    }

    /// Generate a valid IFC GlobalId (22 characters, base64-like encoding)
    ///
    /// IFC GlobalId is a compressed UUID representation using 64 characters:
    /// 0-9, A-Z, a-z, _ and $
    fn generate_guid(&mut self) -> String {
        // IFC base64 character set (NOT standard base64!)
        const CHARS: &[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$";

        // Generate a pseudo-random 128-bit value
        #[cfg(not(target_arch = "wasm32"))]
        let seed = {
            use std::time::{SystemTime, UNIX_EPOCH};
            let time_part = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0);
            // Mix in counter for uniqueness within same timestamp
            time_part.wrapping_add(self.guid_counter as u128 * 0x517cc1b727220a95)
        };

        #[cfg(target_arch = "wasm32")]
        let seed = {
            // Use counter-based approach for WASM
            let base = 0x0123456789ABCDEFu128;
            base.wrapping_mul(self.guid_counter as u128)
                .wrapping_add(0xFEDCBA9876543210u128)
        };

        self.guid_counter += 1;

        // Convert 128-bit value to 22 base-64 characters
        // Each character encodes 6 bits, 22 * 6 = 132 bits (we use 128)
        let mut result = String::with_capacity(22);
        let mut n = seed;

        for _ in 0..22 {
            result.push(CHARS[(n % 64) as usize] as char);
            n /= 64;
        }

        // Reverse to get most-significant bits first
        result.chars().rev().collect()
    }

    /// Escape a string for STEP format
    fn escape_string(s: &str) -> String {
        s.replace('\\', "\\\\").replace('\'', "''")
    }

    /// Create an IFCAXIS2PLACEMENT3D entity (properly, not inline!)
    fn add_axis2_placement_3d(
        &mut self,
        origin: EntityRef,
        axis: Option<EntityRef>,
        ref_dir: Option<EntityRef>,
    ) -> EntityRef {
        let axis_str = axis
            .map(|a| a.to_string())
            .unwrap_or_else(|| "$".to_string());
        let ref_str = ref_dir
            .map(|r| r.to_string())
            .unwrap_or_else(|| "$".to_string());
        let entity_str = format!("IFCAXIS2PLACEMENT3D({},{},{})", origin, axis_str, ref_str);
        self.add_entity(entity_str)
    }

    /// Create an IFCLOCALPLACEMENT entity with proper references
    fn add_local_placement(
        &mut self,
        relative_to: Option<EntityRef>,
        placement: EntityRef,
    ) -> EntityRef {
        let relative_str = relative_to
            .map(|r| r.to_string())
            .unwrap_or_else(|| "$".to_string());
        let entity_str = format!("IFCLOCALPLACEMENT({},{})", relative_str, placement);
        self.add_entity(entity_str)
    }

    // =========================================================================
    // Core IFC entities
    // =========================================================================

    /// Add IfcOwnerHistory
    pub fn add_owner_history(&mut self, organization_name: &str) -> EntityRef {
        // Person
        let person = self.add_entity("IFCPERSON($,$,'',$,$,$,$,$)".to_string());

        // Organization
        let org_str = format!(
            "IFCORGANIZATION($,'{}','GLDF Export',$,$)",
            Self::escape_string(organization_name)
        );
        let org = self.add_entity(org_str);

        // PersonAndOrganization
        let po_str = format!("IFCPERSONANDORGANIZATION({},{},$)", person, org);
        let person_org = self.add_entity(po_str);

        // Application
        let app_str = format!("IFCAPPLICATION({},'0.3.3','gldf-rs','gldf-rs')", org);
        let app = self.add_entity(app_str);

        // OwnerHistory
        let oh_str = format!(
            "IFCOWNERHISTORY({},{},{},{},$,$,$,{})",
            person_org,
            app,
            ".READWRITE.",
            ".ADDED.",
            Self::current_timestamp()
        );
        self.add_entity(oh_str)
    }

    /// Add IfcProject
    pub fn add_project(&mut self, name: &str, owner_history: EntityRef) -> EntityRef {
        // Units
        let si_length = self.add_entity("IFCSIUNIT(*,.LENGTHUNIT.,$,.METRE.)".to_string());
        let si_area = self.add_entity("IFCSIUNIT(*,.AREAUNIT.,$,.SQUARE_METRE.)".to_string());
        let si_volume = self.add_entity("IFCSIUNIT(*,.VOLUMEUNIT.,$,.CUBIC_METRE.)".to_string());
        let si_angle = self.add_entity("IFCSIUNIT(*,.PLANEANGLEUNIT.,$,.RADIAN.)".to_string());
        let si_lumen = self.add_entity("IFCSIUNIT(*,.LUMINOUSFLUXUNIT.,$,.LUMEN.)".to_string());
        let si_power = self.add_entity("IFCSIUNIT(*,.POWERUNIT.,$,.WATT.)".to_string());

        let ua_str = format!(
            "IFCUNITASSIGNMENT(({},{},{},{},{},{}))",
            si_length, si_area, si_volume, si_angle, si_lumen, si_power
        );
        let unit_assignment = self.add_entity(ua_str);

        // Geometric context - create entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis = self.add_entity("IFCDIRECTION((0.,0.,1.))".to_string());
        let ref_dir = self.add_entity("IFCDIRECTION((1.,0.,0.))".to_string());
        let placement = self.add_axis2_placement_3d(origin, Some(axis), Some(ref_dir));

        let ctx_str = format!(
            "IFCGEOMETRICREPRESENTATIONCONTEXT($,'Model',3,1.0E-5,{},{})",
            placement,
            OptionalRef::None
        );
        let context_3d = self.add_entity(ctx_str);

        // Project
        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let proj_str = format!(
            "IFCPROJECT('{}',{},'{}','GLDF to IFC Export',$,$,$,({}),{})",
            guid, owner_history, escaped_name, context_3d, unit_assignment
        );
        let project = self.add_entity(proj_str);

        self.entity_map.insert("project".to_string(), project);
        self.entity_map.insert("context_3d".to_string(), context_3d);
        project
    }

    /// Add IfcSite
    pub fn add_site(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        project: EntityRef,
    ) -> EntityRef {
        // Create placement entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);
        let placement = self.add_local_placement(None, axis2_placement);

        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let site_str = format!(
            "IFCSITE('{}',{},'{}','',$,{},$,$,.ELEMENT.,$,$,$,$,$)",
            guid, owner_history, escaped_name, placement
        );
        let site = self.add_entity(site_str);

        // Aggregate site to project
        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELAGGREGATES('{}',{},$,$,{},({}))",
            guid2, owner_history, project, site
        );
        self.add_entity(rel_str);

        site
    }

    /// Add IfcBuilding
    pub fn add_building(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        site: EntityRef,
    ) -> EntityRef {
        // Create placement entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);
        let placement = self.add_local_placement(None, axis2_placement);

        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let bldg_str = format!(
            "IFCBUILDING('{}',{},'{}','',$,{},$,$,.ELEMENT.,$,$,$)",
            guid, owner_history, escaped_name, placement
        );
        let building = self.add_entity(bldg_str);

        // Aggregate building to site
        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELAGGREGATES('{}',{},$,$,{},({}))",
            guid2, owner_history, site, building
        );
        self.add_entity(rel_str);

        building
    }

    /// Add IfcBuildingStorey
    pub fn add_storey(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        building: EntityRef,
    ) -> EntityRef {
        // Create placement entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);
        let placement = self.add_local_placement(None, axis2_placement);

        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let storey_str = format!(
            "IFCBUILDINGSTOREY('{}',{},'{}','',$,{},$,$,.ELEMENT.,0.)",
            guid, owner_history, escaped_name, placement
        );
        let storey = self.add_entity(storey_str);

        // Aggregate storey to building
        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELAGGREGATES('{}',{},$,$,{},({}))",
            guid2, owner_history, building, storey
        );
        self.add_entity(rel_str);

        storey
    }

    // =========================================================================
    // Light fixture entities
    // =========================================================================

    /// Add IfcLightFixtureType with optional shared geometry
    ///
    /// # Arguments
    /// * `name` - Type name
    /// * `manufacturer` - Manufacturer name
    /// * `predefined_type` - Light fixture predefined type
    /// * `owner_history` - Owner history reference
    /// * `representation_map` - Optional shared geometry (IFCREPRESENTATIONMAP)
    ///
    /// When `representation_map` is provided, all fixture types share the same geometry,
    /// avoiding duplication. This is the correct IFC pattern for variants.
    pub fn add_light_fixture_type(
        &mut self,
        name: &str,
        manufacturer: &str,
        predefined_type: LightFixtureTypeEnum,
        owner_history: EntityRef,
    ) -> EntityRef {
        self.add_light_fixture_type_with_geometry(
            name,
            manufacturer,
            predefined_type,
            owner_history,
            None,
        )
    }

    /// Add IfcLightFixtureType with shared representation map
    pub fn add_light_fixture_type_with_geometry(
        &mut self,
        name: &str,
        manufacturer: &str,
        predefined_type: LightFixtureTypeEnum,
        owner_history: EntityRef,
        representation_map: Option<EntityRef>,
    ) -> EntityRef {
        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);

        // RepresentationMaps is parameter 8 (index 7) in IFCLIGHTFIXTURETYPE
        // IFCLIGHTFIXTURETYPE(GlobalId, OwnerHistory, Name, Description, ApplicableOccurrence,
        //                     HasPropertySets, RepresentationMaps, Tag, ElementType, PredefinedType)
        let rep_maps = representation_map
            .map(|r| format!("({})", r))
            .unwrap_or_else(|| "$".to_string());

        let type_str = format!(
            "IFCLIGHTFIXTURETYPE('{}',{},'{}','Luminaire from GLDF',$,$,{},$,$,{})",
            guid,
            owner_history,
            escaped_name,
            rep_maps,
            predefined_type.to_step()
        );
        let fixture_type = self.add_entity(type_str);

        // Add manufacturer property set
        self.add_manufacturer_pset(fixture_type, owner_history, manufacturer, name);

        fixture_type
    }

    /// Create simple box geometry for the light fixture (fallback when no L3D mesh)
    fn create_fixture_geometry(&mut self, width: f64, depth: f64, height: f64) -> EntityRef {
        let context_3d = self.entity_map.get("context_3d").copied();

        // Create a simple extruded box - Rectangle profile
        let profile_str = format!(
            "IFCRECTANGLEPROFILEDEF(.AREA.,$,$,{:.3},{:.3})",
            width, depth
        );
        let profile = self.add_entity(profile_str);

        // Placement for extrusion (at origin, extruding upward)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis = self.add_entity("IFCDIRECTION((0.,0.,1.))".to_string());
        let ref_dir = self.add_entity("IFCDIRECTION((1.,0.,0.))".to_string());
        let extrusion_placement = self.add_axis2_placement_3d(origin, Some(axis), Some(ref_dir));

        // Direction of extrusion
        let extrude_dir = self.add_entity("IFCDIRECTION((0.,0.,1.))".to_string());

        // Extruded solid
        let solid_str = format!(
            "IFCEXTRUDEDAREASOLID({},{},{},{:.3})",
            profile, extrusion_placement, extrude_dir, height
        );
        let solid = self.add_entity(solid_str);

        // Shape representation
        let context_str = context_3d
            .map(|c| c.to_string())
            .unwrap_or_else(|| "$".to_string());
        let shape_str = format!(
            "IFCSHAPEREPRESENTATION({},'Body','SweptSolid',({}))",
            context_str, solid
        );
        let shape_rep = self.add_entity(shape_str);

        // Product definition shape
        let prod_str = format!("IFCPRODUCTDEFINITIONSHAPE($,$,({}))", shape_rep);
        self.add_entity(prod_str)
    }

    /// Create a representation map for shared geometry
    ///
    /// Creates geometry that can be referenced by multiple IFCLIGHTFIXTURETYPE entities.
    /// This avoids duplicating geometry for each variant.
    ///
    /// # Arguments
    /// * `mesh` - Optional triangulated mesh data. If None, creates default box.
    ///
    /// # Returns
    /// EntityRef to IFCREPRESENTATIONMAP
    pub fn create_representation_map(&mut self, mesh: Option<&MeshData>) -> EntityRef {
        // Create origin for the map
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);

        // Create the shape representation
        let shape_rep = if let Some(m) = mesh {
            if !m.vertices.is_empty() && !m.triangles.is_empty() {
                self.create_tessellated_shape_representation(m)
            } else {
                self.create_box_shape_representation(0.3, 0.3, 0.1)
            }
        } else {
            self.create_box_shape_representation(0.3, 0.3, 0.1)
        };

        // Create the representation map
        let map_str = format!("IFCREPRESENTATIONMAP({},{})", axis2_placement, shape_rep);
        self.add_entity(map_str)
    }

    /// Create tessellated shape representation (without ProductDefinitionShape wrapper)
    fn create_tessellated_shape_representation(&mut self, mesh: &MeshData) -> EntityRef {
        let context_3d = self.entity_map.get("context_3d").copied();

        // Build vertex list
        let coords_str: String = mesh
            .vertices
            .iter()
            .map(|(x, y, z)| format!("({:.6},{:.6},{:.6})", x, y, z))
            .collect::<Vec<_>>()
            .join(",");

        let point_list_str = format!("IFCCARTESIANPOINTLIST3D(({}))", coords_str);
        let point_list = self.add_entity(point_list_str);

        // Build triangle index list
        let triangles_str: String = mesh
            .triangles
            .iter()
            .map(|(a, b, c)| format!("({},{},{})", a, b, c))
            .collect::<Vec<_>>()
            .join(",");

        let faceset_str = format!(
            "IFCTRIANGULATEDFACESET({},$,.T.,({}),())",
            point_list, triangles_str
        );
        let faceset = self.add_entity(faceset_str);

        let context_str = context_3d
            .map(|c| c.to_string())
            .unwrap_or_else(|| "$".to_string());
        let shape_str = format!(
            "IFCSHAPEREPRESENTATION({},'Body','Tessellation',({}))",
            context_str, faceset
        );
        self.add_entity(shape_str)
    }

    /// Create box shape representation (without ProductDefinitionShape wrapper)
    fn create_box_shape_representation(
        &mut self,
        width: f64,
        height: f64,
        depth: f64,
    ) -> EntityRef {
        let context_3d = self.entity_map.get("context_3d").copied();

        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2 = self.add_axis2_placement_3d(origin, None, None);

        let profile_str = format!(
            "IFCRECTANGLEPROFILEDEF(.AREA.,'Luminaire Profile',{},{},{})",
            axis2, width, height
        );
        let profile = self.add_entity(profile_str);

        let dir_str = "IFCDIRECTION((0.,0.,1.))";
        let dir = self.add_entity(dir_str.to_string());

        let solid_str = format!(
            "IFCEXTRUDEDAREASOLID({},{},{},{})",
            profile, axis2, dir, depth
        );
        let solid = self.add_entity(solid_str);

        let context_str = context_3d
            .map(|c| c.to_string())
            .unwrap_or_else(|| "$".to_string());
        let shape_str = format!(
            "IFCSHAPEREPRESENTATION({},'Body','SweptSolid',({}))",
            context_str, solid
        );
        self.add_entity(shape_str)
    }

    /// Create tessellated geometry from L3D mesh data
    ///
    /// Converts triangulated mesh to IFC format:
    /// - IFCCARTESIANPOINTLIST3D for vertex positions
    /// - IFCTRIANGULATEDFACESET for triangle indices
    /// - IFCSHAPEREPRESENTATION with 'Tessellation' type
    ///
    /// # Arguments
    /// * `mesh` - Triangulated mesh data with vertices and triangle indices
    ///
    /// # Returns
    /// EntityRef to IFCPRODUCTDEFINITIONSHAPE
    pub fn create_tessellated_geometry(&mut self, mesh: &MeshData) -> EntityRef {
        let context_3d = self.entity_map.get("context_3d").copied();

        // Build vertex list: ((x1,y1,z1),(x2,y2,z2),...)
        let coords_str: String = mesh
            .vertices
            .iter()
            .map(|(x, y, z)| format!("({:.6},{:.6},{:.6})", x, y, z))
            .collect::<Vec<_>>()
            .join(",");

        let point_list_str = format!("IFCCARTESIANPOINTLIST3D(({}))", coords_str);
        let point_list = self.add_entity(point_list_str);

        // Build triangle index list: ((1,2,3),(4,5,6),...)
        // IFC uses 1-based indices
        let triangles_str: String = mesh
            .triangles
            .iter()
            .map(|(a, b, c)| format!("({},{},{})", a, b, c))
            .collect::<Vec<_>>()
            .join(",");

        // IFCTRIANGULATEDFACESET(Coordinates, Normals, Closed, CoordIndex, PnIndex)
        // We use:
        // - Coordinates: the point list
        // - Normals: $ (auto-calculate)
        // - Closed: .T. (closed solid) or .F. (open surface)
        // - CoordIndex: triangle indices
        // - PnIndex: $ (not used when Normals is $)
        let faceset_str = format!(
            "IFCTRIANGULATEDFACESET({},$,.T.,({}),())",
            point_list, triangles_str
        );
        let faceset = self.add_entity(faceset_str);

        // Shape representation with 'Tessellation' type
        let context_str = context_3d
            .map(|c| c.to_string())
            .unwrap_or_else(|| "$".to_string());
        let shape_str = format!(
            "IFCSHAPEREPRESENTATION({},'Body','Tessellation',({}))",
            context_str, faceset
        );
        let shape_rep = self.add_entity(shape_str);

        // Product definition shape
        let prod_str = format!("IFCPRODUCTDEFINITIONSHAPE($,$,({}))", shape_rep);
        self.add_entity(prod_str)
    }

    /// Add IfcLightFixture occurrence with geometry
    ///
    /// # Arguments
    /// * `name` - Fixture name
    /// * `owner_history` - Owner history reference
    /// * `storey` - Building storey reference
    /// * `fixture_type` - Optional fixture type reference
    /// * `representation_map` - Optional shared representation map. If provided, uses IFCMAPPEDITEM.
    ///   If None, creates inline geometry from mesh_data or default box.
    /// * `mesh_data` - Optional L3D mesh data for inline tessellated geometry (used when no rep map).
    pub fn add_light_fixture_with_map(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        storey: EntityRef,
        fixture_type: Option<EntityRef>,
        representation_map: Option<EntityRef>,
        mesh_data: Option<&MeshData>,
    ) -> EntityRef {
        // Create placement entities
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);
        let placement = self.add_local_placement(None, axis2_placement);

        // Create geometry - use mapped item if rep map provided, otherwise inline geometry
        let geometry = if let Some(rep_map) = representation_map {
            self.create_mapped_item_geometry(rep_map)
        } else if let Some(mesh) = mesh_data {
            if !mesh.vertices.is_empty() && !mesh.triangles.is_empty() {
                self.create_tessellated_geometry(mesh)
            } else {
                self.create_fixture_geometry(0.3, 0.3, 0.1)
            }
        } else {
            self.create_fixture_geometry(0.3, 0.3, 0.1)
        };

        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let fixture_str = format!(
            "IFCLIGHTFIXTURE('{}',{},'{}','',$,{},{},$,.NOTDEFINED.)",
            guid, owner_history, escaped_name, placement, geometry
        );
        let fixture = self.add_entity(fixture_str);

        // Assign to spatial container (storey)
        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELCONTAINEDINSPATIALSTRUCTURE('{}',{},$,$,({}),{})",
            guid2, owner_history, fixture, storey
        );
        self.add_entity(rel_str);

        // Assign type if provided
        if let Some(ft) = fixture_type {
            let guid3 = self.generate_guid();
            let rel_type_str = format!(
                "IFCRELDEFINESBYTYPE('{}',{},$,$,({}),{})",
                guid3, owner_history, fixture, ft
            );
            self.add_entity(rel_type_str);
        }

        // Associate any accumulated light sources with this fixture
        if !self.light_source_ids.is_empty() {
            let guid4 = self.generate_guid();
            let sources_list = self
                .light_source_ids
                .iter()
                .map(|id| id.to_string())
                .collect::<Vec<_>>()
                .join(",");
            let rel_group_str = format!(
                "IFCRELASSIGNSTOGROUP('{}',{},'LightSources','Light sources for fixture',$,({}),{})",
                guid4, owner_history, sources_list, fixture
            );
            self.add_entity(rel_group_str);
        }

        fixture
    }

    /// Create geometry using IFCMAPPEDITEM referencing a representation map
    fn create_mapped_item_geometry(&mut self, rep_map: EntityRef) -> EntityRef {
        let context_3d = self.entity_map.get("context_3d").copied();

        // Create identity transform for the mapped item
        // IFCCARTESIANTRANSFORMATIONOPERATOR3D(Axis1, Axis2, LocalOrigin, Scale, Axis3)
        // LocalOrigin must be IFCCARTESIANPOINT, not IFCAXIS2PLACEMENT3D
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let transform = self.add_entity(format!(
            "IFCCARTESIANTRANSFORMATIONOPERATOR3D($,$,{},1.,$)",
            origin
        ));

        // Create the mapped item
        let mapped_item = self.add_entity(format!("IFCMAPPEDITEM({},{})", rep_map, transform));

        // Shape representation using the mapped item
        let context_str = context_3d
            .map(|c| c.to_string())
            .unwrap_or_else(|| "$".to_string());
        let shape_str = format!(
            "IFCSHAPEREPRESENTATION({},'Body','MappedRepresentation',({}))",
            context_str, mapped_item
        );
        let shape_rep = self.add_entity(shape_str);

        // Product definition shape
        let prod_str = format!("IFCPRODUCTDEFINITIONSHAPE($,$,({}))", shape_rep);
        self.add_entity(prod_str)
    }

    /// Add IfcLightFixture occurrence with geometry and connected light sources
    ///
    /// # Arguments
    /// * `name` - Fixture name
    /// * `owner_history` - Owner history reference
    /// * `storey` - Building storey reference
    /// * `fixture_type` - Optional fixture type reference
    /// * `mesh_data` - Optional L3D mesh data for tessellated geometry.
    ///   If None, creates a default 0.3m x 0.3m x 0.1m box.
    pub fn add_light_fixture(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        storey: EntityRef,
        fixture_type: Option<EntityRef>,
        mesh_data: Option<&MeshData>,
    ) -> EntityRef {
        // Create placement entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis2_placement = self.add_axis2_placement_3d(origin, None, None);
        let placement = self.add_local_placement(None, axis2_placement);

        // Create geometry - use L3D mesh if available, otherwise fallback to box
        let geometry = if let Some(mesh) = mesh_data {
            if !mesh.vertices.is_empty() && !mesh.triangles.is_empty() {
                self.create_tessellated_geometry(mesh)
            } else {
                // Empty mesh, use default box
                self.create_fixture_geometry(0.3, 0.3, 0.1)
            }
        } else {
            // No mesh data, use default 0.3m x 0.3m x 0.1m box
            self.create_fixture_geometry(0.3, 0.3, 0.1)
        };

        let guid = self.generate_guid();
        let escaped_name = Self::escape_string(name);
        let fixture_str = format!(
            "IFCLIGHTFIXTURE('{}',{},'{}','',$,{},{},$,.NOTDEFINED.)",
            guid, owner_history, escaped_name, placement, geometry
        );
        let fixture = self.add_entity(fixture_str);

        // Assign to spatial container (storey)
        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELCONTAINEDINSPATIALSTRUCTURE('{}',{},$,$,({}),{})",
            guid2, owner_history, fixture, storey
        );
        self.add_entity(rel_str);

        // Assign type if provided
        if let Some(ft) = fixture_type {
            let guid3 = self.generate_guid();
            let type_str = format!(
                "IFCRELDEFINESBYTYPE('{}',{},$,$,({}),{})",
                guid3, owner_history, fixture, ft
            );
            self.add_entity(type_str);
        }

        // Connect light sources to fixture (Issue 2 fix)
        if !self.light_source_ids.is_empty() {
            let source_refs: Vec<String> = self
                .light_source_ids
                .iter()
                .map(|s| s.to_string())
                .collect();
            let guid4 = self.generate_guid();
            let group_str = format!(
                "IFCRELASSIGNSTOGROUP('{}',{},'LightSources','Light sources for fixture',$,({}),{})",
                guid4, owner_history, source_refs.join(","), fixture
            );
            self.add_entity(group_str);
        }

        fixture
    }

    /// Add manufacturer property set
    fn add_manufacturer_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        manufacturer: &str,
        model_reference: &str,
    ) {
        let mfr_str = format!(
            "IFCPROPERTYSINGLEVALUE('Manufacturer',$,IFCLABEL('{}'),$)",
            Self::escape_string(manufacturer)
        );
        let mfr_value = self.add_entity(mfr_str);

        let model_str = format!(
            "IFCPROPERTYSINGLEVALUE('ModelReference',$,IFCLABEL('{}'),$)",
            Self::escape_string(model_reference)
        );
        let model_value = self.add_entity(model_str);

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_ManufacturerTypeInformation',$,({},{}))",
            guid, owner_history, mfr_value, model_value
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);
    }

    // =========================================================================
    // Photometric data entities
    // =========================================================================

    /// Add IfcExternalReference for LDT/IES photometric file
    pub fn add_external_reference(
        &mut self,
        location: &str,
        identification: Option<&str>,
        name: &str,
    ) -> EntityRef {
        let ident = identification
            .map(|s| format!("'{}'", Self::escape_string(s)))
            .unwrap_or_else(|| "$".to_string());

        let ref_str = format!(
            "IFCEXTERNALREFERENCE('{}',{},'{}')",
            Self::escape_string(location),
            ident,
            Self::escape_string(name)
        );
        self.add_entity(ref_str)
    }

    /// Add IfcLightSourceGoniometric
    pub fn add_light_source_goniometric(
        &mut self,
        name: &str,
        colour_appearance: Option<(f64, f64, f64)>,
        colour_temperature: f64,
        luminous_flux: f64,
        emission_source: LightEmissionSourceEnum,
        photometry_file: Option<&str>,
    ) -> EntityRef {
        // Position at origin - create entities separately (Issue 1 fix)
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis = self.add_entity("IFCDIRECTION((0.,0.,-1.))".to_string()); // Light points down
        let ref_dir = self.add_entity("IFCDIRECTION((1.,0.,0.))".to_string());
        let position = self.add_axis2_placement_3d(origin, Some(axis), Some(ref_dir));

        // Colour appearance (IfcColourRgb)
        let colour_ref = if let Some((r, g, b)) = colour_appearance {
            let colour_str = format!("IFCCOLOURRGB($,{:.4},{:.4},{:.4})", r, g, b);
            let colour = self.add_entity(colour_str);
            OptionalRef::Some(colour)
        } else {
            OptionalRef::None
        };

        // External photometric reference
        let distribution_ref = if let Some(file_path) = photometry_file {
            let escaped_name = Self::escape_string(name);
            let ext_str = format!(
                "IFCEXTERNALREFERENCE('{}',$,'{}')",
                Self::escape_string(file_path),
                escaped_name
            );
            let ext_ref = self.add_entity(ext_str);
            OptionalRef::Some(ext_ref)
        } else {
            OptionalRef::None
        };

        // IfcLightSourceGoniometric
        let escaped_name = Self::escape_string(name);
        let ls_str = format!(
            "IFCLIGHTSOURCEGONIOMETRIC('{}',{},0.,1.,{},{},{:.1},{:.1},{},{})",
            escaped_name,
            colour_ref,
            position,
            colour_ref,
            colour_temperature,
            luminous_flux,
            emission_source.to_step(),
            distribution_ref
        );
        let light_source = self.add_entity(ls_str);

        // Track for later connection to fixture (Issue 2 fix)
        self.light_source_ids.push(light_source);

        light_source
    }

    /// Add IfcLightSourceGoniometric with embedded distribution data
    ///
    /// Instead of using an external file reference, this embeds the photometric
    /// distribution directly in the IFC using IFCLIGHTINTENSITYDISTRIBUTION.
    pub fn add_light_source_goniometric_with_distribution(
        &mut self,
        name: &str,
        colour_appearance: Option<(f64, f64, f64)>,
        colour_temperature: f64,
        luminous_flux: f64,
        emission_source: LightEmissionSourceEnum,
        distribution_data: &[(f64, f64, f64)],
    ) -> EntityRef {
        // Position at origin
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis = self.add_entity("IFCDIRECTION((0.,0.,-1.))".to_string());
        let ref_dir = self.add_entity("IFCDIRECTION((1.,0.,0.))".to_string());
        let position = self.add_axis2_placement_3d(origin, Some(axis), Some(ref_dir));

        // Colour appearance
        let colour_ref = if let Some((r, g, b)) = colour_appearance {
            let colour_str = format!("IFCCOLOURRGB($,{:.4},{:.4},{:.4})", r, g, b);
            let colour = self.add_entity(colour_str);
            OptionalRef::Some(colour)
        } else {
            OptionalRef::None
        };

        // Create embedded light intensity distribution
        let distribution_ref = if !distribution_data.is_empty() {
            let dist = self.add_light_intensity_distribution("TYPE_C", distribution_data);
            OptionalRef::Some(dist)
        } else {
            OptionalRef::None
        };

        // IfcLightSourceGoniometric
        let escaped_name = Self::escape_string(name);
        let ls_str = format!(
            "IFCLIGHTSOURCEGONIOMETRIC('{}',{},0.,1.,{},{},{:.1},{:.1},{},{})",
            escaped_name,
            colour_ref,
            position,
            colour_ref,
            colour_temperature,
            luminous_flux,
            emission_source.to_step(),
            distribution_ref
        );
        let light_source = self.add_entity(ls_str);

        // Track for later connection to fixture
        self.light_source_ids.push(light_source);

        light_source
    }

    /// Add IfcLightSourceGoniometric with a pre-existing distribution reference
    ///
    /// This allows sharing the same IFCLIGHTINTENSITYDISTRIBUTION across multiple light sources.
    pub fn add_light_source_goniometric_with_distribution_ref(
        &mut self,
        name: &str,
        colour_appearance: Option<(f64, f64, f64)>,
        colour_temperature: f64,
        luminous_flux: f64,
        emission_source: LightEmissionSourceEnum,
        distribution_ref: EntityRef,
    ) -> EntityRef {
        // Position at origin
        let origin = self.add_entity("IFCCARTESIANPOINT((0.,0.,0.))".to_string());
        let axis = self.add_entity("IFCDIRECTION((0.,0.,-1.))".to_string());
        let ref_dir = self.add_entity("IFCDIRECTION((1.,0.,0.))".to_string());
        let position = self.add_axis2_placement_3d(origin, Some(axis), Some(ref_dir));

        // Colour appearance
        let colour_ref = if let Some((r, g, b)) = colour_appearance {
            let colour_str = format!("IFCCOLOURRGB($,{:.4},{:.4},{:.4})", r, g, b);
            let colour = self.add_entity(colour_str);
            OptionalRef::Some(colour)
        } else {
            OptionalRef::None
        };

        // IfcLightSourceGoniometric with shared distribution reference
        let escaped_name = Self::escape_string(name);
        let ls_str = format!(
            "IFCLIGHTSOURCEGONIOMETRIC('{}',{},0.,1.,{},{},{:.1},{:.1},{},{})",
            escaped_name,
            colour_ref,
            position,
            colour_ref,
            colour_temperature,
            luminous_flux,
            emission_source.to_step(),
            distribution_ref
        );
        let light_source = self.add_entity(ls_str);

        // Track for later connection to fixture
        self.light_source_ids.push(light_source);

        light_source
    }

    /// Add IfcLightIntensityDistribution for embedded photometric data
    ///
    /// Distribution data format: Vec<(main_angle, secondary_angle, intensity)>
    /// This is grouped by main angle to create IFCLIGHTDISTRIBUTIONDATA entries
    pub fn add_light_intensity_distribution(
        &mut self,
        distribution_type: &str,
        distribution_data: &[(f64, f64, f64)],
    ) -> EntityRef {
        // Group data by main angle (C-plane)
        // IFC format: IFCLIGHTDISTRIBUTIONDATA(MainPlaneAngle, (SecondaryAngles...), (Intensities...))
        let mut grouped: std::collections::BTreeMap<i64, Vec<(f64, f64)>> =
            std::collections::BTreeMap::new();
        for &(main, secondary, intensity) in distribution_data {
            // Use integer key to group by main angle (avoid float comparison issues)
            let key = (main * 100.0) as i64;
            grouped.entry(key).or_default().push((secondary, intensity));
        }

        // Create distribution data entries for each C-plane
        let mut data_entries = Vec::new();
        for (main_key, values) in grouped {
            let main_angle = main_key as f64 / 100.0;

            // Sort by secondary angle (gamma)
            let mut sorted_values = values;
            sorted_values
                .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

            // Build lists for secondary angles and intensities
            let secondary_list: Vec<String> = sorted_values
                .iter()
                .map(|(s, _)| format!("{:.2}", s))
                .collect();
            let intensity_list: Vec<String> = sorted_values
                .iter()
                .map(|(_, i)| format!("{:.4}", i))
                .collect();

            let entry_str = format!(
                "IFCLIGHTDISTRIBUTIONDATA({:.2},({}),({}))",
                main_angle,
                secondary_list.join(","),
                intensity_list.join(",")
            );
            let entry = self.add_entity(entry_str);
            data_entries.push(entry.to_string());
        }

        let data_list = if data_entries.is_empty() {
            "$".to_string()
        } else {
            format!("({})", data_entries.join(","))
        };

        let dist_str = format!(
            "IFCLIGHTINTENSITYDISTRIBUTION(.{}.,{})",
            distribution_type, data_list
        );
        self.add_entity(dist_str)
    }

    // =========================================================================
    // Property sets for light fixtures
    // =========================================================================

    /// Add Pset_LightFixtureTypeCommon property set
    pub fn add_light_fixture_common_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        number_of_sources: Option<i32>,
        total_wattage: Option<f64>,
        light_fixture_mounting_type: Option<&str>,
        light_fixture_placement_type: Option<&str>,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(n) = number_of_sources {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('NumberOfSources',$,IFCINTEGER({}),$)",
                n
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(w) = total_wattage {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('TotalWattage',$,IFCPOWERMEASURE({:.2}),$)",
                w
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(mt) = light_fixture_mounting_type {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('LightFixtureMountingType',$,IFCLABEL('{}'),$)",
                Self::escape_string(mt)
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(pt) = light_fixture_placement_type {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('LightFixturePlacementType',$,IFCLABEL('{}'),$)",
                Self::escape_string(pt)
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_LightFixtureTypeCommon',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add electrical properties property set
    pub fn add_electrical_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        rated_voltage: Option<f64>,
        rated_current: Option<f64>,
        power_factor: Option<f64>,
        ip_code: Option<&str>,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(v) = rated_voltage {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('RatedVoltage',$,IFCELECTRICVOLTAGEMEASURE({:.1}),$)",
                v
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(i) = rated_current {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('RatedCurrent',$,IFCELECTRICCURRENTMEASURE({:.3}),$)",
                i
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(pf) = power_factor {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('PowerFactor',$,IFCNORMALISEDRATIOMEASURE({:.3}),$)",
                pf
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if let Some(ip) = ip_code {
            // Issue 5 fix: Don't double-prefix with IP
            let normalized_ip = ip.trim_start_matches("IP").trim_start_matches("ip");
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('IPCode',$,IFCLABEL('IP{}'),$)",
                Self::escape_string(normalized_ip)
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_ElectricalDeviceCommon',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add comprehensive electrical device properties (Pset_ElectricalDeviceCommon)
    ///
    /// Full support for Electrical Information Exchange MVD (SPARKIE).
    /// See: <https://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/Pset_ElectricalDeviceCommon.htm>
    pub fn add_electrical_device_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        props: &ElectricalDeviceProperties,
    ) -> EntityRef {
        let mut properties = Vec::new();

        // RatedCurrent (IfcElectricCurrentMeasure)
        if let Some(current) = props.rated_current {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('RatedCurrent',$,IFCELECTRICCURRENTMEASURE({:.3}),$)",
                current
            ));
            properties.push(prop);
        }

        // RatedVoltage (IfcElectricVoltageMeasure) - can be bounded
        if let Some(voltage) = props.rated_voltage {
            if let Some(voltage_max) = props.rated_voltage_max {
                // Bounded value with min/max
                let prop = self.add_entity(format!(
                    "IFCPROPERTYBOUNDEDVALUE('RatedVoltage',$,IFCELECTRICVOLTAGEMEASURE({:.1}),IFCELECTRICVOLTAGEMEASURE({:.1}),$,$)",
                    voltage, voltage_max
                ));
                properties.push(prop);
            } else {
                let prop = self.add_entity(format!(
                    "IFCPROPERTYSINGLEVALUE('RatedVoltage',$,IFCELECTRICVOLTAGEMEASURE({:.1}),$)",
                    voltage
                ));
                properties.push(prop);
            }
        }

        // NominalFrequencyRange (IfcFrequencyMeasure) - bounded
        if let (Some(freq_min), Some(freq_max)) =
            (props.nominal_frequency_min, props.nominal_frequency_max)
        {
            let prop = self.add_entity(format!(
                "IFCPROPERTYBOUNDEDVALUE('NominalFrequencyRange',$,IFCFREQUENCYMEASURE({:.1}),IFCFREQUENCYMEASURE({:.1}),$,$)",
                freq_min, freq_max
            ));
            properties.push(prop);
        }

        // PowerFactor (IfcNormalisedRatioMeasure) - 0.0 to 1.0
        if let Some(pf) = props.power_factor {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('PowerFactor',$,IFCNORMALISEDRATIOMEASURE({:.3}),$)",
                pf.clamp(0.0, 1.0)
            ));
            properties.push(prop);
        }

        // ConductorFunction (PEnum_ConductorFunctionEnum)
        if let Some(ref cf) = props.conductor_function {
            let prop = self.add_entity(format!(
                "IFCPROPERTYENUMERATEDVALUE('ConductorFunction',$,(IFCLABEL('{}')),{})",
                cf.to_step().trim_matches('.'),
                "$" // No enum reference needed for single value
            ));
            properties.push(prop);
        }

        // NumberOfPoles (IfcCountMeasure)
        if let Some(poles) = props.number_of_poles {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('NumberOfPoles',$,IFCCOUNTMEASURE({}),$)",
                poles
            ));
            properties.push(prop);
        }

        // HasProtectiveEarth (IfcBoolean)
        if let Some(has_pe) = props.has_protective_earth {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('HasProtectiveEarth',$,IFCBOOLEAN({}),$)",
                if has_pe { ".T." } else { ".F." }
            ));
            properties.push(prop);
        }

        // InsulationStandardClass (PEnum_InsulationStandardClass)
        if let Some(ref isc) = props.insulation_standard_class {
            if *isc != InsulationStandardClass::NotDefined {
                let prop = self.add_entity(format!(
                    "IFCPROPERTYENUMERATEDVALUE('InsulationStandardClass',$,(IFCLABEL('{}')),{})",
                    isc.to_step().trim_matches('.'),
                    "$"
                ));
                properties.push(prop);
            }
        }

        // IP_Code (IfcLabel)
        if let Some(ref ip) = props.ip_code {
            // Normalize IP code - ensure it starts with "IP"
            let normalized_ip = if ip.to_uppercase().starts_with("IP") {
                ip.clone()
            } else {
                format!("IP{}", ip)
            };
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('IP_Code',$,IFCLABEL('{}'),$)",
                Self::escape_string(&normalized_ip)
            ));
            properties.push(prop);
        }

        // IK_Code (IfcLabel)
        if let Some(ref ik) = props.ik_code {
            // Normalize IK code - ensure it starts with "IK"
            let normalized_ik = if ik.to_uppercase().starts_with("IK") {
                ik.clone()
            } else {
                format!("IK{}", ik)
            };
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('IK_Code',$,IFCLABEL('{}'),$)",
                Self::escape_string(&normalized_ik)
            ));
            properties.push(prop);
        }

        // EarthingStyle (IfcLabel)
        if let Some(ref es) = props.earthing_style {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('EarthingStyle',$,IFCLABEL('{}'),$)",
                Self::escape_string(es)
            ));
            properties.push(prop);
        }

        // HeatDissipation (IfcPowerMeasure)
        if let Some(heat) = props.heat_dissipation {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('HeatDissipation',$,IFCPOWERMEASURE({:.2}),$)",
                heat
            ));
            properties.push(prop);
        }

        // Power (IfcPowerMeasure)
        if let Some(power) = props.power {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('Power',$,IFCPOWERMEASURE({:.2}),$)",
                power
            ));
            properties.push(prop);
        }

        // NominalPowerConsumption (IfcPowerMeasure)
        if let Some(npc) = props.nominal_power_consumption {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('NominalPowerConsumption',$,IFCPOWERMEASURE({:.2}),$)",
                npc
            ));
            properties.push(prop);
        }

        // NumberOfPowerSupplyPorts (IfcInteger)
        if let Some(ports) = props.number_of_power_supply_ports {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('NumberOfPowerSupplyPorts',$,IFCINTEGER({}),$)",
                ports
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset = self.add_entity(format!(
            "IFCPROPERTYSET('{}',{},'Pset_ElectricalDeviceCommon',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        ));

        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        ));

        pset
    }

    /// Add light fixture common properties (Pset_LightFixtureTypeCommon)
    ///
    /// Full support for Electrical Information Exchange MVD.
    pub fn add_light_fixture_common_pset_full(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        props: &LightFixtureCommonProperties,
    ) -> EntityRef {
        let mut properties = Vec::new();

        // Reference (IfcIdentifier)
        if let Some(ref reference) = props.reference {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('Reference',$,IFCIDENTIFIER('{}'),$)",
                Self::escape_string(reference)
            ));
            properties.push(prop);
        }

        // Status (PEnum_ElementStatus)
        if let Some(ref status) = props.status {
            let prop = self.add_entity(format!(
                "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('{}')),$)",
                Self::escape_string(status)
            ));
            properties.push(prop);
        }

        // NumberOfSources (IfcInteger)
        if let Some(num) = props.number_of_sources {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('NumberOfSources',$,IFCINTEGER({}),$)",
                num
            ));
            properties.push(prop);
        }

        // TotalWattage (IfcPowerMeasure)
        if let Some(wattage) = props.total_wattage {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('TotalWattage',$,IFCPOWERMEASURE({:.2}),$)",
                wattage
            ));
            properties.push(prop);
        }

        // LightFixtureMountingType (PEnum_LightFixtureMountingType)
        if let Some(ref mounting) = props.mounting_type {
            let prop = self.add_entity(format!(
                "IFCPROPERTYENUMERATEDVALUE('LightFixtureMountingType',$,(IFCLABEL('{}')),$)",
                Self::escape_string(mounting)
            ));
            properties.push(prop);
        }

        // LightFixturePlacingType (PEnum_LightFixturePlacingType)
        if let Some(ref placing) = props.placing_type {
            let prop = self.add_entity(format!(
                "IFCPROPERTYENUMERATEDVALUE('LightFixturePlacingType',$,(IFCLABEL('{}')),$)",
                Self::escape_string(placing)
            ));
            properties.push(prop);
        }

        // MaintenanceFactor (IfcReal)
        if let Some(mf) = props.maintenance_factor {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('MaintenanceFactor',$,IFCREAL({:.3}),$)",
                mf
            ));
            properties.push(prop);
        }

        // MaximumPlenumSensibleLoad (IfcPowerMeasure)
        if let Some(load) = props.max_plenum_sensible_load {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('MaximumPlenumSensibleLoad',$,IFCPOWERMEASURE({:.2}),$)",
                load
            ));
            properties.push(prop);
        }

        // MaximumSpaceSensibleLoad (IfcPowerMeasure)
        if let Some(load) = props.max_space_sensible_load {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('MaximumSpaceSensibleLoad',$,IFCPOWERMEASURE({:.2}),$)",
                load
            ));
            properties.push(prop);
        }

        // SensibleLoadToRadiant (IfcPositiveRatioMeasure)
        if let Some(ratio) = props.sensible_load_to_radiant {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('SensibleLoadToRadiant',$,IFCPOSITIVERATIOMEASURE({:.3}),$)",
                ratio
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset = self.add_entity(format!(
            "IFCPROPERTYSET('{}',{},'Pset_LightFixtureTypeCommon',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        ));

        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        ));

        pset
    }

    /// Add manufacturer type information (Pset_ManufacturerTypeInformation) - COBie
    pub fn add_manufacturer_info_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        info: &ManufacturerTypeInfo,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(ref gtin) = info.global_trade_item_number {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('GlobalTradeItemNumber',$,IFCIDENTIFIER('{}'),$)",
                Self::escape_string(gtin)
            ));
            properties.push(prop);
        }

        if let Some(ref article) = info.article_number {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ArticleNumber',$,IFCIDENTIFIER('{}'),$)",
                Self::escape_string(article)
            ));
            properties.push(prop);
        }

        if let Some(ref model_ref) = info.model_reference {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ModelReference',$,IFCLABEL('{}'),$)",
                Self::escape_string(model_ref)
            ));
            properties.push(prop);
        }

        if let Some(ref model_label) = info.model_label {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ModelLabel',$,IFCLABEL('{}'),$)",
                Self::escape_string(model_label)
            ));
            properties.push(prop);
        }

        if let Some(ref mfr) = info.manufacturer {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('Manufacturer',$,IFCLABEL('{}'),$)",
                Self::escape_string(mfr)
            ));
            properties.push(prop);
        }

        if let Some(year) = info.production_year {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ProductionYear',$,IFCINTEGER({}),$)",
                year
            ));
            properties.push(prop);
        }

        if let Some(ref place) = info.assembly_place {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('AssemblyPlace',$,IFCLABEL('{}'),$)",
                Self::escape_string(place)
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset = self.add_entity(format!(
            "IFCPROPERTYSET('{}',{},'Pset_ManufacturerTypeInformation',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        ));

        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        ));

        pset
    }

    /// Add warranty information (Pset_Warranty) - COBie
    pub fn add_warranty_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        warranty: &WarrantyInfo,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(ref id) = warranty.warranty_identifier {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('WarrantyIdentifier',$,IFCIDENTIFIER('{}'),$)",
                Self::escape_string(id)
            ));
            properties.push(prop);
        }

        if let Some(ref start) = warranty.warranty_start_date {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('WarrantyStartDate',$,IFCDATE('{}'),$)",
                Self::escape_string(start)
            ));
            properties.push(prop);
        }

        if let Some(ref end) = warranty.warranty_end_date {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('WarrantyEndDate',$,IFCDATE('{}'),$)",
                Self::escape_string(end)
            ));
            properties.push(prop);
        }

        if let Some(period) = warranty.warranty_period {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('WarrantyPeriod',$,IFCREAL({:.1}),$)",
                period
            ));
            properties.push(prop);
        }

        if let Some(ref contact) = warranty.point_of_contact {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('PointOfContact',$,IFCLABEL('{}'),$)",
                Self::escape_string(contact)
            ));
            properties.push(prop);
        }

        if let Some(ref terms) = warranty.terms_and_conditions {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('TermsAndConditions',$,IFCTEXT('{}'),$)",
                Self::escape_string(terms)
            ));
            properties.push(prop);
        }

        if let Some(ref exclusions) = warranty.exclusions {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('Exclusions',$,IFCTEXT('{}'),$)",
                Self::escape_string(exclusions)
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset = self.add_entity(format!(
            "IFCPROPERTYSET('{}',{},'Pset_Warranty',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        ));

        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        ));

        pset
    }

    /// Add service life information (Pset_ServiceLife) - COBie
    pub fn add_service_life_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        service_life: &ServiceLifeInfo,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(duration) = service_life.service_life_duration {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ServiceLifeDuration',$,IFCREAL({:.1}),$)",
                duration
            ));
            properties.push(prop);
        }

        if let Some(ref sl_type) = service_life.service_life_type {
            let prop = self.add_entity(format!(
                "IFCPROPERTYENUMERATEDVALUE('ServiceLifeType',$,(IFCLABEL('{}')),$)",
                Self::escape_string(sl_type)
            ));
            properties.push(prop);
        }

        if let Some(mtbf) = service_life.mean_time_between_failure {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('MeanTimeBetweenFailure',$,IFCREAL({:.0}),$)",
                mtbf
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset = self.add_entity(format!(
            "IFCPROPERTYSET('{}',{},'Pset_ServiceLife',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        ));

        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        ));

        pset
    }

    /// Add a distribution port for electrical connection
    ///
    /// Creates an IfcDistributionPort to represent a power connection point on a light fixture.
    /// This enables circuit connectivity in Electrical MVD.
    pub fn add_distribution_port(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        parent_element: EntityRef,
        flow_direction: FlowDirectionEnum,
        system_type: DistributionSystemEnum,
    ) -> EntityRef {
        let guid = self.generate_guid();

        // Create the port
        let port = self.add_entity(format!(
            "IFCDISTRIBUTIONPORT('{}',{},'{}','Electrical connection port',$,$,{},{})",
            guid,
            owner_history,
            Self::escape_string(name),
            flow_direction.to_step(),
            system_type.to_step()
        ));

        // Nest the port within the parent element
        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELNESTS('{}',{},$,$,{},({}))",
            guid2, owner_history, parent_element, port
        ));

        port
    }

    /// Add a lighting distribution system
    ///
    /// Creates an IfcDistributionSystem to group light fixtures into a lighting circuit.
    pub fn add_lighting_system(
        &mut self,
        name: &str,
        owner_history: EntityRef,
        building: EntityRef,
    ) -> EntityRef {
        let guid = self.generate_guid();

        let system = self.add_entity(format!(
            "IFCDISTRIBUTIONSYSTEM('{}',{},'{}','Lighting distribution system',$,.LIGHTING.)",
            guid,
            owner_history,
            Self::escape_string(name)
        ));

        // Reference the system in the building
        let guid2 = self.generate_guid();
        self.add_entity(format!(
            "IFCRELSERVICESBUILDINGS('{}',{},$,$,{},({}))",
            guid2, owner_history, system, building
        ));

        system
    }

    /// Assign elements to a distribution system (e.g., light fixtures to lighting circuit)
    pub fn assign_to_system(
        &mut self,
        owner_history: EntityRef,
        system: EntityRef,
        elements: &[EntityRef],
    ) {
        if elements.is_empty() {
            return;
        }

        let guid = self.generate_guid();
        let element_list: Vec<String> = elements.iter().map(|e| e.to_string()).collect();

        self.add_entity(format!(
            "IFCRELASSIGNSTOGROUP('{}',{},$,$,({}),$.PRODUCT.,{})",
            guid,
            owner_history,
            element_list.join(","),
            system
        ));
    }

    /// Add variant-specific property set (Pset_LuminaireVariant)
    #[allow(clippy::too_many_arguments)]
    pub fn add_variant_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        variant_id: &str,
        cct: Option<i32>,
        luminous_flux: Option<f64>,
        power: Option<f64>,
        cri: Option<i32>,
        weight: Option<f64>,
    ) -> EntityRef {
        let mut properties = Vec::new();

        // GLDF Variant ID (for traceability)
        let variant_prop_str = format!(
            "IFCPROPERTYSINGLEVALUE('GLDF_VariantId',$,IFCLABEL('{}'),$)",
            Self::escape_string(variant_id)
        );
        let variant_prop = self.add_entity(variant_prop_str);
        properties.push(variant_prop);

        // Color Temperature
        if let Some(temp) = cct {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('ColorTemperature',$,IFCTHERMODYNAMICTEMPERATUREMEASURE({:.0}),$)",
                temp as f64
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        // Luminous Flux
        if let Some(flux) = luminous_flux {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('LuminousFlux',$,IFCLUMINOUSFLUXMEASURE({:.1}),$)",
                flux
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        // Power
        if let Some(p) = power {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('Power',$,IFCPOWERMEASURE({:.2}),$)",
                p
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        // Efficacy (calculated)
        if let (Some(flux), Some(p)) = (luminous_flux, power) {
            if p > 0.0 {
                let efficacy = flux / p;
                let prop_str = format!(
                    "IFCPROPERTYSINGLEVALUE('Efficacy',$,IFCREAL({:.1}),$)",
                    efficacy
                );
                let prop = self.add_entity(prop_str);
                properties.push(prop);
            }
        }

        // Color Rendering Index
        if let Some(index) = cri {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('ColorRenderingIndex',$,IFCINTEGER({}),$)",
                index
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        // Weight
        if let Some(w) = weight {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('Weight',$,IFCMASSMEASURE({:.3}),$)",
                w
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_LuminaireVariant',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Clear light source tracking for a new fixture type
    pub fn clear_light_sources(&mut self) {
        self.light_source_ids.clear();
    }

    /// Add photometry filenames property set (for roundtrip preservation)
    ///
    /// Stores original LDT filenames so they can be restored after IFC import.
    pub fn add_photometry_filenames_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        filenames: &[String],
    ) -> EntityRef {
        let mut properties = Vec::new();

        // Store each filename as a separate property
        for (i, filename) in filenames.iter().enumerate() {
            let prop_str = format!(
                "IFCPROPERTYSINGLEVALUE('PhotometryFile_{}',$,IFCLABEL('{}'),$)",
                i + 1,
                Self::escape_string(filename)
            );
            let prop = self.add_entity(prop_str);
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_GLDF_PhotometryFiles',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add GLDF descriptive attributes property set
    ///
    /// Stores GLDF-specific descriptive attributes for roundtrip preservation:
    /// - ElectricalSafetyClass
    /// - MedianUsefulLife
    /// - MountingType (Ceiling/Wall/etc.)
    /// - RecessedDepth
    pub fn add_gldf_descriptive_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        safety_class: Option<&str>,
        median_useful_life: Option<&str>,
        mounting_type: Option<&str>,
        recessed_depth: Option<f64>,
    ) -> EntityRef {
        let mut properties = Vec::new();

        if let Some(sc) = safety_class {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('ElectricalSafetyClass',$,IFCLABEL('{}'),$)",
                Self::escape_string(sc)
            ));
            properties.push(prop);
        }

        if let Some(mul) = median_useful_life {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('MedianUsefulLife',$,IFCLABEL('{}'),$)",
                Self::escape_string(mul)
            ));
            properties.push(prop);
        }

        if let Some(mt) = mounting_type {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('GLDF_MountingType',$,IFCLABEL('{}'),$)",
                Self::escape_string(mt)
            ));
            properties.push(prop);
        }

        if let Some(rd) = recessed_depth {
            let prop = self.add_entity(format!(
                "IFCPROPERTYSINGLEVALUE('GLDF_RecessedDepth',$,IFCLENGTHMEASURE({:.1}),$)",
                rd
            ));
            properties.push(prop);
        }

        if properties.is_empty() {
            return EntityRef::new(0);
        }

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_GLDF_DescriptiveAttributes',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add raw LDT file content as base64 for exact roundtrip preservation
    ///
    /// This stores the complete LDT file so it can be restored exactly.
    pub fn add_ldt_raw_content_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        index: usize,
        filename: &str,
        raw_content: &str,
    ) -> EntityRef {
        use base64::{engine::general_purpose::STANDARD, Engine};

        let encoded = STANDARD.encode(raw_content.as_bytes());

        let filename_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Filename',$,IFCLABEL('{}'),$)",
            index,
            Self::escape_string(filename)
        ));

        let content_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Content',$,IFCLABEL('{}'),$)",
            index,
            Self::escape_string(&encoded)
        ));

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_GLDF_LDTRawContent',$,({},{}))",
            guid, owner_history, filename_prop, content_prop
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add LDT metadata property set (for roundtrip preservation)
    ///
    /// Stores original LDT file metadata (symmetry, Mc, Ng, Dc, Dg, DR values, etc.)
    /// so they can be restored after IFC import.
    #[allow(clippy::too_many_arguments)]
    pub fn add_ldt_metadata_pset(
        &mut self,
        element: EntityRef,
        owner_history: EntityRef,
        index: usize,
        symmetry: i32,
        num_c_planes: i32,
        num_g_angles: i32,
        dc: f64,
        dg: f64,
        total_flux: f64,
        dr: &[f64; 10],
        luminaire_name: &str,
    ) -> EntityRef {
        let mut properties = Vec::new();

        // LDT index (for matching with photometry files)
        let idx_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Index',$,IFCINTEGER({}),$)",
            index, index
        ));
        properties.push(idx_prop);

        // Symmetry
        let sym_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Symmetry',$,IFCINTEGER({}),$)",
            index, symmetry
        ));
        properties.push(sym_prop);

        // Number of C-planes
        let mc_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_NumCPlanes',$,IFCINTEGER({}),$)",
            index, num_c_planes
        ));
        properties.push(mc_prop);

        // Number of gamma angles
        let ng_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_NumGAngles',$,IFCINTEGER({}),$)",
            index, num_g_angles
        ));
        properties.push(ng_prop);

        // C-plane distance (Dc)
        let dc_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Dc',$,IFCREAL({:.2}),$)",
            index, dc
        ));
        properties.push(dc_prop);

        // Gamma distance (Dg)
        let dg_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_Dg',$,IFCREAL({:.2}),$)",
            index, dg
        ));
        properties.push(dg_prop);

        // Total flux
        let flux_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_TotalFlux',$,IFCLUMINOUSFLUXMEASURE({:.1}),$)",
            index, total_flux
        ));
        properties.push(flux_prop);

        // DR values (as comma-separated string)
        let dr_str = dr
            .iter()
            .map(|v| format!("{:.5}", v))
            .collect::<Vec<_>>()
            .join(",");
        let dr_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_DR',$,IFCLABEL('{}'),$)",
            index,
            Self::escape_string(&dr_str)
        ));
        properties.push(dr_prop);

        // Luminaire name
        let name_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('LDT_{}_LuminaireName',$,IFCLABEL('{}'),$)",
            index,
            Self::escape_string(luminaire_name)
        ));
        properties.push(name_prop);

        let prop_list: Vec<String> = properties.iter().map(|p| p.to_string()).collect();

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_GLDF_LDTMetadata',$,({}))",
            guid,
            owner_history,
            prop_list.join(",")
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, element, pset
        );
        self.add_entity(rel_str);

        pset
    }

    /// Add a generic GLDF file as base64-encoded property set
    ///
    /// Used for preserving product images, sensor files, etc. through IFC roundtrip.
    pub fn add_gldf_file_pset(
        &mut self,
        project: EntityRef,
        owner_history: EntityRef,
        file_type: &str, // "image", "sensor", etc.
        filename: &str,
        content_type: &str, // "image/png", "sensor/sensldt", etc.
        content: &[u8],
    ) -> EntityRef {
        use base64::{engine::general_purpose::STANDARD, Engine};

        let encoded = STANDARD.encode(content);

        let type_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('GLDF_FileType',$,IFCLABEL('{}'),$)",
            Self::escape_string(file_type)
        ));

        let filename_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('GLDF_Filename',$,IFCLABEL('{}'),$)",
            Self::escape_string(filename)
        ));

        let content_type_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('GLDF_ContentType',$,IFCLABEL('{}'),$)",
            Self::escape_string(content_type)
        ));

        let content_prop = self.add_entity(format!(
            "IFCPROPERTYSINGLEVALUE('GLDF_FileContent',$,IFCLABEL('{}'),$)",
            Self::escape_string(&encoded)
        ));

        let guid = self.generate_guid();
        let pset_str = format!(
            "IFCPROPERTYSET('{}',{},'Pset_GLDF_EmbeddedFile',$,({},{},{},{}))",
            guid, owner_history, type_prop, filename_prop, content_type_prop, content_prop
        );
        let pset = self.add_entity(pset_str);

        let guid2 = self.generate_guid();
        let rel_str = format!(
            "IFCRELDEFINESBYPROPERTIES('{}',{},$,$,({}),{})",
            guid2, owner_history, project, pset
        );
        self.add_entity(rel_str);

        pset
    }

    // =========================================================================
    // Output
    // =========================================================================

    /// Get current Unix timestamp
    fn current_timestamp() -> i64 {
        #[cfg(not(target_arch = "wasm32"))]
        {
            use std::time::{SystemTime, UNIX_EPOCH};
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_secs() as i64)
                .unwrap_or(0)
        }

        #[cfg(target_arch = "wasm32")]
        {
            1704067200_i64
        }
    }

    /// Generate the complete IFC STEP file content
    pub fn to_step_string(&self) -> String {
        #[cfg(not(target_arch = "wasm32"))]
        let timestamp = {
            let now: DateTime<Utc> = Utc::now();
            now.format("%Y-%m-%dT%H:%M:%S").to_string()
        };

        #[cfg(target_arch = "wasm32")]
        let timestamp = "2024-01-01T00:00:00".to_string();

        let mut output = String::new();

        // HEADER section
        output.push_str("ISO-10303-21;\n");
        output.push_str("HEADER;\n");
        output.push_str("FILE_DESCRIPTION(('GLDF to IFC Export'),'2;1');\n");
        output.push_str(&format!(
            "FILE_NAME('export.ifc','{}',(''),(''),'gldf-rs','gldf-rs','');\n",
            timestamp
        ));
        output.push_str(&format!("FILE_SCHEMA(('{}'));\n", self.schema));
        output.push_str("ENDSEC;\n");
        output.push('\n');

        // DATA section
        output.push_str("DATA;\n");
        for entity in &self.entities {
            output.push_str(entity);
            output.push_str(";\n");
        }
        output.push_str("ENDSEC;\n");
        output.push('\n');

        // End
        output.push_str("END-ISO-10303-21;\n");

        output
    }
}

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

    #[test]
    fn test_minimal_ifc() {
        let mut writer = StepWriter::new("IFC4");
        let oh = writer.add_owner_history("Test Corp");
        let project = writer.add_project("Test Project", oh);
        let site = writer.add_site("Test Site", oh, project);
        let building = writer.add_building("Test Building", oh, site);
        let storey = writer.add_storey("Ground Floor", oh, building);

        let fixture_type = writer.add_light_fixture_type(
            "LED Downlight",
            "Test Corp",
            LightFixtureTypeEnum::PointSource,
            oh,
        );

        let _fixture =
            writer.add_light_fixture("LED Downlight 001", oh, storey, Some(fixture_type), None);

        let output = writer.to_step_string();

        // Verify structure
        assert!(output.contains("ISO-10303-21"));
        assert!(output.contains("IFC4"));
        assert!(output.contains("IFCPROJECT"));
        assert!(output.contains("IFCSITE"));
        assert!(output.contains("IFCBUILDING"));
        assert!(output.contains("IFCBUILDINGSTOREY"));
        assert!(output.contains("IFCLIGHTFIXTURETYPE"));
        assert!(output.contains("IFCLIGHTFIXTURE"));
        assert!(output.contains("Pset_ManufacturerTypeInformation"));

        // Verify no inline entities (Issue 1)
        assert!(!output.contains("IFCLOCALPLACEMENT($,IFCAXIS2PLACEMENT3D"));

        // When fixture_type is provided without mesh_data, instance geometry should be $
        // (inheriting from type), not a separate geometry definition
        // Note: The type itself doesn't have geometry via add_light_fixture_type() without rep map,
        // but the instance correctly uses $ for representation when type is provided

        println!("{}", output);
    }

    #[test]
    fn test_light_source_goniometric() {
        let mut writer = StepWriter::new("IFC4");
        let oh = writer.add_owner_history("Test Corp");
        let project = writer.add_project("Test Project", oh);
        let site = writer.add_site("Test Site", oh, project);
        let building = writer.add_building("Test Building", oh, site);
        let storey = writer.add_storey("Ground Floor", oh, building);

        let fixture_type = writer.add_light_fixture_type(
            "LED Panel",
            "Test Corp",
            LightFixtureTypeEnum::DirectionSource,
            oh,
        );

        // Add goniometric light source
        let _light_source = writer.add_light_source_goniometric(
            "LED Panel Light Source",
            Some((1.0, 0.95, 0.9)),
            3000.0,
            4500.0,
            LightEmissionSourceEnum::Led,
            Some("photometry/led_panel.ldt"),
        );

        // Add common property set
        writer.add_light_fixture_common_pset(
            fixture_type,
            oh,
            Some(1),
            Some(36.0),
            Some("SURFACE"),
            Some("CEILING"),
        );

        // Add electrical property set
        writer.add_electrical_pset(
            fixture_type,
            oh,
            Some(230.0),
            Some(0.16),
            Some(0.95),
            Some("IP20"),
        );

        let _fixture =
            writer.add_light_fixture("LED Panel 001", oh, storey, Some(fixture_type), None);

        let output = writer.to_step_string();

        // Verify goniometric light source
        assert!(output.contains("IFCLIGHTSOURCEGONIOMETRIC"));
        assert!(output.contains("led_panel.ldt"));
        assert!(output.contains("3000.0"));
        assert!(output.contains("4500.0"));
        assert!(output.contains(".LED."));

        // Verify property sets
        assert!(output.contains("Pset_LightFixtureTypeCommon"));
        assert!(output.contains("NumberOfSources"));
        assert!(output.contains("TotalWattage"));

        assert!(output.contains("Pset_ElectricalDeviceCommon"));
        assert!(output.contains("RatedVoltage"));
        assert!(output.contains("PowerFactor"));
        assert!(output.contains("IPCode"));

        // Verify IP code is correct (Issue 5)
        assert!(output.contains("IFCLABEL('IP20')"));
        assert!(!output.contains("IPIP20"));

        // Verify light source is connected (Issue 2)
        assert!(output.contains("IFCRELASSIGNSTOGROUP"));

        println!("{}", output);
    }

    #[test]
    fn test_embedded_light_distribution() {
        let mut writer = StepWriter::new("IFC4");

        let distribution_data: Vec<(f64, f64, f64)> = vec![
            (0.0, 0.0, 1.0),
            (0.0, 30.0, 0.866),
            (0.0, 60.0, 0.5),
            (0.0, 90.0, 0.0),
        ];

        let _dist = writer.add_light_intensity_distribution("TYPE_C", &distribution_data);

        let output = writer.to_step_string();
        assert!(output.contains("IFCLIGHTINTENSITYDISTRIBUTION"));
        assert!(output.contains("IFCLIGHTDISTRIBUTIONDATA"));
        assert!(output.contains(".TYPE_C."));
    }

    #[test]
    fn test_guid_format() {
        let mut writer = StepWriter::new("IFC4");

        for _ in 0..10 {
            let guid = writer.generate_guid();
            assert_eq!(
                guid.len(),
                22,
                "GUID should be exactly 22 characters: {}",
                guid
            );

            for c in guid.chars() {
                assert!(
                    c.is_ascii_alphanumeric() || c == '_' || c == '$',
                    "Invalid character in GUID: {} (full: {})",
                    c,
                    guid
                );
            }
        }
    }

    #[test]
    fn test_ip_code_normalization() {
        let mut writer = StepWriter::new("IFC4");
        let oh = writer.add_owner_history("Test");
        let project = writer.add_project("Test", oh);
        let site = writer.add_site("Site", oh, project);
        let building = writer.add_building("Building", oh, site);
        let _storey = writer.add_storey("Storey", oh, building);

        let fixture_type =
            writer.add_light_fixture_type("Test", "Test", LightFixtureTypeEnum::NotDefined, oh);

        // Test with "IP20" input
        writer.add_electrical_pset(fixture_type, oh, None, None, None, Some("IP20"));
        let output = writer.to_step_string();
        assert!(output.contains("IFCLABEL('IP20')"));
        assert!(!output.contains("IPIP"));

        // Test with "20" input (no prefix)
        let mut writer2 = StepWriter::new("IFC4");
        let oh2 = writer2.add_owner_history("Test");
        let project2 = writer2.add_project("Test", oh2);
        let site2 = writer2.add_site("Site", oh2, project2);
        let building2 = writer2.add_building("Building", oh2, site2);
        let _storey2 = writer2.add_storey("Storey", oh2, building2);
        let fixture_type2 =
            writer2.add_light_fixture_type("Test", "Test", LightFixtureTypeEnum::NotDefined, oh2);
        writer2.add_electrical_pset(fixture_type2, oh2, None, None, None, Some("20"));
        let output2 = writer2.to_step_string();
        assert!(output2.contains("IFCLABEL('IP20')"));
    }
}