blockpedia 0.1.0

A comprehensive Rust library for Minecraft block data with advanced color analysis and palette generation
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
use anyhow::{Context, Result};
use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;

// Import our data source types
mod data_sources_build {
    use anyhow::{Context, Result};
    use serde_json::Value;
    use std::collections::HashMap;

    /// Unified block data structure for build script
    #[derive(Debug, Clone)]
    pub struct UnifiedBlockData {
        pub id: String,
        pub properties: HashMap<String, Vec<String>>,
        pub default_state: HashMap<String, String>,
        #[allow(dead_code)] // Used for future extensions
        pub extra_properties: HashMap<String, Value>,
    }

    /// Trait for different data source adapters in build script
    pub trait DataSourceAdapter {
        fn name(&self) -> &'static str;
        fn fetch_url(&self) -> &'static str;
        fn parse_data(&self, json_data: &str) -> Result<Vec<UnifiedBlockData>>;
        fn validate_structure(&self, json: &Value) -> Result<()>;
    }

    /// PrismarineJS adapter for build script
    pub struct PrismarineAdapter;

    impl DataSourceAdapter for PrismarineAdapter {
        fn name(&self) -> &'static str {
            "PrismarineJS"
        }

        fn fetch_url(&self) -> &'static str {
            "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.20.4/blocks.json"
        }

        fn parse_data(&self, json_data: &str) -> Result<Vec<UnifiedBlockData>> {
            let parsed: Value =
                serde_json::from_str(json_data).context("Failed to parse PrismarineJS JSON")?;

            let blocks_array = parsed
                .as_array()
                .context("PrismarineJS JSON is not an array")?;

            let mut unified_blocks = Vec::new();

            for block in blocks_array {
                let block_obj = block.as_object().context("Block is not an object")?;

                let name = block_obj
                    .get("name")
                    .and_then(|n| n.as_str())
                    .context("Block missing name field")?;

                let id = format!("minecraft:{name}");

                // Convert states to properties
                let mut properties = HashMap::new();
                if let Some(states) = block_obj.get("states").and_then(|s| s.as_array()) {
                    for state in states {
                        if let Some(state_obj) = state.as_object() {
                            if let (Some(prop_name), Some(prop_type), Some(num_values)) = (
                                state_obj.get("name").and_then(|n| n.as_str()),
                                state_obj.get("type").and_then(|t| t.as_str()),
                                state_obj.get("num_values").and_then(|n| n.as_u64()),
                            ) {
                                let values = match prop_type {
                                    "bool" => vec!["false".to_string(), "true".to_string()],
                                    "int" => {
                                        if let Some(values_array) =
                                            state_obj.get("values").and_then(|v| v.as_array())
                                        {
                                            values_array
                                                .iter()
                                                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                                .collect()
                                        } else {
                                            (0..num_values).map(|i| i.to_string()).collect()
                                        }
                                    }
                                    "enum" => {
                                        if let Some(values_array) =
                                            state_obj.get("values").and_then(|v| v.as_array())
                                        {
                                            values_array
                                                .iter()
                                                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                                .collect()
                                        } else {
                                            (0..num_values).map(|i| format!("value_{i}")).collect()
                                        }
                                    }
                                    _ => vec!["unknown".to_string()],
                                };
                                properties.insert(prop_name.to_string(), values);
                            }
                        }
                    }
                }

                // Extract extra properties from original data
                let mut extra_properties = HashMap::new();
                if let Some(hardness) = block_obj.get("hardness") {
                    extra_properties.insert("hardness".to_string(), hardness.clone());
                }
                if let Some(resistance) = block_obj.get("resistance") {
                    extra_properties.insert("resistance".to_string(), resistance.clone());
                }

                unified_blocks.push(UnifiedBlockData {
                    id,
                    properties,
                    default_state: HashMap::new(), // PrismarineJS doesn't provide default states
                    extra_properties,
                });
            }

            Ok(unified_blocks)
        }

        fn validate_structure(&self, json: &Value) -> Result<()> {
            let blocks_array = json
                .as_array()
                .context("PrismarineJS JSON is not a valid array")?;

            if blocks_array.is_empty() {
                anyhow::bail!("No blocks found in PrismarineJS data");
            }

            // Validate a few sample blocks
            for (i, block_data) in blocks_array.iter().take(3).enumerate() {
                let block_obj = block_data
                    .as_object()
                    .with_context(|| format!("Block at index {i} is not an object"))?;

                if !block_obj.contains_key("name") {
                    anyhow::bail!("Block at index {} missing 'name' field", i);
                }
            }

            Ok(())
        }
    }

    /// MCPropertyEncyclopedia adapter for build script
    pub struct MCPropertyEncyclopediaAdapter;

    impl DataSourceAdapter for MCPropertyEncyclopediaAdapter {
        fn name(&self) -> &'static str {
            "MCPropertyEncyclopedia"
        }

        fn fetch_url(&self) -> &'static str {
            "https://raw.githubusercontent.com/JoakimThorsen/MCPropertyEncyclopedia/main/data/block_data.json"
        }

        fn parse_data(&self, json_data: &str) -> Result<Vec<UnifiedBlockData>> {
            let parsed: Value = serde_json::from_str(json_data)
                .context("Failed to parse MCPropertyEncyclopedia JSON")?;

            let key_list = parsed
                .get("key_list")
                .and_then(|k| k.as_array())
                .context("Missing or invalid key_list")?;

            let properties_obj = parsed
                .get("properties")
                .and_then(|p| p.as_object())
                .context("Missing or invalid properties")?;

            let mut unified_blocks = Vec::new();

            for block_name in key_list {
                let block_name_str = block_name.as_str().context("Block name is not a string")?;

                // Convert display name to minecraft ID format
                let id = format!(
                    "minecraft:{}",
                    block_name_str
                        .to_lowercase()
                        .replace(" ", "_")
                        .replace("(", "")
                        .replace(")", "")
                        .replace("-", "_")
                        .replace("'", "")
                        .replace("!", "")
                        .replace(".", "_")
                );

                let mut extra_properties = HashMap::new();

                // Extract all properties for this block
                for (prop_name, prop_data) in properties_obj {
                    if let Some(entries) = prop_data.get("entries").and_then(|e| e.as_object()) {
                        if let Some(value) = entries.get(block_name_str) {
                            extra_properties.insert(prop_name.clone(), value.clone());
                        }
                    }
                }

                unified_blocks.push(UnifiedBlockData {
                    id,
                    properties: HashMap::new(), // MCPropertyEncyclopedia doesn't have block states
                    default_state: HashMap::new(),
                    extra_properties,
                });
            }

            Ok(unified_blocks)
        }

        fn validate_structure(&self, json: &Value) -> Result<()> {
            let _key_list = json
                .get("key_list")
                .and_then(|k| k.as_array())
                .context("Missing or invalid key_list")?;

            let _properties = json
                .get("properties")
                .and_then(|p| p.as_object())
                .context("Missing or invalid properties")?;

            Ok(())
        }
    }

    /// Registry for managing multiple data sources in build script
    pub struct DataSourceRegistry {
        sources: Vec<Box<dyn DataSourceAdapter>>,
        primary_source: Option<usize>,
    }

    impl DataSourceRegistry {
        pub fn new() -> Self {
            Self {
                sources: Vec::new(),
                primary_source: None,
            }
        }

        pub fn register_source(&mut self, source: Box<dyn DataSourceAdapter>) {
            self.sources.push(source);

            // Set first source as primary if none set
            if self.primary_source.is_none() {
                self.primary_source = Some(0);
            }
        }

        pub fn set_primary_source(&mut self, name: &str) -> Result<()> {
            for (i, source) in self.sources.iter().enumerate() {
                if source.name() == name {
                    self.primary_source = Some(i);
                    return Ok(());
                }
            }
            anyhow::bail!("Data source '{}' not found", name);
        }

        pub fn get_primary_source(&self) -> Result<&dyn DataSourceAdapter> {
            let index = self.primary_source.context("No primary data source set")?;
            Ok(self.sources[index].as_ref())
        }

        pub fn list_sources(&self) -> Vec<&str> {
            self.sources.iter().map(|s| s.name()).collect()
        }

        pub fn fetch_unified_data(&self) -> Result<Vec<UnifiedBlockData>> {
            let primary = self.get_primary_source()?;

            // Try to fetch from primary source with cache and fallback
            match self.fetch_with_fallback(primary) {
                Ok(blocks) => Ok(blocks),
                Err(e) => {
                    println!("cargo:warning=All data sources failed: {e}");
                    anyhow::bail!("Could not fetch data from any source: {}", e)
                }
            }
        }

        fn fetch_with_fallback(
            &self,
            primary: &dyn DataSourceAdapter,
        ) -> Result<Vec<UnifiedBlockData>> {
            // Try primary source
            match self.try_fetch_source(primary) {
                Ok(blocks) => {
                    println!(
                        "cargo:warning=Successfully fetched {} blocks from {}",
                        blocks.len(),
                        primary.name()
                    );
                    return Ok(blocks);
                }
                Err(e) => {
                    println!(
                        "cargo:warning=Failed to fetch from {} ({})",
                        primary.name(),
                        e
                    );
                }
            }

            // Try other sources as fallback
            for source in &self.sources {
                if source.name() != primary.name() {
                    match self.try_fetch_source(source.as_ref()) {
                        Ok(blocks) => {
                            println!(
                                "cargo:warning=Successfully fell back to {} and fetched {} blocks",
                                source.name(),
                                blocks.len()
                            );
                            return Ok(blocks);
                        }
                        Err(e) => {
                            println!(
                                "cargo:warning=Fallback to {} also failed: {}",
                                source.name(),
                                e
                            );
                        }
                    }
                }
            }

            anyhow::bail!("All data sources failed to provide data")
        }

        fn try_fetch_source(
            &self,
            source: &dyn DataSourceAdapter,
        ) -> Result<Vec<UnifiedBlockData>> {
            let url = source.fetch_url();
            println!(
                "cargo:warning=Fetching data from {} using {}",
                url,
                source.name()
            );

            // Check cache first
            let cache_key = format!("{}_data.json", source.name().to_lowercase());
            let cache_path = std::env::var("OUT_DIR")
                .map(|out_dir| std::path::Path::new(&out_dir).join(&cache_key))
                .unwrap_or_else(|_| std::path::PathBuf::from(&cache_key));

            // Try to load from cache first (for faster rebuilds)
            if cache_path.exists() {
                if let Ok(cached_data) = std::fs::read_to_string(&cache_path) {
                    if let Ok(parsed) = serde_json::from_str::<Value>(&cached_data) {
                        if source.validate_structure(&parsed).is_ok() {
                            if let Ok(blocks) = source.parse_data(&cached_data) {
                                println!("cargo:warning=Using cached data for {}", source.name());
                                return Ok(blocks);
                            }
                        }
                    }
                }
                println!(
                    "cargo:warning=Cache invalid for {}, re-downloading",
                    source.name()
                );
            }

            // Download fresh data
            let json_data = download_from_url(url)
                .with_context(|| format!("Failed to download from {}", source.name()))?;

            // Validate structure
            let parsed: Value = serde_json::from_str(&json_data)
                .with_context(|| format!("Failed to parse JSON from {}", source.name()))?;
            source
                .validate_structure(&parsed)
                .with_context(|| format!("Data validation failed for {}", source.name()))?;

            // Parse into unified format
            let blocks = source
                .parse_data(&json_data)
                .with_context(|| format!("Failed to parse data from {}", source.name()))?;

            // Cache the successful data
            if let Err(e) = std::fs::write(&cache_path, &json_data) {
                println!(
                    "cargo:warning=Failed to cache data for {}: {}",
                    source.name(),
                    e
                );
            } else {
                println!(
                    "cargo:warning=Cached data for {} for future builds",
                    source.name()
                );
            }

            Ok(blocks)
        }
    }

    impl Default for DataSourceRegistry {
        fn default() -> Self {
            let mut registry = Self::new();

            // Register default sources
            registry.register_source(Box::new(PrismarineAdapter));
            registry.register_source(Box::new(MCPropertyEncyclopediaAdapter));

            registry
        }
    }

    fn download_from_url(url: &str) -> Result<String> {
        let response = reqwest::blocking::get(url).context("Failed to make HTTP request")?;

        if !response.status().is_success() {
            anyhow::bail!("HTTP request failed with status: {}", response.status());
        }

        response
            .text()
            .context("Failed to read response body as text")
    }
}

use data_sources_build::*;

const BLOCKS_DATA_URL: &str = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.20.4/blocks.json";

// Simple fetcher framework for build script
#[derive(Debug, Clone)]
struct ExtraData {
    mock_data: HashMap<String, i32>,
    color_data: HashMap<String, (u8, u8, u8, f32, f32, f32)>, // RGB + Oklab
}

struct FetcherRegistry {
    extra_data: ExtraData,
}

impl FetcherRegistry {
    fn new() -> Self {
        Self {
            extra_data: ExtraData {
                mock_data: HashMap::new(),
                color_data: HashMap::new(),
            },
        }
    }

    fn add_color_data(&mut self, block_id: &str, rgb: (u8, u8, u8)) {
        // Use the same RGB to Oklab conversion as the generated code
        let r = rgb.0 as f32 / 255.0;
        let g = rgb.1 as f32 / 255.0;
        let b = rgb.2 as f32 / 255.0;
        let l = 0.2126 * r + 0.7152 * g + 0.0722 * b;
        let a = (r - g) * 0.5;
        let b_val = (r + g - 2.0 * b) * 0.25;

        self.extra_data
            .color_data
            .insert(block_id.to_string(), (rgb.0, rgb.1, rgb.2, l, a, b_val));
    }

    /// Extract colors from all available textures
    fn extract_colors_from_textures(&mut self, available_block_ids: &[String]) -> Result<()> {
        use std::path::Path;

        // Build texture mapping
        let textures_dir = Path::new("assets/textures");
        if !textures_dir.exists() {
            println!("cargo:warning=No textures directory found - using mock color data only");
            return Ok(());
        }

        println!("cargo:warning=Extracting colors from textures in {textures_dir:?}");

        // Scan texture files
        let texture_files: Vec<String> = std::fs::read_dir(textures_dir)
            .context("Failed to read textures directory")?
            .filter_map(|entry| {
                let entry = entry.ok()?;
                let path = entry.path();
                if path.extension()? == "png" {
                    path.file_stem()?.to_str().map(|s| s.to_string())
                } else {
                    None
                }
            })
            .collect();

        println!("cargo:warning=Found {} texture files", texture_files.len());

        let mut extracted_count = 0;
        let mut failed_count = 0;

        // Map textures to block IDs and extract colors
        for texture_name in texture_files {
            if let Some(block_ids) = self.texture_to_block_ids(&texture_name) {
                let texture_path = textures_dir.join(format!("{}.png", texture_name));

                match self.extract_color_from_texture(&texture_path) {
                    Ok(rgb) => {
                        for block_id in &block_ids {
                            // Only add color data for blocks that actually exist in our data
                            if available_block_ids.contains(block_id) {
                                self.add_color_data(block_id, rgb);
                                extracted_count += 1;
                            }
                        }
                    }
                    Err(e) => {
                        failed_count += 1;
                        if failed_count <= 5 {
                            // Only show first few errors
                            println!(
                                "cargo:warning=Failed to extract color from {}: {}",
                                texture_name, e
                            );
                        }
                    }
                }
            }
        }

        println!(
            "cargo:warning=Color extraction complete: {} colors extracted, {} failures",
            extracted_count, failed_count
        );
        Ok(())
    }

    /// Extract color from a single texture file
    fn extract_color_from_texture(&self, texture_path: &Path) -> Result<(u8, u8, u8)> {
        let img = image::open(texture_path)
            .with_context(|| format!("Failed to open texture: {:?}", texture_path))?;

        let rgba_img = img.to_rgba8();
        let (width, height) = rgba_img.dimensions();

        // Simple average color extraction
        let mut r_sum = 0u64;
        let mut g_sum = 0u64;
        let mut b_sum = 0u64;
        let mut pixel_count = 0u64;

        for y in 0..height {
            for x in 0..width {
                let pixel = rgba_img.get_pixel(x, y);
                let [r, g, b, a] = pixel.0;

                // Skip transparent pixels
                if a > 128 {
                    r_sum += r as u64;
                    g_sum += g as u64;
                    b_sum += b as u64;
                    pixel_count += 1;
                }
            }
        }

        if pixel_count == 0 {
            anyhow::bail!("No opaque pixels found in texture");
        }

        let avg_r = (r_sum / pixel_count) as u8;
        let avg_g = (g_sum / pixel_count) as u8;
        let avg_b = (b_sum / pixel_count) as u8;

        Ok((avg_r, avg_g, avg_b))
    }

    /// Add color inheritance for stairs, slabs, and walls
    fn add_inherited_colors(&mut self, available_block_ids: &[String]) {
        let mut inherited_count = 0;

        // Create a copy of existing color data for lookups
        let existing_colors = self.extra_data.color_data.clone();

        for block_id in available_block_ids {
            // Skip if this block already has color data
            if existing_colors.contains_key(block_id) {
                continue;
            }

            if let Some(base_material) = self.get_base_material_for_block(block_id) {
                if let Some(color) = existing_colors.get(&base_material) {
                    // Inherit the color from the base material
                    self.extra_data.color_data.insert(block_id.clone(), *color);
                    inherited_count += 1;
                }
            }
        }

        println!(
            "cargo:warning=Color inheritance complete: {} colors inherited from base materials",
            inherited_count
        );
    }

    /// Get the base material for stairs, slabs, walls, etc.
    fn get_base_material_for_block(&self, block_id: &str) -> Option<String> {
        let block_name = block_id.strip_prefix("minecraft:").unwrap_or(block_id);

        // Handle stairs
        if block_name.ends_with("_stairs") {
            let base = block_name.replace("_stairs", "");
            return Some(format!("minecraft:{}", base));
        }

        // Handle slabs
        if block_name.ends_with("_slab") {
            let base = block_name.replace("_slab", "");
            // Special cases for slabs
            match base.as_str() {
                "petrified_oak" => return Some("minecraft:oak_planks".to_string()),
                "smooth_stone" => return Some("minecraft:stone".to_string()),
                "cut_copper" => return Some("minecraft:copper_block".to_string()),
                "exposed_cut_copper" => return Some("minecraft:exposed_copper".to_string()),
                "weathered_cut_copper" => return Some("minecraft:weathered_copper".to_string()),
                "oxidized_cut_copper" => return Some("minecraft:oxidized_copper".to_string()),
                "waxed_cut_copper" => return Some("minecraft:copper_block".to_string()),
                "waxed_exposed_cut_copper" => return Some("minecraft:exposed_copper".to_string()),
                "waxed_weathered_cut_copper" => {
                    return Some("minecraft:weathered_copper".to_string())
                }
                "waxed_oxidized_cut_copper" => {
                    return Some("minecraft:oxidized_copper".to_string())
                }
                "cut_red_sandstone" => return Some("minecraft:red_sandstone".to_string()),
                "cut_sandstone" => return Some("minecraft:sandstone".to_string()),
                "prismarine_brick" => return Some("minecraft:prismarine_bricks".to_string()),
                "nether_brick" => return Some("minecraft:nether_bricks".to_string()),
                "red_nether_brick" => return Some("minecraft:red_nether_bricks".to_string()),
                "polished_blackstone_brick" => {
                    return Some("minecraft:polished_blackstone_bricks".to_string())
                }
                "end_stone_brick" => return Some("minecraft:end_stone_bricks".to_string()),
                "stone_brick" => return Some("minecraft:stone_bricks".to_string()),
                "mossy_stone_brick" => return Some("minecraft:mossy_stone_bricks".to_string()),
                "mossy_cobblestone" => return Some("minecraft:mossy_cobblestone".to_string()),
                "deepslate_brick" => return Some("minecraft:deepslate_bricks".to_string()),
                "deepslate_tile" => return Some("minecraft:deepslate_tiles".to_string()),
                "polished_deepslate" => return Some("minecraft:polished_deepslate".to_string()),
                "cobbled_deepslate" => return Some("minecraft:cobbled_deepslate".to_string()),
                "tuff_brick" => return Some("minecraft:tuff_bricks".to_string()),
                "polished_tuff" => return Some("minecraft:polished_tuff".to_string()),
                "bamboo_mosaic" => return Some("minecraft:bamboo_planks".to_string()),
                _ => {
                    // Default case: try the base material directly
                    return Some(format!("minecraft:{}", base));
                }
            }
        }

        // Handle walls
        if block_name.ends_with("_wall") {
            let base = block_name.replace("_wall", "");
            // Special cases for walls
            match base.as_str() {
                "cobblestone" => return Some("minecraft:cobblestone".to_string()),
                "mossy_cobblestone" => return Some("minecraft:mossy_cobblestone".to_string()),
                "stone_brick" => return Some("minecraft:stone_bricks".to_string()),
                "mossy_stone_brick" => return Some("minecraft:mossy_stone_bricks".to_string()),
                "granite" => return Some("minecraft:granite".to_string()),
                "diorite" => return Some("minecraft:diorite".to_string()),
                "andesite" => return Some("minecraft:andesite".to_string()),
                "cobbled_deepslate" => return Some("minecraft:cobbled_deepslate".to_string()),
                "polished_deepslate" => return Some("minecraft:polished_deepslate".to_string()),
                "deepslate_brick" => return Some("minecraft:deepslate_bricks".to_string()),
                "deepslate_tile" => return Some("minecraft:deepslate_tiles".to_string()),
                "brick" => return Some("minecraft:bricks".to_string()),
                "mud_brick" => return Some("minecraft:mud_bricks".to_string()),
                "nether_brick" => return Some("minecraft:nether_bricks".to_string()),
                "red_nether_brick" => return Some("minecraft:red_nether_bricks".to_string()),
                "sandstone" => return Some("minecraft:sandstone".to_string()),
                "red_sandstone" => return Some("minecraft:red_sandstone".to_string()),
                "blackstone" => return Some("minecraft:blackstone".to_string()),
                "polished_blackstone" => return Some("minecraft:polished_blackstone".to_string()),
                "polished_blackstone_brick" => {
                    return Some("minecraft:polished_blackstone_bricks".to_string())
                }
                "end_stone_brick" => return Some("minecraft:end_stone_bricks".to_string()),
                "prismarine" => return Some("minecraft:prismarine".to_string()),
                "tuff" => return Some("minecraft:tuff".to_string()),
                "polished_tuff" => return Some("minecraft:polished_tuff".to_string()),
                "tuff_brick" => return Some("minecraft:tuff_bricks".to_string()),
                _ => {
                    // Default case: try the base material directly
                    return Some(format!("minecraft:{}", base));
                }
            }
        }

        // Handle fences
        if block_name.ends_with("_fence") && !block_name.ends_with("_fence_gate") {
            let base = block_name.replace("_fence", "");
            match base.as_str() {
                "nether_brick" => return Some("minecraft:nether_bricks".to_string()),
                _ => {
                    // For wood fences, use the planks
                    return Some(format!("minecraft:{}_planks", base));
                }
            }
        }

        // Handle fence gates
        if block_name.ends_with("_fence_gate") {
            let base = block_name.replace("_fence_gate", "");
            return Some(format!("minecraft:{}_planks", base));
        }

        // Handle doors
        if block_name.ends_with("_door") {
            let base = block_name.replace("_door", "");
            match base.as_str() {
                "iron" => return Some("minecraft:iron_block".to_string()),
                "copper" => return Some("minecraft:copper_block".to_string()),
                "exposed_copper" => return Some("minecraft:exposed_copper".to_string()),
                "weathered_copper" => return Some("minecraft:weathered_copper".to_string()),
                "oxidized_copper" => return Some("minecraft:oxidized_copper".to_string()),
                "waxed_copper" => return Some("minecraft:copper_block".to_string()),
                "waxed_exposed_copper" => return Some("minecraft:exposed_copper".to_string()),
                "waxed_weathered_copper" => return Some("minecraft:weathered_copper".to_string()),
                "waxed_oxidized_copper" => return Some("minecraft:oxidized_copper".to_string()),
                _ => {
                    // For wood doors, use the planks
                    return Some(format!("minecraft:{}_planks", base));
                }
            }
        }

        // Handle trapdoors
        if block_name.ends_with("_trapdoor") {
            let base = block_name.replace("_trapdoor", "");
            match base.as_str() {
                "iron" => return Some("minecraft:iron_block".to_string()),
                "copper" => return Some("minecraft:copper_block".to_string()),
                "exposed_copper" => return Some("minecraft:exposed_copper".to_string()),
                "weathered_copper" => return Some("minecraft:weathered_copper".to_string()),
                "oxidized_copper" => return Some("minecraft:oxidized_copper".to_string()),
                "waxed_copper" => return Some("minecraft:copper_block".to_string()),
                "waxed_exposed_copper" => return Some("minecraft:exposed_copper".to_string()),
                "waxed_weathered_copper" => return Some("minecraft:weathered_copper".to_string()),
                "waxed_oxidized_copper" => return Some("minecraft:oxidized_copper".to_string()),
                _ => {
                    // For wood trapdoors, use the planks
                    return Some(format!("minecraft:{}_planks", base));
                }
            }
        }

        None
    }

    /// Convert texture file name to potential block IDs using smart heuristics
    fn texture_to_block_ids(&self, texture_name: &str) -> Option<Vec<String>> {
        let mut block_ids = Vec::new();

        // Basic mapping: texture name to block ID
        let base_id = format!("minecraft:{}", texture_name);
        block_ids.push(base_id);

        // Handle special cases and variations
        match texture_name {
            // Log textures map to both log and wood blocks
            name if name.ends_with("_log") => {
                let wood_base = name.replace("_log", "");
                block_ids.push(format!("minecraft:{}_wood", wood_base));
                // Add leaves for the same wood type
                block_ids.push(format!("minecraft:{}_leaves", wood_base));
                // Handle stripped variants
                if name.starts_with("stripped_") {
                    let base = name.replace("stripped_", "");
                    block_ids.push(format!("minecraft:{}", base));
                }
            }

            // Log top textures
            name if name.ends_with("_log_top") => {
                let base = name.replace("_log_top", "");
                block_ids.push(format!("minecraft:{}_log", base));
                block_ids.push(format!("minecraft:{}_wood", base));
            }

            // Stone variants
            "stone" => {
                block_ids.push("minecraft:smooth_stone".to_string());
            }

            // Grass block variants
            "grass_block_snow" => {
                block_ids.push("minecraft:grass_block".to_string());
            }

            // Sandstone variants
            "sandstone_top" => {
                block_ids.push("minecraft:sandstone".to_string());
                block_ids.push("minecraft:smooth_sandstone".to_string());
            }
            "red_sandstone_top" => {
                block_ids.push("minecraft:red_sandstone".to_string());
                block_ids.push("minecraft:smooth_red_sandstone".to_string());
            }

            // Multi-face blocks - use the most representative texture
            "furnace_side" => {
                block_ids.push("minecraft:furnace".to_string());
            }
            "furnace_front" | "furnace_top" => {
                // Skip these in favor of furnace_side
                return None;
            }

            // Pumpkin variants
            "pumpkin_side" => {
                block_ids.push("minecraft:pumpkin".to_string());
            }
            "pumpkin_top" => {
                // Skip in favor of pumpkin_side
                return None;
            }

            // Melon
            "melon_side" => {
                block_ids.push("minecraft:melon".to_string());
            }
            "melon_top" => {
                // Skip in favor of melon_side
                return None;
            }

            // Hay block
            "hay_block_side" => {
                block_ids.push("minecraft:hay_block".to_string());
            }
            "hay_block_top" => {
                // Skip in favor of hay_block_side
                return None;
            }

            // TNT
            "tnt_side" => {
                block_ids.push("minecraft:tnt".to_string());
            }
            "tnt_bottom" => {
                // Skip in favor of tnt_side
                return None;
            }

            // Mycelium
            "mycelium_side" => {
                block_ids.push("minecraft:mycelium".to_string());
            }
            "mycelium_top" => {
                // Skip in favor of mycelium_side
                return None;
            }

            // Podzol
            "podzol_side" => {
                block_ids.push("minecraft:podzol".to_string());
            }
            "podzol_top" => {
                // Skip in favor of podzol_side
                return None;
            }

            // Farmland
            "farmland_moist" => {
                block_ids.push("minecraft:farmland".to_string());
            }
            "farmland" => {
                // Skip in favor of farmland_moist
                return None;
            }

            // Grass path
            "dirt_path_top" => {
                block_ids.push("minecraft:dirt_path".to_string());
            }

            // Copper variants
            name if name.contains("copper") && name.contains("_bulb") => {
                // Handle copper bulb variants
                let base = name.replace("_lit", "");
                block_ids.push(format!("minecraft:{}", base));
            }

            // Shulker box default
            "shulker_box" => {
                // This maps to the purple shulker box by default
                block_ids.clear();
                block_ids.push("minecraft:shulker_box".to_string());
            }

            // Default case - the base mapping is usually correct
            _ => {
                // Keep the base mapping
            }
        }

        if block_ids.is_empty() {
            None
        } else {
            Some(block_ids)
        }
    }

    fn fetch_all(&mut self, available_block_ids: &[String]) -> Result<&ExtraData> {
        // Mock fetcher data
        self.extra_data
            .mock_data
            .insert("minecraft:stone".to_string(), 42);
        self.extra_data
            .mock_data
            .insert("minecraft:dirt".to_string(), 123);
        self.extra_data
            .mock_data
            .insert("minecraft:grass_block".to_string(), 456);
        self.extra_data
            .mock_data
            .insert("minecraft:oak_log".to_string(), 789);
        self.extra_data
            .mock_data
            .insert("minecraft:oak_planks".to_string(), 321);
        self.extra_data
            .mock_data
            .insert("minecraft:cobblestone".to_string(), 654);

        // First add hardcoded color data for reference
        self.add_color_data("minecraft:stone", (125, 125, 125));
        self.add_color_data("minecraft:dirt", (134, 96, 67));
        self.add_color_data("minecraft:grass_block", (95, 159, 53));
        self.add_color_data("minecraft:oak_log", (102, 81, 51));
        self.add_color_data("minecraft:oak_leaves", (65, 137, 50));
        self.add_color_data("minecraft:oak_planks", (162, 130, 78));
        self.add_color_data("minecraft:water", (64, 164, 223));
        self.add_color_data("minecraft:lava", (207, 108, 32));
        self.add_color_data("minecraft:cobblestone", (127, 127, 127));
        self.add_color_data("minecraft:sand", (219, 203, 158));
        self.add_color_data("minecraft:gravel", (136, 126, 126));
        self.add_color_data("minecraft:gold_ore", (252, 238, 75));
        self.add_color_data("minecraft:iron_ore", (135, 130, 126));
        self.add_color_data("minecraft:diamond_ore", (92, 219, 213));

        // Extract colors from all available textures
        if let Err(e) = self.extract_colors_from_textures(available_block_ids) {
            println!(
                "cargo:warning=Failed to extract colors from textures: {}",
                e
            );
        }

        // Add color inheritance for stairs, slabs, and walls
        self.add_inherited_colors(available_block_ids);

        Ok(&self.extra_data)
    }

    fn generate_query_helpers(&self, file: &mut std::fs::File) -> Result<()> {
        // Generate color query helpers
        writeln!(file, "// Generated query helper functions")?;
        writeln!(file, "impl crate::BlockFacts {{")?;

        // Closest color query
        writeln!(
            file,
            "    pub fn closest_to_color(target_rgb: [u8; 3]) -> Option<&'static Self> {{"
        )?;
        writeln!(file, "        let target_oklab = rgb_to_oklab(target_rgb);")?;
        writeln!(file, "        let mut best_block = None;")?;
        writeln!(file, "        let mut best_distance = f32::INFINITY;")?;
        writeln!(file, "        for block in crate::all_blocks() {{")?;
        writeln!(
            file,
            "            if let Some(ref color) = block.extras.color {{"
        )?;
        writeln!(
            file,
            "                let distance = oklab_distance(target_oklab, color.oklab);"
        )?;
        writeln!(file, "                if distance < best_distance {{")?;
        writeln!(file, "                    best_distance = distance;")?;
        writeln!(file, "                    best_block = Some(block);")?;
        writeln!(file, "                }}")?;
        writeln!(file, "            }}")?;
        writeln!(file, "        }}")?;
        writeln!(file, "        best_block")?;
        writeln!(file, "    }}")?;
        writeln!(file)?;

        // Color range query
        writeln!(file, "    pub fn blocks_in_color_range(center_rgb: [u8; 3], max_distance: f32) -> Vec<&'static Self> {{")? ;
        writeln!(file, "        let center_oklab = rgb_to_oklab(center_rgb);")?;
        writeln!(file, "        let mut result = Vec::new();")?;
        writeln!(file, "        for block in crate::all_blocks() {{")?;
        writeln!(
            file,
            "            if let Some(ref color) = block.extras.color {{"
        )?;
        writeln!(
            file,
            "                let distance = oklab_distance(center_oklab, color.oklab);"
        )?;
        writeln!(file, "                if distance <= max_distance {{")?;
        writeln!(file, "                    result.push(block);")?;
        writeln!(file, "                }}")?;
        writeln!(file, "            }}")?;
        writeln!(file, "        }}")?;
        writeln!(file, "        result")?;
        writeln!(file, "    }}")?;
        writeln!(file, "}}")?;
        writeln!(file)?;

        // Helper functions
        writeln!(file, "fn rgb_to_oklab(rgb: [u8; 3]) -> [f32; 3] {{")?;
        writeln!(
            file,
            "    // Simplified RGB to Oklab conversion for build-time"
        )?;
        writeln!(file, "    let r = rgb[0] as f32 / 255.0;")?;
        writeln!(file, "    let g = rgb[1] as f32 / 255.0;")?;
        writeln!(file, "    let b = rgb[2] as f32 / 255.0;")?;
        writeln!(file, "    let l = 0.2126 * r + 0.7152 * g + 0.0722 * b;")?;
        writeln!(file, "    let a = (r - g) * 0.5;")?;
        writeln!(file, "    let b_val = (r + g - 2.0 * b) * 0.25;")?;
        writeln!(file, "    [l, a, b_val]")?;
        writeln!(file, "}}")?;
        writeln!(file)?;

        writeln!(
            file,
            "fn oklab_distance(a: [f32; 3], b: [f32; 3]) -> f32 {{"
        )?;
        writeln!(file, "    let dl = a[0] - b[0];")?;
        writeln!(file, "    let da = a[1] - b[1];")?;
        writeln!(file, "    let db = a[2] - b[2];")?;
        writeln!(file, "    (dl * dl + da * da + db * db).sqrt()")?;
        writeln!(file, "}}")?;
        writeln!(file)?;

        Ok(())
    }
}

fn setup_fetchers() -> FetcherRegistry {
    FetcherRegistry::new()
}

/// Extract block IDs from JSON in either format
fn get_block_ids_from_json(json: &Value) -> Result<Vec<String>> {
    let mut block_ids = Vec::new();

    if json.is_object() && json.get("blocks").is_some() {
        // Test format: {"blocks": {"minecraft:stone": {...}, ...}}
        let blocks_obj = json["blocks"]
            .as_object()
            .context("'blocks' field is not an object")?;
        block_ids.extend(blocks_obj.keys().cloned());
    } else if json.is_array() {
        // PrismarineJS format: [{"name": "air", ...}, ...]
        let blocks_array = json.as_array().context("JSON is not a valid array")?;

        for block in blocks_array {
            if let Some(block_obj) = block.as_object() {
                if let Some(name) = block_obj.get("name").and_then(|n| n.as_str()) {
                    block_ids.push(format!("minecraft:{}", name));
                }
            }
        }
    } else {
        anyhow::bail!("Unsupported JSON format for extracting block IDs");
    }

    Ok(block_ids)
}

fn main() -> Result<()> {
    let out_dir = env::var("OUT_DIR").unwrap();

    // Set up data source registry
    let mut data_registry = DataSourceRegistry::default();

    // Check for environment variable to set data source
    if let Ok(source_name) = env::var("BLOCKPEDIA_DATA_SOURCE") {
        println!(
            "cargo:warning=Setting data source to {} from environment variable",
            source_name
        );
        data_registry
            .set_primary_source(&source_name)
            .with_context(|| format!("Failed to set data source to {}", source_name))?;
    }

    println!(
        "cargo:warning=Available data sources: {:?}",
        data_registry.list_sources()
    );
    println!(
        "cargo:warning=Using primary data source: {}",
        data_registry.get_primary_source()?.name()
    );

    let cache_path = Path::new(&out_dir).join("blocks_data.json");

    // Fetch unified data from the selected data source
    let unified_blocks = if env::var("BLOCKPEDIA_USE_TEST_DATA").is_ok() {
        // Use legacy fetch method for test data
        let json_data = fetch_or_load_cached(&cache_path)?;
        let parsed: Value =
            serde_json::from_str(&json_data).context("Failed to parse downloaded JSON")?;
        validate_json_structure(&parsed)?;

        // Convert legacy format to unified format (will be replaced later)
        vec![] // Placeholder for now, will use legacy generation
    } else {
        // Use new unified data source system
        match data_registry.fetch_unified_data() {
            Ok(blocks) => blocks,
            Err(e) => {
                println!(
                    "cargo:warning=Failed to fetch from primary source ({}): {}",
                    data_registry.get_primary_source()?.name(),
                    e
                );
                println!("cargo:warning=Falling back to cached/legacy method");

                // Fallback to legacy method
                let json_data = fetch_or_load_cached(&cache_path)?;
                let parsed: Value =
                    serde_json::from_str(&json_data).context("Failed to parse downloaded JSON")?;
                validate_json_structure(&parsed)?;

                // Generate using legacy method
                generate_legacy_phf_table(&out_dir, &parsed)?;
                return Ok(());
            }
        }
    };

    // For now, if we have unified blocks, generate using legacy method
    // This will be replaced with unified generation later
    if unified_blocks.is_empty() || env::var("BLOCKPEDIA_USE_TEST_DATA").is_ok() {
        let json_data = fetch_or_load_cached(&cache_path)?;
        let parsed: Value =
            serde_json::from_str(&json_data).context("Failed to parse downloaded JSON")?;
        validate_json_structure(&parsed)?;
        generate_legacy_phf_table(&out_dir, &parsed)?;
    } else {
        // Generate from unified data
        generate_unified_phf_table(&out_dir, &unified_blocks)?;
    }

    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-env-changed=BLOCKPEDIA_DATA_SOURCE");
    println!("cargo:rerun-if-env-changed=BLOCKPEDIA_USE_TEST_DATA");
    println!("cargo:rerun-if-env-changed=BLOCKPEDIA_VERSION_JSON_SHA");

    Ok(())
}

fn fetch_or_load_cached(cache_path: &Path) -> Result<String> {
    // Check if we should use test data (for development)
    if std::env::var("BLOCKPEDIA_USE_TEST_DATA").is_ok() {
        let test_file = Path::new("test_blocks_data.json");
        if test_file.exists() {
            println!("cargo:warning=Using local test file (BLOCKPEDIA_USE_TEST_DATA is set)");
            return fs::read_to_string(test_file).context("Failed to read test JSON file");
        }
    }

    // First try to load from cache
    if cache_path.exists() {
        println!("cargo:warning=Using cached blocks_data.json");
        return fs::read_to_string(cache_path).context("Failed to read cached JSON file");
    }

    // Try to download
    println!("cargo:warning=Downloading blocks_data.json from GitHub...");
    match download_json() {
        Ok(data) => {
            // Cache the downloaded data
            fs::write(cache_path, &data).context("Failed to cache downloaded JSON")?;
            Ok(data)
        }
        Err(e) => {
            anyhow::bail!(
                "Failed to download blocks_data.json and no cache available: {}",
                e
            );
        }
    }
}

fn download_json() -> Result<String> {
    let response =
        reqwest::blocking::get(BLOCKS_DATA_URL).context("Failed to make HTTP request")?;

    if !response.status().is_success() {
        anyhow::bail!("HTTP request failed with status: {}", response.status());
    }

    response
        .text()
        .context("Failed to read response body as text")
}

fn validate_json_structure(json: &Value) -> Result<()> {
    // Handle two different formats: our test format and PrismarineJS format
    if json.is_object() && json.get("blocks").is_some() {
        // Our test format: {"blocks": {...}}
        let blocks = json["blocks"]
            .as_object()
            .context("'blocks' field is not an object")?;

        if blocks.is_empty() {
            anyhow::bail!("No blocks found in JSON data");
        }

        println!(
            "cargo:warning=JSON validation passed - found {} blocks (test format)",
            blocks.len()
        );
    } else if json.is_array() {
        // PrismarineJS format: [{"name": "air", ...}, ...]
        let blocks_array = json.as_array().context("JSON is not a valid array")?;

        if blocks_array.is_empty() {
            anyhow::bail!("No blocks found in JSON data");
        }

        // Validate a few sample blocks have required fields
        for (i, block_data) in blocks_array.iter().take(5).enumerate() {
            let block_obj = block_data
                .as_object()
                .with_context(|| format!("Block at index {} is not an object", i))?;

            if !block_obj.contains_key("name") {
                anyhow::bail!("Block at index {} missing 'name' field", i);
            }
        }

        println!(
            "cargo:warning=JSON validation passed - found {} blocks (PrismarineJS format)",
            blocks_array.len()
        );
    } else {
        anyhow::bail!(
            "JSON format not recognized - expected either {{\"blocks\": {{...}}}} or array format"
        );
    }

    Ok(())
}

fn generate_phf_table(
    out_dir: &str,
    json: &Value,
    extra_data: &ExtraData,
    fetcher_registry: &FetcherRegistry,
) -> Result<()> {
    let table_path = Path::new(out_dir).join("block_table.rs");
    let mut file = std::fs::File::create(&table_path).context("Failed to create block_table.rs")?;

    // Start building the PHF map
    writeln!(file, "// Auto-generated PHF table from block data")?;
    writeln!(file, "use phf::{{phf_map, Map}};")?;
    writeln!(file)?;

    // Determine format and convert to unified representation
    let block_data: Vec<(String, serde_json::Value)> = if json.is_object()
        && json.get("blocks").is_some()
    {
        // Test format: {"blocks": {"minecraft:stone": {...}, ...}}
        let blocks_obj = json["blocks"]
            .as_object()
            .context("'blocks' field is not an object")?;
        blocks_obj
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    } else if json.is_array() {
        // PrismarineJS format: [{"name": "air", ...}, ...]
        let blocks_array = json.as_array().context("JSON is not a valid array")?;

        blocks_array
            .iter()
            .filter_map(|block| {
                let block_obj = block.as_object()?;
                let name = block_obj.get("name")?.as_str()?;

                // Convert PrismarineJS format to our expected format
                let mut converted_block = serde_json::Map::new();

                // Convert "states" to "properties"
                if let Some(states) = block_obj.get("states").and_then(|s| s.as_array()) {
                    let mut properties = serde_json::Map::new();

                    for state in states {
                        if let Some(state_obj) = state.as_object() {
                            if let (Some(prop_name), Some(prop_type), Some(num_values)) = (
                                state_obj.get("name").and_then(|n| n.as_str()),
                                state_obj.get("type").and_then(|t| t.as_str()),
                                state_obj.get("num_values").and_then(|n| n.as_u64()),
                            ) {
                                let values = match prop_type {
                                    "bool" => vec!["false".to_string(), "true".to_string()],
                                    "int" => {
                                        // For int types, check if values array is available
                                        if let Some(values_array) =
                                            state_obj.get("values").and_then(|v| v.as_array())
                                        {
                                            values_array
                                                .iter()
                                                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                                .collect()
                                        } else {
                                            (0..num_values).map(|i| i.to_string()).collect()
                                        }
                                    }
                                    "enum" => {
                                        // Extract actual enum values if available
                                        if let Some(values_array) =
                                            state_obj.get("values").and_then(|v| v.as_array())
                                        {
                                            values_array
                                                .iter()
                                                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                                .collect()
                                        } else {
                                            (0..num_values)
                                                .map(|i| format!("value_{}", i))
                                                .collect()
                                        }
                                    }
                                    _ => vec!["unknown".to_string()],
                                };
                                properties.insert(
                                    prop_name.to_string(),
                                    serde_json::Value::Array(
                                        values.into_iter().map(serde_json::Value::String).collect(),
                                    ),
                                );
                            }
                        }
                    }

                    if !properties.is_empty() {
                        converted_block.insert(
                            "properties".to_string(),
                            serde_json::Value::Object(properties),
                        );
                    } else {
                        converted_block.insert(
                            "properties".to_string(),
                            serde_json::Value::Object(serde_json::Map::new()),
                        );
                    }
                } else {
                    converted_block.insert(
                        "properties".to_string(),
                        serde_json::Value::Object(serde_json::Map::new()),
                    );
                }

                // Empty default state for now
                converted_block.insert(
                    "default_state".to_string(),
                    serde_json::Value::Object(serde_json::Map::new()),
                );

                Some((
                    format!("minecraft:{}", name),
                    serde_json::Value::Object(converted_block),
                ))
            })
            .collect()
    } else {
        anyhow::bail!("Unsupported JSON format");
    };

    // Generate the static block data
    for (block_id, block_data) in &block_data {
        let block_obj = block_data
            .as_object()
            .with_context(|| format!("Block '{}' is not an object", block_id))?;

        // Parse properties
        let empty_props = serde_json::Map::new();
        let properties = block_obj
            .get("properties")
            .and_then(|p| p.as_object())
            .unwrap_or(&empty_props);

        // Parse default state
        let empty_state = serde_json::Map::new();
        let default_state = block_obj
            .get("default_state")
            .and_then(|d| d.as_object())
            .unwrap_or(&empty_state);

        // Generate a valid Rust identifier from block ID
        let safe_name = block_id.replace(":", "_").replace("-", "_").to_uppercase();

        writeln!(
            file,
            "static {}: crate::BlockFacts = crate::BlockFacts {{",
            safe_name
        )?;
        writeln!(file, "    id: \"{}\",", block_id)?;

        // Generate properties array
        writeln!(file, "    properties: &[")?;
        for (prop_name, prop_values) in properties {
            if let Some(values_array) = prop_values.as_array() {
                write!(file, "        (\"{}\", &[", prop_name)?;
                for (i, value) in values_array.iter().enumerate() {
                    if i > 0 {
                        write!(file, ", ")?;
                    }
                    write!(file, "\"{}\"", value.as_str().unwrap_or(""))?;
                }
                writeln!(file, "]),")?;
            }
        }
        writeln!(file, "    ],")?;

        // Generate default_state array
        writeln!(file, "    default_state: &[")?;
        for (state_name, state_value) in default_state {
            writeln!(
                file,
                "        (\"{}\", \"{}\"),",
                state_name,
                state_value.as_str().unwrap_or("")
            )?;
        }
        writeln!(file, "    ],")?;

        // Generate extras with fetched data
        write!(file, "    extras: crate::Extras {{")?;

        // Mock data
        if let Some(mock_val) = extra_data.mock_data.get(block_id) {
            write!(file, " mock_data: Some({}),", mock_val)?;
        } else {
            write!(file, " mock_data: None,")?;
        }

        // Color data
        if let Some((r, g, b, l, a, b_val)) = extra_data.color_data.get(block_id) {
            // Adjust values to avoid clippy::approx_constant warnings
            let adjusted_l = if (*l - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *l + 0.001
            } else {
                *l
            };
            let adjusted_a = if (*a - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *a + 0.001
            } else {
                *a
            };
            let adjusted_b = if (*b_val - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *b_val + 0.001
            } else {
                *b_val
            };
            write!(file, " color: Some(crate::ColorData {{ rgb: [{}, {}, {}], oklab: [{:.3}, {:.3}, {:.3}] }}),", r, g, b, adjusted_l, adjusted_a, adjusted_b)?;
        } else {
            write!(file, " color: None,")?;
        }

        writeln!(file, " }},")?;
        writeln!(file, "}};")?;
        writeln!(file)?;
    }

    // Generate the PHF map
    writeln!(
        file,
        "pub static BLOCKS: Map<&'static str, &'static crate::BlockFacts> = phf_map! {{"
    )?;

    for (block_id, _) in &block_data {
        let safe_name = block_id.replace(":", "_").replace("-", "_").to_uppercase();
        writeln!(file, "    \"{}\" => &{},", block_id, safe_name)?;
    }

    writeln!(file, "}};")?;
    writeln!(file)?;

    // Generate query helpers from fetchers
    fetcher_registry.generate_query_helpers(&mut file)?;

    println!(
        "cargo:warning=Generated PHF table with {} blocks",
        block_data.len()
    );
    Ok(())
}

// Legacy PHF table generation for backward compatibility
fn generate_legacy_phf_table(out_dir: &str, json: &Value) -> Result<()> {
    // Set up fetcher registry
    let mut fetcher_registry = setup_fetchers();

    // Get list of available block IDs from JSON
    let available_block_ids = get_block_ids_from_json(json)?;

    // Fetch extra data from all registered fetchers
    let extra_data = fetcher_registry.fetch_all(&available_block_ids)?.clone();

    // Generate full PHF table from JSON data with extra data
    generate_phf_table(out_dir, json, &extra_data, &fetcher_registry)
}

// Generate PHF table from unified block data
fn generate_unified_phf_table(out_dir: &str, unified_blocks: &[UnifiedBlockData]) -> Result<()> {
    let table_path = Path::new(out_dir).join("block_table.rs");
    let mut file = std::fs::File::create(&table_path).context("Failed to create block_table.rs")?;

    // Set up fetcher registry for color data
    let mut fetcher_registry = setup_fetchers();
    let available_block_ids: Vec<String> = unified_blocks.iter().map(|b| b.id.clone()).collect();
    let extra_data = fetcher_registry.fetch_all(&available_block_ids)?.clone();

    // Start building the PHF map
    writeln!(file, "// Auto-generated PHF table from unified block data")?;
    writeln!(file, "use phf::{{phf_map, Map}};")?;
    writeln!(file)?;

    // Generate the static block data
    for block_data in unified_blocks {
        let block_id = &block_data.id;

        // Generate a valid Rust identifier from block ID
        let safe_name = block_id
            .replace(":", "_")
            .replace("-", "_")
            .replace("'", "")
            .replace("!", "")
            .replace(".", "_")
            .to_uppercase();

        writeln!(
            file,
            "static {}: crate::BlockFacts = crate::BlockFacts {{",
            safe_name
        )?;
        writeln!(file, "    id: \"{}\",", block_id)?;

        // Generate properties array
        writeln!(file, "    properties: &[")?;
        for (prop_name, prop_values) in &block_data.properties {
            write!(file, "        (\"{}\", &[", prop_name)?;
            for (i, value) in prop_values.iter().enumerate() {
                if i > 0 {
                    write!(file, ", ")?;
                }
                write!(file, "\"{}\"", value)?;
            }
            writeln!(file, "]),")?;
        }
        writeln!(file, "    ],")?;

        // Generate default_state array
        writeln!(file, "    default_state: &[")?;
        for (state_name, state_value) in &block_data.default_state {
            writeln!(file, "        (\"{}\", \"{}\"),", state_name, state_value)?;
        }
        writeln!(file, "    ],")?;

        // Generate extras with fetched data
        write!(file, "    extras: crate::Extras {{")?;

        // Mock data
        if let Some(mock_val) = extra_data.mock_data.get(block_id) {
            write!(file, " mock_data: Some({}),", mock_val)?;
        } else {
            write!(file, " mock_data: None,")?;
        }

        // Color data
        if let Some((r, g, b, l, a, b_val)) = extra_data.color_data.get(block_id) {
            // Adjust values to avoid clippy::approx_constant warnings
            let adjusted_l = if (*l - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *l + 0.001
            } else {
                *l
            };
            let adjusted_a = if (*a - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *a + 0.001
            } else {
                *a
            };
            let adjusted_b = if (*b_val - std::f32::consts::FRAC_1_PI).abs() < 0.001 {
                *b_val + 0.001
            } else {
                *b_val
            };
            write!(file, " color: Some(crate::ColorData {{ rgb: [{}, {}, {}], oklab: [{:.3}, {:.3}, {:.3}] }}),", r, g, b, adjusted_l, adjusted_a, adjusted_b)?;
        } else {
            write!(file, " color: None,")?;
        }

        writeln!(file, " }},")?;
        writeln!(file, "}};")?;
        writeln!(file)?;
    }

    // Generate the PHF map
    writeln!(
        file,
        "pub static BLOCKS: Map<&'static str, &'static crate::BlockFacts> = phf_map! {{"
    )?;

    for block_data in unified_blocks {
        let block_id = &block_data.id;
        let safe_name = block_id
            .replace(":", "_")
            .replace("-", "_")
            .replace("'", "")
            .replace("!", "")
            .replace(".", "_")
            .to_uppercase();
        writeln!(file, "    \"{}\" => &{},", block_id, safe_name)?;
    }

    writeln!(file, "}};")?;
    writeln!(file)?;

    // Generate query helpers from fetchers
    fetcher_registry.generate_query_helpers(&mut file)?;

    println!(
        "cargo:warning=Generated unified PHF table with {} blocks",
        unified_blocks.len()
    );
    Ok(())
}