go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
#![feature(portable_simd)]

//! llm-brrr - Token-efficient code analysis for LLMs.
//!
//! This library provides tools for extracting and analyzing code structure
//! using tree-sitter parsers for multiple languages. It enables 95% token
//! savings when analyzing codebases by providing structured summaries instead
//! of raw source code.
//!
//! # Architecture
//!
//! The library is organized into several layers:
//!
//! - **AST Layer** ([`ast`]): File tree generation, code structure extraction, and AST parsing
//! - **CFG Layer** ([`cfg`]): Control flow graph extraction with cyclomatic complexity
//! - **DFG Layer** ([`dfg`]): Data flow graph extraction and basic program slicing
//! - **PDG Layer** ([`pdg`]): Program Dependence Graph combining CFG + DFG for accurate slicing
//! - **Call Graph Layer** ([`callgraph`]): Cross-file call graph analysis and impact detection
//! - **Semantic Layer** ([`semantic`]): Semantic pattern detection, embedding unit extraction, and code enrichment
//! - **Language Layer** ([`lang`]): Multi-language support via tree-sitter
//!
//! # Quick Start
//!
//! ```no_run
//! use go_brrr::{get_tree, get_tree_default, get_structure, extract_file, get_cfg, get_slice, get_backward_slice};
//!
//! // Get file tree for a project (convenience wrapper with default options)
//! let tree = get_tree_default("./src", Some(".py"))?;
//!
//! // Or use full API with explicit options
//! let tree_full = get_tree("./src", Some(".py"), true, true)?;
//!
//! // Get code structure (functions, classes) summary
//! let structure = get_structure("./src", Some("python"), 100, true)?;
//!
//! // Extract full AST from a single file (None = no base path validation)
//! let module = extract_file("./src/main.py", None)?;
//!
//! // Get control flow graph for a function
//! let cfg = get_cfg("./src/main.py", "process_data")?;
//!
//! // Get program slice (what affects line 42?) - using convenience wrapper
//! let affected_lines = get_backward_slice("./src/main.py", "process_data", 42)?;
//!
//! // Or use full API with direction, variable, and language options
//! let forward = get_slice("./src/main.py", "process_data", 10, Some("forward"), None, None)?;
//! let var_slice = get_slice("./src/main.py", "process_data", 42, None, Some("x"), None)?;
//! # Ok::<(), go_brrr::BrrrError>(())
//! ```
//!
//! # Call Graph Analysis
//!
//! ```no_run
//! use go_brrr::{build_callgraph, get_impact, find_dead_code, get_context};
//!
//! // Build project-wide call graph
//! let graph = build_callgraph("./src")?;
//!
//! // Find all callers of a function (impact analysis)
//! let callers = get_impact("./src", "critical_function", 3)?;
//!
//! // Find unreachable/dead code
//! let dead = find_dead_code("./src")?;
//!
//! // Get LLM-ready context for an entry point
//! let context = get_context("./src", "main", 2)?;
//! # Ok::<(), go_brrr::BrrrError>(())
//! ```
//!
//! # Project Scanning
//!
//! ```no_run
//! use go_brrr::{scan_project_files, scan_extensions, get_project_metadata, ScanConfig, scan_with_config};
//!
//! // Scan all source files (respects .gitignore and .brrrignore)
//! let result = scan_project_files("./project", None, true)?;
//! println!("Found {} files", result.files.len());
//!
//! // Scan only Python files
//! let py_result = scan_project_files("./project", Some("python"), true)?;
//!
//! // Scan by file extension
//! let rs_files = scan_extensions("./project", &[".rs", ".toml"])?;
//!
//! // Get file metadata (size, modification time, language)
//! let metadata = get_project_metadata("./project", None)?;
//! for meta in &metadata {
//!     println!("{}: {} bytes", meta.path.display(), meta.size);
//! }
//!
//! // Advanced: custom scan configuration
//! let config = ScanConfig::for_language("python")
//!     .with_excludes(&["**/test/**"])
//!     .with_metadata();
//! let result = scan_with_config("./project", &config)?;
//! # Ok::<(), go_brrr::BrrrError>(())
//! ```
//!
//! # Semantic Pattern Detection
//!
//! Automatically detect semantic patterns in code for enriched embeddings:
//!
//! ```
//! use go_brrr::{detect_semantic_patterns, SemanticPattern, SEMANTIC_PATTERNS};
//!
//! // Detect patterns in code
//! let code = "def validate_user(user): assert user is not None";
//! let patterns = detect_semantic_patterns(code);
//! assert!(patterns.contains(&"validation".to_string()));
//!
//! // Access all available patterns
//! for pattern in SEMANTIC_PATTERNS {
//!     println!("Pattern: {} - {}", pattern.name, pattern.pattern);
//! }
//! ```
//!
//! Detected patterns include: `crud`, `validation`, `transform`, `error_handling`,
//! `async_ops`, `iteration`, `api_endpoint`, `database`, `auth`, `cache`, `test`,
//! `logging`, and `config`.

// =============================================================================
// Module Declarations
// =============================================================================

pub mod ast;
pub mod callgraph;
pub mod cfg;
pub mod dfg;
pub mod embedding;
pub mod error;
pub mod metrics;
pub mod patterns;
pub mod pdg;
pub mod quality;
pub mod security;
pub mod semantic;
pub mod simd;
pub mod util;

/// Language support module with implementations for all supported languages.
pub mod lang {
    pub mod c;
    pub mod cpp;
    pub mod go;
    pub mod java;
    pub mod python;
    pub mod registry;
    pub mod rust_lang;
    pub mod traits;
    pub mod typescript;

    pub use registry::LanguageRegistry;
    pub use traits::{BoxedLanguage, Language};
}

// =============================================================================
// Public Type Re-exports
// =============================================================================

// Error types - most important for users
pub use error::{Result, BrrrError};

// AST types and utilities
pub use ast::{
    CallGraphInfo, ClassInfo, ClassSummary, CodeStructure, FieldInfo, FileTreeEntry, FunctionInfo,
    FunctionSummary, ImportInfo, ModuleInfo,
};

// AST extractor for direct access to parsing functionality
pub use ast::AstExtractor;

// AST cache management - allows consumers to free memory
pub use ast::{clear_parser_cache, clear_query_cache};

// Import extraction utility
pub use ast::extract_imports;

// CFG types
pub use cfg::{BlockId, BlockType, CFGBlock, CFGEdge, CFGError, CFGInfo, EdgeType};

// CFG rendering functions
pub use cfg::{
    to_ascii as cfg_to_ascii, to_dot as cfg_to_dot, to_json as cfg_to_json,
    to_json_compact as cfg_to_json_compact, to_mermaid as cfg_to_mermaid,
};

// DFG types
pub use dfg::{DFGInfo, DataflowEdge, DataflowKind};

// PDG types (combines CFG + DFG for accurate slicing)
pub use pdg::{
    BranchType, ControlDependence, PDGInfo, SliceCriteria, SliceMetrics, SliceResult,
};
// PDG slicing functions for advanced use cases (when you want to build PDG once and slice multiple times)
pub use pdg::{backward_slice as pdg_backward_slice, forward_slice as pdg_forward_slice};

// Metrics types
pub use metrics::{
    analyze_complexity, analyze_file_complexity, ComplexityAnalysis, ComplexityStats,
    CyclomaticComplexity, FunctionComplexity, RiskLevel,
};

// Nesting depth metrics
pub use metrics::{
    analyze_nesting, analyze_file_nesting, NestingMetrics, NestingAnalysis,
    NestingStats, FunctionNesting, NestingDepthLevel, NestingConstruct,
    DeepNesting, NestingAnalysisError,
};

// Call graph types
pub use callgraph::{CallEdge, CallGraph, FunctionRef};

// Function index types for fast lookups
pub use callgraph::{FunctionDef, FunctionIndex, IndexStats};

// Scanner types for project file discovery
pub use callgraph::scanner::{
    ErrorHandling, FileMetadata, ProjectScanner, ScanConfig, ScanError, ScanErrorKind, ScanResult,
};

// Dead code analysis types
pub use callgraph::{
    analyze_dead_code, analyze_dead_code_with_config, DeadCodeConfig, DeadCodeResult,
    DeadCodeStats, DeadFunction, DeadReason,
};

// Entry point detection
pub use callgraph::{classify_entry_point, detect_entry_points_with_config, EntryPointKind};

// Impact analysis types (reverse call graph)
pub use callgraph::{analyze_impact, CallerInfo, ImpactConfig, ImpactResult};

// Architecture analysis types
pub use callgraph::{analyze_architecture, ArchAnalysis, ArchStats, CycleDependency};

// Call graph cache utilities
pub use callgraph::{
    get_cache_dir, get_cache_file, get_or_build_graph_with_config,
    invalidate_cache, warm_cache_with_config, CachedCallGraph, CachedEdge,
};

// Language types
pub use lang::{BoxedLanguage, Language, LanguageRegistry};

// Semantic types
pub use semantic::{
    ChunkInfo, CodeComplexity, CodeLocation, ContentHashedIndex, EmbeddingUnit, SearchResult,
    SemanticPattern, UnitKind, CHUNK_OVERLAP_TOKENS, MAX_CODE_PREVIEW_TOKENS, MAX_EMBEDDING_TOKENS,
    SEMANTIC_PATTERNS,
};

// Embedding types
pub use embedding::{
    distances_to_scores, distances_to_scores_for_metric, is_normalized, normalize_vector,
    IndexConfig, Metric, Quantization, VectorIndex,
};

// Security analysis types - Command Injection
pub use security::injection::command::{
    CommandInjectionFinding, CommandSink, Confidence, InjectionKind,
    Severity as CommandSeverity, SourceLocation, TaintSource, TaintSourceKind,
    scan_command_injection, scan_file_command_injection,
};

// Security analysis types - SQL Injection
pub use security::injection::sql::{
    Location as SqlLocation, SQLInjectionFinding, ScanResult as SqlScanResult,
    Severity as SqlSeverity, SqlInjectionDetector, SqlSinkType, UnsafePattern,
};

// Security analysis types - Path Traversal
pub use security::injection::path_traversal::{
    Confidence as PathTraversalConfidence, FileOperationType, FileSink,
    PathTraversalFinding, ScanResult as PathTraversalScanResult,
    Severity as PathTraversalSeverity, SourceLocation as PathTraversalLocation,
    VulnerablePattern as PathTraversalPattern,
    scan_path_traversal, scan_file_path_traversal, get_file_sinks,
};

// Security analysis types - Weak Cryptography
pub use security::crypto::{
    Algorithm as CryptoAlgorithm, Confidence as CryptoConfidence,
    Location as CryptoLocation, ScanResult as CryptoScanResult,
    Severity as CryptoSeverity, UsageContext as CryptoUsageContext,
    WeakCryptoDetector, WeakCryptoFinding, WeakCryptoIssue,
    scan_weak_crypto, scan_file_weak_crypto,
};

// Unified Security API (runs all analyzers in parallel)
pub use security::{
    scan_security, Confidence as UnifiedConfidence, InjectionType,
    Location as UnifiedLocation, ScanSummary, SecurityCategory, SecurityConfig,
    SecurityFinding, SecurityReport, Severity as UnifiedSeverity,
    check_suppression, is_suppressed,
};

// SARIF output format for CI/CD integration
pub use security::sarif::SarifLog;

// Code quality types - Clone Detection
pub use quality::clones::{
    detect_clones, format_clone_summary, Clone, CloneAnalysis, CloneConfig, CloneError,
    CloneInstance, CloneStats, CloneType, TextualCloneDetector,
};

// Design pattern detection types
pub use patterns::{
    detect_patterns, format_pattern_summary, DesignPattern, Location as PatternLocation,
    PatternAnalysis, PatternCategory, PatternConfig, PatternDetector, PatternError,
    PatternMatch, PatternStats,
};

// =============================================================================
// Context Types for LLM API Parity
// =============================================================================

use serde::{Deserialize, Serialize};
use std::path::Path;

/// Context about a function including its code and metadata.
///
/// This struct provides a complete context for a single function, suitable for
/// LLM consumption. It matches Python's `FunctionContext` dataclass for API parity.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::{FunctionContext, extract_file};
///
/// // Create from extracted function info
/// let module = extract_file("./src/main.py", None)?;
/// if let Some(func) = module.functions.first() {
///     let source = std::fs::read_to_string("./src/main.py")?;
///     let ctx = FunctionContext::from_function_info(func, "./src/main.py", &source, "python");
///     println!("Function: {} at lines {}-{}", ctx.name, ctx.start_line, ctx.end_line);
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionContext {
    /// Function name
    pub name: String,
    /// File path containing the function
    pub file: String,
    /// Start line number (1-indexed)
    pub start_line: usize,
    /// End line number (1-indexed)
    pub end_line: usize,
    /// Function signature (e.g., "def foo(x: int) -> str")
    pub signature: Option<String>,
    /// Docstring or documentation comment
    pub docstring: Option<String>,
    /// Full source code of the function
    pub source: String,
    /// Programming language (e.g., "python", "rust", "go")
    pub language: String,
}

impl FunctionContext {
    /// Create a `FunctionContext` from a `FunctionInfo` and source file content.
    ///
    /// Extracts the function source code from the full file content using
    /// the line number information in `FunctionInfo`.
    ///
    /// # Arguments
    ///
    /// * `info` - The function info from AST extraction
    /// * `file_path` - Path to the source file
    /// * `source` - Full source content of the file
    /// * `language` - Programming language identifier
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use go_brrr::{FunctionContext, extract_file, FunctionInfo};
    ///
    /// let module = extract_file("./src/lib.rs", None)?;
    /// let source = std::fs::read_to_string("./src/lib.rs")?;
    ///
    /// for func in &module.functions {
    ///     let ctx = FunctionContext::from_function_info(func, "./src/lib.rs", &source, "rust");
    ///     println!("{}: {} lines", ctx.name, ctx.end_line - ctx.start_line + 1);
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_function_info(
        info: &FunctionInfo,
        file_path: &str,
        source: &str,
        language: &str,
    ) -> Self {
        let start = info.line_number.saturating_sub(1);
        let end = info.end_line_number.unwrap_or(info.line_number);
        let lines: Vec<&str> = source.lines().collect();
        let func_source = lines
            .get(start..end)
            .map(|ls| ls.join("\n"))
            .unwrap_or_default();

        Self {
            name: info.name.clone(),
            file: file_path.to_string(),
            start_line: info.line_number,
            end_line: end,
            signature: Some(info.signature()),
            docstring: info.docstring.clone(),
            source: func_source,
            language: language.to_string(),
        }
    }

    /// Create a minimal context with just name and file location.
    ///
    /// Useful when you have limited information about a function reference.
    pub fn minimal(name: &str, file: &str, line: usize, language: &str) -> Self {
        Self {
            name: name.to_string(),
            file: file.to_string(),
            start_line: line,
            end_line: line,
            signature: None,
            docstring: None,
            source: String::new(),
            language: language.to_string(),
        }
    }
}

/// Relevant context for LLM consumption with call graph information.
///
/// This struct aggregates function context for an entry point along with
/// its direct callees and callers, providing a complete picture for LLM analysis.
/// Matches Python's `RelevantContext` dataclass for API parity.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::{RelevantContext, FunctionContext};
///
/// // Build context programmatically
/// let entry = FunctionContext::minimal("main", "src/main.rs", 10, "rust");
/// let callee = FunctionContext::minimal("helper", "src/utils.rs", 25, "rust");
///
/// let context = RelevantContext {
///     entry,
///     callees: vec![callee],
///     callers: vec![],
///     token_count: 500,
///     depth: 1,
/// };
///
/// println!("Entry: {} calls {} functions", context.entry.name, context.callees.len());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelevantContext {
    /// The entry point function being analyzed
    pub entry: FunctionContext,
    /// Functions called by the entry point (direct dependencies)
    pub callees: Vec<FunctionContext>,
    /// Functions that call the entry point (reverse dependencies)
    pub callers: Vec<FunctionContext>,
    /// Estimated token count for LLM context window budgeting
    pub token_count: usize,
    /// Depth of call graph traversal used
    pub depth: usize,
}

impl RelevantContext {
    /// Create a new RelevantContext with just an entry point.
    ///
    /// Callees and callers are initialized empty; use builder pattern to add them.
    pub fn new(entry: FunctionContext, depth: usize) -> Self {
        Self {
            entry,
            callees: Vec::new(),
            callers: Vec::new(),
            token_count: 0,
            depth,
        }
    }

    /// Add a callee (function called by entry point).
    pub fn with_callee(mut self, callee: FunctionContext) -> Self {
        self.callees.push(callee);
        self
    }

    /// Add a caller (function that calls the entry point).
    pub fn with_caller(mut self, caller: FunctionContext) -> Self {
        self.callers.push(caller);
        self
    }

    /// Set the token count estimate.
    pub fn with_token_count(mut self, count: usize) -> Self {
        self.token_count = count;
        self
    }

    /// Calculate approximate token count based on source code length.
    ///
    /// Uses a rough estimate of 4 characters per token.
    pub fn estimate_tokens(&mut self) {
        let mut total_chars = self.entry.source.len();
        for callee in &self.callees {
            total_chars += callee.source.len();
        }
        for caller in &self.callers {
            total_chars += caller.source.len();
        }
        self.token_count = total_chars / 4;
    }

    /// Total number of functions in this context.
    pub fn function_count(&self) -> usize {
        1 + self.callees.len() + self.callers.len()
    }

    /// Format context as a string suitable for LLM prompts.
    ///
    /// Produces a structured representation showing the entry point
    /// and its relationships to other functions.
    pub fn to_llm_string(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!(
            "## Code Context: {} (depth={})\n\n",
            self.entry.name, self.depth
        ));

        // Entry point
        let short_file = Path::new(&self.entry.file)
            .file_name()
            .map(|f| f.to_string_lossy().to_string())
            .unwrap_or_else(|| self.entry.file.clone());
        output.push_str(&format!(
            "### Entry Point: {} ({}:{})\n",
            self.entry.name, short_file, self.entry.start_line
        ));
        if let Some(sig) = &self.entry.signature {
            output.push_str(&format!("```\n{}\n```\n", sig));
        }
        if let Some(doc) = &self.entry.docstring {
            let first_line = doc.lines().next().unwrap_or("");
            output.push_str(&format!("> {}\n", first_line));
        }
        output.push('\n');

        // Callees
        if !self.callees.is_empty() {
            output.push_str("### Calls:\n");
            for callee in &self.callees {
                let short = Path::new(&callee.file)
                    .file_name()
                    .map(|f| f.to_string_lossy().to_string())
                    .unwrap_or_else(|| callee.file.clone());
                output.push_str(&format!("- {} ({}:{})\n", callee.name, short, callee.start_line));
            }
            output.push('\n');
        }

        // Callers
        if !self.callers.is_empty() {
            output.push_str("### Called By:\n");
            for caller in &self.callers {
                let short = Path::new(&caller.file)
                    .file_name()
                    .map(|f| f.to_string_lossy().to_string())
                    .unwrap_or_else(|| caller.file.clone());
                output.push_str(&format!("- {} ({}:{})\n", caller.name, short, caller.start_line));
            }
            output.push('\n');
        }

        output.push_str(&format!(
            "Token estimate: {} | Functions: {}\n",
            self.token_count,
            self.function_count()
        ));

        output
    }
}

// =============================================================================
// Source Input Types
// =============================================================================

/// Input that can be either a file path or source code string.
///
/// This enum provides flexibility for analysis functions, allowing them to
/// accept either a file path (which will be read and language-detected) or
/// source code directly with an explicit language hint.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::SourceInput;
///
/// // From a file path
/// let from_file = SourceInput::Path("./src/main.py");
///
/// // From source code with language hint
/// let from_source = SourceInput::Source {
///     code: "def hello(): return 'world'",
///     language: "python",
/// };
/// ```
#[derive(Debug, Clone)]
pub enum SourceInput<'a> {
    /// Path to a source file (language auto-detected from extension).
    Path(&'a str),
    /// Source code string with explicit language hint.
    Source {
        /// The source code as a string.
        code: &'a str,
        /// Language identifier (e.g., "python", "typescript", "rust").
        language: &'a str,
    },
}

impl<'a> SourceInput<'a> {
    /// Resolve input to (source_bytes, language_trait, optional_path).
    ///
    /// For `Path` variant: reads file, detects language from extension.
    /// For `Source` variant: uses provided code and language directly.
    ///
    /// # Returns
    ///
    /// A tuple of:
    /// - `Vec<u8>`: The source code as bytes
    /// - `&'static dyn Language`: The language implementation
    /// - `Option<&'a str>`: The file path if `Path` variant was used
    ///
    /// # Errors
    ///
    /// - [`BrrrError::UnsupportedLanguage`] if language cannot be determined
    /// - [`BrrrError::Io`] if file cannot be read (for `Path` variant)
    pub fn resolve(&self) -> Result<(Vec<u8>, &'static dyn Language, Option<&'a str>)> {
        let registry = LanguageRegistry::global();

        match self {
            SourceInput::Path(path) => {
                let p = Path::new(path);
                let lang = registry.detect_language(p).ok_or_else(|| {
                    BrrrError::UnsupportedLanguage(
                        p.extension()
                            .map(|e| e.to_string_lossy().to_string())
                            .unwrap_or_else(|| "unknown".to_string()),
                    )
                })?;
                let source = std::fs::read(p)
                    .map_err(|e| BrrrError::io_with_path(e, p))?;
                Ok((source, lang, Some(*path)))
            }
            SourceInput::Source { code, language } => {
                let lang = registry
                    .get_by_name(language)
                    .ok_or_else(|| BrrrError::UnsupportedLanguage((*language).to_string()))?;
                Ok((code.as_bytes().to_vec(), lang, None))
            }
        }
    }
}

// =============================================================================
// High-Level Public API Functions
// =============================================================================

/// Get the file tree for a directory.
///
/// Generates a hierarchical tree structure suitable for JSON serialization.
/// Skips common build directories (node_modules, __pycache__, .git, etc.).
///
/// # Arguments
///
/// * `path` - Root directory path to scan
/// * `ext_filter` - Optional extension filter (e.g., `Some(".py")` for Python files only)
/// * `exclude_hidden` - If true, exclude hidden files/directories (those starting with '.')
/// * `respect_ignore` - If true, respect .gitignore and .brrrignore patterns
///
/// # Returns
///
/// A [`FileTreeEntry`] representing the root directory with nested children.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_tree;
///
/// // Get all files with default settings (exclude hidden, respect ignore)
/// let tree = get_tree("./project", None, true, true)?;
/// println!("Root: {}", tree.name);
/// for child in &tree.children {
///     println!("  {} (type: {})", child.name, child.entry_type);
/// }
///
/// // Get only Python files, including hidden files
/// let py_tree = get_tree("./project", Some(".py"), false, true)?;
///
/// // Get all files, ignoring .gitignore patterns
/// let all_tree = get_tree("./project", None, true, false)?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError::Io`] if the path cannot be read or does not exist.
pub fn get_tree(
    path: &str,
    ext_filter: Option<&str>,
    exclude_hidden: bool,
    respect_ignore: bool,
) -> Result<FileTreeEntry> {
    let ext_vec: Vec<String> = ext_filter
        .map(|e| vec![e.to_string()])
        .unwrap_or_default();
    // Map parameters to ast::file_tree conventions:
    // - show_hidden = !exclude_hidden (show_hidden=false means exclude hidden files)
    // - no_ignore = !respect_ignore (no_ignore=false means respect ignore patterns)
    let show_hidden = !exclude_hidden;
    let no_ignore = !respect_ignore;
    ast::file_tree(path, &ext_vec, show_hidden, no_ignore, None)
}

/// Get file tree with default options (convenience wrapper).
///
/// Equivalent to calling `get_tree(path, ext_filter, true, true)`:
/// - Excludes hidden files/directories
/// - Respects .gitignore and .brrrignore patterns
///
/// # Arguments
///
/// * `path` - Root directory path to scan
/// * `ext_filter` - Optional extension filter (e.g., `Some(".py")` for Python files only)
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_tree_default;
///
/// let tree = get_tree_default("./project", None)?;
/// let py_tree = get_tree_default("./project", Some(".py"))?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
#[inline]
pub fn get_tree_default(path: &str, ext_filter: Option<&str>) -> Result<FileTreeEntry> {
    get_tree(path, ext_filter, true, true)
}

/// Get code structure summary for a project.
///
/// Scans the directory for source files matching the language filter,
/// extracts function and class information using parallel processing,
/// and returns a summary suitable for LLM consumption.
///
/// # Arguments
///
/// * `path` - Root directory to scan
/// * `lang_filter` - Optional language filter (e.g., `Some("python")`, `Some("typescript")`)
/// * `max_results` - Maximum number of functions/classes to return (0 = unlimited)
/// * `respect_ignore` - If true, respect .gitignore and .brrrignore patterns
///
/// # Returns
///
/// A [`CodeStructure`] containing:
/// - `path`: The analyzed path
/// - `functions`: List of function summaries with file, line, and signature
/// - `classes`: List of class summaries with file, line, and method count
/// - `total_files`: Total number of files analyzed
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_structure;
///
/// // Get all functions and classes (any language), respecting ignore files
/// let structure = get_structure("./src", None, 0, true)?;
/// println!("Found {} functions in {} files", structure.functions.len(), structure.total_files);
///
/// // Get only Python code, limited to 50 results, ignoring .gitignore
/// let py_structure = get_structure("./src", Some("python"), 50, false)?;
/// for func in &py_structure.functions {
///     println!("{}:{} - {}", func.file, func.line, func.signature);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError::Io`] if the path does not exist or cannot be read.
pub fn get_structure(
    path: &str,
    lang_filter: Option<&str>,
    max_results: usize,
    respect_ignore: bool,
) -> Result<CodeStructure> {
    // no_ignore is the inverse of respect_ignore
    let no_ignore = !respect_ignore;
    ast::code_structure(path, lang_filter, max_results, no_ignore)
}

/// Get code structure with default options.
///
/// Convenience wrapper that calls [`get_structure`] with `respect_ignore=true`.
/// This maintains backward compatibility with the original API.
///
/// # Arguments
///
/// * `path` - Project root directory
/// * `lang_filter` - Optional language filter
/// * `max_results` - Maximum files to process
#[inline]
pub fn get_structure_default(
    path: &str,
    lang_filter: Option<&str>,
    max_results: usize,
) -> Result<CodeStructure> {
    get_structure(path, lang_filter, max_results, true)
}

/// Extract complete AST information from a source file with optional path containment validation.
///
/// Parses a single file and returns detailed information about all
/// functions, classes, and imports. This is the most detailed extraction
/// API, suitable for deep analysis of individual files.
///
/// # Arguments
///
/// * `file_path` - Path to the source file
/// * `base_path` - Optional base directory for security validation.
///   If provided, `file_path` must resolve to a location within `base_path`.
///
/// # Returns
///
/// A [`ModuleInfo`] containing:
/// - `path`: The file path
/// - `language`: Detected language (e.g., "python", "typescript")
/// - `functions`: All top-level functions with full details
/// - `classes`: All classes/structs with methods
/// - `imports`: All import statements
///
/// # Examples
///
/// ```no_run
/// use go_brrr::extract_file;
///
/// // Without path containment validation
/// let module = extract_file("./src/main.py", None)?;
/// println!("Language: {}", module.language);
///
/// // With path containment validation - prevents directory traversal
/// let module = extract_file("./src/main.py", Some("./src"))?;
///
/// for func in &module.functions {
///     println!("Function: {} at line {}", func.name, func.line_number);
///     println!("  Signature: {}", func.signature());
///     if let Some(doc) = &func.docstring {
///         println!("  Docstring: {}", doc);
///     }
/// }
///
/// for class in &module.classes {
///     println!("Class: {} with {} methods", class.name, class.methods.len());
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Security
///
/// When `base_path` is provided, this function validates that `file_path`
/// does not escape the base directory via:
/// - Directory traversal (../..)
/// - Symlink resolution
/// - Absolute paths outside base
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the file type is not recognized
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::PathTraversal`] if the path escapes base_path or contains dangerous patterns
pub fn extract_file(file_path: &str, base_path: Option<&str>) -> Result<ModuleInfo> {
    ast::extract_file(file_path, base_path)
}

/// Extract complete AST information from a source file without path validation.
///
/// This is a convenience function equivalent to `extract_file(file_path, None)`.
/// Basic input validation is still performed for obviously dangerous paths.
///
/// # Security Warning
///
/// This function does not validate path containment. Only use when:
/// - The file path comes from a trusted source
/// - Path validation is performed by the caller
/// - You explicitly want to allow any valid file path
#[inline]
pub fn extract_file_unchecked(file_path: &str) -> Result<ModuleInfo> {
    ast::extract_file_unchecked(file_path)
}

/// Extract file information from a source code string.
///
/// Parses source code directly (without reading from a file) and returns
/// detailed information about all functions, classes, and imports. This is
/// useful when you have source code in memory or want to analyze code
/// snippets without creating temporary files.
///
/// # Arguments
///
/// * `source` - Source code as a string
/// * `language` - Language identifier (e.g., "python", "typescript", "rust", "go")
///
/// # Returns
///
/// A [`ModuleInfo`] containing:
/// - `path`: Set to `"<string>"` since there is no file path
/// - `language`: The language name provided
/// - `functions`: All top-level functions with full details
/// - `classes`: All classes/structs with methods
/// - `imports`: All import statements
///
/// # Examples
///
/// ```
/// use go_brrr::extract_from_source;
///
/// let source = r#"
/// def greet(name: str) -> str:
///     """Say hello to someone."""
///     return f"Hello, {name}!"
///
/// class Greeter:
///     def __init__(self, prefix: str):
///         self.prefix = prefix
/// "#;
///
/// let module = extract_from_source(source, "python")?;
/// assert_eq!(module.language, "python");
/// assert_eq!(module.functions.len(), 1);
/// assert_eq!(module.classes.len(), 1);
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
/// - [`BrrrError::Parse`] if the source code cannot be parsed
pub fn extract_from_source(source: &str, language: &str) -> Result<ModuleInfo> {
    ast::AstExtractor::extract_from_source(source, language)
}

/// Extract import statements from a source file.
///
/// Parses a source file and extracts all import statements, returning
/// detailed information about each import including module names, imported
/// names, aliases, and line numbers.
///
/// # Arguments
///
/// * `file_path` - Path to the source file
/// * `language` - Optional language override (auto-detected from file extension if None)
///
/// # Returns
///
/// A vector of [`ImportInfo`] containing:
/// - `module`: The module being imported
/// - `names`: Specific names imported (for `from X import Y` style)
/// - `aliases`: Name to alias mappings
/// - `is_from`: Whether this is a `from X import Y` style import
/// - `level`: Relative import level (0 for absolute imports)
/// - `line_number`: Line number of the import statement
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_imports;
///
/// let imports = get_imports("./src/main.py", None)?;
/// for imp in &imports {
///     println!("Import at line {}: {}", imp.line_number, imp.statement());
/// }
///
/// // With explicit language
/// let ts_imports = get_imports("./src/index.ts", Some("typescript"))?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the file type is not recognized
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_imports(file_path: &str, language: Option<&str>) -> Result<Vec<ImportInfo>> {
    use std::path::Path;

    let path = Path::new(file_path);
    let registry = LanguageRegistry::global();

    // Validate language if provided, but use file extension for parsing
    // This matches the behavior of extract_imports which auto-detects from path
    if let Some(lang_name) = language {
        if registry.get_by_name(lang_name).is_none() {
            return Err(BrrrError::UnsupportedLanguage(lang_name.to_string()));
        }
    }

    // Use the existing extract_imports which handles language detection
    ast::extract_imports(path)
}

/// Get LLM-ready context for a function entry point.
///
/// Builds a call graph and returns structured context about what
/// a function calls (and transitively, what those functions call).
/// This is ideal for providing focused context to an LLM about
/// a specific code path.
///
/// # Arguments
///
/// * `project` - Root project directory
/// * `entry_point` - Function name to start from (e.g., "main", "process_request")
/// * `depth` - How many levels of calls to traverse (typically 1-3)
///
/// # Returns
///
/// A JSON value containing:
/// - `entry_point`: The starting function
/// - `depth`: Traversal depth used
/// - `functions`: List of function names in the call chain
/// - `count`: Total number of functions found
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_context;
///
/// let context = get_context("./project", "handle_request", 2)?;
/// println!("{}", serde_json::to_string_pretty(&context)?);
/// // Output:
/// // {
/// //   "entry_point": "handle_request",
/// //   "depth": 2,
/// //   "functions": ["validate_input", "process_data", "send_response"],
/// //   "count": 3
/// // }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError`] if the project cannot be scanned or parsed.
pub fn get_context(project: &str, entry_point: &str, depth: usize) -> Result<serde_json::Value> {
    callgraph::get_context_with_lang(project, entry_point, depth, None)
}

/// Query project for LLM-ready context string.
///
/// Returns a formatted markdown-style string suitable for direct consumption
/// by LLMs, including the entry point function and all its callees with
/// their complete source code.
///
/// This is a convenience wrapper around [`get_context`] that formats the
/// output as a human/LLM-readable string instead of JSON.
///
/// # Arguments
///
/// * `project` - Project root directory
/// * `entry_point` - Entry point function name (e.g., "main" or "Class.method")
/// * `depth` - Call graph traversal depth (typically 1-3)
/// * `language` - Optional language filter (e.g., "python", "rust")
///
/// # Returns
///
/// Formatted string with function source code:
///
/// ```text
/// # Entry: main (src/main.py:10-25)
/// def main():
///     process_data()
///
/// ## Callees:
///
/// ### process_data (src/utils.py:5-15)
/// def process_data():
///     ...
/// ```
///
/// # Examples
///
/// ```no_run
/// use go_brrr::query;
///
/// // Get context for main function
/// let context = query("./src", "main", 2, None)?;
/// println!("{}", context);
///
/// // Filter to Python files only
/// let py_context = query("./src", "handle_request", 3, Some("python"))?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError`] if:
/// - The project cannot be scanned
/// - Source files cannot be parsed
/// - The entry point function cannot be found
pub fn query(
    project: &str,
    entry_point: &str,
    depth: usize,
    language: Option<&str>,
) -> Result<String> {
    // Get context JSON and extract the LLM-ready text format
    let result = callgraph::get_context_with_lang(project, entry_point, depth, language)?;
    Ok(result
        .get("llm_context")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string())
}

/// Get control flow graph for a function.
///
/// Extracts the CFG showing how control flows through a function,
/// including branches, loops, and exception handling. Useful for
/// understanding function complexity and control flow paths.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A [`CFGInfo`] containing:
/// - `function_name`: The analyzed function
/// - `blocks`: Map of basic blocks (each with statements and line ranges)
/// - `edges`: Control flow edges between blocks
/// - `entry`: Entry block ID
/// - `exits`: Exit block IDs
///
/// The CFG can be rendered to Mermaid format for visualization using
/// [`CFGInfo::to_mermaid`].
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg;
///
/// let cfg = get_cfg("./src/main.py", "process_data")?;
/// println!("Function: {}", cfg.function_name);
/// println!("Blocks: {}", cfg.blocks.len());
/// println!("Cyclomatic complexity: {}", cfg.cyclomatic_complexity());
///
/// // Render to Mermaid diagram
/// println!("{}", cfg.to_mermaid());
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::UnsupportedLanguage`] if language is specified but not supported
pub fn get_cfg(file: &str, function: &str, language: Option<&str>) -> Result<CFGInfo> {
    cfg::extract_with_language(file, function, language)
}

/// Get control flow graph with auto-detected language (convenience wrapper).
///
/// This is a convenience function for [`get_cfg`] with language auto-detection.
/// Equivalent to `get_cfg(file, function, None)`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Example
///
/// ```no_run
/// use go_brrr::get_cfg_auto;
///
/// let cfg = get_cfg_auto("./src/main.py", "process_data")?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
#[inline]
pub fn get_cfg_auto(file: &str, function: &str) -> Result<CFGInfo> {
    get_cfg(file, function, None)
}

/// Get control flow graph from a source code string.
///
/// Parses source code directly (without reading from a file) and extracts
/// the control flow graph for the specified function. This is useful when
/// you have source code in memory or want to analyze code snippets.
///
/// # Arguments
///
/// * `source` - Source code as a string
/// * `function` - Name of the function to analyze
/// * `language` - Language identifier (e.g., "python", "typescript", "rust")
///
/// # Returns
///
/// A [`CFGInfo`] containing the control flow graph for the function.
///
/// # Examples
///
/// ```
/// use go_brrr::get_cfg_from_source;
///
/// let source = r#"
/// def process(x):
///     if x > 0:
///         return x * 2
///     return 0
/// "#;
///
/// let cfg = get_cfg_from_source(source, "process", "python")?;
/// assert_eq!(cfg.function_name, "process");
/// assert!(cfg.cyclomatic_complexity() >= 2); // Has an if branch
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the source cannot be parsed
pub fn get_cfg_from_source(source: &str, function: &str, language: &str) -> Result<CFGInfo> {
    cfg::CfgBuilder::extract_from_source(source, language, function)
}

/// Get CFG blocks for a function.
///
/// Convenience function that extracts the CFG and returns just the blocks.
/// This is equivalent to `get_cfg(file, function)?.blocks.into_values().collect()`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A vector of [`CFGBlock`] containing block id, statements, and line range.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_blocks;
///
/// let blocks = get_cfg_blocks("./src/main.py", "process")?;
/// for block in &blocks {
///     println!("Block {} (lines {}-{}): {}", block.id.0, block.start_line, block.end_line, block.label);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_blocks(file: &str, function: &str) -> Result<Vec<CFGBlock>> {
    let cfg = get_cfg(file, function, None)?;
    Ok(cfg.blocks.into_values().collect())
}

/// Get CFG edges for a function.
///
/// Convenience function that extracts the CFG and returns just the edges.
/// This is equivalent to `get_cfg(file, function)?.edges`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A vector of [`CFGEdge`] containing from/to block IDs and edge type.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_edges;
///
/// let edges = get_cfg_edges("./src/main.py", "process")?;
/// for edge in &edges {
///     println!("Block {} -> Block {} ({})", edge.from.0, edge.to.0, edge.label());
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_edges(file: &str, function: &str) -> Result<Vec<CFGEdge>> {
    let cfg = get_cfg(file, function, None)?;
    Ok(cfg.edges)
}

/// Get CFG as Mermaid diagram string.
///
/// Convenience function that extracts the CFG and renders it as a Mermaid flowchart.
/// The output can be embedded in Markdown or rendered via mermaid.live.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A Mermaid flowchart string.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_mermaid;
///
/// let mermaid = get_cfg_mermaid("./src/main.py", "process")?;
/// println!("{}", mermaid);
/// // Output:
/// // flowchart TD
/// //     B0["entry"]
/// //     B1["if x > 0"]
/// //     B0 --> B1
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_mermaid(file: &str, function: &str) -> Result<String> {
    let cfg = get_cfg(file, function, None)?;
    Ok(cfg::to_mermaid(&cfg))
}

/// Get CFG as DOT (Graphviz) string.
///
/// Convenience function that extracts the CFG and renders it in DOT format.
/// The output can be rendered using Graphviz tools (dot, neato, etc.).
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A DOT graph string.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_dot;
///
/// let dot = get_cfg_dot("./src/main.py", "process")?;
/// std::fs::write("cfg.dot", &dot)?;
/// // Then run: dot -Tpng cfg.dot -o cfg.png
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_dot(file: &str, function: &str) -> Result<String> {
    let cfg = get_cfg(file, function, None)?;
    Ok(cfg::to_dot(&cfg))
}

/// Get CFG as ASCII art string.
///
/// Convenience function that extracts the CFG and renders it as ASCII text.
/// Provides a terminal-friendly, human-readable view of the CFG structure.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// An ASCII text representation of the CFG.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_ascii;
///
/// let ascii = get_cfg_ascii("./src/main.py", "process")?;
/// println!("{}", ascii);
/// // Output:
/// // CFG: process
/// // ========================================
/// // Blocks: 5
/// // Edges: 6
/// // Complexity: 3
/// // ...
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_ascii(file: &str, function: &str) -> Result<String> {
    let cfg = get_cfg(file, function, None)?;
    Ok(cfg::to_ascii(&cfg))
}

/// Get CFG as JSON value.
///
/// Convenience function that extracts the CFG and converts it to a serde_json::Value.
/// Useful for serialization and integration with JSON-based tools.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A JSON value representing the CFG.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_cfg_json;
///
/// let json = get_cfg_json("./src/main.py", "process")?;
/// println!("{}", serde_json::to_string_pretty(&json)?);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_cfg_json(file: &str, function: &str) -> Result<serde_json::Value> {
    let cfg = get_cfg(file, function, None)?;
    Ok(serde_json::to_value(&cfg)?)
}

/// Get data flow graph for a function.
///
/// Extracts the DFG showing how data flows through a function,
/// tracking variable definitions, uses, and mutations. Essential
/// for understanding data dependencies and performing program slicing.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A [`DFGInfo`] containing:
/// - `function_name`: The analyzed function
/// - `edges`: Data flow edges (variable, from_line, to_line, kind)
/// - `definitions`: Map of variable -> definition lines
/// - `uses`: Map of variable -> use lines
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_dfg;
///
/// let dfg = get_dfg("./src/main.py", "calculate")?;
/// println!("Variables: {:?}", dfg.variables());
///
/// // Show where each variable is defined
/// for (var, lines) in &dfg.definitions {
///     println!("{} defined at lines {:?}", var, lines);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::UnsupportedLanguage`] if language is specified but not supported
pub fn get_dfg(file: &str, function: &str, language: Option<&str>) -> Result<DFGInfo> {
    dfg::extract_with_language(file, function, language)
}

/// Get data flow graph with auto-detected language (convenience wrapper).
///
/// This is a convenience function for [`get_dfg`] with language auto-detection.
/// Equivalent to `get_dfg(file, function, None)`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Example
///
/// ```no_run
/// use go_brrr::get_dfg_auto;
///
/// let dfg = get_dfg_auto("./src/main.py", "calculate")?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
#[inline]
pub fn get_dfg_auto(file: &str, function: &str) -> Result<DFGInfo> {
    get_dfg(file, function, None)
}

/// Get data flow graph from a source code string.
///
/// Parses source code directly (without reading from a file) and extracts
/// the data flow graph for the specified function. This is useful when
/// you have source code in memory or want to analyze code snippets.
///
/// # Arguments
///
/// * `source` - Source code as a string
/// * `function` - Name of the function to analyze
/// * `language` - Language identifier (e.g., "python", "typescript", "rust")
///
/// # Returns
///
/// A [`DFGInfo`] containing the data flow graph for the function.
///
/// # Examples
///
/// ```
/// use go_brrr::get_dfg_from_source;
///
/// let source = r#"
/// def compute(x, y):
///     z = x + y
///     result = z * 2
///     return result
/// "#;
///
/// let dfg = get_dfg_from_source(source, "compute", "python")?;
/// assert_eq!(dfg.function_name, "compute");
/// assert!(dfg.definitions.contains_key("z"));
/// assert!(dfg.definitions.contains_key("result"));
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the source cannot be parsed
pub fn get_dfg_from_source(source: &str, function: &str, language: &str) -> Result<DFGInfo> {
    dfg::DfgBuilder::extract_from_source(source, language, function)
}

/// Get DFG edges for a function.
///
/// Convenience function that extracts the DFG and returns just the edges.
/// This is equivalent to `get_dfg(file, function)?.edges`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A vector of [`DataflowEdge`] containing variable, from/to lines, and flow kind.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_dfg_edges;
///
/// let edges = get_dfg_edges("./src/main.py", "calculate")?;
/// for edge in &edges {
///     println!("{}: line {} -> line {} ({:?})",
///         edge.variable, edge.from_line, edge.to_line, edge.kind);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_dfg_edges(file: &str, function: &str) -> Result<Vec<DataflowEdge>> {
    let dfg = get_dfg(file, function, None)?;
    Ok(dfg.edges)
}

/// Get variables tracked in DFG.
///
/// Convenience function that extracts the DFG and returns the list of
/// variables that have definitions or uses tracked.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A vector of variable names tracked in the function.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_dfg_variables;
///
/// let vars = get_dfg_variables("./src/main.py", "calculate")?;
/// println!("Variables tracked: {:?}", vars);
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_dfg_variables(file: &str, function: &str) -> Result<Vec<String>> {
    let dfg = get_dfg(file, function, None)?;
    use std::collections::HashSet;
    let vars: HashSet<_> = dfg.edges.iter().map(|e| e.variable.clone()).collect();
    Ok(vars.into_iter().collect())
}

/// Get def-use chains for a specific variable.
///
/// Extracts the data flow graph and filters edges for a specific variable,
/// returning pairs of (definition_line, use_line).
///
/// This is useful for understanding how a specific variable flows through
/// a function - where it's defined and where those definitions are used.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
/// * `variable` - Name of the variable to track
///
/// # Returns
///
/// A vector of (from_line, to_line) tuples representing def-use chains.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_def_use_chains;
///
/// // Track how variable 'x' flows through the function
/// let chains = get_def_use_chains("./src/main.py", "calculate", "x")?;
/// for (def_line, use_line) in &chains {
///     println!("x: defined at line {} -> used at line {}", def_line, use_line);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_def_use_chains(file: &str, function: &str, variable: &str) -> Result<Vec<(usize, usize)>> {
    let dfg = get_dfg(file, function, None)?;
    let chains: Vec<_> = dfg
        .edges
        .iter()
        .filter(|e| e.variable == variable)
        .map(|e| (e.from_line, e.to_line))
        .collect();
    Ok(chains)
}

/// Get program dependence graph (PDG) for a function.
///
/// A PDG combines control flow (CFG) and data flow (DFG) into a unified graph
/// that enables accurate program slicing. It includes:
/// - Control dependencies: which conditions determine if a statement executes
/// - Data dependencies: def-use chains for variables
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Returns
///
/// A [`PDGInfo`] containing the combined CFG, DFG, and computed control dependencies.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_pdg;
///
/// let pdg = get_pdg("./src/main.py", "process")?;
/// println!("Control dependencies: {}", pdg.control_dep_count());
/// println!("Data edges: {}", pdg.data_edge_count());
///
/// // Check if line 5 is control-dependent on line 3
/// if pdg.is_control_dependent(5, 3) {
///     println!("Line 5 depends on condition at line 3");
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::UnsupportedLanguage`] if language is specified but not supported
pub fn get_pdg(file: &str, function: &str, language: Option<&str>) -> Result<PDGInfo> {
    pdg::build_pdg_with_language(file, function, language)
}

/// Get PDG with auto-detected language (convenience wrapper).
///
/// This is a convenience function for [`get_pdg`] with language auto-detection.
/// Equivalent to `get_pdg(file, function, None)`.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function to analyze
///
/// # Example
///
/// ```no_run
/// use go_brrr::get_pdg_auto;
///
/// let pdg = get_pdg_auto("./src/main.py", "process")?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
#[inline]
pub fn get_pdg_auto(file: &str, function: &str) -> Result<PDGInfo> {
    get_pdg(file, function, None)
}

/// Get program slice for a line of code.
///
/// Performs program slicing to find lines that affect (backward) or are affected by
/// (forward) the given target line. This is extremely useful for debugging
/// ("what could have caused this value?") and impact analysis ("what will this change affect?").
///
/// **NOTE**: This function uses PDG-based slicing which follows BOTH control
/// dependencies (conditions that determine if code executes) AND data dependencies
/// (variable def-use chains). This is more accurate than DFG-only slicing.
///
/// For example, given:
/// ```python
/// if x > 0:        # Line 2 - condition
///     result = x * 2  # Line 3
/// return result    # Line 4
/// ```
///
/// A backward slice from line 4 correctly includes line 2 (the condition) because
/// whether `result` is assigned depends on the condition's outcome.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function containing the line
/// * `line` - Target line number (1-indexed)
/// * `direction` - Slice direction: "backward" (default) or "forward"
/// * `variable` - Optional variable name for variable-specific slicing
/// * `language` - Optional language override (auto-detected if None)
///
/// # Returns
///
/// A sorted vector of line numbers in the slice, including the target line itself.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_slice;
///
/// // Backward slice from line 42 (default direction)
/// let affected_lines = get_slice("./src/main.py", "process", 42, None, None, None)?;
/// println!("Line 42 is affected by lines: {:?}", affected_lines);
///
/// // Forward slice to see what line 10 affects
/// let impacts = get_slice("./src/main.py", "process", 10, Some("forward"), None, None)?;
/// println!("Line 10 affects lines: {:?}", impacts);
///
/// // Variable-specific backward slice
/// let x_deps = get_slice("./src/main.py", "process", 42, None, Some("x"), None)?;
/// println!("Lines affecting 'x' at line 42: {:?}", x_deps);
///
/// // Forward slice for variable 'result'
/// let result_impacts = get_slice("./src/main.py", "process", 5, Some("forward"), Some("result"), None)?;
/// println!("Lines affected by 'result' from line 5: {:?}", result_impacts);
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::InvalidArgument`] if line is 0 or direction is not "backward" or "forward"
pub fn get_slice(
    file: &str,
    function: &str,
    line: usize,
    direction: Option<&str>,
    variable: Option<&str>,
    language: Option<&str>,
) -> Result<Vec<usize>> {
    // Validate line number is 1-indexed (lines start at 1, not 0)
    if line == 0 {
        return Err(BrrrError::InvalidArgument(
            "Line numbers are 1-indexed, got 0".to_string(),
        ));
    }

    // Build the PDG for the function with optional language override
    let pdg_info = pdg::build_pdg_with_language(file, function, language)?;

    // Create slicing criteria with optional variable filter
    let criteria = match variable {
        Some(var) => pdg::SliceCriteria::at_line_variable(line, var),
        None => pdg::SliceCriteria::at_line(line),
    };

    // Route to appropriate slice direction
    let dir = direction.unwrap_or("backward");
    let result = match dir {
        "backward" => pdg::backward_slice(&pdg_info, &criteria),
        "forward" => pdg::forward_slice(&pdg_info, &criteria),
        _ => {
            return Err(BrrrError::InvalidArgument(format!(
                "Invalid direction '{}', expected 'backward' or 'forward'",
                dir
            )))
        }
    };

    Ok(result.lines)
}

/// Get program slice from a source code string.
///
/// Parses source code directly (without reading from a file) and performs
/// program slicing on the specified function. This uses DFG-based slicing
/// (data flow only) for source strings.
///
/// # Arguments
///
/// * `source` - Source code as a string
/// * `function` - Name of the function containing the line
/// * `line` - Target line number (1-indexed)
/// * `direction` - Slice direction: "backward" (default) or "forward"
/// * `variable` - Optional variable name for variable-specific slicing
/// * `language` - Language identifier (e.g., "python", "typescript", "rust")
///
/// # Returns
///
/// A sorted vector of line numbers in the slice.
///
/// # Examples
///
/// ```
/// use go_brrr::get_slice_from_source;
///
/// let source = r#"
/// def compute(x):
///     a = x + 1
///     b = a * 2
///     c = b + x
///     return c
/// "#;
///
/// // Backward slice from line 5 (c = b + x)
/// let slice = get_slice_from_source(source, "compute", 5, None, None, "python")?;
/// // Should include lines that affect line 5
/// assert!(!slice.is_empty());
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the source cannot be parsed
/// - [`BrrrError::InvalidArgument`] if line is 0 or direction is not "backward" or "forward"
pub fn get_slice_from_source(
    source: &str,
    function: &str,
    line: usize,
    direction: Option<&str>,
    variable: Option<&str>,
    language: &str,
) -> Result<Vec<usize>> {
    // Validate line number is 1-indexed (lines start at 1, not 0)
    if line == 0 {
        return Err(BrrrError::InvalidArgument(
            "Line numbers are 1-indexed, got 0".to_string(),
        ));
    }

    // Build the DFG for the function from source
    let dfg_info = dfg::DfgBuilder::extract_from_source(source, language, function)?;

    // Create slice criteria with optional variable filter
    let criteria = match variable {
        Some(var) => dfg::SliceCriteria::at_line_variable(line, var),
        None => dfg::SliceCriteria::at_line(line),
    };

    let dir = direction.unwrap_or("backward");
    let result = match dir {
        "backward" => dfg::backward_slice(&dfg_info, &criteria).lines,
        "forward" => dfg::forward_slice(&dfg_info, &criteria).lines,
        _ => {
            return Err(BrrrError::InvalidArgument(format!(
                "Invalid direction '{}', expected 'backward' or 'forward'",
                dir
            )))
        }
    };

    Ok(result)
}

/// Get backward program slice (convenience wrapper).
///
/// This is a convenience function that calls `get_slice` with default direction.
/// For the full API with all parameters, use [`get_slice`].
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function containing the line
/// * `line` - Target line number (1-indexed)
///
/// # Returns
///
/// A sorted vector of line numbers that affect the target line.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_backward_slice;
///
/// let affected_lines = get_backward_slice("./src/main.py", "process", 42)?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
pub fn get_backward_slice(file: &str, function: &str, line: usize) -> Result<Vec<usize>> {
    get_slice(file, function, line, Some("backward"), None, None)
}

/// Get backward program slice using DFG-only (data flow only).
///
/// This is the legacy slicing function that only follows data dependencies,
/// NOT control dependencies. For most use cases, prefer [`get_slice`] which
/// uses PDG-based slicing for more accurate results.
///
/// Use this function only if you specifically want data-flow-only analysis
/// without control dependencies.
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function containing the line
/// * `line` - Target line number (1-indexed)
///
/// # Returns
///
/// A sorted vector of line numbers that affect the target line through
/// data dependencies only.
///
/// # Errors
///
/// - [`BrrrError::InvalidArgument`] if line is 0
pub fn get_slice_dfg_only(file: &str, function: &str, line: usize) -> Result<Vec<usize>> {
    // Validate line number is 1-indexed (lines start at 1, not 0)
    if line == 0 {
        return Err(BrrrError::InvalidArgument(
            "Line numbers are 1-indexed, got 0".to_string(),
        ));
    }
    dfg::get_slice(file, function, line)
}

/// Get forward program slice for a line of code.
///
/// Performs forward slicing to find all lines affected by the given source line.
/// This is useful for impact analysis ("what will change if I modify this line?").
///
/// **NOTE**: This function uses PDG-based slicing which follows BOTH control
/// dependencies (conditions that determine if code executes) AND data dependencies
/// (variable def-use chains).
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function containing the line
/// * `line` - Source line number (1-indexed)
///
/// # Returns
///
/// A sorted vector of line numbers that are affected by the source line,
/// including the source line itself.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_forward_slice;
///
/// // Find what line 10 affects
/// let affected_lines = get_forward_slice("./src/main.py", "process", 10)?;
/// println!("Line 10 affects lines: {:?}", affected_lines);
/// // Output: [10, 15, 23, 38, 42]
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::InvalidArgument`] if line is 0
pub fn get_forward_slice(file: &str, function: &str, line: usize) -> Result<Vec<usize>> {
    // Validate line number is 1-indexed (lines start at 1, not 0)
    if line == 0 {
        return Err(BrrrError::InvalidArgument(
            "Line numbers are 1-indexed, got 0".to_string(),
        ));
    }
    pdg::get_forward_slice(file, function, line)
}

/// Get PDG-based slice with direction parameter.
///
/// Unified function for both backward and forward slicing using PDG.
/// For most cases, prefer [`get_slice`] (backward) or [`get_forward_slice`] (forward).
///
/// # Arguments
///
/// * `file` - Path to the source file
/// * `function` - Name of the function containing the line
/// * `line` - Target/source line number (1-indexed)
/// * `direction` - Either "backward" or "forward"
///
/// # Returns
///
/// A sorted vector of line numbers in the slice.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_pdg_slice;
///
/// // Backward slice: what affects line 42?
/// let backward = get_pdg_slice("./src/main.py", "process", 42, "backward")?;
///
/// // Forward slice: what does line 10 affect?
/// let forward = get_pdg_slice("./src/main.py", "process", 10, "forward")?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::FunctionNotFound`] if the function doesn't exist
/// - [`BrrrError::Parse`] if the file cannot be parsed
/// - [`BrrrError::InvalidArgument`] if line is 0 or direction is not "backward" or "forward"
pub fn get_pdg_slice(file: &str, function: &str, line: usize, direction: &str) -> Result<Vec<usize>> {
    // Validate line number is 1-indexed (lines start at 1, not 0)
    if line == 0 {
        return Err(BrrrError::InvalidArgument(
            "Line numbers are 1-indexed, got 0".to_string(),
        ));
    }
    match direction {
        "backward" => pdg::get_slice(file, function, line),
        "forward" => pdg::get_forward_slice(file, function, line),
        _ => Err(BrrrError::InvalidArgument(format!(
            "Invalid direction '{}', expected 'backward' or 'forward'",
            direction
        ))),
    }
}

/// Build the cross-file call graph for a project.
///
/// Scans all source files in the project, indexes function definitions,
/// and resolves call sites to build a complete call graph. This is the
/// foundation for impact analysis and dead code detection.
///
/// # Arguments
///
/// * `path` - Root project directory
///
/// # Returns
///
/// A [`CallGraph`] containing:
/// - `edges`: All call edges (caller -> callee with line numbers)
/// - `callers`: Index of callee -> set of callers (for impact analysis)
/// - `callees`: Index of caller -> set of callees (for forward traversal)
///
/// # Examples
///
/// ```no_run
/// use go_brrr::build_callgraph;
///
/// let graph = build_callgraph("./project")?;
/// println!("Found {} call edges", graph.edges.len());
/// println!("Unique functions: {}", graph.all_functions().len());
///
/// // Serialize for caching
/// let json = serde_json::to_string(&graph)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// # Performance
///
/// Building a call graph requires parsing all source files, so it can be
/// expensive for large projects. Consider using [`warm_callgraph`] to
/// pre-build the cache.
///
/// # Errors
///
/// Returns [`BrrrError`] if files cannot be read or parsed.
pub fn build_callgraph(path: &str) -> Result<CallGraph> {
    callgraph::build(path)
}

/// Find all callers of a function (impact analysis).
///
/// Performs reverse call graph traversal to find all functions that
/// directly or transitively call the target function. This is essential
/// for understanding the impact of changes to a function.
///
/// # Arguments
///
/// * `path` - Root project directory
/// * `function` - Target function name
/// * `depth` - Maximum traversal depth (typically 1-5)
///
/// # Returns
///
/// A vector of [`FunctionRef`] for all functions that call the target
/// (directly or transitively up to the specified depth).
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_impact;
///
/// // Who calls the database connection function?
/// let callers = get_impact("./project", "get_db_connection", 3)?;
/// println!("Functions affected by get_db_connection changes:");
/// for caller in &callers {
///     println!("  {}:{}", caller.file, caller.name);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Use Cases
///
/// - Assess blast radius before refactoring
/// - Find all code paths through a critical function
/// - Identify tightly coupled components
///
/// # Errors
///
/// Returns [`BrrrError`] if the project cannot be scanned.
pub fn get_impact(path: &str, function: &str, depth: usize) -> Result<Vec<FunctionRef>> {
    use callgraph::{cache, analyze_impact, ImpactConfig};

    let project = std::path::Path::new(path);
    let graph = cache::get_or_build_graph_with_config(project, None, false)?;
    let config = ImpactConfig::new().with_depth(depth);
    let result = analyze_impact(&graph, function, config);

    // Convert CallerInfo to FunctionRef for backward compatibility
    Ok(result
        .callers
        .into_iter()
        .map(|c| FunctionRef {
            file: c.file,
            name: c.name,
            qualified_name: c.qualified_name,
        })
        .collect())
}

/// Find dead (unreachable) code in a project.
///
/// Identifies functions that are not reachable from any entry point.
/// Entry points are detected heuristically (main functions, test functions,
/// exported modules, etc.).
///
/// # Arguments
///
/// * `path` - Root project directory
///
/// # Returns
///
/// A vector of [`FunctionRef`] for functions that appear to be unreachable.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::find_dead_code;
///
/// let dead = find_dead_code("./project")?;
/// if dead.is_empty() {
///     println!("No dead code detected!");
/// } else {
///     println!("Potentially dead code ({} functions):", dead.len());
///     for func in &dead {
///         println!("  {}:{}", func.file, func.name);
///     }
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Caveats
///
/// - Dynamic dispatch (e.g., `getattr`, reflection) may cause false positives
/// - Decorator-wrapped functions may not be detected as entry points
/// - Test discovery patterns are language-specific
///
/// # Errors
///
/// Returns [`BrrrError`] if the project cannot be scanned.
pub fn find_dead_code(path: &str) -> Result<Vec<FunctionRef>> {
    use callgraph::{cache, dead, DeadCodeConfig};

    let project = std::path::Path::new(path);
    let mut graph = cache::get_or_build_graph_with_config(project, None, false)?;
    graph.build_indexes();
    let result = dead::analyze_dead_code_with_config(&graph, &DeadCodeConfig::default());

    // Convert DeadFunction to FunctionRef for backward compatibility
    Ok(result
        .dead_functions
        .into_iter()
        .map(|d| FunctionRef {
            file: d.file,
            name: d.name,
            qualified_name: d.qualified_name,
        })
        .collect())
}

/// Warm the call graph cache for a project.
///
/// Pre-builds the call graph for a project so subsequent queries are fast.
/// This is useful for large projects where building the call graph is expensive.
///
/// # Arguments
///
/// * `path` - Root project directory
/// * `langs` - Optional language filter (e.g., `Some(&["python", "typescript"])`)
///
/// # Examples
///
/// ```no_run
/// use go_brrr::warm_callgraph;
///
/// // Pre-warm cache for all languages
/// warm_callgraph("./project", None)?;
///
/// // Pre-warm cache for specific languages
/// warm_callgraph("./project", Some(&["python".to_string(), "rust".to_string()]))?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError`] if the project cannot be scanned.
pub fn warm_callgraph(path: &str, langs: Option<&[String]>) -> Result<()> {
    let project = std::path::Path::new(path);
    let lang = langs.and_then(|l| l.first().map(|s| s.as_str()));
    callgraph::warm_cache_with_config(project, lang, false)
}

// =============================================================================
// Semantic Extraction API
// =============================================================================

/// Extract semantic units from a project for embedding.
///
/// Scans the project for source files, extracts functions, classes, and methods,
/// calculates token counts, adds semantic tags, and handles chunking for
/// oversized units (>8K tokens).
///
/// # Arguments
///
/// * `path` - Root project directory
/// * `lang` - Programming language to filter by (e.g., "python", "typescript")
///
/// # Returns
///
/// Vector of [`EmbeddingUnit`] objects ready for embedding and indexing.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::extract_semantic_units;
///
/// let units = extract_semantic_units("./my_project", "python")?;
/// for unit in &units {
///     println!("{}: {} ({} tokens, tags: {:?})",
///         unit.kind,
///         unit.name,
///         unit.token_count,
///         unit.semantic_tags
///     );
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError::Io`] if the path does not exist or cannot be read.
pub fn extract_semantic_units(path: &str, lang: &str) -> Result<Vec<EmbeddingUnit>> {
    semantic::extract_units(path, lang)
}

/// Extract semantic units with call graph information.
///
/// Similar to [`extract_semantic_units`], but also builds a call graph and
/// populates the `calls` and `called_by` fields for each unit. This provides
/// richer semantic context for embedding and retrieval.
///
/// # Arguments
///
/// * `path` - Root project directory
/// * `lang` - Programming language to filter by (e.g., "python", "typescript")
///
/// # Returns
///
/// Vector of [`EmbeddingUnit`] objects with populated call relationships.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::extract_semantic_units_with_callgraph;
///
/// let units = extract_semantic_units_with_callgraph("./my_project", "python")?;
/// for unit in &units {
///     if !unit.calls.is_empty() {
///         println!("{} calls: {:?}", unit.name, unit.calls);
///     }
///     if !unit.called_by.is_empty() {
///         println!("{} called by: {:?}", unit.name, unit.called_by);
///     }
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Performance
///
/// This function builds a call graph in addition to extracting units, so it
/// is more expensive than [`extract_semantic_units`]. Use this when call
/// relationships are important for your use case.
///
/// # Errors
///
/// Returns [`BrrrError`] if the path does not exist, cannot be read, or
/// call graph building fails.
pub fn extract_semantic_units_with_callgraph(path: &str, lang: &str) -> Result<Vec<EmbeddingUnit>> {
    semantic::extract_units_with_callgraph(path, lang)
}

/// Extract semantic units from a single file.
///
/// Convenience function for extracting units from a single source file
/// rather than an entire project.
///
/// # Arguments
///
/// * `file_path` - Path to the source file
///
/// # Returns
///
/// Vector of [`EmbeddingUnit`] objects from the file.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::extract_file_units;
///
/// let units = extract_file_units("./src/main.py")?;
/// for unit in &units {
///     println!("  {} ({}): {} tokens", unit.name, unit.kind, unit.token_count);
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the file type is not recognized
pub fn extract_file_units(file_path: &str) -> Result<Vec<EmbeddingUnit>> {
    semantic::extract_units_from_file(file_path)
}

/// Build embedding text from a semantic unit.
///
/// Creates a rich text representation suitable for embedding with a language model.
/// Includes natural language description, semantic tags, signature, complexity,
/// call relationships, and code preview.
///
/// # Arguments
///
/// * `unit` - The [`EmbeddingUnit`] to convert to embedding text
///
/// # Returns
///
/// A text string containing all relevant information for semantic embedding.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::{extract_file_units, build_embedding_text};
///
/// let units = extract_file_units("./src/auth.py")?;
/// for unit in &units {
///     let text = build_embedding_text(&unit);
///     // Send text to embedding model...
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
pub fn build_embedding_text(unit: &EmbeddingUnit) -> String {
    semantic::build_embedding_text(unit)
}

/// Detect semantic patterns in code.
///
/// Analyzes code to identify semantic categories like "crud", "validation",
/// "error_handling", "async_ops", etc. These patterns are used to enrich
/// embedding text for better semantic search retrieval.
///
/// # Arguments
///
/// * `code` - Source code to analyze for patterns
///
/// # Returns
///
/// Vector of detected pattern names (e.g., `["validation", "error_handling"]`).
///
/// # Examples
///
/// ```
/// use go_brrr::detect_semantic_patterns;
///
/// let code = "def validate_user(user): assert user is not None";
/// let patterns = detect_semantic_patterns(code);
/// assert!(patterns.contains(&"validation".to_string()));
/// ```
pub fn detect_semantic_patterns(code: &str) -> Vec<String> {
    semantic::detect_semantic_patterns(code)
}

/// Count tokens in text using tiktoken (cl100k_base).
///
/// Uses the same tokenizer as GPT-4 and Claude for accurate token counting.
/// Falls back to character-based estimation if tokenizer is unavailable.
///
/// # Arguments
///
/// * `text` - The text to count tokens in
///
/// # Returns
///
/// Number of tokens in the text.
///
/// # Examples
///
/// ```
/// use go_brrr::count_tokens;
///
/// let tokens = count_tokens("Hello, world!");
/// assert!(tokens > 0);
/// ```
pub fn count_tokens(text: &str) -> usize {
    semantic::count_tokens(text)
}

// =============================================================================
// Project Scanner API
// =============================================================================

/// Scan project for source files of a specific language.
///
/// This is a high-level convenience function that wraps [`ProjectScanner`].
/// For more control over scanning options, use [`ProjectScanner`] directly
/// with a [`ScanConfig`].
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `language` - Optional language filter (e.g., `Some("python")`, `Some("rust")`)
/// * `respect_ignore` - Whether to respect `.gitignore` and `.brrrignore` patterns.
///                      Note: Currently always true. This parameter is reserved for
///                      future use when ignore bypass is needed.
///
/// # Returns
///
/// A [`ScanResult`] containing:
/// - `files`: Vector of matching file paths
/// - `errors`: Any errors encountered during scanning (permission denied, broken symlinks, etc.)
/// - `warnings`: Non-fatal warnings
///
/// # Examples
///
/// ```no_run
/// use go_brrr::scan_project_files;
///
/// // Scan all supported source files
/// let result = scan_project_files("./src", None, true)?;
/// println!("Found {} files", result.files.len());
///
/// // Scan only Python files
/// let py_result = scan_project_files("./src", Some("python"), true)?;
/// println!("Found {} Python files", py_result.files.len());
///
/// // Check for errors
/// if result.has_errors() {
///     eprintln!("Warning: {}", result.error_summary());
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the path does not exist or cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
pub fn scan_project_files(
    root: &str,
    language: Option<&str>,
    _respect_ignore: bool, // Reserved for future use; currently always respects ignore
) -> Result<ScanResult> {
    let scanner = ProjectScanner::new(root)?;

    match language {
        Some(lang) => scanner.scan_language_with_errors(lang),
        None => scanner.scan_files_with_errors(),
    }
}

/// Scan project for files with specific extensions.
///
/// This is a high-level convenience function for extension-based filtering.
/// Respects `.gitignore` and `.brrrignore` patterns automatically.
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `extensions` - File extensions to include (e.g., `&[".py", ".pyi"]` or `&["py", "pyi"]`)
///                  Leading dots are optional; matching is case-insensitive.
///
/// # Returns
///
/// Vector of file paths matching the extensions.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::scan_extensions;
///
/// // Find all Python files (including type stubs)
/// let py_files = scan_extensions("./src", &[".py", ".pyi"])?;
/// println!("Found {} Python files", py_files.len());
///
/// // Extensions without leading dots also work
/// let rs_files = scan_extensions("./src", &["rs"])?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError::Io`] if the path does not exist or cannot be read.
pub fn scan_extensions(root: &str, extensions: &[&str]) -> Result<Vec<std::path::PathBuf>> {
    let scanner = ProjectScanner::new(root)?;
    scanner.scan_extensions(extensions)
}

/// Get file metadata for a project directory.
///
/// Scans the project and returns detailed metadata for each source file,
/// including file size, modification time, and detected language.
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `language` - Optional language filter (e.g., `Some("python")`)
///
/// # Returns
///
/// Vector of [`FileMetadata`] containing:
/// - `path`: Absolute path to the file
/// - `size`: File size in bytes
/// - `modified`: Last modification time (if available)
/// - `language`: Detected programming language (if recognized)
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_project_metadata;
///
/// // Get metadata for all source files
/// let metadata = get_project_metadata("./src", None)?;
/// for meta in &metadata {
///     println!("{}: {} bytes, lang={:?}",
///         meta.path.display(),
///         meta.size,
///         meta.language
///     );
/// }
///
/// // Get metadata for only Rust files
/// let rust_meta = get_project_metadata("./src", Some("rust"))?;
/// let total_bytes: u64 = rust_meta.iter().map(|m| m.size).sum();
/// println!("Total Rust code: {} bytes", total_bytes);
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the path does not exist or cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
pub fn get_project_metadata(root: &str, language: Option<&str>) -> Result<Vec<FileMetadata>> {
    let scanner = ProjectScanner::new(root)?;

    match language {
        Some(lang) => scanner.scan_language_with_metadata(lang),
        None => scanner.scan_with_metadata(),
    }
}

/// Scan project files with custom configuration.
///
/// This is the most flexible scanning function, supporting all configuration
/// options available in [`ScanConfig`]:
/// - Language and extension filtering
/// - Include/exclude glob patterns
/// - Metadata collection
/// - Parallel processing control
/// - Error handling strategy
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `config` - Scan configuration options
///
/// # Returns
///
/// A [`ScanResult`] containing files, metadata (if requested), and errors.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::{scan_with_config, ScanConfig, ErrorHandling};
///
/// // Scan Python files, excluding tests, with metadata
/// let config = ScanConfig::for_language("python")
///     .with_excludes(&["**/test/**", "**/tests/**"])
///     .with_metadata()
///     .with_error_handling(ErrorHandling::CollectAndContinue);
///
/// let result = scan_with_config("./src", &config)?;
/// println!("Found {} files ({} bytes total)",
///     result.files.len(),
///     result.total_bytes
/// );
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError`] if the path does not exist or scanning fails.
pub fn scan_with_config(root: &str, config: &ScanConfig) -> Result<ScanResult> {
    let scanner = ProjectScanner::new(root)?;
    scanner.scan_with_config(config)
}

/// Estimate the number of source files in a project.
///
/// Performs a full directory traversal to count supported source files.
/// Useful for progress bars or deciding scan strategy before more expensive
/// operations.
///
/// # Arguments
///
/// * `root` - Project root directory path
///
/// # Returns
///
/// Count of supported source files (respecting `.gitignore` and `.brrrignore`).
///
/// # Examples
///
/// ```no_run
/// use go_brrr::estimate_file_count;
///
/// let count = estimate_file_count("./my_project")?;
/// println!("Project contains {} source files", count);
///
/// if count > 1000 {
///     println!("Large project - consider using parallel scanning");
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// Returns [`BrrrError::Io`] if the path does not exist or cannot be read.
pub fn estimate_file_count(root: &str) -> Result<usize> {
    let scanner = ProjectScanner::new(root)?;
    scanner.estimate_file_count()
}

// =============================================================================
// Function Index API
// =============================================================================

/// Configuration for function indexing.
///
/// Controls how functions are discovered and indexed in a project.
/// Use the builder pattern to customize behavior.
///
/// # Examples
///
/// ```
/// use go_brrr::IndexingConfig;
///
/// // Index only Python files, excluding tests
/// let config = IndexingConfig::new()
///     .with_language("python")
///     .exclude_tests();
///
/// // Index all languages with parallel processing
/// let config = IndexingConfig::new()
///     .with_parallel(true);
/// ```
#[derive(Debug, Clone, Default)]
pub struct IndexingConfig {
    /// Optional language filter (e.g., "python", "typescript", "rust").
    /// If None, indexes all supported languages.
    pub language: Option<String>,
    /// Whether to respect .gitignore and .brrrignore patterns.
    /// Defaults to true.
    pub respect_ignore: bool,
    /// Whether to use parallel processing for indexing.
    /// Defaults to true.
    pub parallel: bool,
    /// Whether to include test files in the index.
    /// Defaults to true.
    pub include_tests: bool,
}

impl IndexingConfig {
    /// Create a new indexing configuration with default settings.
    ///
    /// Defaults:
    /// - No language filter (indexes all languages)
    /// - Respects ignore patterns
    /// - Uses parallel processing
    /// - Includes test files
    pub fn new() -> Self {
        Self {
            language: None,
            respect_ignore: true,
            parallel: true,
            include_tests: true,
        }
    }

    /// Set the language filter.
    ///
    /// Only files matching this language will be indexed.
    ///
    /// # Arguments
    ///
    /// * `lang` - Language name (e.g., "python", "typescript", "rust", "go")
    pub fn with_language(mut self, lang: &str) -> Self {
        self.language = Some(lang.to_string());
        self
    }

    /// Set whether to respect ignore patterns.
    ///
    /// If true (default), respects .gitignore and .brrrignore.
    pub fn with_respect_ignore(mut self, respect: bool) -> Self {
        self.respect_ignore = respect;
        self
    }

    /// Set whether to use parallel processing.
    ///
    /// If true (default), uses rayon for parallel file processing.
    pub fn with_parallel(mut self, parallel: bool) -> Self {
        self.parallel = parallel;
        self
    }

    /// Exclude test files from the index.
    ///
    /// Filters out files matching common test patterns.
    pub fn exclude_tests(mut self) -> Self {
        self.include_tests = false;
        self
    }

    /// Include test files in the index (default).
    pub fn include_tests(mut self) -> Self {
        self.include_tests = true;
        self
    }
}

/// Build a function index for a project.
///
/// Creates an index of all functions in the project that can be used for
/// fast lookups by name, qualified name, file, or class+method. This is the
/// foundation for call graph analysis, impact detection, and code navigation.
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `language` - Optional language filter (indexes all languages if None)
///
/// # Returns
///
/// A [`FunctionIndex`] with multiple lookup strategies:
/// - `lookup(name)` - Find all functions with a simple name
/// - `lookup_qualified(qname)` - Find by fully qualified name (e.g., "module.Class.method")
/// - `lookup_in_file(file, name)` - Find a function in a specific file
/// - `lookup_method(class, method)` - Find a method in a specific class
/// - `lookup_pattern(pattern)` - Find by partial qualified name pattern
///
/// # Examples
///
/// ```no_run
/// use go_brrr::build_function_index;
///
/// // Index all source files in a project
/// let index = build_function_index("./src", None)?;
/// println!("Indexed {} functions", index.len());
///
/// // Lookup by simple name (returns all matches)
/// let funcs = index.lookup("process_data");
/// for f in funcs {
///     println!("  Found: {} in {}", f.name, f.file);
/// }
///
/// // Lookup by qualified name (exact match)
/// if let Some(func) = index.lookup_qualified("mymodule.MyClass.process_data") {
///     println!("Found specific function in {}", func.file);
/// }
///
/// // Lookup by class and method name
/// let methods = index.lookup_method("MyClass", "process_data");
/// for m in methods {
///     println!("  Method: {}", m.qualified_name.as_deref().unwrap_or(&m.name));
/// }
///
/// // Index only Python files
/// let py_index = build_function_index("./src", Some("python"))?;
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Performance
///
/// Building the index requires parsing all matching source files, so it can be
/// expensive for large projects. The index is typically built once and reused
/// for multiple lookups.
///
/// # Errors
///
/// - [`BrrrError::Io`] if the path does not exist or cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the language filter is not recognized
pub fn build_function_index(root: &str, language: Option<&str>) -> Result<FunctionIndex> {
    use std::path::Path;

    let scanner = ProjectScanner::new(root)?;
    let project_root = Path::new(root);

    let files = match language {
        Some(lang) => scanner.scan_language(lang)?,
        None => scanner.scan_files()?,
    };

    FunctionIndex::build_with_root(&files, Some(project_root))
}

/// Build a function index with custom configuration.
///
/// Provides more control over indexing behavior than [`build_function_index`].
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `config` - Indexing configuration
///
/// # Returns
///
/// A [`FunctionIndex`] with the configured settings.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::{build_function_index_with_config, IndexingConfig};
///
/// // Index Python files, excluding tests
/// let config = IndexingConfig::new()
///     .with_language("python")
///     .exclude_tests();
///
/// let index = build_function_index_with_config("./src", &config)?;
/// println!("Indexed {} functions (excluding tests)", index.len());
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the path does not exist or cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the language filter is not recognized
pub fn build_function_index_with_config(root: &str, config: &IndexingConfig) -> Result<FunctionIndex> {
    use std::path::Path;

    let scanner = ProjectScanner::new(root)?;
    let project_root = Path::new(root);

    // Build scan config based on configuration options
    let mut scan_config = match &config.language {
        Some(lang) => ScanConfig::for_language(lang),
        None => ScanConfig::default(),
    };

    // Add test exclusion patterns if needed
    if !config.include_tests {
        scan_config = scan_config.with_excludes(&[
            "**/test/**",
            "**/tests/**",
            "**/*_test.*",
            "**/*_spec.*",
            "**/test_*.*",
        ]);
    }

    let result = scanner.scan_with_config(&scan_config)?;
    FunctionIndex::build_with_root(&result.files, Some(project_root))
}

// =============================================================================
// Import Analysis API
// =============================================================================

/// Result of an importer search - a file that imports a given module.
#[derive(Debug, Clone)]
pub struct ImporterInfo {
    /// Path to the file containing the import.
    pub file: std::path::PathBuf,
    /// The import statement details.
    pub import: ImportInfo,
}

/// Find all files that import a given module.
///
/// Scans the project for source files and analyzes their import statements
/// to find files that import the specified module. This is the reverse of
/// `get_imports` - instead of listing what a file imports, it lists what
/// files import a specific module.
///
/// # Arguments
///
/// * `root` - Project root directory path
/// * `module` - Module name to search for importers
/// * `language` - Optional language filter (e.g., `Some("python")`)
///
/// # Matching Rules
///
/// A file is considered to import the module if any of these match:
/// 1. Exact module match: `import.module == module`
/// 2. Module is the last component: "json" matches "os.json"
/// 3. Module is the first component: "os" matches "os.path"
/// 4. Module is in the middle: "path" matches "os.path.join"
/// 5. Any of the imported names match: "Path" matches `from pathlib import Path`
///
/// # Returns
///
/// Vector of [`ImporterInfo`] containing the file path and import details
/// for each file that imports the specified module.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_importers;
///
/// // Find all files that import the "json" module
/// let importers = get_importers("./src", "json", None)?;
/// println!("Found {} files importing 'json':", importers.len());
/// for importer in &importers {
///     println!("  {} at line {}", importer.file.display(), importer.import.line_number);
/// }
///
/// // Find Python files importing "flask"
/// let flask_users = get_importers("./src", "flask", Some("python"))?;
/// for importer in &flask_users {
///     println!("{}: {}", importer.file.display(), importer.import.statement());
/// }
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the path does not exist or cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the language is not recognized
pub fn get_importers(
    root: &str,
    module: &str,
    language: Option<&str>,
) -> Result<Vec<ImporterInfo>> {
    let scanner = ProjectScanner::new(root)?;

    // Get files - optionally filtered by language
    let files = match language {
        Some(lang) => scanner.scan_language(lang)?,
        None => scanner.scan_files()?,
    };

    let mut importers = Vec::new();

    for file_path in files {
        // Extract imports from file
        let imports = match ast::extract_imports(&file_path) {
            Ok(i) => i,
            Err(_) => continue, // Skip files that fail to parse
        };

        // Check if any import matches the module
        for import in imports {
            if import_matches_module(&import, module) {
                importers.push(ImporterInfo {
                    file: file_path.clone(),
                    import,
                });
            }
        }
    }

    Ok(importers)
}

/// Check if an import matches the target module name.
///
/// # Matching Rules
///
/// 1. Exact module match: `import.module == module`
/// 2. Module is the last component (e.g., "json" matches "os.json")
/// 3. Module is the first component (e.g., "os" matches "os.path")
/// 4. Module is in the middle (e.g., "path" matches "os.path.join")
/// 5. Any of the imported names match (e.g., "Path" matches `from pathlib import Path`)
fn import_matches_module(import: &ImportInfo, module: &str) -> bool {
    // Exact module match
    if import.module == module {
        return true;
    }

    // Module ends with ".{module}" (module is the last component)
    if import.module.ends_with(&format!(".{}", module)) {
        return true;
    }

    // Module starts with "{module}." (module is the first component)
    if import.module.starts_with(&format!("{}.", module)) {
        return true;
    }

    // Module is in the middle (e.g., "path" matches "os.path.join")
    if import.module.contains(&format!(".{}.", module)) {
        return true;
    }

    // Any of the imported names match exactly
    if import.names.iter().any(|name| name == module) {
        return true;
    }

    false
}

// =============================================================================
// Intra-File Call Graph API
// =============================================================================

/// Detailed information about a single intra-file function call.
///
/// Represents a call from one function to another within the same source file,
/// including the exact location of the call site.
#[derive(Debug, Clone, serde::Serialize)]
pub struct IntraFileCall {
    /// Name of the calling function.
    pub caller: String,
    /// Name of the called function.
    pub callee: String,
    /// Line number of the call (1-indexed).
    pub line: usize,
    /// Column number of the call (0-indexed).
    pub column: usize,
}

/// Get function call relationships within a single file.
///
/// Analyzes a source file and returns a mapping of which functions call which
/// other functions, considering only functions defined within the same file.
/// This is useful for understanding local code structure without needing to
/// build a full project call graph.
///
/// # Arguments
///
/// * `file_path` - Path to the source file to analyze
///
/// # Returns
///
/// A `HashMap` where keys are caller function names and values are vectors
/// of callee function names. Only includes calls to functions defined in the
/// same file (intra-file calls). Each caller has a deduplicated list of callees.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_intra_file_calls;
///
/// let calls = get_intra_file_calls("src/main.py")?;
/// for (caller, callees) in &calls {
///     println!("{} calls: {:?}", caller, callees);
/// }
/// // Output:
/// // main calls: ["process_data", "cleanup"]
/// // process_data calls: ["validate", "transform"]
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the file type is not recognized
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_intra_file_calls(file_path: &str) -> Result<std::collections::HashMap<String, Vec<String>>> {
    use std::collections::{HashMap, HashSet};
    use std::path::Path;

    let path = Path::new(file_path);
    let registry = LanguageRegistry::global();

    // Detect language from file extension
    let lang = registry.detect_language(path).ok_or_else(|| {
        BrrrError::UnsupportedLanguage(
            path.extension()
                .and_then(|e| e.to_str())
                .unwrap_or("unknown")
                .to_string(),
        )
    })?;

    // Read and parse the file
    let source = std::fs::read(path)
        .map_err(|e| BrrrError::io_with_path(e, path))?;
    let mut parser = lang.parser_for_path(path)?;
    let tree = parser.parse(&source, None).ok_or_else(|| BrrrError::Parse {
        file: file_path.to_string(),
        message: "Failed to parse file".to_string(),
    })?;

    // Extract all function definitions
    let module_info = ast::AstExtractor::extract_file(path)?;

    // Build set of function names defined in this file (including methods)
    let mut defined_functions: HashSet<String> = HashSet::new();
    for func in &module_info.functions {
        defined_functions.insert(func.name.clone());
    }
    for class in &module_info.classes {
        for method in &class.methods {
            defined_functions.insert(method.name.clone());
        }
    }

    // Build function line ranges for determining which function contains a call
    struct FuncRange {
        name: String,
        start_line: usize,
        end_line: usize,
    }
    let mut func_ranges: Vec<FuncRange> = Vec::new();

    for func in &module_info.functions {
        func_ranges.push(FuncRange {
            name: func.name.clone(),
            start_line: func.line_number,
            end_line: func.end_line_number.unwrap_or(func.line_number),
        });
    }
    for class in &module_info.classes {
        for method in &class.methods {
            func_ranges.push(FuncRange {
                name: method.name.clone(),
                start_line: method.line_number,
                end_line: method.end_line_number.unwrap_or(method.line_number),
            });
        }
    }

    // Sort by start line for efficient lookup
    func_ranges.sort_by_key(|r| r.start_line);

    // Initialize call map with empty vectors for all defined functions
    let mut calls: HashMap<String, Vec<String>> = HashMap::new();
    for func_name in &defined_functions {
        calls.insert(func_name.clone(), Vec::new());
    }

    // Use tree-sitter query to find call expressions
    let query_str = lang.call_query();
    let query = tree_sitter::Query::new(&tree.language(), query_str).map_err(|e| {
        BrrrError::TreeSitter(format!("Failed to compile call query: {}", e))
    })?;

    let mut cursor = tree_sitter::QueryCursor::new();
    let mut matches = cursor.matches(&query, tree.root_node(), source.as_slice());

    // Process all call matches
    use streaming_iterator::StreamingIterator;
    while let Some(match_) = matches.next() {
        // Find the callee capture (function name being called)
        let callee_capture = match_.captures.iter().find(|c| {
            let name = &query.capture_names()[c.index as usize];
            *name == "callee"
        });

        if let Some(capture) = callee_capture {
            let callee_node = capture.node;
            let callee_name = std::str::from_utf8(
                &source[callee_node.start_byte()..callee_node.end_byte()],
            )
            .unwrap_or("")
            .to_string();

            // Only consider calls to functions defined in this file
            if !defined_functions.contains(&callee_name) {
                continue;
            }

            // Find which function contains this call (by line number)
            let call_line = callee_node.start_position().row + 1;

            // Binary search would be more efficient, but linear is fine for typical file sizes
            for func_range in &func_ranges {
                if call_line >= func_range.start_line && call_line <= func_range.end_line {
                    // Don't add self-calls to the same function unless it's actually recursion
                    // (the call site line != function start line)
                    calls
                        .entry(func_range.name.clone())
                        .or_default()
                        .push(callee_name.clone());
                    break;
                }
            }
        }
    }

    // Deduplicate callees for each caller
    for callees in calls.values_mut() {
        callees.sort();
        callees.dedup();
    }

    Ok(calls)
}

/// Get detailed intra-file call information with line and column numbers.
///
/// Similar to [`get_intra_file_calls`], but returns detailed information about
/// each call site including the exact line and column where the call occurs.
/// This is useful for precise navigation and analysis.
///
/// # Arguments
///
/// * `file_path` - Path to the source file to analyze
///
/// # Returns
///
/// A vector of [`IntraFileCall`] structs, each representing a single call from
/// one function to another within the file. Multiple calls from the same caller
/// to the same callee will result in multiple entries with different locations.
///
/// # Examples
///
/// ```no_run
/// use go_brrr::get_intra_file_calls_detailed;
///
/// let calls = get_intra_file_calls_detailed("src/main.py")?;
/// for call in &calls {
///     println!("{}:{} - {} calls {}",
///         call.line, call.column, call.caller, call.callee);
/// }
/// // Output:
/// // 15:4 - main calls process_data
/// // 16:4 - main calls cleanup
/// // 25:8 - process_data calls validate
/// # Ok::<(), go_brrr::BrrrError>(())
/// ```
///
/// # Errors
///
/// - [`BrrrError::Io`] if the file cannot be read
/// - [`BrrrError::UnsupportedLanguage`] if the file type is not recognized
/// - [`BrrrError::Parse`] if the file cannot be parsed
pub fn get_intra_file_calls_detailed(file_path: &str) -> Result<Vec<IntraFileCall>> {
    use std::collections::HashSet;
    use std::path::Path;

    let path = Path::new(file_path);
    let registry = LanguageRegistry::global();

    // Detect language from file extension
    let lang = registry.detect_language(path).ok_or_else(|| {
        BrrrError::UnsupportedLanguage(
            path.extension()
                .and_then(|e| e.to_str())
                .unwrap_or("unknown")
                .to_string(),
        )
    })?;

    // Read and parse the file
    let source = std::fs::read(path)
        .map_err(|e| BrrrError::io_with_path(e, path))?;
    let mut parser = lang.parser_for_path(path)?;
    let tree = parser.parse(&source, None).ok_or_else(|| BrrrError::Parse {
        file: file_path.to_string(),
        message: "Failed to parse file".to_string(),
    })?;

    // Extract all function definitions
    let module_info = ast::AstExtractor::extract_file(path)?;

    // Build set of function names defined in this file (including methods)
    let mut defined_functions: HashSet<String> = HashSet::new();
    for func in &module_info.functions {
        defined_functions.insert(func.name.clone());
    }
    for class in &module_info.classes {
        for method in &class.methods {
            defined_functions.insert(method.name.clone());
        }
    }

    // Build function line ranges for determining which function contains a call
    struct FuncRange {
        name: String,
        start_line: usize,
        end_line: usize,
    }
    let mut func_ranges: Vec<FuncRange> = Vec::new();

    for func in &module_info.functions {
        func_ranges.push(FuncRange {
            name: func.name.clone(),
            start_line: func.line_number,
            end_line: func.end_line_number.unwrap_or(func.line_number),
        });
    }
    for class in &module_info.classes {
        for method in &class.methods {
            func_ranges.push(FuncRange {
                name: method.name.clone(),
                start_line: method.line_number,
                end_line: method.end_line_number.unwrap_or(method.line_number),
            });
        }
    }

    // Sort by start line for efficient lookup
    func_ranges.sort_by_key(|r| r.start_line);

    let mut detailed_calls: Vec<IntraFileCall> = Vec::new();

    // Use tree-sitter query to find call expressions
    let query_str = lang.call_query();
    let query = tree_sitter::Query::new(&tree.language(), query_str).map_err(|e| {
        BrrrError::TreeSitter(format!("Failed to compile call query: {}", e))
    })?;

    let mut cursor = tree_sitter::QueryCursor::new();
    let mut matches = cursor.matches(&query, tree.root_node(), source.as_slice());

    // Process all call matches
    use streaming_iterator::StreamingIterator;
    while let Some(match_) = matches.next() {
        // Find the callee capture (function name being called)
        let callee_capture = match_.captures.iter().find(|c| {
            let name = &query.capture_names()[c.index as usize];
            *name == "callee"
        });

        if let Some(capture) = callee_capture {
            let callee_node = capture.node;
            let callee_name = std::str::from_utf8(
                &source[callee_node.start_byte()..callee_node.end_byte()],
            )
            .unwrap_or("")
            .to_string();

            // Only consider calls to functions defined in this file
            if !defined_functions.contains(&callee_name) {
                continue;
            }

            // Get call location
            let position = callee_node.start_position();
            let call_line = position.row + 1;
            let call_column = position.column;

            // Find which function contains this call (by line number)
            for func_range in &func_ranges {
                if call_line >= func_range.start_line && call_line <= func_range.end_line {
                    detailed_calls.push(IntraFileCall {
                        caller: func_range.name.clone(),
                        callee: callee_name.clone(),
                        line: call_line,
                        column: call_column,
                    });
                    break;
                }
            }
        }
    }

    // Sort by line number for consistent output
    detailed_calls.sort_by(|a, b| {
        a.line.cmp(&b.line).then_with(|| a.column.cmp(&b.column))
    });

    Ok(detailed_calls)
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_public_api_types_exported() {
        // Verify that all public types are accessible
        fn _assert_types() {
            let _: Option<FileTreeEntry> = None;
            let _: Option<CodeStructure> = None;
            let _: Option<ModuleInfo> = None;
            let _: Option<SourceInput> = None;
            let _: Option<FunctionInfo> = None;
            let _: Option<ClassInfo> = None;
            let _: Option<ImportInfo> = None;
            let _: Option<CFGInfo> = None;
            let _: Option<CFGBlock> = None;
            let _: Option<CFGEdge> = None;
            let _: Option<BlockId> = None;
            let _: Option<DFGInfo> = None;
            let _: Option<DataflowEdge> = None;
            let _: Option<DataflowKind> = None;
            // PDG types
            let _: Option<PDGInfo> = None;
            let _: Option<ControlDependence> = None;
            let _: Option<BranchType> = None;
            let _: Option<SliceCriteria> = None;
            let _: Option<SliceResult> = None;
            let _: Option<SliceMetrics> = None;
            let _: Option<CallGraph> = None;
            let _: Option<CallEdge> = None;
            let _: Option<FunctionRef> = None;
            let _: Option<BrrrError> = None;
            // Function index types
            let _: Option<FunctionIndex> = None;
            let _: Option<FunctionDef> = None;
            let _: Option<IndexStats> = None;
            let _: Option<IndexingConfig> = None;
            // Semantic types
            let _: Option<EmbeddingUnit> = None;
            let _: Option<SearchResult> = None;
            let _: Option<UnitKind> = None;
            let _: Option<CodeComplexity> = None;
            let _: Option<ChunkInfo> = None;
            // Scanner types
            let _: Option<ProjectScanner> = None;
            let _: Option<ScanConfig> = None;
            let _: Option<ScanResult> = None;
            let _: Option<ScanError> = None;
            let _: Option<ScanErrorKind> = None;
            let _: Option<FileMetadata> = None;
            let _: Option<ErrorHandling> = None;
            // Dead code analysis types
            let _: Option<DeadCodeConfig> = None;
            let _: Option<DeadCodeResult> = None;
            let _: Option<DeadCodeStats> = None;
            let _: Option<DeadFunction> = None;
            let _: Option<DeadReason> = None;
            let _: Option<EntryPointKind> = None;
            // Impact analysis types
            let _: Option<ImpactConfig> = None;
            let _: Option<ImpactResult> = None;
            let _: Option<CallerInfo> = None;
            // Architecture analysis types
            let _: Option<ArchAnalysis> = None;
            let _: Option<ArchStats> = None;
            let _: Option<CycleDependency> = None;
            // Call graph cache types
            let _: Option<CachedCallGraph> = None;
            let _: Option<CachedEdge> = None;
            // Import analysis types
            let _: Option<ImporterInfo> = None;
            // Intra-file call graph types
            let _: Option<IntraFileCall> = None;
            // LLM context types
            let _: Option<FunctionContext> = None;
            let _: Option<RelevantContext> = None;
        }
    }

    #[test]
    fn test_public_api_functions_exist() {
        // Verify that all public API functions are callable
        // (compilation test - doesn't actually run them)
        fn _assert_functions() {
            let _ = get_tree as fn(&str, Option<&str>, bool, bool) -> Result<FileTreeEntry>;
            let _ = get_structure as fn(&str, Option<&str>, usize, bool) -> Result<CodeStructure>;
            let _ = extract_file as fn(&str, Option<&str>) -> Result<ModuleInfo>;
            let _ = extract_file_unchecked as fn(&str) -> Result<ModuleInfo>;
            let _ = extract_from_source as fn(&str, &str) -> Result<ModuleInfo>;
            let _ = get_imports as fn(&str, Option<&str>) -> Result<Vec<ImportInfo>>;
            let _ = get_context as fn(&str, &str, usize) -> Result<serde_json::Value>;
            let _ = get_cfg as fn(&str, &str, Option<&str>) -> Result<CFGInfo>;
            let _ = get_cfg_auto as fn(&str, &str) -> Result<CFGInfo>;
            let _ = get_cfg_from_source as fn(&str, &str, &str) -> Result<CFGInfo>;
            let _ = get_dfg as fn(&str, &str, Option<&str>) -> Result<DFGInfo>;
            let _ = get_dfg_auto as fn(&str, &str) -> Result<DFGInfo>;
            let _ = get_dfg_from_source as fn(&str, &str, &str) -> Result<DFGInfo>;
            let _ = get_pdg as fn(&str, &str, Option<&str>) -> Result<PDGInfo>;
            let _ = get_pdg_auto as fn(&str, &str) -> Result<PDGInfo>;
            let _ = get_slice as fn(&str, &str, usize, Option<&str>, Option<&str>, Option<&str>) -> Result<Vec<usize>>;
            let _ = get_slice_from_source as fn(&str, &str, usize, Option<&str>, Option<&str>, &str) -> Result<Vec<usize>>;
            let _ = get_backward_slice as fn(&str, &str, usize) -> Result<Vec<usize>>;
            let _ = get_slice_dfg_only as fn(&str, &str, usize) -> Result<Vec<usize>>;
            let _ = get_forward_slice as fn(&str, &str, usize) -> Result<Vec<usize>>;
            let _ = get_pdg_slice as fn(&str, &str, usize, &str) -> Result<Vec<usize>>;
            // PDG slicing functions (lower-level API)
            let _ = pdg_backward_slice as fn(&PDGInfo, &SliceCriteria) -> SliceResult;
            let _ = pdg_forward_slice as fn(&PDGInfo, &SliceCriteria) -> SliceResult;
            let _ = build_callgraph as fn(&str) -> Result<CallGraph>;
            let _ = get_impact as fn(&str, &str, usize) -> Result<Vec<FunctionRef>>;
            let _ = find_dead_code as fn(&str) -> Result<Vec<FunctionRef>>;
            let _ = warm_callgraph as fn(&str, Option<&[String]>) -> Result<()>;
            // Semantic API functions
            let _ = extract_semantic_units as fn(&str, &str) -> Result<Vec<EmbeddingUnit>>;
            let _ = extract_semantic_units_with_callgraph as fn(&str, &str) -> Result<Vec<EmbeddingUnit>>;
            let _ = extract_file_units as fn(&str) -> Result<Vec<EmbeddingUnit>>;
            let _ = build_embedding_text as fn(&EmbeddingUnit) -> String;
            let _ = count_tokens as fn(&str) -> usize;
            // Scanner API functions
            let _ = scan_project_files as fn(&str, Option<&str>, bool) -> Result<ScanResult>;
            let _ = scan_extensions as fn(&str, &[&str]) -> Result<Vec<std::path::PathBuf>>;
            let _ = get_project_metadata as fn(&str, Option<&str>) -> Result<Vec<FileMetadata>>;
            let _ = scan_with_config as fn(&str, &ScanConfig) -> Result<ScanResult>;
            let _ = estimate_file_count as fn(&str) -> Result<usize>;
            // Function index API functions
            let _ = build_function_index as fn(&str, Option<&str>) -> Result<FunctionIndex>;
            let _ = build_function_index_with_config as fn(&str, &IndexingConfig) -> Result<FunctionIndex>;
            // Import analysis API functions
            let _ = get_importers as fn(&str, &str, Option<&str>) -> Result<Vec<ImporterInfo>>;
            // Intra-file call graph API functions
            let _ = get_intra_file_calls as fn(&str) -> Result<std::collections::HashMap<String, Vec<String>>>;
            let _ = get_intra_file_calls_detailed as fn(&str) -> Result<Vec<IntraFileCall>>;
        }
    }

    #[test]
    fn test_semantic_constants_exported() {
        // Verify semantic constants are accessible
        assert!(MAX_EMBEDDING_TOKENS > 0);
        assert!(MAX_CODE_PREVIEW_TOKENS > 0);
        assert!(CHUNK_OVERLAP_TOKENS > 0);
    }

    #[test]
    fn test_semantic_pattern_exports() {
        // Verify SemanticPattern type is accessible
        let pattern = &SEMANTIC_PATTERNS[0];
        let _: &SemanticPattern = pattern;
        assert!(!pattern.name.is_empty());
        assert!(!pattern.pattern.is_empty());

        // Verify detect_semantic_patterns function works
        let patterns = detect_semantic_patterns("def validate_user(): assert x");
        assert!(patterns.contains(&"validation".to_string()));

        // Verify SEMANTIC_PATTERNS constant is accessible and non-empty
        assert!(!SEMANTIC_PATTERNS.is_empty());
    }

    #[test]
    fn test_callgraph_advanced_type_exports() {
        // Verify dead code analysis types and defaults
        let dead_config = DeadCodeConfig::default();
        assert!(!dead_config.include_public_api_patterns);
        assert!(dead_config.min_confidence > 0.0);
        let _: DeadCodeResult;
        let _: DeadCodeStats;
        let _: DeadFunction;
        let _: DeadReason;

        // Verify entry point detection types
        let kind = classify_entry_point("main");
        assert_eq!(kind, Some(EntryPointKind::Main));
        let test_kind = classify_entry_point("test_something");
        assert_eq!(test_kind, Some(EntryPointKind::Test));

        // Verify impact analysis types and defaults
        let impact_config = ImpactConfig::default();
        assert_eq!(impact_config.max_depth, 0);
        assert!(!impact_config.exclude_tests);
        let _: ImpactResult;
        let _: CallerInfo;

        // Verify architecture analysis types
        let _: ArchAnalysis;
        let _: ArchStats;
        let _: CycleDependency;

        // Verify cache types
        let _: CachedCallGraph;
        let _: CachedEdge;

        // Verify analysis functions are accessible
        let _ = analyze_dead_code as fn(&CallGraph) -> DeadCodeResult;
        let _ = analyze_dead_code_with_config as fn(&CallGraph, &DeadCodeConfig) -> DeadCodeResult;
        let _ = analyze_impact as fn(&CallGraph, &str, ImpactConfig) -> ImpactResult;
        let _ = detect_entry_points_with_config as fn(&CallGraph, &DeadCodeConfig) -> Vec<FunctionRef>;
        let _ = analyze_architecture as fn(&CallGraph) -> ArchAnalysis;
    }

    #[test]
    fn test_extract_from_source() {
        let source = r#"
def greet(name: str) -> str:
    """Say hello."""
    return f"Hello, {name}!"

class Greeter:
    def __init__(self, prefix: str):
        self.prefix = prefix
"#;
        let module = extract_from_source(source, "python").unwrap();
        assert_eq!(module.language, "python");
        assert_eq!(module.path, "<string>");
        assert_eq!(module.functions.len(), 1);
        assert_eq!(module.functions[0].name, "greet");
        assert_eq!(module.classes.len(), 1);
        assert_eq!(module.classes[0].name, "Greeter");
    }

    #[test]
    fn test_get_cfg_from_source() {
        let source = r#"
def process(x):
    if x > 0:
        return x * 2
    return 0
"#;
        let cfg = get_cfg_from_source(source, "process", "python").unwrap();
        assert_eq!(cfg.function_name, "process");
        // Should have at least entry, if-branch, else-branch, and exit
        assert!(cfg.blocks.len() >= 2, "CFG should have multiple blocks");
    }

    #[test]
    fn test_get_dfg_from_source() {
        let source = r#"
def compute(x, y):
    z = x + y
    result = z * 2
    return result
"#;
        let dfg = get_dfg_from_source(source, "compute", "python").unwrap();
        assert_eq!(dfg.function_name, "compute");
        assert!(dfg.definitions.contains_key("z"), "Should track 'z' definition");
        assert!(dfg.definitions.contains_key("result"), "Should track 'result' definition");
        assert!(dfg.uses.contains_key("x"), "Should track 'x' use");
        assert!(dfg.uses.contains_key("y"), "Should track 'y' use");
    }

    #[test]
    fn test_get_slice_from_source() {
        let source = r#"
def compute(x):
    a = x + 1
    b = a * 2
    c = b + x
    return c
"#;
        // Backward slice from line 5 (c = b + x)
        let slice = get_slice_from_source(source, "compute", 5, None, None, "python").unwrap();
        // Should include lines affecting line 5 (definitions of b, a, x)
        assert!(!slice.is_empty(), "Backward slice should not be empty");

        // Forward slice from line 3 (a = x + 1)
        let fwd_slice = get_slice_from_source(source, "compute", 3, Some("forward"), None, "python").unwrap();
        // Should include lines affected by line 3
        assert!(!fwd_slice.is_empty(), "Forward slice should not be empty");
    }

    #[test]
    fn test_source_input_enum() {
        // Test SourceInput::Path variant
        let path_input: SourceInput = SourceInput::Path("./test.py");
        match &path_input {
            SourceInput::Path(p) => assert_eq!(*p, "./test.py"),
            _ => panic!("Expected Path variant"),
        }

        // Test SourceInput::Source variant
        let source_input: SourceInput = SourceInput::Source {
            code: "def foo(): pass",
            language: "python",
        };
        match &source_input {
            SourceInput::Source { code, language } => {
                assert_eq!(*code, "def foo(): pass");
                assert_eq!(*language, "python");
            }
            _ => panic!("Expected Source variant"),
        }

        // Test resolve for Source variant
        let (bytes, lang, path) = source_input.resolve().unwrap();
        assert_eq!(bytes, b"def foo(): pass");
        assert_eq!(lang.name(), "python");
        assert!(path.is_none());
    }

    #[test]
    fn test_get_intra_file_calls_python() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = r#"
def helper():
    return 42

def process(x):
    result = helper()
    return result * 2

def main():
    value = process(10)
    helper()
    return value
"#;
        let mut file = tempfile::Builder::new()
            .suffix(".py")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls(file.path().to_str().unwrap()).unwrap();

        // Check that main calls both process and helper
        assert!(calls.contains_key("main"), "Should have main in call map");
        let main_calls = calls.get("main").unwrap();
        assert!(main_calls.contains(&"process".to_string()), "main should call process");
        assert!(main_calls.contains(&"helper".to_string()), "main should call helper");

        // Check that process calls helper
        assert!(calls.contains_key("process"), "Should have process in call map");
        let process_calls = calls.get("process").unwrap();
        assert!(process_calls.contains(&"helper".to_string()), "process should call helper");

        // Check that helper has no intra-file calls
        assert!(calls.contains_key("helper"), "Should have helper in call map");
        let helper_calls = calls.get("helper").unwrap();
        assert!(helper_calls.is_empty(), "helper should not call any local functions");
    }

    #[test]
    fn test_get_intra_file_calls_detailed_python() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = r#"def helper():
    return 42

def process(x):
    result = helper()
    return result * 2

def main():
    value = process(10)
    helper()
    return value
"#;
        let mut file = tempfile::Builder::new()
            .suffix(".py")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls_detailed(file.path().to_str().unwrap()).unwrap();

        // Should have at least 3 intra-file calls
        assert!(calls.len() >= 3, "Should have at least 3 intra-file calls, got {}", calls.len());

        // Check that calls are sorted by line number
        for window in calls.windows(2) {
            assert!(
                window[0].line <= window[1].line,
                "Calls should be sorted by line number"
            );
        }

        // Check that all calls have valid caller and callee names
        for call in &calls {
            assert!(!call.caller.is_empty(), "Caller name should not be empty");
            assert!(!call.callee.is_empty(), "Callee name should not be empty");
            assert!(call.line > 0, "Line number should be positive");
        }
    }

    #[test]
    fn test_get_intra_file_calls_typescript() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = r#"
function helper(): number {
    return 42;
}

function process(x: number): number {
    const result = helper();
    return result * 2;
}

function main(): number {
    const value = process(10);
    helper();
    return value;
}
"#;
        let mut file = tempfile::Builder::new()
            .suffix(".ts")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls(file.path().to_str().unwrap()).unwrap();

        // Check that main calls both process and helper
        assert!(calls.contains_key("main"), "Should have main in call map");
        let main_calls = calls.get("main").unwrap();
        assert!(main_calls.contains(&"process".to_string()), "main should call process");
        assert!(main_calls.contains(&"helper".to_string()), "main should call helper");
    }

    #[test]
    fn test_get_intra_file_calls_with_class_methods() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = r#"
def standalone():
    return 1

class Calculator:
    def add(self, a, b):
        return a + b

    def compute(self, x):
        result = self.add(x, 1)
        standalone()
        return result
"#;
        let mut file = tempfile::Builder::new()
            .suffix(".py")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls(file.path().to_str().unwrap()).unwrap();

        // Methods should be tracked
        assert!(calls.contains_key("add"), "Should have add method in call map");
        assert!(calls.contains_key("compute"), "Should have compute method in call map");
        assert!(calls.contains_key("standalone"), "Should have standalone in call map");

        // compute should call standalone (direct function call)
        let compute_calls = calls.get("compute").unwrap();
        assert!(
            compute_calls.contains(&"standalone".to_string()),
            "compute should call standalone"
        );
    }

    #[test]
    fn test_get_intra_file_calls_no_external_calls() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = r#"
import os

def local_func():
    return 42

def main():
    # This calls os.path.join which is external
    path = os.path.join("a", "b")
    # This calls local_func which is internal
    value = local_func()
    # This calls print which is builtin/external
    print(value)
    return value
"#;
        let mut file = tempfile::Builder::new()
            .suffix(".py")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls(file.path().to_str().unwrap()).unwrap();

        let main_calls = calls.get("main").unwrap();
        // Should only contain local_func, not os, join, or print
        assert_eq!(
            main_calls.len(), 1,
            "main should only call one local function, got {:?}", main_calls
        );
        assert!(
            main_calls.contains(&"local_func".to_string()),
            "main should call local_func"
        );
    }

    #[test]
    fn test_get_intra_file_calls_empty_file() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = "# Empty Python file with just a comment\n";
        let mut file = tempfile::Builder::new()
            .suffix(".py")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let calls = get_intra_file_calls(file.path().to_str().unwrap()).unwrap();

        // Should be empty since there are no functions
        assert!(calls.is_empty(), "Empty file should have no calls");
    }

    #[test]
    fn test_get_intra_file_calls_unsupported_language() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let source = "Some random content";
        let mut file = tempfile::Builder::new()
            .suffix(".xyz")
            .tempfile()
            .unwrap();
        file.write_all(source.as_bytes()).unwrap();

        let result = get_intra_file_calls(file.path().to_str().unwrap());
        assert!(result.is_err(), "Should return error for unsupported language");
        assert!(matches!(result, Err(BrrrError::UnsupportedLanguage(_))));
    }

    // =========================================================================
    // FunctionContext and RelevantContext Tests
    // =========================================================================

    #[test]
    fn test_function_context_minimal() {
        let ctx = FunctionContext::minimal("test_func", "src/main.rs", 10, "rust");
        assert_eq!(ctx.name, "test_func");
        assert_eq!(ctx.file, "src/main.rs");
        assert_eq!(ctx.start_line, 10);
        assert_eq!(ctx.end_line, 10);
        assert_eq!(ctx.language, "rust");
        assert!(ctx.signature.is_none());
        assert!(ctx.docstring.is_none());
        assert!(ctx.source.is_empty());
    }

    #[test]
    fn test_function_context_from_function_info() {
        let info = FunctionInfo {
            name: "process".to_string(),
            params: vec!["x: int".to_string(), "y: int".to_string()],
            return_type: Some("int".to_string()),
            docstring: Some("Process two numbers.".to_string()),
            is_method: false,
            is_async: false,
            decorators: vec![],
            line_number: 2,
            end_line_number: Some(5),
            language: "python".to_string(),
        };

        let source = "# Header\ndef process(x: int, y: int) -> int:\n    \"\"\"Process two numbers.\"\"\"\n    return x + y\n# Footer";
        let ctx = FunctionContext::from_function_info(&info, "src/main.py", source, "python");

        assert_eq!(ctx.name, "process");
        assert_eq!(ctx.file, "src/main.py");
        assert_eq!(ctx.start_line, 2);
        assert_eq!(ctx.end_line, 5);
        assert_eq!(ctx.language, "python");
        assert!(ctx.signature.is_some());
        assert!(ctx.signature.as_ref().unwrap().contains("process"));
        assert_eq!(ctx.docstring, Some("Process two numbers.".to_string()));
        // Source should contain lines 2-4 (0-indexed: 1-4)
        assert!(ctx.source.contains("def process"));
    }

    #[test]
    fn test_function_context_serialization() {
        let ctx = FunctionContext::minimal("test", "test.py", 1, "python");
        let json = serde_json::to_string(&ctx).unwrap();
        assert!(json.contains("\"name\":\"test\""));
        assert!(json.contains("\"file\":\"test.py\""));
        assert!(json.contains("\"language\":\"python\""));

        // Deserialize back
        let deserialized: FunctionContext = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.name, ctx.name);
        assert_eq!(deserialized.file, ctx.file);
        assert_eq!(deserialized.language, ctx.language);
    }

    #[test]
    fn test_relevant_context_new() {
        let entry = FunctionContext::minimal("main", "src/main.rs", 1, "rust");
        let ctx = RelevantContext::new(entry, 2);

        assert_eq!(ctx.entry.name, "main");
        assert_eq!(ctx.depth, 2);
        assert!(ctx.callees.is_empty());
        assert!(ctx.callers.is_empty());
        assert_eq!(ctx.token_count, 0);
    }

    #[test]
    fn test_relevant_context_builder_pattern() {
        let entry = FunctionContext::minimal("main", "src/main.rs", 1, "rust");
        let callee = FunctionContext::minimal("helper", "src/utils.rs", 10, "rust");
        let caller = FunctionContext::minimal("test_main", "tests/test.rs", 5, "rust");

        let ctx = RelevantContext::new(entry, 2)
            .with_callee(callee)
            .with_caller(caller)
            .with_token_count(500);

        assert_eq!(ctx.callees.len(), 1);
        assert_eq!(ctx.callees[0].name, "helper");
        assert_eq!(ctx.callers.len(), 1);
        assert_eq!(ctx.callers[0].name, "test_main");
        assert_eq!(ctx.token_count, 500);
        assert_eq!(ctx.function_count(), 3);
    }

    #[test]
    fn test_relevant_context_estimate_tokens() {
        let mut entry = FunctionContext::minimal("main", "src/main.rs", 1, "rust");
        entry.source = "fn main() { println!(\"hello\"); }".to_string(); // 33 chars

        let mut callee = FunctionContext::minimal("helper", "src/utils.rs", 10, "rust");
        callee.source = "fn helper() {}".to_string(); // 14 chars

        let mut ctx = RelevantContext::new(entry, 1).with_callee(callee);
        ctx.estimate_tokens();

        // Total: 47 chars / 4 = 11 tokens (integer division)
        assert_eq!(ctx.token_count, 11);
    }

    #[test]
    fn test_relevant_context_to_llm_string() {
        let mut entry = FunctionContext::minimal("process", "src/main.py", 10, "python");
        entry.signature = Some("def process(x: int) -> int".to_string());
        entry.docstring = Some("Process a number.".to_string());

        let callee = FunctionContext::minimal("helper", "src/utils.py", 25, "python");
        let caller = FunctionContext::minimal("main", "src/app.py", 5, "python");

        let ctx = RelevantContext::new(entry, 2)
            .with_callee(callee)
            .with_caller(caller)
            .with_token_count(100);

        let output = ctx.to_llm_string();

        // Verify structure
        assert!(output.contains("## Code Context: process (depth=2)"));
        assert!(output.contains("### Entry Point: process (main.py:10)"));
        assert!(output.contains("def process(x: int) -> int"));
        assert!(output.contains("> Process a number."));
        assert!(output.contains("### Calls:"));
        assert!(output.contains("- helper (utils.py:25)"));
        assert!(output.contains("### Called By:"));
        assert!(output.contains("- main (app.py:5)"));
        assert!(output.contains("Token estimate: 100"));
        assert!(output.contains("Functions: 3"));
    }

    #[test]
    fn test_relevant_context_serialization() {
        let entry = FunctionContext::minimal("test", "test.py", 1, "python");
        let callee = FunctionContext::minimal("helper", "helper.py", 5, "python");

        let ctx = RelevantContext::new(entry, 1)
            .with_callee(callee)
            .with_token_count(50);

        let json = serde_json::to_string(&ctx).unwrap();
        assert!(json.contains("\"depth\":1"));
        assert!(json.contains("\"token_count\":50"));

        // Deserialize back
        let deserialized: RelevantContext = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.entry.name, "test");
        assert_eq!(deserialized.callees.len(), 1);
        assert_eq!(deserialized.depth, 1);
        assert_eq!(deserialized.token_count, 50);
    }

    #[test]
    fn test_context_types_exported() {
        // Verify FunctionContext and RelevantContext are accessible
        fn _assert_context_types() {
            let _: Option<FunctionContext> = None;
            let _: Option<RelevantContext> = None;
        }
    }
}