hardware_report 0.1.7

Hardware inventory reports for observability and cluster acceptance testing
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
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
/*
Copyright 2024 San Francisco Compute Company

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! Hardware Report Library
//!
//! This library provides hardware information collection capabilities using a
//! Ports and Adapters (Hexagonal) architecture for maintainability and testability.
//!
//! # Architecture
//!
//! - **Domain**: Core business logic and entities
//! - **Ports**: Interfaces for external interactions
//! - **Adapters**: Platform-specific implementations
//!
//! # Usage
//!
//! ## As a Library
//!
//! ```rust,no_run
//! use hardware_report::{HardwareReportingService, ReportConfig};
//!
//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
//!     // Create service with platform-specific adapters
//!     let service = hardware_report::create_service(None).await?;
//!     
//!     // Generate hardware report
//!     let config = ReportConfig::default();
//!     let report = service.generate_report(config).await?;
//!     
//!     println!("System: {} ({})", report.hostname, report.summary.system_info.product_name);
//!     Ok(())
//! }
//! ```
//!
//! ## Legacy Compatibility
//!
//! The library maintains backward compatibility with the original `ServerInfo` API:
//!
//! ```rust,no_run
//! use hardware_report::ServerInfo;
//!
//! fn legacy_example() -> Result<(), Box<dyn std::error::Error>> {
//!     let server_info = ServerInfo::collect()?;
//!     println!("Hostname: {}", server_info.hostname);
//!     Ok(())
//! }
//! ```

// New Ports and Adapters Architecture
pub mod adapters;
pub mod container;
pub mod domain;
pub mod ports;

// Re-export public API - specific exports to avoid conflicts with legacy types
// Only export new types that don't conflict with legacy compatibility layer
pub use adapters::{
    FileDataPublisher, FileSystemRepository, HttpDataPublisher, LinuxSystemInfoProvider,
    MacOSSystemInfoProvider, UnixCommandExecutor,
};
pub use container::{ContainerConfig, ContainerConfigBuilder, ServiceContainer};
pub use domain::{PublishConfig, PublishError, ReportConfig, ReportError};
pub use ports::{
    CommandExecutor, ConfigurationProvider, DataPublisher, FileRepository,
    HardwareReportingService, OutputFormat, SystemInfoProvider,
};

// Re-export domain entities under a namespace to avoid conflicts
pub use domain::HardwareReport as NewHardwareReport;
pub mod new_domain {
    pub use crate::domain::*;
}

// Legacy compatibility - keep original types and implementations
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::process::Command;

lazy_static! {
    static ref STORAGE_SIZE_RE: Regex = Regex::new(r"(\d+(?:\.\d+)?)(B|K|M|G|T)").unwrap();
    static ref NETWORK_SPEED_RE: Regex = Regex::new(r"Speed:\s+(\S+)").unwrap();
}

/// CPU topology information
#[derive(Debug, Serialize, Deserialize)]
pub struct CpuTopology {
    pub total_cores: u32,
    pub total_threads: u32,
    pub sockets: u32,
    pub cores_per_socket: u32,
    pub threads_per_core: u32,
    pub numa_nodes: u32,
    pub cpu_model: String,
}

/// Motherboard information
#[derive(Debug, Serialize, Deserialize)]
pub struct MotherboardInfo {
    pub manufacturer: String,
    pub product_name: String,
    pub version: String,
    pub serial: String,
    pub features: String,
    pub location: String,
    pub type_: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SystemInfo {
    pub uuid: String,
    pub serial: String,
    pub product_name: String,
    pub product_manufacturer: String,
}

/// Summary of key system components
#[derive(Debug, Serialize, Deserialize)]
pub struct SystemSummary {
    /// System information
    pub system_info: SystemInfo,
    /// Total system memory capacity
    pub total_memory: String,
    /// Memory speed and type
    pub memory_config: String,
    /// Total storage capacity
    pub total_storage: String,
    /// Total storage capacity in TB
    pub total_storage_tb: f64,
    /// Available filesystems
    pub filesystems: Vec<String>,
    /// BIOS information
    pub bios: BiosInfo,
    /// System chassis information
    pub chassis: ChassisInfo,
    /// Motherboard information
    pub motherboard: MotherboardInfo,
    /// Total number of GPUs
    pub total_gpus: usize,
    /// Total number of network interfaces
    pub total_nics: usize,
    /// NUMA topology information
    pub numa_topology: HashMap<String, NumaNode>,
    /// CPU topology information
    pub cpu_topology: CpuTopology,
    /// CPU configuration summary
    pub cpu_summary: String,
}

/// BIOS information
#[derive(Debug, Serialize, Deserialize)]
pub struct BiosInfo {
    pub vendor: String,
    pub version: String,
    pub release_date: String,
    pub firmware_version: String,
}

/// Chassis information
#[derive(Debug, Serialize, Deserialize)]
pub struct ChassisInfo {
    pub manufacturer: String,
    pub type_: String,
    pub serial: String,
}

/// Represents the overall server information
#[derive(Debug, Serialize, Deserialize)]
pub struct ServerInfo {
    /// System summary
    pub summary: SystemSummary,
    /// Other fields remain the same
    pub hostname: String,
    pub fqdn: String,
    pub os_ip: Vec<InterfaceIPs>,
    pub bmc_ip: Option<String>,
    pub bmc_mac: Option<String>,
    pub hardware: HardwareInfo,
    pub network: NetworkInfo,
}

/// Contains detailed hardware information
#[derive(Debug, Serialize, Deserialize)]
pub struct HardwareInfo {
    /// CPU information.
    pub cpu: CpuInfo,
    /// Memory information.
    pub memory: MemoryInfo,
    /// Storage information.
    pub storage: StorageInfo,
    /// GPU information.
    pub gpus: GpuInfo,
}

/// Represents CPU information.
#[derive(Debug, Serialize, Deserialize)]
pub struct CpuInfo {
    /// CPU model name.
    pub model: String,
    /// Number of cores per socket.
    pub cores: u32,
    /// Number of threads per core.
    pub threads: u32,
    /// Number of sockets.
    pub sockets: u32,
    /// CPU speed in MHz.
    pub speed: String,
}

/// Represents memory information.
#[derive(Debug, Serialize, Deserialize)]
pub struct MemoryInfo {
    /// Total memory size.
    pub total: String,
    /// Memory type (e.g., DDR4).
    pub type_: String,
    /// Memory speed.
    pub speed: String,
    /// Individual memory modules.
    pub modules: Vec<MemoryModule>,
}

/// Represents a memory module.
#[derive(Debug, Serialize, Deserialize)]
pub struct MemoryModule {
    /// Size of the memory module.
    pub size: String,
    /// Type of the memory module.
    pub type_: String,
    /// Speed of the memory module.
    pub speed: String,
    /// Physical location of the memory module.
    pub location: String,
    /// Manufacturer of the memory module.
    pub manufacturer: String,
    /// Serial number of the memory module.
    pub serial: String,
}

/// Represents storage information.
#[derive(Debug, Serialize, Deserialize)]
pub struct StorageInfo {
    /// List of storage devices.
    pub devices: Vec<StorageDevice>,
}

/// Represents a storage device.
#[derive(Debug, Serialize, Deserialize)]
pub struct StorageDevice {
    /// Device name.
    pub name: String,
    /// Device type (e.g., disk).
    pub type_: String,
    /// Device size.
    pub size: String,
    /// Device model.
    pub model: String,
}

/// Represents GPU information.
#[derive(Debug, Serialize, Deserialize)]
pub struct GpuInfo {
    /// List of GPU devices.
    pub devices: Vec<GpuDevice>,
}

/// Represents a GPU device.
#[derive(Debug, Serialize, Deserialize)]
pub struct GpuDevice {
    /// GPU index
    pub index: u32,
    /// GPU name
    pub name: String,
    /// GPU UUID
    pub uuid: String,
    /// Total GPU memory
    pub memory: String,
    /// PCI ID (vendor:device)
    pub pci_id: String,
    /// Vendor name
    pub vendor: String,
    /// NUMA node
    pub numa_node: Option<i32>,
}

/// Represents a NUMA node
#[derive(Debug, Serialize, Deserialize)]
pub struct NumaNode {
    /// Node ID
    pub id: i32,
    /// CPU list
    pub cpus: Vec<u32>,
    /// Memory size
    pub memory: String,
    /// Devices attached to this node
    pub devices: Vec<NumaDevice>,
    /// distances to other nodse (node_id _> distance)
    pub distances: HashMap<String, u32>,
}

/// Represents a device attached to a NUMA node
#[derive(Debug, Serialize, Deserialize)]
pub struct NumaDevice {
    /// Device type (GPU, NIC, etc.)
    pub type_: String,
    /// PCI ID
    pub pci_id: String,
    /// Device name
    pub name: String,
}

/// Represents network information.
#[derive(Debug, Serialize, Deserialize)]
pub struct NetworkInfo {
    /// List of network interfaces.
    pub interfaces: Vec<NetworkInterface>,
    /// Infiniband information, if available.
    pub infiniband: Option<InfinibandInfo>,
}

/// Represents a network interface.
#[derive(Debug, Serialize, Deserialize)]
pub struct NetworkInterface {
    /// Interface name.
    pub name: String,
    /// MAC address.
    pub mac: String,
    /// IP address.
    pub ip: String,
    /// IP prefix.
    pub prefix: String,
    /// Interface speed.
    pub speed: Option<String>,
    /// Interface type.
    pub type_: String,
    pub vendor: String,
    pub model: String,
    pub pci_id: String,
    pub numa_node: Option<i32>,
}

/// Represents Infiniband information.
#[derive(Debug, Serialize, Deserialize)]
pub struct InfinibandInfo {
    /// List of Infiniband interfaces.
    pub interfaces: Vec<IbInterface>,
}

/// Represents an Infiniband interface.
#[derive(Debug, Serialize, Deserialize)]
pub struct IbInterface {
    /// Interface name.
    pub name: String,
    /// Port number.
    pub port: u32,
    /// Interface state.
    pub state: String,
    /// Interface rate.
    pub rate: String,
}

#[allow(dead_code)]
pub struct NumaInfo {
    pub nodes: Vec<NumaNode>,
}

pub mod posting;

#[derive(Debug, Serialize, Deserialize)]
pub struct InterfaceIPs {
    pub interface: String,
    pub ip_addresses: Vec<String>,
}

#[allow(unused_variables)]
#[allow(unused_assignments)]
#[allow(clippy::useless_format)]
#[allow(clippy::manual_map)]
#[allow(clippy::format_in_format_args)]
#[allow(clippy::needless_borrows_for_generic_args)]
impl ServerInfo {
    /// Checks for required system dependencies and returns any missing ones
    fn check_dependencies() -> Result<Vec<&'static str>, Box<dyn Error>> {
        let required_packages = if cfg!(target_os = "macos") {
            vec![
                ("system_profiler", "System hardware information"),
                ("sysctl", "System configuration information"),
                ("ioreg", "Hardware registry information"),
                ("hostname", "System hostname"),
                ("df", "Filesystem information"),
            ]
        } else {
            vec![
                ("numactl", "NUMA topology information"),
                ("lspci", "PCI device information"),
                ("ethtool", "Network interface information"),
                ("dmidecode", "System hardware information"),
                ("lscpu", "CPU information"),
                ("ip", "Network interface details"),
                ("lsblk", "Storage device information"),
                ("hostname", "System hostname"),
                ("free", "Memory usage information"),
                ("df", "Filesystem information"),
            ]
        };

        let mut missing_packages = Vec::new();
        let mut missing_info = Vec::new();

        // Check which packages are missing
        for (package, purpose) in &required_packages {
            let status = Command::new("which").arg(package).output()?;

            if !status.status.success() {
                missing_packages.push(*package);
                missing_info.push(format!("  - {package}: {purpose}"));
            }
        }

        if !missing_packages.is_empty() {
            eprintln!("\nWarning: Some system utilities are not installed.");
            eprintln!("Missing utilities:");
            for info in &missing_info {
                eprintln!("{info}");
            }
            eprintln!("\nSome hardware information may be incomplete or unavailable.");

            // Separate packages that are typically pre-installed vs specialized tools
            let core_utils = ["hostname", "ip", "lscpu", "free", "df", "lsblk"];
            let specialized_tools = ["numactl", "lspci", "ethtool", "dmidecode"];

            let missing_core: Vec<&str> = missing_packages
                .iter()
                .filter(|&&pkg| core_utils.contains(&pkg))
                .copied()
                .collect();
            let missing_specialized: Vec<&str> = missing_packages
                .iter()
                .filter(|&&pkg| specialized_tools.contains(&pkg))
                .copied()
                .collect();

            if !missing_core.is_empty() {
                eprintln!("\nCore utilities missing (usually pre-installed):");
                eprintln!(
                    "  Ubuntu/Debian: sudo apt install {}",
                    missing_core
                        .iter()
                        .map(|&pkg| match pkg {
                            "ip" => "iproute2",
                            "lscpu" | "lsblk" => "util-linux",
                            "free" | "hostname" => "procps",
                            "df" => "coreutils",
                            _ => pkg,
                        })
                        .collect::<Vec<_>>()
                        .join(" ")
                );
                eprintln!(
                    "  RHEL/Fedora: sudo dnf install {}",
                    missing_core
                        .iter()
                        .map(|&pkg| match pkg {
                            "ip" => "iproute",
                            "lscpu" | "lsblk" => "util-linux",
                            "free" => "procps-ng",
                            "hostname" | "df" => "coreutils",
                            _ => pkg,
                        })
                        .collect::<Vec<_>>()
                        .join(" ")
                );
            }

            if !missing_specialized.is_empty() {
                eprintln!("\nSpecialized tools missing:");
                eprintln!(
                    "  Ubuntu/Debian: sudo apt install {}",
                    missing_specialized.join(" ")
                );
                eprintln!(
                    "  RHEL/Fedora: sudo dnf install {}",
                    missing_specialized.join(" ")
                );
            }
            eprintln!();
        }

        Ok(missing_packages)
    }
    /// Gets motherboard information using dmidecode
    fn get_motherboard_info() -> Result<MotherboardInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_motherboard_info_macos()
        } else {
            Self::get_motherboard_info_linux()
        }
    }

    fn get_motherboard_info_macos() -> Result<MotherboardInfo, Box<dyn Error>> {
        let manufacturer = "Apple Inc.".to_string();
        let mut product_name = "Unknown Product".to_string();
        let mut version = "Unknown Version".to_string();
        let mut serial = "Unknown S/N".to_string();

        // Get hardware information from system_profiler
        if let Ok(output) = Command::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);

            for line in output_str.lines() {
                let trimmed = line.trim();
                if trimmed.starts_with("Model Identifier:") {
                    product_name = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown Product")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("System Firmware Version:") {
                    version = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown Version")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("Serial Number (system):") {
                    serial = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown S/N")
                        .trim()
                        .to_string();
                }
            }
        }

        // Try to get MLB (Main Logic Board) serial from ioreg if system serial not found
        if serial == "Unknown S/N" {
            if let Ok(output) = Command::new("ioreg")
                .args(&["-c", "IOPlatformExpertDevice", "-d", "2"])
                .output()
            {
                let output_str = String::from_utf8_lossy(&output.stdout);
                for line in output_str.lines() {
                    if line.contains("\"serial-number\"") {
                        if let Some(start) = line.find('<') {
                            if let Some(end) = line.find('>') {
                                let hex_bytes = &line[start + 1..end];
                                // Convert hex bytes to string if possible
                                serial = hex_bytes.replace(" ", "");
                                break;
                            }
                        }
                    }
                }
            }
        }

        Ok(MotherboardInfo {
            manufacturer,
            product_name,
            version,
            serial,
            features: "Integrated".to_string(),
            location: "System Board".to_string(),
            type_: "Motherboard".to_string(),
        })
    }

    fn get_motherboard_info_linux() -> Result<MotherboardInfo, Box<dyn Error>> {
        let output = match Command::new("dmidecode").args(&["-t", "2"]).output() {
            Ok(out) => {
                if !out.status.success() {
                    Command::new("sudo")
                        .args(&["dmidecode", "-t", "2"])
                        .output()?
                } else {
                    out
                }
            }
            Err(_) => Command::new("sudo")
                .args(&["dmidecode", "-t", "2"])
                .output()?,
        };

        let stdout = String::from_utf8_lossy(&output.stdout);

        if !output.status.success() || stdout.trim().is_empty() {
            return Ok(MotherboardInfo {
                manufacturer: "Unknown Manufacturer".to_string(),
                product_name: "Unknown Product".to_string(),
                version: "Unknown Version".to_string(),
                serial: "Unknown S/N".to_string(),
                features: "Unknown".to_string(),
                location: "Unknown".to_string(),
                type_: "Unknown".to_string(),
            });
        }

        Ok(MotherboardInfo {
            manufacturer: Self::extract_dmidecode_value(&stdout, "Manufacturer")
                .unwrap_or_else(|_| "Unknown Manufacturer".to_string()),
            product_name: Self::extract_dmidecode_value(&stdout, "Product Name")
                .unwrap_or_else(|_| "Unknown Product".to_string()),
            version: Self::extract_dmidecode_value(&stdout, "Version")
                .unwrap_or_else(|_| "Unknown Version".to_string()),
            serial: Self::extract_dmidecode_value(&stdout, "Serial Number")
                .unwrap_or_else(|_| "Unknown S/N".to_string()),
            features: Self::extract_dmidecode_value(&stdout, "Features")
                .unwrap_or_else(|_| "Unknown".to_string()),
            location: Self::extract_dmidecode_value(&stdout, "Location In Chassis")
                .unwrap_or_else(|_| "Unknown".to_string()),
            type_: Self::extract_dmidecode_value(&stdout, "Type")
                .unwrap_or_else(|_| "Unknown".to_string()),
        })
    }

    /// Converts storage size string to bytes
    fn parse_storage_size(size: &str) -> Result<u64, Box<dyn Error>> {
        if size == "Unknown" || size.is_empty() {
            return Ok(0);
        }

        // First try to extract exact byte count from parentheses (macOS format)
        // Example: "2.0 TB (2001111162880 Bytes)" or "2 TB (1,995,218,165,760 bytes)"
        if let Some(start) = size.find('(') {
            // Try both uppercase and lowercase "bytes"
            let end_pos = size.find(" Bytes)").or_else(|| size.find(" bytes)"));
            if let Some(end) = end_pos {
                let bytes_str = &size[start + 1..end];
                if let Ok(bytes) = bytes_str.replace(",", "").parse::<u64>() {
                    return Ok(bytes);
                }
            }
        }

        let size_str = size.replace(" ", "").to_uppercase();

        // Handle both Linux format (123G) and macOS format (123 GB)
        let re = Regex::new(r"(\d+(?:\.\d+)?)\s*(B|KB?|MB?|GB?|TB?|BYTES?)$")?;

        if let Some(caps) = re.captures(&size_str) {
            let value: f64 = caps[1].parse()?;
            let unit = &caps[2];

            let multiplier = match unit {
                "B" | "BYTES" => 1_u64,
                "K" | "KB" => 1024_u64,
                "M" | "MB" => 1024_u64 * 1024,
                "G" | "GB" => 1024_u64 * 1024 * 1024,
                "T" | "TB" => 1024_u64 * 1024 * 1024 * 1024,
                _ => return Err(format!("Unknown storage unit: {unit}").into()),
            };

            Ok((value * multiplier as f64) as u64)
        } else {
            // Try to handle other formats gracefully
            if size_str.contains("BYTES") || size_str.contains("B") {
                Ok(0) // Return 0 for unparseable sizes instead of erroring
            } else {
                Err(format!("Invalid storage size format: {size}").into())
            }
        }
    }

    /// Automatically installs numactl if not present
    fn auto_install_numactl() -> Result<bool, Box<dyn Error>> {
        // Check if we have sudo/root privileges
        let euid = unsafe { libc::geteuid() };
        let use_sudo = euid != 0;

        // Detect the package manager
        let pkg_managers = vec![
            ("apt-get", vec!["update"], vec!["install", "-y", "numactl"]),
            ("apt", vec!["update"], vec!["install", "-y", "numactl"]),
            ("dnf", vec![], vec!["install", "-y", "numactl"]),
            ("yum", vec![], vec!["install", "-y", "numactl"]),
            ("zypper", vec!["refresh"], vec!["install", "-y", "numactl"]),
        ];

        for (manager, update_args, install_args) in pkg_managers {
            // Check if the package manager exists
            if Command::new("which")
                .arg(manager)
                .output()?
                .status
                .success()
            {
                // Run update command if needed
                if !update_args.is_empty() {
                    let mut update_cmd = if use_sudo {
                        let mut cmd = Command::new("sudo");
                        cmd.arg(manager);
                        cmd
                    } else {
                        Command::new(manager)
                    };

                    update_cmd.args(&update_args);
                    let _ = update_cmd.output(); // Ignore update errors
                }

                // Run install command
                let mut install_cmd = if use_sudo {
                    let mut cmd = Command::new("sudo");
                    cmd.arg(manager);
                    cmd
                } else {
                    Command::new(manager)
                };

                install_cmd.args(&install_args);
                let output = install_cmd.output()?;

                if output.status.success() {
                    // Verify numactl was installed
                    if Command::new("which")
                        .arg("numactl")
                        .output()?
                        .status
                        .success()
                    {
                        return Ok(true);
                    }
                }
            }
        }

        Ok(false)
    }

    // Remove automatic package installation
    #[allow(dead_code)]
    fn suggest_package_installation(missing_packages: &[&str]) {
        if !missing_packages.is_empty() {
            eprintln!(
                "\nTo get complete hardware information, please install the missing utilities:"
            );
            eprintln!("\nFor Ubuntu/Debian:");
            eprintln!("  sudo apt install {}", missing_packages.join(" "));
            eprintln!("\nFor RHEL/Fedora:");
            eprintln!("  sudo dnf install {}", missing_packages.join(" "));
        }
    }

    /// Gets hostname of the server
    fn get_hostname() -> Result<String, Box<dyn Error>> {
        match Command::new("hostname").output() {
            Ok(output) => Ok(String::from_utf8(output.stdout)?.trim().to_string()),
            Err(_) => {
                // Fallback to reading /etc/hostname or use system name
                if let Ok(contents) = std::fs::read_to_string("/etc/hostname") {
                    Ok(contents.trim().to_string())
                } else {
                    Ok("unknown".to_string())
                }
            }
        }
    }

    fn get_fqdn() -> Result<String, Box<dyn Error>> {
        match Command::new("hostname").args(&["-f"]).output() {
            Ok(output) => Ok(String::from_utf8(output.stdout)?.trim().to_string()),
            Err(_) => {
                // Fallback to hostname if FQDN lookup fails
                Self::get_hostname()
            }
        }
    }

    /// Gets PCI information for a device
    fn get_pci_info(pci_addr: &str) -> Result<(String, String, String), Box<dyn Error>> {
        // Run lspci with verbose output and machine-readable format
        let output = match Command::new("lspci")
            .args(&["-vmm", "-s", pci_addr])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                // lspci not available, return unknown values
                return Ok((
                    "Unknown".to_string(),
                    "Unknown".to_string(),
                    "Unknown".to_string(),
                ));
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        let mut vendor = String::new();
        let mut device = String::new();
        let mut vendor_id = String::new();
        let mut device_id = String::new();

        for line in output_str.lines() {
            let parts: Vec<&str> = line.split(':').collect();
            if parts.len() >= 2 {
                let value = parts[1].trim();
                match parts[0].trim() {
                    "Vendor" => vendor = value.to_string(),
                    "Device" => device = value.to_string(),
                    "SVendor" if vendor.is_empty() => vendor = value.to_string(),
                    "SDevice" if device.is_empty() => device = value.to_string(),
                    _ => {}
                }
            }
        }

        // Get vendor and device IDs using -n flag
        let id_output = match Command::new("lspci").args(&["-n", "-s", pci_addr]).output() {
            Ok(output) => output,
            Err(_) => {
                // Return early if lspci is not available
                return Ok((vendor, device, "Unknown".to_string()));
            }
        };

        let id_str = String::from_utf8(id_output.stdout)?;
        if let Some(line) = id_str.lines().next() {
            if let Some(ids) = line.split_whitespace().nth(2) {
                let parts: Vec<&str> = ids.split(':').collect();
                if parts.len() >= 2 {
                    vendor_id = parts[0].to_string();
                    device_id = parts[1].to_string();
                }
            }
        }

        let pci_id = format!("{vendor_id}:{device_id}");
        Ok((vendor, device, pci_id))
    }

    /// Gets NUMA node for a PCI device
    fn get_numa_node(pci_addr: &str) -> Option<i32> {
        if let Ok(path) = std::fs::read_link(format!("/sys/bus/pci/devices/{pci_addr}/numa_node")) {
            if let Ok(content) = std::fs::read_to_string(path) {
                if let Ok(node) = content.trim().parse() {
                    return Some(node);
                }
            }
        }
        None
    }

    fn collect_numa_topology() -> Result<HashMap<String, NumaNode>, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            // NUMA topology is not applicable on macOS in the same way
            // Return empty HashMap for macOS
            return Ok(HashMap::new());
        }

        let mut nodes = HashMap::new();
        let mut collecting_distances = false;

        // Get NUMA information using numactl
        let output = Command::new("numactl").args(&["--hardware"]).output()?;

        let output_str = String::from_utf8(output.stdout)?;

        for line in output_str.lines() {
            if line.starts_with("node ") && line.contains("size:") {
                // Parse node and memory information
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() >= 4 {
                    if let Ok(id) = parts[1].parse::<i32>() {
                        let memory = format!("{} {}", parts[3], parts[4]);

                        // Create new node entry
                        nodes.insert(
                            id.to_string(),
                            NumaNode {
                                id,
                                memory,
                                cpus: Vec::new(),
                                distances: HashMap::new(),
                                devices: Vec::new(),
                            },
                        );
                    }
                }
            } else if line.contains("node distances:") {
                collecting_distances = true;
                continue;
            } else if collecting_distances && line.trim().starts_with("node") {
                // Parse distance information
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() > 2 {
                    if let Ok(from_node) = parts[1].parse::<i32>() {
                        for (i, dist_str) in parts[2..].iter().enumerate() {
                            if let Ok(distance) = dist_str.parse::<u32>() {
                                if let Some(node) = nodes.get_mut(&from_node.to_string()) {
                                    node.distances.insert(i.to_string(), distance);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Get CPU to node mapping
        let output = match Command::new("lscpu").args(&["-p=cpu,node"]).output() {
            Ok(output) => output,
            Err(_) => {
                // lscpu not available, skip CPU to node mapping
                return Ok(nodes);
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        for line in output_str.lines() {
            if line.starts_with('#') {
                continue;
            }
            let parts: Vec<&str> = line.split(',').collect();
            if parts.len() >= 2 {
                if let (Ok(cpu), Ok(node)) = (parts[0].parse::<u32>(), parts[1].parse::<i32>()) {
                    if let Some(numa_node) = nodes.get_mut(&node.to_string()) {
                        numa_node.cpus.push(cpu);
                    }
                }
            }
        }

        // Sort CPUs within each node
        for node in nodes.values_mut() {
            node.cpus.sort();
        }

        Ok(nodes)
    }

    fn collect_ip_addresses() -> Result<Vec<InterfaceIPs>, Box<dyn Error>> {
        let output = match Command::new("ip").args(&["-j", "addr"]).output() {
            Ok(output) => output,
            Err(_) => {
                // ip command not available, return empty list
                return Ok(Vec::new());
            }
        };
        let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;

        let mut interfaces = Vec::new();

        if let Some(ifaces) = json.as_array() {
            for iface in ifaces {
                if let Some(name) = iface["ifname"].as_str() {
                    if name == "lo" {
                        continue;
                    } // Skip loopback

                    let mut ip_addresses = Vec::new();

                    if let Some(addr_info) = iface["addr_info"].as_array() {
                        for addr in addr_info {
                            if addr["family"].as_str() == Some("inet") {
                                if let Some(ip) = addr["local"].as_str() {
                                    ip_addresses.push(ip.to_string());
                                }
                            }
                        }
                    }

                    if !ip_addresses.is_empty() {
                        interfaces.push(InterfaceIPs {
                            interface: name.to_string(),
                            ip_addresses,
                        });
                    }
                }
            }
        }

        Ok(interfaces)
    }

    /// Gets system UUID and serial using platform-specific commands
    fn get_system_info() -> Result<SystemInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_system_info_macos()
        } else {
            Self::get_system_info_linux()
        }
    }

    /// Gets system UUID and serial on macOS using system_profiler
    fn get_system_info_macos() -> Result<SystemInfo, Box<dyn Error>> {
        let output = match Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                return Ok(SystemInfo {
                    uuid: "Unknown".to_string(),
                    serial: "Unknown".to_string(),
                    product_name: "Mac".to_string(),
                    product_manufacturer: "Apple Inc.".to_string(),
                });
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        let mut uuid = "Unknown".to_string();
        let mut serial = "Unknown".to_string();
        let mut model = "Mac".to_string();

        for line in output_str.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Hardware UUID:") {
                uuid = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Serial Number (system):") {
                serial = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Model Name:") {
                model = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Mac")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Chip:") {
                // Also extract chip info for newer Macs that don't show "Processor Name:"
                let chip_name = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
                if !chip_name.is_empty() && chip_name != "Unknown" {
                    model = format!("{model} ({chip_name})");
                }
            }
        }

        Ok(SystemInfo {
            uuid,
            serial,
            product_name: model,
            product_manufacturer: "Apple Inc.".to_string(),
        })
    }

    /// Gets system UUID and serial from dmidecode on Linux
    fn get_system_info_linux() -> Result<SystemInfo, Box<dyn Error>> {
        let output = match Command::new("dmidecode").args(&["-t", "system"]).output() {
            Ok(out) => {
                if !out.status.success() {
                    Command::new("sudo")
                        .args(&["dmidecode", "-t", "system"])
                        .output()?
                } else {
                    out
                }
            }
            Err(_) => Command::new("sudo")
                .args(&["dmidecode", "-t", "system"])
                .output()?,
        };

        let stdout = String::from_utf8_lossy(&output.stdout);

        if !output.status.success() || stdout.trim().is_empty() {
            return Ok(SystemInfo {
                uuid: "Unknown".to_string(),
                serial: "Unknown".to_string(),
                product_name: "Unknown".to_string(),
                product_manufacturer: "Unknown".to_string(),
            });
        }

        let uuid = Self::extract_dmidecode_value(&stdout, "UUID")
            .unwrap_or_else(|_| "Unknown".to_string());
        let serial = Self::extract_dmidecode_value(&stdout, "Serial Number")
            .unwrap_or_else(|_| "Unknown".to_string());
        let product_name = Self::extract_dmidecode_value(&stdout, "Product Name")
            .unwrap_or_else(|_| "Unknown".to_string());
        let product_manufacturer = Self::extract_dmidecode_value(&stdout, "Manufacturer")
            .unwrap_or_else(|_| "Unknown".to_string());

        Ok(SystemInfo {
            uuid,
            serial,
            product_name,
            product_manufacturer,
        })
    }

    /// Collects all server information
    pub fn collect() -> Result<Self, Box<dyn Error>> {
        // Check dependencies first and warn about missing packages
        let missing_packages = Self::check_dependencies()?;

        // Automatically install numactl if it's missing (Linux only)
        if !cfg!(target_os = "macos") && missing_packages.contains(&"numactl") {
            eprintln!("numactl is not installed. Attempting automatic installation...");

            // Try to detect the package manager and install numactl
            if Self::auto_install_numactl()? {
                eprintln!("Successfully installed numactl.");
            } else {
                eprintln!("Warning: Could not automatically install numactl. NUMA information may be incomplete.");
            }
        }

        // Check if running as root
        let euid = unsafe { libc::geteuid() };
        if euid != 0 {
            eprintln!(
            "\nWarning: This program requires root privileges to access all hardware information."
        );
            eprintln!(
                "Please run it with: sudo {}",
                std::env::args().next().unwrap_or_default()
            );
            eprintln!("Continuing with limited functionality...\n");
        }

        let hostname = Self::get_hostname()?;
        let fqdn = Self::get_fqdn()?;
        let hardware = Self::collect_hardware_info()?;
        let network = Self::collect_network_info()?;
        let system_info = Self::get_system_info()?;
        let (bmc_ip, bmc_mac) = Self::collect_ipmi_info()?;
        let os_ip = Self::collect_ip_addresses()?;

        let summary = Self::generate_summary(&hardware, &network, &system_info)?;

        Ok(ServerInfo {
            summary,
            hostname,
            fqdn,
            os_ip,
            bmc_ip,
            bmc_mac,
            hardware,
            network,
        })
    }

    /// Calculates total storage in terabytes
    fn calculate_total_storage_tb(storage: &StorageInfo) -> Result<f64, Box<dyn Error>> {
        let mut total_bytes: u64 = 0;

        for device in &storage.devices {
            total_bytes += Self::parse_storage_size(&device.size)?;
        }

        Ok(total_bytes as f64 / (1024.0 * 1024.0 * 1024.0 * 1024.0))
    }

    /// Calculates total storage capacity
    fn calculate_total_storage(storage: &StorageInfo) -> Result<String, Box<dyn Error>> {
        let mut total_bytes: u64 = 0;
        let re = Regex::new(r"(\d+(?:\.\d+)?)(B|K|M|G|T)")?;

        for device in &storage.devices {
            let size_str = device.size.replace(" ", "");

            if let Some(caps) = re.captures(&size_str) {
                let value: f64 = caps[1].parse()?;
                let unit = &caps[2];

                let multiplier = match unit {
                    "B" => 1_u64,
                    "K" => 1024_u64,
                    "M" => 1024_u64 * 1024,
                    "G" => 1024_u64 * 1024 * 1024,
                    "T" => 1024_u64 * 1024 * 1024 * 1024,
                    _ => 0_u64,
                };

                total_bytes += (value * multiplier as f64) as u64;
            }
        }

        if total_bytes >= 1024 * 1024 * 1024 * 1024 {
            Ok(format!(
                "{:.1} TB",
                total_bytes as f64 / (1024.0 * 1024.0 * 1024.0 * 1024.0)
            ))
        } else {
            Ok(format!(
                "{:.1} GB",
                total_bytes as f64 / (1024.0 * 1024.0 * 1024.0)
            ))
        }
    }

    /// Gets filesystem information
    fn get_filesystems() -> Result<Vec<String>, Box<dyn Error>> {
        let output = match Command::new("df")
            .args(["-h", "--output=source,fstype,size,used,avail,target"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                // df not available, return empty list
                return Ok(Vec::new());
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        let mut filesystems = Vec::new();

        for line in output_str.lines().skip(1) {
            let fields: Vec<&str> = line.split_whitespace().collect();
            if fields.len() >= 6 {
                filesystems.push(format!(
                    "{} ({}) - {} total, {} used, {} available, mounted on {}",
                    fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]
                ));
            }
        }

        Ok(filesystems)
    }

    /// Gets BIOS information using platform-specific commands
    fn get_bios_info() -> Result<BiosInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_bios_info_macos()
        } else {
            Self::get_bios_info_linux()
        }
    }

    /// Gets firmware information on macOS using system_profiler
    fn get_bios_info_macos() -> Result<BiosInfo, Box<dyn Error>> {
        let output = match Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                return Ok(BiosInfo {
                    vendor: "Apple Inc.".to_string(),
                    version: "Unknown Version".to_string(),
                    release_date: "Unknown Date".to_string(),
                    firmware_version: "N/A".to_string(),
                });
            }
        };

        let output_str = String::from_utf8_lossy(&output.stdout);
        let mut firmware_version = "N/A".to_string();

        for line in output_str.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("System Firmware Version:") {
                firmware_version = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("N/A")
                    .trim()
                    .to_string();
                break;
            }
        }

        Ok(BiosInfo {
            vendor: "Apple Inc.".to_string(),
            version: firmware_version.clone(),
            release_date: "N/A".to_string(),
            firmware_version,
        })
    }

    /// Gets BIOS information using dmidecode on Linux
    fn get_bios_info_linux() -> Result<BiosInfo, Box<dyn Error>> {
        // Try without sudo first, then with sudo if needed
        let output = match Command::new("dmidecode").args(&["-t", "0"]).output() {
            Ok(out) => {
                if !out.status.success() {
                    Command::new("sudo")
                        .args(&["dmidecode", "-t", "0"])
                        .output()?
                } else {
                    out
                }
            }
            Err(_) => Command::new("sudo")
                .args(&["dmidecode", "-t", "0"])
                .output()?,
        };

        let stdout = String::from_utf8_lossy(&output.stdout);

        if !output.status.success() || stdout.trim().is_empty() {
            return Ok(BiosInfo {
                vendor: "Unknown Vendor".to_string(),
                version: "Unknown Version".to_string(),
                release_date: "Unknown Date".to_string(),
                firmware_version: "N/A".to_string(),
            });
        }

        Ok(BiosInfo {
            vendor: Self::extract_dmidecode_value(&stdout, "Vendor")
                .unwrap_or_else(|_| "Unknown Vendor".to_string()),
            version: Self::extract_dmidecode_value(&stdout, "Version")
                .unwrap_or_else(|_| "Unknown Version".to_string()),
            release_date: Self::extract_dmidecode_value(&stdout, "Release Date")
                .unwrap_or_else(|_| "Unknown Date".to_string()),
            firmware_version: Self::extract_dmidecode_value(&stdout, "Firmware Revision")
                .unwrap_or_else(|_| "N/A".to_string()),
        })
    }

    /// Gets chassis information using platform-specific commands
    fn get_chassis_info() -> Result<ChassisInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_chassis_info_macos()
        } else {
            Self::get_chassis_info_linux()
        }
    }

    /// Gets chassis information on macOS using system_profiler
    fn get_chassis_info_macos() -> Result<ChassisInfo, Box<dyn Error>> {
        let output = match Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                return Ok(ChassisInfo {
                    manufacturer: "Apple Inc.".to_string(),
                    type_: "Laptop".to_string(),
                    serial: "Unknown S/N".to_string(),
                });
            }
        };

        let output_str = String::from_utf8_lossy(&output.stdout);
        let mut serial = "Unknown S/N".to_string();
        let mut chassis_type = "Laptop".to_string();

        for line in output_str.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Serial Number (system):") {
                serial = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown S/N")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Model Name:") {
                let model = trimmed.split(":").nth(1).unwrap_or("").trim().to_string();
                if model.contains("Mac Pro")
                    || model.contains("Mac Studio")
                    || model.contains("iMac Pro")
                {
                    chassis_type = "Desktop".to_string();
                } else if model.contains("iMac") {
                    chassis_type = "All-in-One".to_string();
                } else if model.contains("MacBook") {
                    chassis_type = "Laptop".to_string();
                } else if model.contains("Mac mini") {
                    chassis_type = "Mini PC".to_string();
                }
            }
        }

        Ok(ChassisInfo {
            manufacturer: "Apple Inc.".to_string(),
            type_: chassis_type,
            serial,
        })
    }

    /// Gets chassis information using dmidecode on Linux
    fn get_chassis_info_linux() -> Result<ChassisInfo, Box<dyn Error>> {
        let output = match Command::new("dmidecode").args(&["-t", "3"]).output() {
            Ok(out) => {
                if !out.status.success() {
                    Command::new("sudo")
                        .args(&["dmidecode", "-t", "3"])
                        .output()?
                } else {
                    out
                }
            }
            Err(_) => Command::new("sudo")
                .args(&["dmidecode", "-t", "3"])
                .output()?,
        };

        let stdout = String::from_utf8_lossy(&output.stdout);

        if !output.status.success() || stdout.trim().is_empty() {
            return Ok(ChassisInfo {
                manufacturer: "Unknown Manufacturer".to_string(),
                type_: "Unknown Type".to_string(),
                serial: "Unknown S/N".to_string(),
            });
        }

        Ok(ChassisInfo {
            manufacturer: Self::extract_dmidecode_value(&stdout, "Manufacturer")
                .unwrap_or_else(|_| "Unknown Manufacturer".to_string()),
            type_: Self::extract_dmidecode_value(&stdout, "Type")
                .unwrap_or_else(|_| "Unknown Type".to_string()),
            serial: Self::extract_dmidecode_value(&stdout, "Serial Number")
                .unwrap_or_else(|_| "Unknown S/N".to_string()),
        })
    }

    /// Extracts a value from 'dmidecode' output without debug output
    pub fn extract_dmidecode_value(text: &str, key: &str) -> Result<String, Box<dyn Error>> {
        let patterns = [
            format!(r"(?im)^\s*{}: (.*)$", regex::escape(key)),
            format!(r"(?im)^\s*{}\s+(.*)$", regex::escape(key)),
            format!(r"(?im)^{}: (.*)$", regex::escape(key)),
        ];

        for pattern in patterns.iter() {
            if let Ok(re) = Regex::new(pattern) {
                if let Some(cap) = re.captures(text) {
                    if let Some(value) = cap.get(1) {
                        let result = value.as_str().trim().to_string();
                        if !result.is_empty() {
                            return Ok(result);
                        }
                    }
                }
            }
        }

        Err(format!("Could not find key: {key}").into())
    }

    /// Gets detailed CPU topology information
    fn get_cpu_topology() -> Result<CpuTopology, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_cpu_topology_macos()
        } else {
            Self::get_cpu_topology_linux()
        }
    }

    /// Gets CPU topology information on macOS
    fn get_cpu_topology_macos() -> Result<CpuTopology, Box<dyn Error>> {
        let physical_cores = Self::get_macos_cpu_cores().unwrap_or(0);
        let logical_cores = Self::get_macos_logical_cpu_cores().unwrap_or(physical_cores);
        let threads_per_core = logical_cores.checked_div(physical_cores).unwrap_or(1);

        // Get CPU model from system_profiler
        let mut cpu_model = "Unknown".to_string();
        if let Ok(output) = Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            for line in output_str.lines() {
                if line.trim().starts_with("Processor Name:") || line.trim().starts_with("Chip:") {
                    cpu_model = line
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                    break;
                }
            }
        }

        Ok(CpuTopology {
            total_cores: physical_cores,
            total_threads: logical_cores,
            sockets: 1, // Most Macs have 1 socket
            cores_per_socket: physical_cores,
            threads_per_core,
            numa_nodes: 1, // macOS typically has 1 NUMA node
            cpu_model,
        })
    }

    /// Gets CPU topology information on Linux using lscpu
    fn get_cpu_topology_linux() -> Result<CpuTopology, Box<dyn Error>> {
        let output = match Command::new("lscpu").args(&["-J"]).output() {
            Ok(output) => output,
            Err(_) => {
                // lscpu not available, return default topology
                return Ok(CpuTopology {
                    total_cores: 0,
                    total_threads: 0,
                    sockets: 0,
                    cores_per_socket: 0,
                    threads_per_core: 0,
                    numa_nodes: 0,
                    cpu_model: "Unknown".to_string(),
                });
            }
        };

        let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;
        let mut info_map = HashMap::new();

        if let Some(entries) = json["lscpu"].as_array() {
            for entry in entries {
                if let (Some(field), Some(data)) = (entry["field"].as_str(), entry["data"].as_str())
                {
                    let key = field.trim_end_matches(':');
                    info_map.insert(key.to_string(), data.to_string());
                }
            }
        }

        Ok(CpuTopology {
            total_cores: info_map
                .get("Core(s) per socket")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0)
                * info_map
                    .get("Socket(s)")
                    .and_then(|v| v.parse().ok())
                    .unwrap_or(0),
            total_threads: info_map
                .get("CPU(s)")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0),
            sockets: info_map
                .get("Socket(s)")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0),
            cores_per_socket: info_map
                .get("Core(s) per socket")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0),
            threads_per_core: info_map
                .get("Thread(s) per core")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0),
            numa_nodes: info_map
                .get("NUMA node(s)")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0),
            cpu_model: info_map
                .get("Model name")
                .cloned()
                .unwrap_or_else(|| "Unknown".to_string()),
        })
    }

    /// Generates system summary with better error handling
    /// Enhanced summary generation with NUMA topology
    fn generate_summary(
        hardware: &HardwareInfo,
        network: &NetworkInfo,
        system_info: &SystemInfo,
    ) -> Result<SystemSummary, Box<dyn Error>> {
        let bios = Self::get_bios_info().unwrap_or_else(|_| BiosInfo {
            vendor: "Unknown Vendor".to_string(),
            version: "Unknown Version".to_string(),
            release_date: "Unknown Date".to_string(),
            firmware_version: "N/A".to_string(),
        });

        let chassis = Self::get_chassis_info().unwrap_or_else(|_| ChassisInfo {
            manufacturer: "Unknown Manufacturer".to_string(),
            type_: "Unknown Type".to_string(),
            serial: "Unknown S/N".to_string(),
        });
        let motherboard = Self::get_motherboard_info().unwrap_or_else(|_| MotherboardInfo {
            manufacturer: "Unknown Manufacturer".to_string(),
            product_name: "Unknown Product".to_string(),
            version: "Unknown Version".to_string(),
            serial: "Unknown S/N".to_string(),
            features: "Unknown".to_string(),
            location: "Unknown".to_string(),
            type_: "Unknown".to_string(),
        });

        let cpu_topology = Self::get_cpu_topology()?;

        // Create a detailed CPU summary string
        let cpu_summary = format!(
            "{} ({} Socket{}, {} Core{}/Socket, {} Thread{}/Core, {} NUMA Node{})",
            cpu_topology.cpu_model,
            cpu_topology.sockets,
            if cpu_topology.sockets > 1 { "s" } else { "" },
            cpu_topology.cores_per_socket,
            if cpu_topology.cores_per_socket > 1 {
                "s"
            } else {
                ""
            },
            cpu_topology.threads_per_core,
            if cpu_topology.threads_per_core > 1 {
                "s"
            } else {
                ""
            },
            cpu_topology.numa_nodes,
            if cpu_topology.numa_nodes > 1 { "s" } else { "" }
        );

        let total_storage_tb = Self::calculate_total_storage_tb(&hardware.storage)?;

        Ok(SystemSummary {
            system_info: SystemInfo {
                uuid: system_info.uuid.clone(),
                serial: system_info.serial.clone(),
                product_name: system_info.product_name.clone(),
                product_manufacturer: system_info.product_manufacturer.clone(),
            },
            total_memory: hardware.memory.total.clone(),
            memory_config: format!("{} @ {}", hardware.memory.type_, hardware.memory.speed),
            total_storage_tb,
            total_storage: Self::calculate_total_storage(&hardware.storage)?,
            filesystems: Self::get_filesystems().unwrap_or_default(),
            bios,
            chassis,
            motherboard,
            total_gpus: hardware.gpus.devices.len(),
            total_nics: network.interfaces.len(),
            numa_topology: Self::collect_numa_topology()?,
            cpu_topology,
            cpu_summary,
        })
    }

    /// Collects detailed hardware information.
    fn collect_hardware_info() -> Result<HardwareInfo, Box<dyn Error>> {
        Ok(HardwareInfo {
            cpu: Self::collect_cpu_info()?,
            memory: Self::collect_memory_info()?,
            storage: Self::collect_storage_info()?,
            gpus: Self::collect_gpu_info()?,
        })
    }

    /// Collects CPU information by parsing platform-specific commands.
    fn collect_cpu_info() -> Result<CpuInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::collect_cpu_info_macos()
        } else {
            Self::collect_cpu_info_linux()
        }
    }

    /// Collects CPU information on Linux using lscpu
    fn collect_cpu_info_linux() -> Result<CpuInfo, Box<dyn Error>> {
        // Use 'lscpu -J' for JSON output to ensure reliable parsing.
        let output = match Command::new("lscpu").args(&["-J"]).output() {
            Ok(output) => output,
            Err(_) => {
                // lscpu not available, return basic CPU info
                return Ok(CpuInfo {
                    model: "Unknown".to_string(),
                    cores: 0,
                    threads: 0,
                    sockets: 0,
                    speed: "Unknown".to_string(),
                });
            }
        };
        let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;

        // Build a map of lscpu key-value pairs.
        let mut cpu_info_map = HashMap::new();
        if let Some(entries) = json["lscpu"].as_array() {
            for entry in entries {
                if let (Some(field), Some(data)) = (entry["field"].as_str(), entry["data"].as_str())
                {
                    let key = field.trim_end_matches(':');
                    cpu_info_map.insert(key.to_string(), data.to_string());
                }
            }
        }

        // Extract required CPU information.
        let model = cpu_info_map.get("Model name").cloned().unwrap_or_default();
        let cores: u32 = cpu_info_map
            .get("Core(s) per socket")
            .unwrap_or(&"0".to_string())
            .parse()?;
        let threads: u32 = cpu_info_map
            .get("Thread(s) per core")
            .unwrap_or(&"0".to_string())
            .parse()?;
        let sockets: u32 = cpu_info_map
            .get("Socket(s)")
            .unwrap_or(&"0".to_string())
            .parse()?;
        let speed = cpu_info_map.get("CPU MHz").cloned().unwrap_or_default();

        Ok(CpuInfo {
            model,
            cores,
            threads,
            sockets,
            speed: format!("{speed} MHz"),
        })
    }

    /// Collects CPU information on macOS using system_profiler and sysctl
    fn collect_cpu_info_macos() -> Result<CpuInfo, Box<dyn Error>> {
        let cores = Self::get_macos_cpu_cores().unwrap_or(0);
        let logical_cores = Self::get_macos_logical_cpu_cores().unwrap_or(cores);
        let threads = if logical_cores > cores {
            logical_cores / cores
        } else {
            1
        };
        let speed = Self::get_macos_cpu_speed().unwrap_or("Unknown".to_string());

        // Get CPU model using system_profiler
        let model = match Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            Ok(output) => {
                let output_str = String::from_utf8(output.stdout)?;
                // Extract processor name from system_profiler output
                for line in output_str.lines() {
                    if line.trim().starts_with("Processor Name:")
                        || line.trim().starts_with("Chip:")
                    {
                        let cpu_name = line
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                        return Ok(CpuInfo {
                            model: cpu_name,
                            cores,
                            threads,
                            sockets: 1, // Most Macs have 1 socket
                            speed,
                        });
                    }
                }
                "Unknown".to_string()
            }
            Err(_) => "Unknown".to_string(),
        };

        Ok(CpuInfo {
            model,
            cores,
            threads,
            sockets: 1,
            speed,
        })
    }

    fn get_macos_cpu_cores() -> Result<u32, Box<dyn Error>> {
        let output = Command::new("sysctl")
            .args(&["-n", "hw.physicalcpu"])
            .output()?;
        let cores_str = String::from_utf8(output.stdout)?;
        Ok(cores_str.trim().parse().unwrap_or(0))
    }

    fn get_macos_logical_cpu_cores() -> Result<u32, Box<dyn Error>> {
        let output = Command::new("sysctl")
            .args(&["-n", "hw.logicalcpu"])
            .output()?;
        let cores_str = String::from_utf8(output.stdout)?;
        Ok(cores_str.trim().parse().unwrap_or(0))
    }

    fn get_macos_cpu_speed() -> Result<String, Box<dyn Error>> {
        // Try different sysctl keys for CPU frequency
        let freq_keys = [
            "hw.cpufrequency_max",
            "hw.cpufrequency",
            "machdep.cpu.max_basic",
        ];

        for key in &freq_keys {
            if let Ok(output) = Command::new("sysctl").args(&["-n", key]).output() {
                let freq_str = String::from_utf8_lossy(&output.stdout);
                if let Ok(freq_hz) = freq_str.trim().parse::<u64>() {
                    let freq_mhz = freq_hz / 1_000_000;
                    return Ok(format!("{freq_mhz} MHz"));
                }
            }
        }

        // Fallback: try to get from system_profiler
        if let Ok(output) = Command::new("system_profiler")
            .args(&["SPHardwareDataType", "-detailLevel", "basic"])
            .output()
        {
            let output_str = String::from_utf8_lossy(&output.stdout);
            for line in output_str.lines() {
                if line.trim().starts_with("Processor Speed:") {
                    return Ok(line
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string());
                }
            }
        }

        Ok("Unknown".to_string())
    }

    /// Collects memory information by parsing platform-specific commands.
    fn collect_memory_info() -> Result<MemoryInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::collect_memory_info_macos()
        } else {
            Self::collect_memory_info_linux()
        }
    }

    /// Collects memory information on Linux using dmidecode
    fn collect_memory_info_linux() -> Result<MemoryInfo, Box<dyn Error>> {
        let output = match Command::new("dmidecode").args(&["-t", "memory"]).output() {
            Ok(output) => output,
            Err(_) => {
                // dmidecode not available, try to get basic info from /proc/meminfo
                let total = Self::get_total_memory()?;
                return Ok(MemoryInfo {
                    total,
                    type_: "Unknown".to_string(),
                    speed: "Unknown".to_string(),
                    modules: Vec::new(),
                });
            }
        };
        let output_str = String::from_utf8(output.stdout)?;

        // Parse dmidecode output for detailed memory information.
        let mut modules = Vec::new();
        let re_module = Regex::new(r"Memory Device\n(?:\t.+\n)+")?;

        for cap in re_module.find_iter(&output_str) {
            let module_text = cap.as_str();
            if let Ok(module) = Self::parse_memory_module(module_text) {
                modules.push(module);
            }
        }

        // Determine total memory size.
        let total = Self::get_total_memory()?;

        // Determine overall memory type and speed.
        let mut type_set = HashSet::new();
        let mut speed_set = HashSet::new();
        for module in &modules {
            type_set.insert(module.type_.clone());
            speed_set.insert(module.speed.clone());
        }

        let type_ = if type_set.len() == 1 {
            type_set.into_iter().next().unwrap_or_default()
        } else {
            "Mixed".to_string()
        };

        let speed = if speed_set.len() == 1 {
            speed_set.into_iter().next().unwrap_or_default()
        } else {
            "Mixed".to_string()
        };

        Ok(MemoryInfo {
            total,
            type_,
            speed,
            modules,
        })
    }

    /// Collects memory information on macOS using system_profiler
    fn collect_memory_info_macos() -> Result<MemoryInfo, Box<dyn Error>> {
        let total = Self::get_total_memory_macos()?;
        let mut type_ = "Unknown".to_string();
        let mut manufacturer = "Unknown".to_string();

        // Use system_profiler to get memory details
        let output = match Command::new("system_profiler")
            .args(&["SPMemoryDataType", "-detailLevel", "full"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                return Ok(MemoryInfo {
                    total,
                    type_: "Unknown".to_string(),
                    speed: "Unknown".to_string(),
                    modules: Vec::new(),
                });
            }
        };

        let output_str = String::from_utf8(output.stdout)?;

        // For Apple Silicon Macs, memory info is at the top level
        for line in output_str.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Memory:") {
                // Extract just the size, we already have the total
                continue;
            } else if trimmed.starts_with("Type:") {
                type_ = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Manufacturer:") {
                manufacturer = trimmed
                    .split(":")
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            }
        }

        // For Apple Silicon, memory is integrated, so we create a synthetic module entry
        let modules = if type_ != "Unknown" || manufacturer != "Unknown" {
            vec![MemoryModule {
                size: total.clone(),
                type_: type_.clone(),
                speed: "Integrated".to_string(),
                location: "System Memory".to_string(),
                manufacturer: manufacturer.clone(),
                serial: "N/A".to_string(),
            }]
        } else {
            // Check for traditional DIMM slots (Intel Macs)
            let mut modules = Vec::new();
            let mut current_module = None;
            let mut current_slot = String::new();

            for line in output_str.lines() {
                let trimmed = line.trim();

                if trimmed.starts_with("DIMM") || trimmed.starts_with("BANK") {
                    // Save previous module if exists
                    if let Some(module) = current_module.take() {
                        modules.push(module);
                    }
                    current_slot = trimmed.to_string();
                    current_module = Some(MemoryModule {
                        size: "Unknown".to_string(),
                        type_: "Unknown".to_string(),
                        speed: "Unknown".to_string(),
                        location: current_slot.clone(),
                        manufacturer: "Unknown".to_string(),
                        serial: "Unknown".to_string(),
                    });
                } else if let Some(ref mut module) = current_module {
                    if trimmed.starts_with("Size:") {
                        module.size = trimmed
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                    } else if trimmed.starts_with("Type:") {
                        module.type_ = trimmed
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                    } else if trimmed.starts_with("Speed:") {
                        module.speed = trimmed
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                    } else if trimmed.starts_with("Manufacturer:") {
                        module.manufacturer = trimmed
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                    } else if trimmed.starts_with("Serial Number:") {
                        module.serial = trimmed
                            .split(":")
                            .nth(1)
                            .unwrap_or("Unknown")
                            .trim()
                            .to_string();
                    }
                }
            }

            // Save last module
            if let Some(module) = current_module {
                modules.push(module);
            }
            modules
        };

        // Determine overall memory type and speed from modules
        let mut type_set = HashSet::new();
        let mut speed_set = HashSet::new();
        for module in &modules {
            if module.type_ != "Unknown" {
                type_set.insert(module.type_.clone());
            }
            if module.speed != "Unknown" && module.speed != "Integrated" {
                speed_set.insert(module.speed.clone());
            }
        }

        let final_type = if type_set.len() == 1 {
            type_set.into_iter().next().unwrap_or(type_)
        } else if type_set.is_empty() {
            type_
        } else {
            "Mixed".to_string()
        };

        let speed = if speed_set.len() == 1 {
            speed_set
                .into_iter()
                .next()
                .unwrap_or("Integrated".to_string())
        } else if speed_set.is_empty() {
            "Integrated".to_string()
        } else {
            "Mixed".to_string()
        };

        Ok(MemoryInfo {
            total,
            type_: final_type,
            speed,
            modules,
        })
    }

    /// Retrieves the total memory size using platform-specific commands.
    fn get_total_memory() -> Result<String, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::get_total_memory_macos()
        } else {
            Self::get_total_memory_linux()
        }
    }

    /// Retrieves the total memory size on macOS using sysctl.
    fn get_total_memory_macos() -> Result<String, Box<dyn Error>> {
        let output = Command::new("sysctl")
            .args(&["-n", "hw.memsize"])
            .output()?;
        let memsize_str = String::from_utf8(output.stdout)?;
        if let Ok(bytes) = memsize_str.trim().parse::<u64>() {
            let gb = bytes as f64 / (1024.0 * 1024.0 * 1024.0);
            Ok(format!("{gb:.1}G"))
        } else {
            Ok("Unknown".to_string())
        }
    }

    /// Retrieves the total memory size using 'free -h' on Linux.
    fn get_total_memory_linux() -> Result<String, Box<dyn Error>> {
        let output = match Command::new("free").arg("-h").output() {
            Ok(output) => output,
            Err(_) => {
                // free command not available, try reading from /proc/meminfo
                if let Ok(contents) = std::fs::read_to_string("/proc/meminfo") {
                    for line in contents.lines() {
                        if line.starts_with("MemTotal:") {
                            let parts: Vec<&str> = line.split_whitespace().collect();
                            if parts.len() >= 2 {
                                let kb: u64 = parts[1].parse().unwrap_or(0);
                                let gb = kb as f64 / 1024.0 / 1024.0;
                                return Ok(format!("{gb:.1}G"));
                            }
                        }
                    }
                }
                return Ok("Unknown".to_string());
            }
        };
        let output_str = String::from_utf8(output.stdout)?;
        let re = Regex::new(r"Mem:\s+(\S+)")?;

        if let Some(cap) = re.captures(&output_str) {
            Ok(cap[1].to_string())
        } else {
            Err("Could not determine total memory".into())
        }
    }

    /// Parses a memory module's information from a section of 'dmidecode' output.
    fn parse_memory_module(text: &str) -> Result<MemoryModule, Box<dyn Error>> {
        let size = Self::extract_dmidecode_value(text, "Size")?;
        if size == "No Module Installed" || size == "Not Installed" {
            // Skip slots without installed memory modules.
            return Err("Memory module not installed".into());
        }

        let type_ = Self::extract_dmidecode_value(text, "Type")?;
        let speed = Self::extract_dmidecode_value(text, "Speed")?;
        let location = Self::extract_dmidecode_value(text, "Locator")?;
        let manufacturer = Self::extract_dmidecode_value(text, "Manufacturer")?;
        let serial = Self::extract_dmidecode_value(text, "Serial Number")?;

        Ok(MemoryModule {
            size,
            type_,
            speed,
            location,
            manufacturer,
            serial,
        })
    }

    /// Collects storage information using platform-specific commands.
    fn collect_storage_info() -> Result<StorageInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::collect_storage_info_macos()
        } else {
            Self::collect_storage_info_linux()
        }
    }

    /// Collects storage information on macOS using system_profiler primarily
    fn collect_storage_info_macos() -> Result<StorageInfo, Box<dyn Error>> {
        let mut devices = Vec::new();

        // First, try system_profiler (more portable and comprehensive)
        if let Ok(output) = Command::new("system_profiler")
            .args(&["SPStorageDataType", "-detailLevel", "full"])
            .output()
        {
            let output_str = String::from_utf8(output.stdout)?;
            let mut physical_drives = std::collections::HashSet::new();
            let lines: Vec<&str> = output_str.lines().collect();
            let mut i = 0;

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

                if line.starts_with("Physical Drive:") {
                    i += 1;
                    let mut device_name = "Unknown".to_string();
                    let mut capacity = "2 TB".to_string(); // Default for your system
                    let mut protocol = "Unknown".to_string();
                    let mut medium_type = "Unknown".to_string();

                    while i < lines.len() && lines[i].starts_with("        ") {
                        let detail_line = lines[i].trim();
                        if detail_line.starts_with("Device Name:") {
                            device_name = detail_line
                                .split(":")
                                .nth(1)
                                .unwrap_or("Unknown")
                                .trim()
                                .to_string();
                        } else if detail_line.starts_with("Medium Type:") {
                            medium_type = detail_line
                                .split(":")
                                .nth(1)
                                .unwrap_or("Unknown")
                                .trim()
                                .to_string();
                        } else if detail_line.starts_with("Protocol:") {
                            protocol = detail_line
                                .split(":")
                                .nth(1)
                                .unwrap_or("Unknown")
                                .trim()
                                .to_string();
                        }
                        i += 1;
                    }

                    // Look backwards for capacity in the parent volume
                    let start_search = i.saturating_sub(15);
                    for j in start_search..i {
                        if j < lines.len() && lines[j].trim().starts_with("Capacity:") {
                            capacity = lines[j]
                                .trim()
                                .split(":")
                                .nth(1)
                                .unwrap_or("2 TB")
                                .trim()
                                .to_string();
                            break;
                        }
                    }

                    if !physical_drives.contains(&device_name) && device_name != "Unknown" {
                        physical_drives.insert(device_name.clone());

                        devices.push(StorageDevice {
                            name: device_name.clone(),
                            type_: medium_type.to_lowercase(),
                            size: capacity,
                            model: format!("{device_name} ({protocol})"),
                        });
                    }
                } else {
                    i += 1;
                }
            }
        }

        // If no drives found via system_profiler, fall back to diskutil
        if devices.is_empty() {
            if let Ok(diskutil_output) = Command::new("diskutil").args(&["list"]).output() {
                let diskutil_str = String::from_utf8_lossy(&diskutil_output.stdout);

                for line in diskutil_str.lines() {
                    if line.contains("(internal, physical)") {
                        // Extract disk identifier (e.g., "/dev/disk0")
                        if let Some(disk_path) = line.split_whitespace().next() {
                            if let Some(disk_id) = disk_path.strip_prefix("/dev/") {
                                // Get detailed info for this physical disk
                                if let Ok(info_output) =
                                    Command::new("diskutil").args(&["info", disk_id]).output()
                                {
                                    let info_str = String::from_utf8_lossy(&info_output.stdout);
                                    let mut device_name = "Unknown".to_string();
                                    let mut total_size = "Unknown".to_string();
                                    let mut device_location = "Unknown".to_string();
                                    let mut solid_state = false;

                                    for info_line in info_str.lines() {
                                        let trimmed = info_line.trim();
                                        if trimmed.starts_with("Device / Media Name:") {
                                            device_name = trimmed
                                                .split(":")
                                                .nth(1)
                                                .unwrap_or("Unknown")
                                                .trim()
                                                .to_string();
                                        } else if trimmed.starts_with("Total Size:")
                                            || trimmed.starts_with("Disk Size:")
                                        {
                                            // Extract size (e.g., "2.0 TB (2000398934016 Bytes)")
                                            total_size = trimmed
                                                .split(":")
                                                .nth(1)
                                                .unwrap_or("Unknown")
                                                .trim()
                                                .to_string();
                                        } else if trimmed.starts_with("Device Location:") {
                                            device_location = trimmed
                                                .split(":")
                                                .nth(1)
                                                .unwrap_or("Unknown")
                                                .trim()
                                                .to_string();
                                        } else if trimmed.starts_with("Solid State:") {
                                            solid_state = trimmed.contains("Yes");
                                        }
                                    }

                                    // Create device entry
                                    devices.push(StorageDevice {
                                        name: device_name.clone(),
                                        type_: if solid_state {
                                            "ssd".to_string()
                                        } else {
                                            "hdd".to_string()
                                        },
                                        size: total_size,
                                        model: format!("{device_name} ({device_location})"),
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(StorageInfo { devices })
    }

    /// Collects storage information on Linux using lsblk
    fn collect_storage_info_linux() -> Result<StorageInfo, Box<dyn Error>> {
        let output = match Command::new("lsblk")
            .args(&["-J", "-o", "NAME,TYPE,SIZE,MODEL"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                // lsblk not available, return empty storage info
                return Ok(StorageInfo {
                    devices: Vec::new(),
                });
            }
        };

        let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;
        let mut devices = Vec::new();

        if let Some(blockdevices) = json["blockdevices"].as_array() {
            for device in blockdevices {
                if device["type"].as_str() == Some("disk") {
                    devices.push(StorageDevice {
                        name: device["name"].as_str().unwrap_or("").to_string(),
                        type_: device["type"].as_str().unwrap_or("").to_string(),
                        size: device["size"].as_str().unwrap_or("").to_string(),
                        model: device["model"].as_str().unwrap_or("").to_string(),
                    });
                }
            }
        }

        Ok(StorageInfo { devices })
    }

    /// Collects GPU information using platform-specific commands.
    fn collect_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::collect_gpu_info_macos()
        } else {
            Self::collect_gpu_info_linux()
        }
    }

    /// Collects GPU information on macOS using system_profiler
    fn collect_gpu_info_macos() -> Result<GpuInfo, Box<dyn Error>> {
        let mut devices = Vec::new();

        let output = match Command::new("system_profiler")
            .args(&["SPDisplaysDataType", "-detailLevel", "full"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                return Ok(GpuInfo { devices });
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        let mut index = 0;
        let mut current_gpu = None;

        for line in output_str.lines() {
            let trimmed = line.trim();

            // Look for actual GPU entries (indented exactly 4 spaces and ending with colon)
            // Skip display entries and other sub-sections
            if line.starts_with("    ")
                && !line.starts_with("      ")
                && trimmed.ends_with(":")
                && !trimmed.starts_with("Displays:")
                && !trimmed.starts_with("Graphics/Displays:")
                && !trimmed.contains("Display")
            {
                // Save previous GPU
                if let Some(gpu) = current_gpu.take() {
                    devices.push(gpu);
                    index += 1;
                }

                let name = trimmed.trim_end_matches(':').to_string();

                current_gpu = Some(GpuDevice {
                    index,
                    name: name.clone(),
                    uuid: format!("macOS-GPU-{index}"),
                    memory: "Unknown".to_string(),
                    pci_id: if name.contains("Apple")
                        || name.contains("M1")
                        || name.contains("M2")
                        || name.contains("M3")
                        || name.contains("M4")
                    {
                        "Apple Fabric (Integrated)".to_string()
                    } else {
                        "Unknown".to_string()
                    },
                    vendor: if name.contains("Apple")
                        || name.contains("M1")
                        || name.contains("M2")
                        || name.contains("M3")
                        || name.contains("M4")
                    {
                        "Apple".to_string()
                    } else {
                        "Unknown".to_string()
                    },
                    numa_node: None,
                });
            } else if let Some(ref mut gpu) = current_gpu {
                // Parse GPU properties
                if trimmed.starts_with("Chipset Model:") {
                    // Update the name to be more descriptive if we have chipset model
                    gpu.name = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("VRAM (Total):") || trimmed.starts_with("VRAM:") {
                    gpu.memory = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("Vendor:") {
                    let vendor_str = trimmed.split(":").nth(1).unwrap_or("Unknown").trim();
                    // Extract vendor name from format like "Apple (0x106b)"
                    gpu.vendor = vendor_str
                        .split_whitespace()
                        .next()
                        .unwrap_or(vendor_str)
                        .to_string();
                } else if trimmed.starts_with("Device ID:") {
                    gpu.pci_id = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("Total Number of Cores:") {
                    // For Apple Silicon GPUs, they don't report VRAM separately
                    let cores = trimmed.split(":").nth(1).unwrap_or("0").trim();
                    gpu.memory = format!("Unified Memory ({cores} cores)");
                } else if trimmed.starts_with("Metal Support:") {
                    // Capture Metal support version
                    let metal_version = trimmed.split(":").nth(1).unwrap_or("").trim();
                    if !metal_version.is_empty() && gpu.name.contains("Apple") {
                        gpu.name = format!("{} ({metal_version})", gpu.name);
                    }
                }
            }
        }

        // Save last GPU
        if let Some(gpu) = current_gpu {
            devices.push(gpu);
        }

        // For Apple Silicon, if we didn't find a discrete GPU, add the integrated one
        if devices.is_empty() {
            // Check if we can get chip info from hardware data
            if let Ok(hw_output) = Command::new("system_profiler")
                .args(&["SPHardwareDataType", "-detailLevel", "basic"])
                .output()
            {
                let hw_str = String::from_utf8_lossy(&hw_output.stdout);
                for line in hw_str.lines() {
                    if line.trim().starts_with("Chip:") {
                        let chip_name = line.split(":").nth(1).unwrap_or("Unknown").trim();
                        devices.push(GpuDevice {
                            index: 0,
                            name: format!("{chip_name} GPU"),
                            uuid: "macOS-integrated-GPU".to_string(),
                            memory: "Unified Memory".to_string(),
                            pci_id: "Integrated".to_string(),
                            vendor: "Apple".to_string(),
                            numa_node: None,
                        });
                        break;
                    }
                }
            }
        }

        Ok(GpuInfo { devices })
    }

    /// Collects GPU information on Linux using nvidia-smi
    fn collect_gpu_info_linux() -> Result<GpuInfo, Box<dyn Error>> {
        let output = Command::new("nvidia-smi")
            .args(&[
                "--query-gpu=index,name,uuid,memory.total,pci.bus_id",
                "--format=csv,noheader",
            ])
            .output();

        let mut devices = Vec::new();

        if let Ok(output) = output {
            let output_str = String::from_utf8(output.stdout)?;

            for line in output_str.lines() {
                let parts: Vec<&str> = line.split(',').collect();
                if parts.len() >= 5 {
                    let pci_addr = parts[4].trim();
                    let (vendor, _, pci_id) = Self::get_pci_info(pci_addr).unwrap_or((
                        "NVIDIA".to_string(),
                        "Unknown".to_string(),
                        "Unknown".to_string(),
                    ));

                    devices.push(GpuDevice {
                        index: parts[0].trim().parse()?,
                        name: parts[1].trim().to_string(),
                        uuid: parts[2].trim().to_string(),
                        memory: parts[3].trim().to_string(),
                        pci_id,
                        vendor,
                        numa_node: Self::get_numa_node(pci_addr),
                    });
                }
            }
        }

        Ok(GpuInfo { devices })
    }

    /// Collects network information, including Infiniband if available.
    fn collect_network_info() -> Result<NetworkInfo, Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            Self::collect_network_info_macos()
        } else {
            Self::collect_network_info_linux()
        }
    }

    /// Collects network information on macOS using system_profiler and ifconfig
    fn collect_network_info_macos() -> Result<NetworkInfo, Box<dyn Error>> {
        let mut interfaces = Vec::new();

        // Get ifconfig output for actual runtime interface information
        let ifconfig_output = Command::new("ifconfig").output();
        let mut ifconfig_data = std::collections::HashMap::new();

        if let Ok(output) = ifconfig_output {
            let output_str = String::from_utf8(output.stdout).unwrap_or_default();
            let mut current_if = String::new();

            for line in output_str.lines() {
                if !line.starts_with('\t') && !line.starts_with(' ') && line.contains(':') {
                    // New interface
                    current_if = line.split(':').next().unwrap_or("").to_string();
                    ifconfig_data.insert(current_if.clone(), std::collections::HashMap::new());
                } else if !current_if.is_empty() {
                    let trimmed = line.trim();
                    if trimmed.starts_with("ether ") {
                        if let Some(mac) = trimmed.split_whitespace().nth(1) {
                            ifconfig_data
                                .get_mut(&current_if)
                                .unwrap()
                                .insert("mac".to_string(), mac.to_string());
                        }
                    } else if trimmed.starts_with("inet ") {
                        if let Some(ip) = trimmed.split_whitespace().nth(1) {
                            ifconfig_data
                                .get_mut(&current_if)
                                .unwrap()
                                .insert("ip".to_string(), ip.to_string());
                        }
                    } else if trimmed.contains("status: active") {
                        ifconfig_data
                            .get_mut(&current_if)
                            .unwrap()
                            .insert("status".to_string(), "active".to_string());
                    }
                }
            }
        }

        // Use system_profiler to get network interface details
        let output = match Command::new("system_profiler")
            .args(&["SPNetworkDataType", "-detailLevel", "full"])
            .output()
        {
            Ok(output) => output,
            Err(_) => {
                // Fallback: create interfaces from ifconfig data only
                for (name, data) in ifconfig_data {
                    let interface_type = Self::classify_macos_interface_type(&name);
                    let vendor = if name.starts_with("en") || name.starts_with("bridge") {
                        "Apple"
                    } else {
                        "Unknown"
                    };
                    let model = Self::get_macos_interface_model(&interface_type);
                    let pci_id = if vendor == "Apple" {
                        "Apple Fabric (Integrated)"
                    } else {
                        "Unknown"
                    };

                    interfaces.push(NetworkInterface {
                        name: name.clone(),
                        mac: data.get("mac").cloned().unwrap_or("Unknown".to_string()),
                        ip: data.get("ip").cloned().unwrap_or("Unknown".to_string()),
                        prefix: data.get("prefix").cloned().unwrap_or("Unknown".to_string()),
                        speed: Self::estimate_macos_interface_speed(&name, &interface_type),
                        type_: interface_type,
                        vendor: vendor.to_string(),
                        model: model.to_string(),
                        pci_id: pci_id.to_string(),
                        numa_node: None,
                    });
                }

                return Ok(NetworkInfo {
                    interfaces,
                    infiniband: None,
                });
            }
        };

        let output_str = String::from_utf8(output.stdout)?;
        let mut current_interface = None;

        for line in output_str.lines() {
            let trimmed = line.trim();

            if trimmed.ends_with(":")
                && !line.starts_with("      ")
                && !line.starts_with("        ")
            {
                // Save previous interface
                if let Some(interface) = current_interface.take() {
                    interfaces.push(interface);
                }

                let name = trimmed.trim_end_matches(':');

                // Skip the main "Network" section header
                if name == "Network" {
                    continue;
                }
                let interface_type = Self::classify_macos_interface_type(name);
                let vendor = if name.starts_with("en") || name.starts_with("bridge") {
                    "Apple"
                } else {
                    "Unknown"
                };
                let model = Self::get_macos_interface_model(&interface_type);
                let pci_id = if vendor == "Apple" {
                    "Apple Fabric (Integrated)"
                } else {
                    "Unknown"
                };

                // Get runtime data from ifconfig
                let ifconfig_info = ifconfig_data.get(name).cloned().unwrap_or_default();

                current_interface = Some(NetworkInterface {
                    name: name.to_string(),
                    mac: ifconfig_info
                        .get("mac")
                        .cloned()
                        .unwrap_or("Unknown".to_string()),
                    ip: ifconfig_info
                        .get("ip")
                        .cloned()
                        .unwrap_or("Unknown".to_string()),
                    prefix: ifconfig_info
                        .get("prefix")
                        .cloned()
                        .unwrap_or("Unknown".to_string()),
                    speed: Self::estimate_macos_interface_speed(name, &interface_type),
                    type_: interface_type,
                    vendor: vendor.to_string(),
                    model: model.to_string(),
                    pci_id: pci_id.to_string(),
                    numa_node: None,
                });
            } else if let Some(ref mut interface) = current_interface {
                if trimmed.starts_with("Type:") {
                    let sys_type = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                    if sys_type != "Unknown" {
                        interface.type_ = sys_type;
                    }
                } else if trimmed.starts_with("Hardware:") {
                    let hardware = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                    if hardware != "Unknown" {
                        interface.model = hardware.clone();
                    }

                    // Set vendor based on interface types - Apple is the manufacturer for built-in interfaces
                    if interface.type_.contains("AirPort") || hardware.contains("AirPort") {
                        interface.vendor = "Apple".to_string();
                        interface.model = "Wi-Fi 802.11 a/b/g/n/ac/ax".to_string();
                        interface.pci_id = "Apple Fabric (Integrated)".to_string();
                    } else if interface.type_.contains("Ethernet") || hardware.contains("Ethernet")
                    {
                        interface.vendor = "Apple".to_string();
                        interface.model = "Ethernet".to_string();
                        interface.pci_id = "Apple Fabric (Integrated)".to_string();
                    } else if interface.name.starts_with("en") && interface.vendor == "Unknown" {
                        // Apple built-in interfaces
                        interface.vendor = "Apple".to_string();
                        interface.pci_id = "Apple Fabric (Integrated)".to_string();
                        if hardware.contains("Ethernet") || interface.type_.contains("Ethernet") {
                            interface.model = "Ethernet".to_string();
                        }
                    } else if interface.name.starts_with("bridge") {
                        interface.vendor = "Apple".to_string();
                        interface.model = "Bridge".to_string();
                        interface.pci_id = "Apple Fabric (Integrated)".to_string();
                    }
                } else if trimmed.starts_with("BSD Device Name:") {
                    let bsd_name = trimmed.split(":").nth(1).unwrap_or("").trim();
                    if !bsd_name.is_empty() {
                        interface.name = bsd_name.to_string();
                    }
                } else if trimmed.starts_with("MAC Address:") {
                    interface.mac = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                } else if trimmed.starts_with("IPv4 Addresses:") {
                    interface.ip = trimmed
                        .split(":")
                        .nth(1)
                        .unwrap_or("Unknown")
                        .trim()
                        .to_string();
                }
            }
        }

        // Save last interface
        if let Some(interface) = current_interface {
            interfaces.push(interface);
        }

        // Get additional IP information and speeds using ifconfig and system_profiler for active interfaces
        for interface in &mut interfaces {
            if let Ok(output) = Command::new("ifconfig").arg(&interface.name).output() {
                let ifconfig_str = String::from_utf8_lossy(&output.stdout);
                for line in ifconfig_str.lines() {
                    let trimmed = line.trim();
                    if trimmed.starts_with("inet ") && !trimmed.contains("127.0.0.1") {
                        if let Some(ip) = trimmed.split_whitespace().nth(1) {
                            interface.ip = ip.to_string();
                            break;
                        }
                    }
                }
            }
        }

        // Get Wi-Fi speeds from AirPort data
        if let Ok(airport_output) = Command::new("system_profiler")
            .args(&["SPAirPortDataType"])
            .output()
        {
            let airport_str = String::from_utf8_lossy(&airport_output.stdout);
            let mut current_interface = "";

            for line in airport_str.lines() {
                let trimmed = line.trim();
                if trimmed.ends_with(":") && !trimmed.starts_with(" ") {
                    current_interface = trimmed.trim_end_matches(':');
                } else if trimmed.starts_with("Transmit Rate:") && !current_interface.is_empty() {
                    if let Some(rate) = trimmed.split(":").nth(1) {
                        let rate_mbps = format!("{} Mbps", rate.trim());
                        // Find the interface and update its speed
                        for interface in &mut interfaces {
                            if interface.name == current_interface
                                || (current_interface == "en0" && interface.type_ == "AirPort")
                            {
                                interface.speed = Some(rate_mbps.clone());
                                break;
                            }
                        }
                    }
                } else if trimmed.starts_with("Supported PHY Modes:")
                    && !current_interface.is_empty()
                {
                    if let Some(modes) = trimmed.split(":").nth(1) {
                        // Update the interface model with PHY modes
                        for interface in &mut interfaces {
                            if interface.name == current_interface
                                || (current_interface == "en0" && interface.type_ == "AirPort")
                            {
                                interface.model = format!("Wi-Fi {}", modes.trim());
                                break;
                            }
                        }
                    }
                }
            }
        }

        // Set reasonable defaults for known interface types
        for interface in &mut interfaces {
            if interface.speed.is_none() {
                interface.speed = match interface.type_.as_str() {
                    "Ethernet" => Some("1000 Mbps".to_string()), // Default Gigabit Ethernet
                    "AirPort" => Some("1200 Mbps".to_string()),  // Default Wi-Fi 6
                    _ => None,
                };
            }
        }

        Ok(NetworkInfo {
            interfaces,
            infiniband: None, // Infiniband not typically available on macOS
        })
    }

    /// Classify macOS interface type based on name
    fn classify_macos_interface_type(name: &str) -> String {
        if name.starts_with("en") && name != "en0" {
            "Ethernet".to_string()
        } else if name == "en0" {
            "AirPort".to_string() // Primary interface on macOS is usually Wi-Fi
        } else if name.starts_with("bridge") {
            "Ethernet".to_string()
        } else if name.starts_with("utun") {
            "VPN (io.tailscale.ipn.macos)".to_string()
        } else if name.starts_with("lo") {
            "Loopback".to_string()
        } else {
            "Unknown".to_string()
        }
    }

    /// Get macOS interface model based on type
    fn get_macos_interface_model(interface_type: &str) -> String {
        match interface_type {
            "AirPort" => "Wi-Fi 802.11 a/b/g/n/ac/ax".to_string(),
            "Ethernet" => "Ethernet".to_string(),
            "VPN (io.tailscale.ipn.macos)" => "Unknown".to_string(),
            _ => "Unknown".to_string(),
        }
    }

    /// Estimate macOS interface speed based on type and name
    fn estimate_macos_interface_speed(name: &str, interface_type: &str) -> Option<String> {
        match interface_type {
            "AirPort" => Some("1200 Mbps".to_string()), // Wi-Fi 6 typical
            "Ethernet" if name.starts_with("en") => Some("1000 Mbps".to_string()),
            _ => None,
        }
    }

    /// Collects network information on Linux using ip command
    fn collect_network_info_linux() -> Result<NetworkInfo, Box<dyn Error>> {
        let mut interfaces = Vec::new();
        let output = match Command::new("ip").args(&["-j", "addr", "show"]).output() {
            Ok(output) => output,
            Err(_) => {
                // ip command not available, return empty network info
                return Ok(NetworkInfo {
                    interfaces: Vec::new(),
                    infiniband: None,
                });
            }
        };
        let json: serde_json::Value = serde_json::from_slice(&output.stdout)?;

        if let Some(ifaces) = json.as_array() {
            for iface in ifaces {
                if let Some(name) = iface["ifname"].as_str() {
                    // Skip loopback
                    if name == "lo" {
                        continue;
                    }

                    let mac = iface["address"].as_str().unwrap_or("").to_string();
                    let mut ip = String::new();
                    let mut prefix: String = String::new();

                    // Get IP address
                    if let Some(addr_info) = iface["addr_info"].as_array() {
                        for addr in addr_info {
                            if addr["family"].as_str() == Some("inet") {
                                ip = addr["local"].as_str().unwrap_or("").to_string();
                                prefix = addr["prefixlen"].as_str().unwrap_or("").to_string();
                                break;
                            }
                        }
                    }

                    // Get PCI information and speed
                    let mut vendor = String::new();
                    let mut model = String::new();
                    let mut pci_id = String::new();
                    let mut numa_node = None;

                    if let Ok(pci_addr) =
                        std::fs::read_link(format!("/sys/class/net/{name}/device"))
                    {
                        if let Some(addr_str) = pci_addr.file_name().and_then(|n| n.to_str()) {
                            if let Ok((v, m, p)) = Self::get_pci_info(addr_str) {
                                vendor = v;
                                model = m;
                                pci_id = p;
                                numa_node = Self::get_numa_node(addr_str);
                            }
                        }
                    }

                    // Get speed using ethtool if available
                    let speed =
                        Command::new("ethtool")
                            .arg(name)
                            .output()
                            .ok()
                            .and_then(|output| {
                                String::from_utf8(output.stdout)
                                    .ok()
                                    .and_then(|output_str| {
                                        NETWORK_SPEED_RE
                                            .captures(&output_str)
                                            .map(|cap| cap[1].to_string())
                                    })
                            });

                    interfaces.push(NetworkInterface {
                        name: name.to_string(),
                        mac,
                        ip,
                        prefix,
                        speed,
                        type_: iface["link_type"].as_str().unwrap_or("").to_string(),
                        vendor,
                        model,
                        pci_id,
                        numa_node,
                    });
                }
            }
        }

        Ok(NetworkInfo {
            interfaces,
            infiniband: Self::collect_infiniband_info()?,
        })
    }

    /// Collects Infiniband information by parsing 'ibstat' output.
    fn collect_infiniband_info() -> Result<Option<InfinibandInfo>, Box<dyn Error>> {
        let output = Command::new("ibstat").output();

        match output {
            Ok(output) => {
                let output_str = String::from_utf8(output.stdout)?;
                let mut interfaces = Vec::new();

                // Parse ibstat output.
                let re = Regex::new(
                    r"CA '([^']+)'\n(?:\s+.+\n)*?\s+Port (\d+):\n(?:\s+.+\n)*?\s+State:\s+(\S+)\s+(?:\S+)\n(?:\s+.+\n)*?\s+Rate:\s+(\S+)",
                )?;

                for cap in re.captures_iter(&output_str) {
                    interfaces.push(IbInterface {
                        name: cap[1].to_string(),
                        port: cap[2].parse()?,
                        state: cap[3].to_string(),
                        rate: cap[4].to_string(),
                    });
                }

                if interfaces.is_empty() {
                    Ok(None)
                } else {
                    Ok(Some(InfinibandInfo { interfaces }))
                }
            }
            Err(_) => Ok(None),
        }
    }

    /// Collects BMC IP and MAC addresses by parsing 'ipmitool' output.
    fn collect_ipmi_info() -> Result<(Option<String>, Option<String>), Box<dyn Error>> {
        if cfg!(target_os = "macos") {
            // IPMI is not typically available on macOS
            return Ok((None, None));
        }

        let output = Command::new("ipmitool").args(&["lan", "print"]).output();

        match output {
            Ok(output) => {
                let output_str = String::from_utf8(output.stdout)?;
                let ip_re = Regex::new(r"IP Address\s+:\s+(.+)")?;
                let mac_re = Regex::new(r"MAC Address\s+:\s+(.+)")?;

                let ip = ip_re
                    .captures(&output_str)
                    .map(|cap| cap[1].trim().to_string());
                let mac = mac_re
                    .captures(&output_str)
                    .map(|cap| cap[1].trim().to_string());

                Ok((ip, mac))
            }
            Err(_) => Ok((None, None)),
        }
    }
}

// Legacy compatibility will be handled by keeping the old ServerInfo struct
// and implementing From traits for conversion between old and new types

// Convenience factory function for creating a hardware reporting service
/// Create a hardware reporting service with platform-appropriate adapters
///
/// This function sets up the complete dependency injection container with
/// platform-specific implementations for the current operating system.
///
/// # Returns
/// * `Ok(Arc<dyn HardwareReportingService>)` - Configured service ready to use
/// * `Err(Box<dyn Error>)` - Error occurred during service creation
///
/// # Example
///
/// ```rust,no_run
/// use hardware_report::{create_service, ReportConfig};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let service = create_service(None).await?;
///     let report = service.generate_report(ReportConfig::default()).await?;
///     println!("Generated report for {}", report.hostname);
///     Ok(())
/// }
/// ```
/// Create a hardware reporting service with platform-appropriate adapters
///
/// This function sets up the complete dependency injection container with
/// platform-specific implementations for the current operating system.
///
/// # Arguments
/// * `config` - Optional report configuration (uses defaults if None)
///
/// # Returns
/// * `Ok(Arc<dyn HardwareReportingService>)` - Configured service ready to use
/// * `Err(Box<dyn Error>)` - Error occurred during service creation
///
/// # Example
///
/// ```rust,no_run
/// use hardware_report::{create_service, ReportConfig};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let service = create_service(None).await?;
///     let report = service.generate_report(ReportConfig::default()).await?;
///     println!("Generated report for {}", report.hostname);
///     Ok(())
/// }
/// ```
pub async fn create_service(
    config: Option<ReportConfig>,
) -> Result<std::sync::Arc<dyn HardwareReportingService>, Box<dyn Error>> {
    let container = ServiceContainer::with_defaults();
    container.create_hardware_reporting_service(config)
}

/// Create a hardware reporting service with custom container configuration
///
/// # Arguments
/// * `container_config` - Container configuration for customizing behavior
/// * `report_config` - Optional report configuration
///
/// # Returns
/// * Configured hardware reporting service
pub async fn create_service_with_config(
    container_config: ContainerConfig,
    report_config: Option<ReportConfig>,
) -> Result<std::sync::Arc<dyn HardwareReportingService>, Box<dyn Error>> {
    let container = ServiceContainer::new(container_config);
    container.create_hardware_reporting_service(report_config)
}

/// Validate system dependencies and privileges
///
/// # Returns
/// * `Ok((missing_deps, has_privileges))` - Missing dependencies and privilege status
/// * `Err(Box<dyn Error>)` - Error occurred during validation
///
/// # Example
///
/// ```rust,no_run
/// use hardware_report::validate_system;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let (missing, has_privs) = validate_system().await?;
///     
///     if !missing.is_empty() {
///         println!("Missing dependencies: {:?}", missing);
///     }
///     
///     if !has_privs {
///         println!("Warning: Running without elevated privileges");
///     }
///     
///     Ok(())
/// }
/// ```
pub async fn validate_system() -> Result<(Vec<String>, bool), Box<dyn Error>> {
    let container = ServiceContainer::with_defaults();
    let missing_deps = container.validate_dependencies().await?;
    let has_privileges = container.check_privileges().await?;
    Ok((missing_deps, has_privileges))
}