agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
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
//! Simple step-by-step reasoning tool for AGCodex
//!
//! Provides transparent reasoning that's easy for LLMs to follow and understand.
//! Focuses on practical problem-solving with clear step-by-step breakdown.

use serde::Deserialize;
use serde::Serialize;
use std::collections::HashMap;
use std::collections::HashSet;
use thiserror::Error;

/// Tool recommendation for a thinking step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolRecommendation {
    /// Name of the tool being recommended
    pub tool: String,
    /// Confidence level in this recommendation (0.0 to 1.0)
    pub confidence: f32,
    /// Rationale for why this tool is recommended
    pub rationale: String,
    /// Priority order in the recommendation sequence
    pub priority: usize,
}

/// Description of a step in the thinking process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepDescription {
    /// What needs to be done in this step
    pub description: String,
    /// Expected outcome from this step
    pub expected_outcome: String,
    /// Conditions to consider for the next step
    pub next_conditions: Vec<String>,
}

/// Enhanced thought data structure inspired by MCP sequential-thinking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThoughtData {
    /// The actual thought content
    pub thought: String,
    /// Current thought number in the sequence
    pub thought_number: usize,
    /// Total estimated thoughts needed
    pub total_thoughts: usize,
    /// Whether another thought step is needed
    pub next_thought_needed: bool,
    /// Whether this thought revises a previous one
    pub is_revision: bool,
    /// Which thought number is being revised (if is_revision is true)
    pub revises_thought: Option<usize>,
    /// If branching, which thought is the branch point
    pub branch_from_thought: Option<usize>,
    /// Identifier for the current branch (if any)
    pub branch_id: Option<String>,
    /// Tools recommended for this thinking step
    pub recommended_tools: Vec<ToolRecommendation>,
    /// Current step description
    pub current_step: Option<StepDescription>,
    /// Previous steps that have been completed
    pub previous_steps: Vec<StepDescription>,
    /// Remaining high-level steps
    pub remaining_steps: Vec<String>,
    /// Confidence level for this thought (0.0 to 1.0)
    pub confidence: f32,
    /// Timestamp when this thought was created
    pub timestamp: u64,
}

impl ThoughtData {
    /// Create a new thought with basic information
    pub fn new(thought: String, thought_number: usize, total_thoughts: usize) -> Self {
        Self {
            thought,
            thought_number,
            total_thoughts,
            next_thought_needed: thought_number < total_thoughts,
            is_revision: false,
            revises_thought: None,
            branch_from_thought: None,
            branch_id: None,
            recommended_tools: Vec::new(),
            current_step: None,
            previous_steps: Vec::new(),
            remaining_steps: Vec::new(),
            confidence: 0.5,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0),
        }
    }

    /// Mark this thought as a revision of a previous thought
    pub const fn as_revision(mut self, revises: usize) -> Self {
        self.is_revision = true;
        self.revises_thought = Some(revises);
        self
    }

    /// Set this thought as part of a branch
    pub fn with_branch(mut self, branch_from: usize, branch_id: String) -> Self {
        self.branch_from_thought = Some(branch_from);
        self.branch_id = Some(branch_id);
        self
    }

    /// Add tool recommendations to this thought
    pub fn with_tools(mut self, tools: Vec<ToolRecommendation>) -> Self {
        self.recommended_tools = tools;
        self
    }

    /// Set the confidence level for this thought
    pub const fn with_confidence(mut self, confidence: f32) -> Self {
        self.confidence = confidence.max(0.0).min(1.0);
        self
    }
}

/// Session manager for tracking thought history and branches
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkSession {
    /// Main thought history
    pub thought_history: Vec<ThoughtData>,
    /// Branches of thought (key is branch_id)
    pub branches: HashMap<String, Vec<ThoughtData>>,
    /// Maximum number of thoughts to keep in history
    pub max_history_size: usize,
    /// Session identifier
    pub session_id: String,
    /// Current active branch (if any)
    pub active_branch: Option<String>,
    /// Total thoughts across all branches
    pub total_thought_count: usize,
    /// Session creation timestamp
    pub created_at: u64,
    /// Last activity timestamp
    pub last_activity: u64,
}

impl ThinkSession {
    /// Create a new thinking session
    pub fn new(max_history_size: usize) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        Self {
            thought_history: Vec::new(),
            branches: HashMap::new(),
            max_history_size,
            session_id: format!("think_{}", now),
            active_branch: None,
            total_thought_count: 0,
            created_at: now,
            last_activity: now,
        }
    }

    /// Add a thought to the current history (main or branch)
    pub fn add_thought(&mut self, thought: ThoughtData) {
        self.total_thought_count += 1;
        self.last_activity = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        if let Some(branch_id) = &self.active_branch {
            // Add to branch
            self.branches
                .entry(branch_id.clone())
                .or_default()
                .push(thought);

            // Trim branch if it exceeds max size
            if let Some(branch) = self.branches.get_mut(branch_id) {
                while branch.len() > self.max_history_size {
                    branch.remove(0);
                }
            }
        } else {
            // Add to main history
            self.thought_history.push(thought);

            // Trim history if it exceeds max size
            while self.thought_history.len() > self.max_history_size {
                self.thought_history.remove(0);
            }
        }
    }

    /// Create a new branch from a specific thought
    pub fn create_branch(&mut self, from_thought: usize, branch_id: String) {
        let branch_point = if let Some(branch_id) = &self.active_branch {
            // Branching from within a branch
            self.branches
                .get(branch_id)
                .and_then(|b| b.get(from_thought))
                .cloned()
        } else {
            // Branching from main history
            self.thought_history.get(from_thought).cloned()
        };

        if let Some(thought) = branch_point {
            let mut branch_history = vec![thought];
            branch_history[0].branch_from_thought = Some(from_thought);
            branch_history[0].branch_id = Some(branch_id.clone());
            self.branches.insert(branch_id.clone(), branch_history);
            self.active_branch = Some(branch_id);
        }
    }

    /// Switch to a different branch or back to main
    pub fn switch_branch(&mut self, branch_id: Option<String>) {
        self.active_branch = branch_id;
    }

    /// Get the current thought history (main or branch)
    pub fn current_history(&self) -> &[ThoughtData] {
        if let Some(branch_id) = &self.active_branch {
            self.branches
                .get(branch_id)
                .map(Vec::as_slice)
                .unwrap_or(&[])
        } else {
            &self.thought_history
        }
    }

    /// Find a thought that can be revised
    pub fn find_thought_to_revise(&self, thought_number: usize) -> Option<&ThoughtData> {
        self.current_history()
            .iter()
            .find(|t| t.thought_number == thought_number)
    }

    /// Get all thoughts that revised other thoughts
    pub fn get_revisions(&self) -> Vec<&ThoughtData> {
        self.current_history()
            .iter()
            .filter(|t| t.is_revision)
            .collect()
    }

    /// Get thoughts with high confidence
    pub fn get_high_confidence_thoughts(&self, threshold: f32) -> Vec<&ThoughtData> {
        self.current_history()
            .iter()
            .filter(|t| t.confidence >= threshold)
            .collect()
    }

    /// Get recommended tools from all thoughts
    pub fn get_all_recommended_tools(&self) -> Vec<&ToolRecommendation> {
        self.current_history()
            .iter()
            .flat_map(|t| &t.recommended_tools)
            .collect()
    }

    /// Check if more thoughts are needed
    pub fn needs_more_thoughts(&self) -> bool {
        self.current_history()
            .last()
            .map(|t| t.next_thought_needed)
            .unwrap_or(true)
    }
}

impl Default for ThinkSession {
    fn default() -> Self {
        Self::new(100) // Default to keeping 100 thoughts in history
    }
}

#[derive(Debug, Error)]
pub enum ThinkError {
    #[error("problem description is empty or too short")]
    EmptyProblem,

    #[error("failed to generate reasoning steps: {0}")]
    ReasoningFailed(String),

    #[error("confidence calculation error: {0}")]
    ConfidenceError(String),
}

/// A single step in the reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkStep {
    /// Step number (1-based)
    pub step_number: usize,
    /// The thought or reasoning for this step
    pub thought: String,
    /// The reasoning behind this step
    pub reasoning: String,
}

/// Complexity level for code problems
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Complexity {
    Simple,  // Single file, <50 lines, well-defined scope
    Medium,  // Multiple files, moderate scope, some dependencies
    Complex, // Cross-cutting concerns, architectural changes, high impact
}

/// Code-specific problem types for better reasoning
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CodeProblemType {
    BugFix,         // Debugging and fixing issues
    Refactoring,    // Code improvement without changing behavior
    Implementation, // New feature development
    Performance,    // Optimization and performance improvements
    Security,       // Security analysis and fixes
    Testing,        // Test creation and improvement
    Documentation,  // Code documentation and comments
    Architecture,   // System design and structural changes
    CodeReview,     // Code quality and standards review
}

/// Enhanced result for code-specific thinking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeThinkResult {
    /// All reasoning steps (minimum 3)
    pub steps: Vec<ThinkStep>,
    /// Final conclusion
    pub conclusion: String,
    /// Confidence level (0.0 to 1.0)
    pub confidence: f32,
    /// Recommended next action
    pub recommended_action: String,
    /// Files that might need changes
    pub affected_files: Vec<String>,
    /// Problem complexity assessment
    pub complexity: Complexity,
    /// Code problem type
    pub problem_type: CodeProblemType,
}

/// Complete result from the think tool (backward compatibility)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkResult {
    /// All reasoning steps (minimum 3)
    pub steps: Vec<ThinkStep>,
    /// Final conclusion
    pub conclusion: String,
    /// Confidence level (0.0 to 1.0)
    pub confidence: f32,
    /// Thought data for sequential thinking
    pub thought_data: Option<ThoughtData>,
}

/// Simple think tool implementation
#[derive(Debug)]
pub struct ThinkTool {
    /// Optional session for maintaining thinking state
    pub session: Option<ThinkSession>,
}

impl ThinkTool {
    /// Create a new think tool instance
    pub const fn new() -> Self {
        Self { session: None }
    }

    /// Create a new think tool with a session
    pub fn with_session(max_history_size: usize) -> Self {
        Self {
            session: Some(ThinkSession::new(max_history_size)),
        }
    }

    /// Process a single thought with revision support and tool recommendations
    pub fn process_thought(
        &mut self,
        thought: String,
        thought_number: usize,
        total_thoughts: usize,
        next_thought_needed: bool,
        is_revision: Option<bool>,
        revises_thought: Option<usize>,
    ) -> Result<ThinkResult, ThinkError> {
        // Generate tool recommendations based on thought content
        let recommended_tools = self.analyze_for_tool_recommendations(&thought);

        // Create thought data using the constructor
        let mut thought_data = ThoughtData::new(thought.clone(), thought_number, total_thoughts)
            .with_tools(recommended_tools);

        // Handle revision if specified
        if let Some(true) = is_revision {
            thought_data.is_revision = true;
            thought_data.revises_thought = revises_thought;
        }

        // Update next_thought_needed if different from default
        thought_data.next_thought_needed = next_thought_needed;

        // Store in session if available
        if let Some(ref mut session) = self.session {
            session.add_thought(thought_data.clone());
        }

        // Generate simple result for compatibility
        let steps = vec![ThinkStep {
            step_number: thought_number,
            thought: thought.clone(),
            reasoning: format!(
                "Processing thought {} of {}",
                thought_number, total_thoughts
            ),
        }];

        let conclusion = if next_thought_needed {
            format!(
                "Thought {} processed. More thoughts needed.",
                thought_number
            )
        } else {
            format!("Thought {} processed. Thinking complete.", thought_number)
        };

        Ok(ThinkResult {
            steps,
            conclusion,
            confidence: 0.7, // Default confidence
            thought_data: Some(thought_data),
        })
    }

    /// Analyze thought content to recommend appropriate tools
    fn analyze_for_tool_recommendations(&self, thought: &str) -> Vec<ToolRecommendation> {
        let mut recommendations = Vec::new();
        let thought_lower = thought.to_lowercase();
        let mut priority = 1;

        // Search tool recommendations
        if thought_lower.contains("search")
            || thought_lower.contains("find")
            || thought_lower.contains("look")
            || thought_lower.contains("locate")
            || thought_lower.contains("discover")
        {
            recommendations.push(ToolRecommendation {
                tool: "search".to_string(),
                confidence: 0.9,
                rationale: "Thought indicates need for code search or discovery".to_string(),
                priority,
            });
            priority += 1;
        }

        // Edit tool recommendations
        if thought_lower.contains("edit")
            || thought_lower.contains("modify")
            || thought_lower.contains("change")
            || thought_lower.contains("update")
            || thought_lower.contains("fix")
            || thought_lower.contains("correct")
        {
            recommendations.push(ToolRecommendation {
                tool: "edit".to_string(),
                confidence: 0.85,
                rationale: "Thought suggests code modification needed".to_string(),
                priority,
            });
            priority += 1;
        }

        // Patch tool recommendations (for more complex refactoring)
        if thought_lower.contains("refactor")
            || thought_lower.contains("rename")
            || thought_lower.contains("restructure")
            || thought_lower.contains("reorganize")
            || thought_lower.contains("extract")
        {
            recommendations.push(ToolRecommendation {
                tool: "patch".to_string(),
                confidence: 0.9,
                rationale: "Thought indicates structural code changes".to_string(),
                priority,
            });
            priority += 1;
        }

        // Test tool recommendations
        if thought_lower.contains("test")
            || thought_lower.contains("verify")
            || thought_lower.contains("validate")
            || thought_lower.contains("check")
            || thought_lower.contains("ensure")
        {
            recommendations.push(ToolRecommendation {
                tool: "test".to_string(),
                confidence: 0.8,
                rationale: "Thought suggests verification or testing needed".to_string(),
                priority,
            });
            priority += 1;
        }

        // Tree tool recommendations (AST analysis)
        if thought_lower.contains("parse")
            || thought_lower.contains("ast")
            || thought_lower.contains("syntax")
            || thought_lower.contains("structure")
            || thought_lower.contains("analyze")
        {
            recommendations.push(ToolRecommendation {
                tool: "tree".to_string(),
                confidence: 0.75,
                rationale: "Thought indicates need for AST or structural analysis".to_string(),
                priority,
            });
            priority += 1;
        }

        // Index tool recommendations
        if thought_lower.contains("index")
            || thought_lower.contains("catalog")
            || thought_lower.contains("organize")
            || thought_lower.contains("scan")
        {
            recommendations.push(ToolRecommendation {
                tool: "index".to_string(),
                confidence: 0.7,
                rationale: "Thought suggests indexing or cataloging needed".to_string(),
                priority,
            });
            priority += 1;
        }

        // Glob tool recommendations (file discovery)
        if thought_lower.contains("file")
            || thought_lower.contains("files")
            || thought_lower.contains("directory")
            || thought_lower.contains("folder")
            || thought_lower.contains("glob")
        {
            recommendations.push(ToolRecommendation {
                tool: "glob".to_string(),
                confidence: 0.75,
                rationale: "Thought indicates file system operations".to_string(),
                priority,
            });
            priority += 1;
        }

        // Plan tool recommendations
        if thought_lower.contains("plan")
            || thought_lower.contains("decompose")
            || thought_lower.contains("break down")
            || thought_lower.contains("strategy")
            || thought_lower.contains("approach")
        {
            recommendations.push(ToolRecommendation {
                tool: "plan".to_string(),
                confidence: 0.8,
                rationale: "Thought suggests need for task planning".to_string(),
                priority,
            });
        }

        // Sort by priority (already in order, but ensure consistency)
        recommendations.sort_by_key(|r| r.priority);

        recommendations
    }

    /// Perform step-by-step reasoning on a question or problem
    pub fn think(question: &str) -> Result<ThinkResult, ThinkError> {
        if question.trim().is_empty() || question.len() < 5 {
            return Err(ThinkError::EmptyProblem);
        }

        let steps = Self::generate_reasoning_steps(question)?;
        let conclusion = Self::generate_conclusion(question, &steps);
        let confidence = Self::calculate_confidence(question, &steps);

        Ok(ThinkResult {
            steps,
            conclusion,
            confidence,
            thought_data: None,
        })
    }

    /// Enhanced thinking specifically for code problems
    pub fn think_about_code(
        problem: &str,
        language: Option<&str>,
        context: Option<&str>, // File path, function name, etc
    ) -> Result<CodeThinkResult, ThinkError> {
        if problem.trim().is_empty() || problem.len() < 5 {
            return Err(ThinkError::EmptyProblem);
        }

        let problem_type = Self::classify_code_problem(problem);
        let complexity = Self::assess_complexity(problem, language, context);
        let steps = Self::generate_code_reasoning_steps(problem, &problem_type, language, context)?;
        let conclusion = Self::generate_code_conclusion(problem, &steps, &problem_type);
        let confidence = Self::calculate_code_confidence(problem, &steps, &problem_type);
        let recommended_action = Self::determine_recommended_action(&problem_type, problem);
        let affected_files = Self::identify_affected_files(problem, context);

        Ok(CodeThinkResult {
            steps,
            conclusion,
            confidence,
            recommended_action,
            affected_files,
            complexity,
            problem_type,
        })
    }

    /// Quick analysis for error messages and debugging
    pub fn analyze_error(error_message: &str) -> Result<CodeThinkResult, ThinkError> {
        if error_message.trim().is_empty() {
            return Err(ThinkError::EmptyProblem);
        }

        let _problem = format!("Debug and fix error: {}", error_message);
        let steps = Self::generate_error_analysis_steps(error_message)?;
        let recommended_action = Self::determine_error_action(error_message);
        let affected_files = Self::extract_files_from_error(error_message);
        let complexity = if affected_files.len() > 2 {
            Complexity::Complex
        } else if affected_files.len() > 1 {
            Complexity::Medium
        } else {
            Complexity::Simple
        };

        Ok(CodeThinkResult {
            steps,
            conclusion: "Systematic error analysis completed with specific debugging steps."
                .to_string(),
            confidence: 0.8,
            recommended_action,
            affected_files,
            complexity,
            problem_type: CodeProblemType::BugFix,
        })
    }

    /// Specialized thinking for refactoring scenarios
    pub fn plan_refactoring(code_smell: &str) -> Result<CodeThinkResult, ThinkError> {
        if code_smell.trim().is_empty() {
            return Err(ThinkError::EmptyProblem);
        }

        let _problem = format!("Refactor to address: {}", code_smell);
        let steps = Self::generate_refactoring_steps(code_smell)?;
        let recommended_action = "Implement refactoring strategy".to_string();
        let _complexity = 5; // Default complexity score for refactoring

        Ok(CodeThinkResult {
            steps,
            conclusion: "Refactoring strategy defined with clear improvement goals.".to_string(),
            confidence: 0.85,
            recommended_action,
            affected_files: vec![], // Would need more context to determine
            complexity: Complexity::Medium,
            problem_type: CodeProblemType::Refactoring,
        })
    }

    /// Generate at least 3 reasoning steps for the given question
    fn generate_reasoning_steps(question: &str) -> Result<Vec<ThinkStep>, ThinkError> {
        let mut steps = Vec::new();

        // Step 1: Problem Analysis
        steps.push(ThinkStep {
            step_number: 1,
            thought: format!("Analyzing the core question: '{}'", question.trim()),
            reasoning: Self::analyze_problem_type(question),
        });

        // Step 2: Breaking Down the Problem
        steps.push(ThinkStep {
            step_number: 2,
            thought: "Breaking down the problem into key components".to_string(),
            reasoning: Self::identify_key_components(question),
        });

        // Step 3: Considering Approach
        steps.push(ThinkStep {
            step_number: 3,
            thought: "Evaluating possible approaches and considerations".to_string(),
            reasoning: Self::evaluate_approaches(question),
        });

        // Additional steps based on problem complexity
        if Self::is_complex_problem(question) {
            steps.push(ThinkStep {
                step_number: 4,
                thought: "Analyzing potential challenges and constraints".to_string(),
                reasoning: Self::analyze_constraints(question),
            });
        }

        if Self::requires_implementation_thinking(question) {
            let step_num = steps.len() + 1;
            steps.push(ThinkStep {
                step_number: step_num,
                thought: "Considering implementation details and practical aspects".to_string(),
                reasoning: Self::consider_implementation(question),
            });
        }

        Ok(steps)
    }

    /// Generate a conclusion based on the question and reasoning steps
    fn generate_conclusion(question: &str, _steps: &[ThinkStep]) -> String {
        let problem_type = Self::classify_problem_type(question);

        match problem_type {
            ProblemType::Technical => {
                "Based on analysis: Technical solution requires systematic approach with careful implementation, testing, and edge case handling.".to_string()
            }
            ProblemType::Design => {
                "Design approach: Balance user requirements, technical constraints, and maintainability through iterative refinement.".to_string()
            }
            ProblemType::Analysis => {
                "Analysis complete: Key factors identified. Thorough data examination will inform decision-making process.".to_string()
            }
            ProblemType::Planning => {
                "Planning strategy: Break into phases, identify dependencies, maintain flexibility for evolving requirements.".to_string()
            }
            ProblemType::Debugging => {
                "Debug approach: Systematic investigation from most likely causes, methodical elimination of potential issues.".to_string()
            }
            ProblemType::General => {
                "Solution path: Focus on core requirements while maintaining adaptability for changing circumstances.".to_string()
            }
        }
    }

    /// Calculate confidence based on problem clarity and completeness
    fn calculate_confidence(question: &str, steps: &[ThinkStep]) -> f32 {
        let mut confidence = 0.5; // Base confidence

        // Boost confidence for well-defined problems
        if Self::is_well_defined_problem(question) {
            confidence += 0.2;
        }

        // Boost confidence based on number of reasoning steps
        let step_bonus = (steps.len() as f32 - 3.0) * 0.05;
        confidence += step_bonus.min(0.2);

        // Boost confidence for specific technical terms
        if Self::contains_technical_terms(question) {
            confidence += 0.1;
        }

        // Reduce confidence for very broad or vague questions
        if Self::is_vague_problem(question) {
            confidence -= 0.2;
        }

        // Ensure confidence stays within bounds
        confidence.max(0.1).min(0.95)
    }

    // Code-specific helper methods

    fn classify_code_problem(problem: &str) -> CodeProblemType {
        let p_lower = problem.to_lowercase();

        // Check bug/debug/fix keywords first (highest priority for debugging tasks)
        if p_lower.contains("bug")
            || p_lower.contains("crash")
            || p_lower.contains("debug")
            || p_lower.contains("exception")
            || p_lower.contains("failure")
            || p_lower.contains("memory leak") // Memory leaks are bugs to fix
            || (p_lower.contains("fix")
                && (p_lower.contains("error")
                    || p_lower.contains("issue")
                    || p_lower.contains("null")))
        {
            CodeProblemType::BugFix
        // Implementation should take priority when explicitly mentioned
        } else if p_lower.contains("implement")
            || p_lower.contains("build")
            || p_lower.contains("create")
            || p_lower.contains("develop")
            || p_lower.contains("add feature")
            || p_lower.contains("new feature")
        {
            CodeProblemType::Implementation
        // Security checks (without conflicting with implementation)
        } else if p_lower.contains("security")
            || p_lower.contains("vulnerability")
            || p_lower.contains("exploit")
            || p_lower.contains("injection")
            || p_lower.contains("csrf")
            || p_lower.contains("xss")
            || (p_lower.contains("auth")
                && p_lower.contains("secure")
                && !p_lower.contains("implement"))
        // Only pure security-focused auth
        {
            CodeProblemType::Security
        // Performance optimization (without memory leak which is a bug)
        } else if p_lower.contains("optimiz") // Catches both optimize and optimization
            || p_lower.contains("performance")
            || p_lower.contains("speed")
            || p_lower.contains("efficient")
            || p_lower.contains("bottleneck")
            || p_lower.contains("latency")
            || p_lower.contains("throughput")
        {
            CodeProblemType::Performance
        } else if p_lower.contains("architecture")
            || p_lower.contains("design pattern")
            || p_lower.contains("system design")
            || p_lower.contains("microservice")
            || (p_lower.contains("design")
                && (p_lower.contains("system") || p_lower.contains("pattern")))
        {
            CodeProblemType::Architecture
        } else if p_lower.contains("refactor")
            || p_lower.contains("clean up")
            || p_lower.contains("improve code")
            || p_lower.contains("simplify")
            || p_lower.contains("restructure")
            || p_lower.contains("code smell")
        {
            CodeProblemType::Refactoring
        } else if p_lower.contains("test")
            && (p_lower.contains("write")
                || p_lower.contains("create")
                || p_lower.contains("unit")
                || p_lower.contains("integration")
                || p_lower.contains("coverage"))
        {
            CodeProblemType::Testing
        } else if p_lower.contains("document")
            || p_lower.contains("comment")
            || p_lower.contains("explain")
            || p_lower.contains("readme")
            || p_lower.contains("api doc")
            || p_lower.contains("javadoc")
        {
            CodeProblemType::Documentation
        } else if p_lower.contains("review")
            && (p_lower.contains("code")
                || p_lower.contains("quality")
                || p_lower.contains("standards")
                || p_lower.contains("best practice")
                || p_lower.contains("lint"))
        {
            CodeProblemType::CodeReview
        // Catch any remaining auth-related tasks as implementation
        } else if p_lower.contains("auth") && !p_lower.contains("secure") {
            CodeProblemType::Implementation
        } else {
            CodeProblemType::Implementation // Default for ambiguous cases
        }
    }

    fn assess_complexity(
        problem: &str,
        language: Option<&str>,
        context: Option<&str>,
    ) -> Complexity {
        let mut complexity_score = 0;

        // Problem complexity indicators
        let p_lower = problem.to_lowercase();
        if p_lower.contains("system") || p_lower.contains("architecture") {
            complexity_score += 2;
        }
        if p_lower.contains("multiple") || p_lower.contains("several") {
            complexity_score += 1;
        }
        if p_lower.contains("integration") || p_lower.contains("api") {
            complexity_score += 1;
        }
        if p_lower.contains("database") || p_lower.contains("concurrent") {
            complexity_score += 1;
        }
        if p_lower.contains("migration") || p_lower.contains("legacy") {
            complexity_score += 2;
        }

        // Language complexity
        if let Some(lang) = language {
            let lang_lower = lang.to_lowercase();
            if lang_lower == "c++" || lang_lower == "rust" || lang_lower == "haskell" {
                complexity_score += 1;
            }
            if lang_lower == "assembly" || lang_lower == "cuda" {
                complexity_score += 2;
            }
        }

        // Context complexity
        if let Some(ctx) = context {
            if ctx.contains('/') && ctx.matches('/').count() > 3 {
                complexity_score += 1;
            } // Deep file structure
            if ctx.contains("test") && ctx.contains("integration") {
                complexity_score += 1;
            }
        }

        // Problem length as complexity indicator
        if problem.len() > 200 {
            complexity_score += 1;
        }
        if problem.len() > 500 {
            complexity_score += 2;
        }

        match complexity_score {
            0..=2 => Complexity::Simple,
            3..=5 => Complexity::Medium,
            _ => Complexity::Complex,
        }
    }

    fn generate_code_reasoning_steps(
        problem: &str,
        problem_type: &CodeProblemType,
        language: Option<&str>,
        context: Option<&str>,
    ) -> Result<Vec<ThinkStep>, ThinkError> {
        let mut steps = Vec::new();

        // Step 1: Code Context Understanding
        let context_analysis = if let Some(ctx) = context {
            format!(
                "Code context: {}. Understanding scope and dependencies.",
                ctx
            )
        } else {
            "Analyzing code context and identifying scope of changes.".to_string()
        };

        steps.push(ThinkStep {
            step_number: 1,
            thought: format!("Understanding code context for: '{}'", problem.trim()),
            reasoning: context_analysis,
        });

        // Step 2: Problem-specific analysis
        let problem_reasoning = match problem_type {
            CodeProblemType::BugFix => {
                "Identifying root cause, reproduction steps, and potential side effects of the bug."
            }
            CodeProblemType::Refactoring => {
                "Analyzing code structure, identifying improvement opportunities without changing behavior."
            }
            CodeProblemType::Implementation => {
                "Breaking down requirements into implementable components and design decisions."
            }
            CodeProblemType::Performance => {
                "Profiling bottlenecks, measuring current performance, identifying optimization targets."
            }
            CodeProblemType::Security => {
                "Evaluating security vulnerabilities, attack vectors, and mitigation strategies."
            }
            CodeProblemType::Testing => {
                "Designing test cases, coverage analysis, and validation strategies."
            }
            CodeProblemType::Documentation => {
                "Structuring documentation, identifying key concepts, and user scenarios."
            }
            CodeProblemType::Architecture => {
                "Analyzing system components, dependencies, and architectural patterns."
            }
            CodeProblemType::CodeReview => {
                "Evaluating code quality, standards compliance, and best practices."
            }
        };

        steps.push(ThinkStep {
            step_number: 2,
            thought: format!("Analyzing specific challenge: {:?} problem", problem_type),
            reasoning: problem_reasoning.to_string(),
        });

        // Step 3: Implementation approach
        let approach_reasoning = Self::get_implementation_approach(problem_type, language);
        steps.push(ThinkStep {
            step_number: 3,
            thought: "Evaluating implementation approaches and technical considerations"
                .to_string(),
            reasoning: approach_reasoning,
        });

        // Step 4: Trade-offs and constraints (for complex problems)
        if Self::is_complex_code_problem(problem, problem_type) {
            steps.push(ThinkStep {
                step_number: 4,
                thought: "Analyzing trade-offs and technical constraints".to_string(),
                reasoning: Self::analyze_code_tradeoffs(problem_type, language),
            });
        }

        // Step 5: Solution strategy
        let solution_step = steps.len() + 1;
        steps.push(ThinkStep {
            step_number: solution_step,
            thought: "Proposing concrete solution strategy".to_string(),
            reasoning: Self::propose_solution_strategy(problem_type, problem),
        });

        Ok(steps)
    }

    fn generate_error_analysis_steps(error_message: &str) -> Result<Vec<ThinkStep>, ThinkError> {
        let mut steps = Vec::new();

        // Step 1: Error message parsing
        steps.push(ThinkStep {
            step_number: 1,
            thought: "Parsing error message for key information".to_string(),
            reasoning: Self::parse_error_components(error_message),
        });

        // Step 2: Root cause analysis
        steps.push(ThinkStep {
            step_number: 2,
            thought: "Identifying potential root causes".to_string(),
            reasoning: Self::identify_error_causes(error_message),
        });

        // Step 3: Debugging strategy
        steps.push(ThinkStep {
            step_number: 3,
            thought: "Planning systematic debugging approach".to_string(),
            reasoning:
                "Use debugging tools, add logging, isolate components, reproduce consistently."
                    .to_string(),
        });

        Ok(steps)
    }

    fn generate_refactoring_steps(code_smell: &str) -> Result<Vec<ThinkStep>, ThinkError> {
        let mut steps = Vec::new();

        // Step 1: Code smell analysis
        steps.push(ThinkStep {
            step_number: 1,
            thought: "Analyzing code smell and its impact".to_string(),
            reasoning: Self::analyze_code_smell(code_smell),
        });

        // Step 2: Refactoring strategy
        steps.push(ThinkStep {
            step_number: 2,
            thought: "Selecting appropriate refactoring technique".to_string(),
            reasoning: Self::select_refactoring_technique(code_smell),
        });

        // Step 3: Safety considerations
        steps.push(ThinkStep {
            step_number: 3,
            thought: "Planning safe refactoring with behavior preservation".to_string(),
            reasoning: "Ensure comprehensive tests, incremental changes, continuous validation."
                .to_string(),
        });

        Ok(steps)
    }

    // Helper methods for problem analysis

    fn analyze_problem_type(question: &str) -> String {
        let problem_type = Self::classify_problem_type(question);
        match problem_type {
            ProblemType::Technical => {
                "Technical problem - requires systematic analysis and solution design"
            }
            ProblemType::Design => {
                "Design challenge - needs multiple perspectives and trade-off analysis"
            }
            ProblemType::Analysis => {
                "Analysis task - examine data, patterns, and existing conditions"
            }
            ProblemType::Planning => {
                "Planning objective - structured approach with timeline consideration"
            }
            ProblemType::Debugging => {
                "Troubleshooting scenario - systematic investigation required"
            }
            ProblemType::General => "General problem - logical step-by-step reasoning approach",
        }
        .to_string()
    }

    fn identify_key_components(question: &str) -> String {
        let q_lower = question.to_lowercase();
        let mut components = Vec::new();

        // Question type analysis
        if q_lower.contains("how") {
            components.push("Process/method");
        }
        if q_lower.contains("why") {
            components.push("Causal analysis");
        }
        if q_lower.contains("what") {
            components.push("Definition/identification");
        }
        if q_lower.contains("when") {
            components.push("Timing");
        }
        if q_lower.contains("where") {
            components.push("Location/context");
        }

        // Action type analysis
        if q_lower.contains("implement") || q_lower.contains("build") || q_lower.contains("create")
        {
            components.push("Implementation");
        }
        if q_lower.contains("optimize") || q_lower.contains("improve") {
            components.push("Performance/efficiency");
        }
        if q_lower.contains("debug") || q_lower.contains("fix") || q_lower.contains("error") {
            components.push("Problem resolution");
        }

        if components.is_empty() {
            "Core objectives and success criteria need identification".to_string()
        } else {
            format!("Key components: {}", components.join(", "))
        }
    }

    fn evaluate_approaches(question: &str) -> String {
        let q_lower = question.to_lowercase();

        if q_lower.contains("code") || q_lower.contains("program") || q_lower.contains("implement") {
            "Implementation factors: code quality, performance, maintainability, testing, documentation"
        } else if q_lower.contains("design") || q_lower.contains("architecture") {
            "Design factors: user needs, technical constraints, scalability, extensibility"
        } else if q_lower.contains("debug") || q_lower.contains("error") || q_lower.contains("bug") {
            "Debug factors: reproduction steps, error patterns, logs, systematic elimination"
        } else if q_lower.contains("optimize") || q_lower.contains("performance") {
            "Performance factors: bottlenecks, measurement, trade-offs, system impact"
        } else {
            "General factors: resources, constraints, alternatives, risks"
        }.to_string()
    }

    fn analyze_constraints(question: &str) -> String {
        let q_lower = question.to_lowercase();
        let mut constraints = Vec::new();

        if q_lower.contains("time") || q_lower.contains("deadline") || q_lower.contains("urgent") {
            constraints.push("Time");
        }
        if q_lower.contains("budget") || q_lower.contains("cost") || q_lower.contains("resource") {
            constraints.push("Resources");
        }
        if q_lower.contains("compatibility") || q_lower.contains("legacy") {
            constraints.push("Compatibility");
        }
        if q_lower.contains("security") || q_lower.contains("privacy") {
            constraints.push("Security");
        }
        if q_lower.contains("scalab") || q_lower.contains("performance") {
            constraints.push("Performance");
        }

        if constraints.is_empty() {
            "Consider technical, business, and operational constraints impacting solution"
                .to_string()
        } else {
            format!("Constraints: {}", constraints.join(", "))
        }
    }

    fn consider_implementation(question: &str) -> String {
        let q_lower = question.to_lowercase();

        if q_lower.contains("test") {
            "Implementation: comprehensive testing (unit, integration, edge cases)"
        } else if q_lower.contains("deploy") || q_lower.contains("production") {
            "Implementation: deployment strategy, monitoring, rollback, production readiness"
        } else {
            "Implementation: incremental approach with validation, documentation, maintainability"
        }
        .to_string()
    }

    // Classification helpers

    fn classify_problem_type(question: &str) -> ProblemType {
        let q_lower = question.to_lowercase();

        if q_lower.contains("implement")
            || q_lower.contains("code")
            || q_lower.contains("program")
            || q_lower.contains("algorithm")
            || q_lower.contains("function")
        {
            ProblemType::Technical
        } else if q_lower.contains("design")
            || q_lower.contains("architecture")
            || q_lower.contains("structure")
        {
            ProblemType::Design
        } else if q_lower.contains("analyze")
            || q_lower.contains("evaluate")
            || q_lower.contains("compare")
        {
            ProblemType::Analysis
        } else if q_lower.contains("plan")
            || q_lower.contains("schedule")
            || q_lower.contains("organize")
        {
            ProblemType::Planning
        } else if q_lower.contains("debug")
            || q_lower.contains("fix")
            || q_lower.contains("error")
            || q_lower.contains("bug")
            || q_lower.contains("problem")
        {
            ProblemType::Debugging
        } else {
            ProblemType::General
        }
    }

    fn is_complex_problem(question: &str) -> bool {
        let q_lower = question.to_lowercase();
        question.len() > 50
            || q_lower.contains("complex")
            || q_lower.contains("multiple")
            || q_lower.contains("integrate")
            || q_lower.contains("system")
            || (q_lower.matches("and").count() + q_lower.matches("or").count()) > 2
    }

    fn requires_implementation_thinking(question: &str) -> bool {
        let q_lower = question.to_lowercase();
        q_lower.contains("implement")
            || q_lower.contains("build")
            || q_lower.contains("create")
            || q_lower.contains("develop")
            || q_lower.contains("code")
    }

    fn is_well_defined_problem(question: &str) -> bool {
        let q_lower = question.to_lowercase();
        let has_specifics = q_lower.contains("specific")
            || q_lower.contains("exactly")
            || q_lower.contains("precisely");
        let has_context = question.len() > 30;
        let has_clear_intent = q_lower.starts_with("how to")
            || q_lower.starts_with("what is")
            || q_lower.starts_with("why does");

        has_specifics || (has_context && has_clear_intent)
    }

    fn contains_technical_terms(question: &str) -> bool {
        let q_lower = question.to_lowercase();
        let technical_terms = [
            "algorithm",
            "function",
            "class",
            "method",
            "variable",
            "array",
            "object",
            "database",
            "server",
            "client",
            "api",
            "framework",
            "library",
            "module",
            "performance",
            "memory",
            "cpu",
            "thread",
            "process",
            "cache",
            "index",
            "security",
            "authentication",
            "encryption",
            "hash",
            "protocol",
            "network",
        ];

        technical_terms.iter().any(|term| q_lower.contains(term))
    }

    fn is_vague_problem(question: &str) -> bool {
        let q_lower = question.to_lowercase();
        let vague_indicators = [
            "something",
            "anything",
            "stuff",
            "things",
            "whatever",
            "somehow",
            "maybe",
            "perhaps",
            "might",
            "could be",
            "not sure",
            "unclear",
        ];

        question.len() < 20
            || vague_indicators
                .iter()
                .any(|indicator| q_lower.contains(indicator))
    }

    // Additional code-specific helper methods

    fn get_implementation_approach(
        problem_type: &CodeProblemType,
        language: Option<&str>,
    ) -> String {
        let lang_considerations =
            language.map_or(String::new(), |lang| match lang.to_lowercase().as_str() {
                "rust" => " Consider ownership, borrowing, and zero-cost abstractions.".to_string(),
                "javascript" | "typescript" => {
                    " Consider async patterns, type safety, and browser compatibility.".to_string()
                }
                "python" => {
                    " Consider performance implications, type hints, and Pythonic patterns."
                        .to_string()
                }
                "java" => {
                    " Consider OOP design, memory management, and JVM optimizations.".to_string()
                }
                "c++" => {
                    " Consider RAII, memory safety, and performance optimizations.".to_string()
                }
                "go" => " Consider goroutines, channels, and simplicity principles.".to_string(),
                _ => String::new(),
            });

        let base_approach = match problem_type {
            CodeProblemType::BugFix => {
                "Isolate the issue, create minimal reproduction, apply targeted fix"
            }
            CodeProblemType::Refactoring => {
                "Preserve behavior, improve structure, maintain test coverage"
            }
            CodeProblemType::Implementation => {
                "Start with interfaces, implement incrementally, add comprehensive tests"
            }
            CodeProblemType::Performance => {
                "Measure first, optimize bottlenecks, validate improvements"
            }
            CodeProblemType::Security => {
                "Apply defense in depth, validate inputs, use secure defaults"
            }
            CodeProblemType::Testing => {
                "Cover edge cases, aim for behavior verification, maintain readability"
            }
            CodeProblemType::Documentation => {
                "Focus on user scenarios, provide examples, keep up-to-date"
            }
            CodeProblemType::Architecture => {
                "Design for scalability, maintainability, and clear separation of concerns"
            }
            CodeProblemType::CodeReview => {
                "Focus on readability, performance, security, and maintainability"
            }
        };

        format!("{}.{}", base_approach, lang_considerations)
    }

    fn analyze_code_tradeoffs(problem_type: &CodeProblemType, language: Option<&str>) -> String {
        let general_tradeoffs = match problem_type {
            CodeProblemType::BugFix => "Speed vs thoroughness: Quick fixes may introduce new bugs",
            CodeProblemType::Refactoring => {
                "Improvement benefits vs risk of introducing regressions"
            }
            CodeProblemType::Implementation => {
                "Feature completeness vs time to market and code complexity"
            }
            CodeProblemType::Performance => {
                "Optimization effort vs maintainability and code readability"
            }
            CodeProblemType::Security => {
                "Security measures vs user experience and system performance"
            }
            CodeProblemType::Testing => {
                "Test coverage vs test maintenance burden and execution time"
            }
            CodeProblemType::Documentation => "Documentation depth vs maintenance overhead",
            CodeProblemType::Architecture => {
                "Flexibility vs simplicity, future-proofing vs over-engineering"
            }
            CodeProblemType::CodeReview => "Thoroughness vs development velocity",
        };

        if let Some(lang) = language {
            format!("{} (Language: {})", general_tradeoffs, lang)
        } else {
            general_tradeoffs.to_string()
        }
    }

    fn propose_solution_strategy(problem_type: &CodeProblemType, _problem: &str) -> String {
        match problem_type {
            CodeProblemType::BugFix => "1) Reproduce the bug consistently, 2) Identify root cause through debugging, 3) Implement minimal fix, 4) Add regression tests".to_string(),
            CodeProblemType::Refactoring => "1) Ensure comprehensive test coverage, 2) Apply refactoring incrementally, 3) Validate behavior after each change, 4) Update documentation".to_string(),
            CodeProblemType::Implementation => "1) Define clear requirements and interfaces, 2) Implement core functionality with TDD, 3) Add error handling and edge cases, 4) Integrate and test".to_string(),
            CodeProblemType::Performance => "1) Profile and identify bottlenecks, 2) Optimize critical paths, 3) Measure improvements, 4) Ensure correctness maintained".to_string(),
            CodeProblemType::Security => "1) Identify threat model, 2) Apply security best practices, 3) Implement defense mechanisms, 4) Conduct security testing".to_string(),
            CodeProblemType::Testing => "1) Analyze requirements for test scenarios, 2) Implement unit and integration tests, 3) Verify edge cases and error conditions, 4) Maintain test suite".to_string(),
            CodeProblemType::Documentation => "1) Identify target audience and use cases, 2) Structure information logically, 3) Provide examples and tutorials, 4) Keep documentation current".to_string(),
            CodeProblemType::Architecture => "1) Analyze current system constraints, 2) Design modular, extensible architecture, 3) Plan migration strategy, 4) Implement incrementally".to_string(),
            CodeProblemType::CodeReview => "1) Review for correctness and logic, 2) Evaluate performance and security implications, 3) Check coding standards, 4) Provide constructive feedback".to_string(),
        }
    }

    fn is_complex_code_problem(problem: &str, problem_type: &CodeProblemType) -> bool {
        // Always show trade-offs for architecture and performance problems
        matches!(problem_type, CodeProblemType::Architecture | CodeProblemType::Performance)
        // Or if the problem mentions multiple components
        || problem.to_lowercase().contains("multiple")
        || problem.to_lowercase().contains("system")
        || problem.to_lowercase().contains("integration")
        || problem.len() > 150 // Long problem descriptions suggest complexity
    }

    fn generate_code_conclusion(
        problem: &str,
        _steps: &[ThinkStep],
        problem_type: &CodeProblemType,
    ) -> String {
        let base_conclusion = match problem_type {
            CodeProblemType::BugFix => {
                "Bug analysis complete: Systematic debugging approach will isolate root cause and enable targeted fix with regression prevention."
            }
            CodeProblemType::Refactoring => {
                "Refactoring strategy defined: Incremental improvements with behavior preservation and comprehensive test validation."
            }
            CodeProblemType::Implementation => {
                "Implementation plan ready: Clear requirements breakdown with TDD approach ensures robust, maintainable solution."
            }
            CodeProblemType::Performance => {
                "Performance optimization strategy: Profile-driven improvements with measurable results and correctness validation."
            }
            CodeProblemType::Security => {
                "Security analysis complete: Defense-in-depth approach with threat modeling and comprehensive protection measures."
            }
            CodeProblemType::Testing => {
                "Testing strategy established: Comprehensive coverage with behavior verification and maintainable test suite."
            }
            CodeProblemType::Documentation => {
                "Documentation plan ready: User-focused content with examples and maintainable structure."
            }
            CodeProblemType::Architecture => {
                "Architectural design complete: Scalable, maintainable system with clear separation of concerns and migration path."
            }
            CodeProblemType::CodeReview => {
                "Code review framework ready: Systematic evaluation covering correctness, performance, security, and maintainability."
            }
        };

        // Add problem-specific context to conclusion
        let p_lower = problem.to_lowercase();
        let mut specific_context = Vec::new();

        if p_lower.contains("auth") {
            specific_context.push("authentication mechanisms");
        }
        if p_lower.contains("oauth") || p_lower.contains("oauth2") {
            specific_context.push("OAuth2 integration");
        }
        if p_lower.contains("security") || p_lower.contains("secure") {
            specific_context.push("security best practices");
        }
        if p_lower.contains("database") {
            specific_context.push("database optimization");
        }
        if p_lower.contains("scalab") {
            specific_context.push("scalability considerations");
        }

        if specific_context.is_empty() {
            base_conclusion.to_string()
        } else {
            format!(
                "{} Focus areas include: {}.",
                base_conclusion,
                specific_context.join(", ")
            )
        }
    }

    fn calculate_code_confidence(
        problem: &str,
        steps: &[ThinkStep],
        problem_type: &CodeProblemType,
    ) -> f32 {
        let mut confidence: f32 = 0.58; // Base confidence for code problems, slightly reduced

        // Boost confidence for well-defined problem types
        match problem_type {
            CodeProblemType::BugFix | CodeProblemType::Testing | CodeProblemType::Documentation => {
                confidence += 0.15
            }
            CodeProblemType::Implementation => confidence += 0.05, // Reduced for more conservative estimate
            CodeProblemType::Refactoring => confidence += 0.08, // Slightly reduced to stay within bounds
            CodeProblemType::Performance => confidence += 0.04, // More complex, conservative estimate
            CodeProblemType::Security => confidence += 0.05,    // More complex
            CodeProblemType::Architecture | CodeProblemType::CodeReview => confidence += 0.0, // Highly contextual
        }

        // Boost for technical specificity (but not for already complex problem types)
        if Self::contains_technical_terms(problem) {
            // Less boost for complex problem types that already have uncertainty
            match problem_type {
                CodeProblemType::Architecture
                | CodeProblemType::Performance
                | CodeProblemType::Security => {
                    confidence += 0.05; // Smaller boost for complex problems
                }
                _ => confidence += 0.1, // Normal boost for simpler problems
            }
        }

        // Boost for sufficient reasoning steps (but not for already uncertain problem types)
        if steps.len() > 4 {
            // Increased threshold to avoid over-boosting
            // Don't boost confidence for complex problem types that are inherently uncertain
            match problem_type {
                CodeProblemType::Architecture
                | CodeProblemType::Performance
                | CodeProblemType::Security => {
                    // Skip boost for complex problems
                }
                _ => confidence += 0.05,
            }
        }

        // Reduce confidence for vague problems
        if Self::is_vague_problem(problem) {
            confidence -= 0.2;
        }

        confidence.max(0.1).min(0.95)
    }

    fn determine_recommended_action(problem_type: &CodeProblemType, _problem: &str) -> String {
        match problem_type {
            CodeProblemType::BugFix => "Start debugging: Add logging, create minimal reproduction case, use debugger to trace execution",
            CodeProblemType::Refactoring => "Begin refactoring: Ensure tests exist, apply small incremental changes, validate behavior continuously",
            CodeProblemType::Implementation => "Start implementation: Define interfaces first, use TDD approach, implement incrementally",
            CodeProblemType::Performance => "Profile first: Measure current performance, identify bottlenecks before optimizing",
            CodeProblemType::Security => "Conduct security assessment: Review for common vulnerabilities, apply security best practices",
            CodeProblemType::Testing => "Design test cases: Cover happy path, edge cases, error conditions with clear assertions",
            CodeProblemType::Documentation => "Create documentation structure: Focus on user scenarios, include code examples",
            CodeProblemType::Architecture => "Design system architecture: Start with high-level design, define component interfaces",
            CodeProblemType::CodeReview => "Perform systematic review: Check correctness, performance, security, and coding standards",
        }.to_string()
    }

    fn identify_affected_files(problem: &str, context: Option<&str>) -> Vec<String> {
        let mut files = Vec::new();

        // Extract file paths from context
        if let Some(ctx) = context
            && (ctx.contains('/') || ctx.contains('\\'))
        {
            files.push(ctx.to_string());
        }

        // Extract file paths from problem description
        let words: Vec<&str> = problem.split_whitespace().collect();
        for word in words {
            if word.contains('.') && (word.contains('/') || word.contains('\\')) {
                files.push(word.to_string());
            }
        }

        // Remove duplicates
        let unique_files: HashSet<String> = files.into_iter().collect();
        unique_files.into_iter().collect()
    }

    fn determine_error_action(error_message: &str) -> String {
        let error_lower = error_message.to_lowercase();

        let base_action = if error_lower.contains("compilation") || error_lower.contains("syntax") {
            "Fix syntax errors: Check for missing semicolons, brackets, or type mismatches. Review code structure and ensure all language requirements are met"
        } else if error_lower.contains("null") || error_lower.contains("undefined") {
            "Handle null/undefined: Add comprehensive null checks before dereferencing, initialize all variables at declaration, implement Option/Result patterns for safe error handling"
        } else if error_lower.contains("memory") || error_lower.contains("segmentation") {
            "Fix memory issue: Validate array bounds before access, verify pointer validity, audit memory allocation/deallocation patterns, check for use-after-free and double-free bugs"
        } else if error_lower.contains("timeout") || error_lower.contains("deadlock") {
            "Resolve concurrency issue: Audit lock acquisition order, check for race conditions, ensure proper synchronization primitives, implement timeout mechanisms"
        } else if error_lower.contains("permission") || error_lower.contains("access") {
            "Fix access issue: Verify file permissions match requirements, check user privileges, validate path accessibility, ensure proper resource ownership"
        } else {
            "Debug systematically: Add comprehensive logging at key points, reproduce error consistently, trace full execution path, examine state at failure point"
        };

        // Add specific context if line number or file is mentioned
        if error_lower.contains("line")
            || error_lower.contains(".rs")
            || error_lower.contains(".cpp")
            || error_lower.contains(".java")
            || error_lower.contains(".py")
            || error_lower.contains(".js")
        {
            format!(
                "{} - Focus on the specific file and line mentioned in the error message for targeted debugging",
                base_action
            )
        } else {
            base_action.to_string()
        }
    }

    fn extract_files_from_error(error_message: &str) -> Vec<String> {
        let mut files = Vec::new();
        let lines: Vec<&str> = error_message.lines().collect();

        for line in lines {
            // Look for common file path patterns in error messages
            if let Some(start) = line.find('/')
                && let Some(end) = line[start..].find(':')
            {
                let potential_file = &line[start..start + end];
                if potential_file.contains('.') {
                    files.push(potential_file.to_string());
                }
            }
        }

        // Remove duplicates
        let unique_files: HashSet<String> = files.into_iter().collect();
        unique_files.into_iter().collect()
    }

    fn parse_error_components(error_message: &str) -> String {
        let mut components = Vec::new();
        let error_lower = error_message.to_lowercase();

        // Error type detection
        if error_lower.contains("syntax") {
            components.push("Syntax error");
        }
        if error_lower.contains("runtime") {
            components.push("Runtime error");
        }
        if error_lower.contains("compile") {
            components.push("Compilation error");
        }
        if error_lower.contains("null") {
            components.push("Null reference");
        }
        if error_lower.contains("index") || error_lower.contains("bounds") {
            components.push("Array bounds");
        }
        if error_lower.contains("memory") {
            components.push("Memory error");
        }
        if error_lower.contains("network") || error_lower.contains("connection") {
            components.push("Network error");
        }

        // File and line extraction
        let files = Self::extract_files_from_error(error_message);
        if !files.is_empty() {
            components.push("File locations identified");
        }

        if components.is_empty() {
            "General error requiring systematic analysis".to_string()
        } else {
            format!("Error components: {}", components.join(", "))
        }
    }

    fn identify_error_causes(error_message: &str) -> String {
        let error_lower = error_message.to_lowercase();

        if error_lower.contains("null") || error_lower.contains("undefined") {
            "Likely cause: Uninitialized variable, missing null check, or incorrect object access"
        } else if error_lower.contains("index") || error_lower.contains("bounds") {
            "Likely cause: Array access beyond bounds, off-by-one error, or empty collection access"
        } else if error_lower.contains("type") {
            "Likely cause: Type mismatch, incorrect casting, or missing type conversion"
        } else if error_lower.contains("memory") || error_lower.contains("segmentation") {
            "Likely cause: Buffer overflow, dangling pointer, or memory corruption"
        } else if error_lower.contains("permission") || error_lower.contains("access") {
            "Likely cause: Insufficient file permissions, incorrect path, or security restrictions"
        } else if error_lower.contains("network") || error_lower.contains("connection") {
            "Likely cause: Network connectivity, server unavailability, or timeout issues"
        } else {
            "Multiple potential causes: Requires systematic elimination through debugging"
        }
        .to_string()
    }

    fn analyze_code_smell(code_smell: &str) -> String {
        let smell_lower = code_smell.to_lowercase();

        if smell_lower.contains("long method") || smell_lower.contains("long function") {
            "Impact: Reduced readability, difficult testing, multiple responsibilities. Extract smaller methods."
        } else if smell_lower.contains("duplicate") || smell_lower.contains("repetition") {
            "Impact: Maintenance burden, inconsistent changes, code bloat. Extract common functionality."
        } else if smell_lower.contains("large class") || smell_lower.contains("god object") {
            "Impact: Low cohesion, high coupling, difficult maintenance. Apply Single Responsibility Principle."
        } else if smell_lower.contains("complex") || smell_lower.contains("cyclomatic") {
            "Impact: Hard to understand, error-prone, difficult testing. Simplify control flow."
        } else if smell_lower.contains("coupling") {
            "Impact: Rigid design, difficult changes, reduced reusability. Introduce abstractions."
        } else {
            "General code quality issue requiring analysis of structure, readability, and maintainability."
        }.to_string()
    }

    fn select_refactoring_technique(code_smell: &str) -> String {
        let smell_lower = code_smell.to_lowercase();

        if smell_lower.contains("long method") || smell_lower.contains("long function") {
            "Technique: Extract Method - Break down into smaller, focused functions with clear purposes"
        } else if smell_lower.contains("duplicate") {
            "Technique: Extract Common Method/Class - Centralize repeated logic in reusable components"
        } else if smell_lower.contains("large class") {
            "Technique: Extract Class/Module - Split responsibilities into focused, cohesive units"
        } else if smell_lower.contains("complex") {
            "Technique: Simplify Conditional - Use guard clauses, strategy pattern, or state machines"
        } else if smell_lower.contains("coupling") {
            "Technique: Introduce Interface/Dependency Injection - Reduce direct dependencies"
        } else if smell_lower.contains("magic number") || smell_lower.contains("hardcode") {
            "Technique: Extract Constants/Configuration - Make values explicit and configurable"
        } else {
            "Technique: Systematic analysis required - Identify specific structural issues first"
        }.to_string()
    }

    fn _assess_refactoring_complexity(code_smell: &str) -> Complexity {
        let smell_lower = code_smell.to_lowercase();

        if smell_lower.contains("system")
            || smell_lower.contains("architecture")
            || smell_lower.contains("large class")
            || smell_lower.contains("god object")
        {
            Complexity::Complex
        } else if smell_lower.contains("multiple")
            || smell_lower.contains("coupling")
            || smell_lower.contains("duplicate")
        {
            Complexity::Medium
        } else {
            Complexity::Simple
        }
    }

    /// Recommend tools based on the code problem type
    pub fn recommend_tools_for_problem(
        &self,
        problem_type: &CodeProblemType,
    ) -> Vec<ToolRecommendation> {
        match problem_type {
            CodeProblemType::BugFix => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.9,
                    rationale: "Search tool helps locate error patterns, find related code sections, and identify all instances of the problematic code across the codebase".to_string(),
                    priority: 1,
                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.85,
                    rationale: "Edit tool enables precise line-based fixes for identified bugs with immediate feedback and validation".to_string(),
                    priority: 2,
                },
                ToolRecommendation {
                    tool: "test".to_string(),
                    confidence: 0.8,
                    rationale: "Test tool ensures the bug fix doesn't introduce regressions and validates the solution works correctly".to_string(),
                    priority: 3,
                },
            ],
            CodeProblemType::Refactoring => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.85,
                    rationale: "Search tool identifies all code locations that need refactoring and finds usage patterns to ensure consistent changes".to_string(),
                    priority: 1,
                },
                ToolRecommendation {
                    tool: "patch".to_string(),
                    confidence: 0.9,
                    rationale: "Patch tool performs semantic-aware AST transformations that preserve code structure and behavior during refactoring".to_string(),
                    priority: 2,
                },
                ToolRecommendation {
                    tool: "test".to_string(),
                    confidence: 0.8,
                    rationale: "Test tool verifies behavior preservation after refactoring and ensures no functionality is broken".to_string(),
                    priority: 3,
                },
            ],
            CodeProblemType::Implementation => vec![
                ToolRecommendation {
                    tool: "plan".to_string(),
                    confidence: 0.9,
                    rationale: "Plan tool breaks down implementation into manageable tasks with dependencies and parallel execution opportunities".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.85,
                    rationale: "Edit tool creates new code sections and implements features with fast iteration and immediate validation".to_string(),
                    priority: 2,                },
                ToolRecommendation {
                    tool: "test".to_string(),
                    confidence: 0.8,
                    rationale: "Test tool validates new implementation meets requirements and integrates correctly with existing code".to_string(),
                    priority: 3,                },
            ],
            CodeProblemType::Performance => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.8,
                    rationale: "Search tool locates performance-critical code sections, bottlenecks, and resource-intensive operations".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "analyze".to_string(),
                    confidence: 0.85,
                    rationale: "Analyze tool profiles code execution, measures performance metrics, and identifies optimization opportunities".to_string(),
                    priority: 2,                },
                ToolRecommendation {
                    tool: "patch".to_string(),
                    confidence: 0.8,
                    rationale: "Patch tool applies algorithmic optimizations and structural improvements while maintaining correctness".to_string(),
                    priority: 3,                },
            ],
            CodeProblemType::Security => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.9,
                    rationale: "Search tool identifies security-sensitive code patterns, vulnerable functions, and potential attack vectors".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "analyze".to_string(),
                    confidence: 0.9,
                    rationale: "Analyze tool performs security scanning, vulnerability assessment, and compliance checking against security standards".to_string(),
                    priority: 2,                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.85,
                    rationale: "Edit tool applies security patches, input validation, and defensive programming techniques".to_string(),
                    priority: 3,                },
            ],
            CodeProblemType::Testing => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.7,
                    rationale: "Search tool finds existing test patterns, identifies untested code, and locates test utilities".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.9,
                    rationale: "Edit tool creates test cases, implements assertions, and builds comprehensive test suites".to_string(),
                    priority: 2,                },
                ToolRecommendation {
                    tool: "test".to_string(),
                    confidence: 0.95,
                    rationale: "Test tool executes test suites, validates coverage, and ensures all tests pass successfully".to_string(),
                    priority: 3,                },
            ],
            CodeProblemType::Documentation => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.8,
                    rationale: "Search tool analyzes code structure, finds undocumented functions, and identifies documentation patterns".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.9,
                    rationale: "Edit tool adds documentation comments, creates README files, and updates API documentation".to_string(),
                    priority: 2,                },
            ],
            CodeProblemType::Architecture => vec![
                ToolRecommendation {
                    tool: "plan".to_string(),
                    confidence: 0.95,
                    rationale: "Plan tool designs system architecture, defines component relationships, and creates implementation roadmaps".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "think".to_string(),
                    confidence: 0.85,
                    rationale: "Think tool reasons through architectural decisions, evaluates trade-offs, and considers long-term implications".to_string(),
                    priority: 2,                },
                ToolRecommendation {
                    tool: "edit".to_string(),
                    confidence: 0.7,
                    rationale: "Edit tool implements architectural scaffolding, creates interfaces, and establishes module boundaries".to_string(),
                    priority: 3,                },
            ],
            CodeProblemType::CodeReview => vec![
                ToolRecommendation {
                    tool: "search".to_string(),
                    confidence: 0.9,
                    rationale: "Search tool examines code patterns, finds similar implementations, and checks for consistency across codebase".to_string(),
                    priority: 1,                },
                ToolRecommendation {
                    tool: "analyze".to_string(),
                    confidence: 0.85,
                    rationale: "Analyze tool evaluates code quality metrics, complexity scores, and adherence to best practices".to_string(),
                    priority: 2,                },
            ],
        }
    }

    /// Analyze thought content to identify mentioned tools and generate recommendations
    pub fn analyze_thought_for_tools(&self, thought: &str) -> Vec<ToolRecommendation> {
        let thought_lower = thought.to_lowercase();
        let mut recommendations = Vec::new();
        let mut priority = 1;

        // Check for search-related keywords
        if thought_lower.contains("search")
            || thought_lower.contains("find")
            || thought_lower.contains("locate")
            || thought_lower.contains("look for")
            || thought_lower.contains("identify")
            || thought_lower.contains("discover")
        {
            recommendations.push(ToolRecommendation {
                tool: "search".to_string(),
                confidence: 0.9,
                rationale: "Thought mentions searching or finding - search tool provides multi-layer search with symbol, full-text, and AST capabilities".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for edit-related keywords
        if thought_lower.contains("edit")
            || thought_lower.contains("modify")
            || thought_lower.contains("change")
            || thought_lower.contains("update")
            || thought_lower.contains("fix")
            || thought_lower.contains("replace")
        {
            recommendations.push(ToolRecommendation {
                tool: "edit".to_string(),
                confidence: 0.85,
                rationale: "Thought mentions editing or modifying - edit tool provides fast line-based changes with context awareness".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for planning-related keywords
        if thought_lower.contains("plan")
            || thought_lower.contains("organize")
            || thought_lower.contains("structure")
            || thought_lower.contains("break down")
            || thought_lower.contains("decompose")
            || thought_lower.contains("strategy")
        {
            recommendations.push(ToolRecommendation {
                tool: "plan".to_string(),
                confidence: 0.9,
                rationale: "Thought mentions planning or organization - plan tool provides task decomposition with dependency analysis".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for analysis-related keywords
        if thought_lower.contains("analyze")
            || thought_lower.contains("examine")
            || thought_lower.contains("investigate")
            || thought_lower.contains("review")
            || thought_lower.contains("evaluate")
            || thought_lower.contains("assess")
        {
            recommendations.push(ToolRecommendation {
                tool: "analyze".to_string(),
                confidence: 0.85,
                rationale: "Thought mentions analysis or investigation - analyze tool provides deep code analysis and metrics".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for testing-related keywords
        if thought_lower.contains("test")
            || thought_lower.contains("verify")
            || thought_lower.contains("validate")
            || thought_lower.contains("check")
            || thought_lower.contains("ensure")
        {
            recommendations.push(ToolRecommendation {
                tool: "test".to_string(),
                confidence: 0.8,
                rationale: "Thought mentions testing or validation - test tool ensures correctness and prevents regressions".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for AST/tree-related keywords
        if thought_lower.contains("ast")
            || thought_lower.contains("syntax")
            || thought_lower.contains("parse")
            || thought_lower.contains("tree")
            || thought_lower.contains("structure")
        {
            recommendations.push(ToolRecommendation {
                tool: "tree".to_string(),
                confidence: 0.85,
                rationale: "Thought mentions AST or syntax - tree tool provides tree-sitter parsing for 27+ languages".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for pattern matching keywords
        if thought_lower.contains("pattern")
            || thought_lower.contains("grep")
            || thought_lower.contains("match")
            || thought_lower.contains("regex")
        {
            recommendations.push(ToolRecommendation {
                tool: "grep".to_string(),
                confidence: 0.8,
                rationale: "Thought mentions patterns or matching - grep tool provides AST-aware pattern matching".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for file discovery keywords
        if thought_lower.contains("files")
            || thought_lower.contains("directory")
            || thought_lower.contains("folder")
            || thought_lower.contains("glob")
            || thought_lower.contains("discover files")
        {
            recommendations.push(ToolRecommendation {
                tool: "glob".to_string(),
                confidence: 0.75,
                rationale: "Thought mentions file discovery - glob tool provides fast parallel file finding with pattern support".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for command/script execution keywords
        if thought_lower.contains("run")
            || thought_lower.contains("execute")
            || thought_lower.contains("command")
            || thought_lower.contains("script")
            || thought_lower.contains("bash")
            || thought_lower.contains("shell")
        {
            recommendations.push(ToolRecommendation {
                tool: "bash".to_string(),
                confidence: 0.7,
                rationale: "Thought mentions execution or commands - bash tool provides safe command execution with validation".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for refactoring/transformation keywords
        if thought_lower.contains("refactor")
            || thought_lower.contains("transform")
            || thought_lower.contains("restructure")
            || thought_lower.contains("patch")
        {
            recommendations.push(ToolRecommendation {
                tool: "patch".to_string(),
                confidence: 0.9,
                rationale: "Thought mentions refactoring or transformation - patch tool provides semantic-aware AST transformations".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for indexing keywords
        if thought_lower.contains("index")
            || thought_lower.contains("catalog")
            || thought_lower.contains("full-text")
        {
            recommendations.push(ToolRecommendation {
                tool: "index".to_string(),
                confidence: 0.75,
                rationale: "Thought mentions indexing - index tool provides Tantivy-based full-text indexing for fast searches".to_string(),
                priority,            });
            priority += 1;
        }

        // Check for reasoning/thinking keywords
        if thought_lower.contains("think")
            || thought_lower.contains("reason")
            || thought_lower.contains("consider")
            || thought_lower.contains("evaluate options")
        {
            recommendations.push(ToolRecommendation {
                tool: "think".to_string(),
                confidence: 0.8,
                rationale: "Thought mentions reasoning or consideration - think tool provides structured reasoning strategies".to_string(),
                priority,            });
        }

        // If no specific tools were identified, provide general recommendations based on context
        if recommendations.is_empty() {
            // Default to search and edit as they're the most commonly needed tools
            recommendations.push(ToolRecommendation {
                tool: "search".to_string(),
                confidence: 0.6,
                rationale: "No specific tools mentioned - search tool helps understand the codebase before making changes".to_string(),
                priority: 1,            });
            recommendations.push(ToolRecommendation {
                tool: "edit".to_string(),
                confidence: 0.5,
                rationale:
                    "No specific tools mentioned - edit tool enables making necessary code changes"
                        .to_string(),
                priority: 2,
            });
        }

        // Sort by priority to ensure correct order
        recommendations.sort_by_key(|r| r.priority);
        recommendations
    }
}

impl Default for ThinkTool {
    fn default() -> Self {
        Self::new()
    }
}

/// Problem classification for reasoning strategy (backward compatibility)
#[derive(Debug, Clone, PartialEq)]
enum ProblemType {
    Technical, // Code, algorithms, implementation
    Design,    // Architecture, structure, planning
    Analysis,  // Evaluation, comparison, research
    Planning,  // Organization, scheduling, strategy
    Debugging, // Troubleshooting, problem-solving
    General,   // Everything else
}

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

    #[test]
    fn test_basic_thinking() {
        let result = ThinkTool::think("How to implement a hash table?").unwrap();

        assert!(result.steps.len() >= 3);
        assert!(!result.conclusion.is_empty());
        assert!(result.confidence > 0.0 && result.confidence <= 1.0);

        // Check that steps are numbered correctly
        for (i, step) in result.steps.iter().enumerate() {
            assert_eq!(step.step_number, i + 1);
        }
    }

    #[test]
    fn test_empty_question() {
        let result = ThinkTool::think("");
        assert!(matches!(result, Err(ThinkError::EmptyProblem)));
    }

    #[test]
    fn test_short_question() {
        let result = ThinkTool::think("Hi");
        assert!(matches!(result, Err(ThinkError::EmptyProblem)));
    }

    #[test]
    fn test_technical_question_confidence() {
        let result =
            ThinkTool::think("How to optimize database query performance using indexes?").unwrap();

        // Technical questions with specific terms should have higher confidence
        assert!(result.confidence > 0.6);
    }

    #[test]
    fn test_vague_question_confidence() {
        let result = ThinkTool::think("How to do something with stuff?").unwrap();

        // Vague questions should have lower confidence
        assert!(result.confidence < 0.6);
    }

    #[test]
    fn test_complex_problem_extra_steps() {
        let result = ThinkTool::think(
            "How to implement a distributed system with multiple microservices, message queues, and load balancing?"
        ).unwrap();

        // Complex problems should generate more than 3 steps
        assert!(result.steps.len() > 3);
    }

    #[test]
    fn test_step_content_quality() {
        let result = ThinkTool::think("How to debug a memory leak in a C++ application?").unwrap();

        // Each step should have meaningful content
        for step in &result.steps {
            assert!(!step.thought.is_empty());
            assert!(!step.reasoning.is_empty());
            assert!(step.thought.len() > 10);
            assert!(step.reasoning.len() > 20);
        }
    }

    #[test]
    fn test_conclusion_relevance() {
        let result = ThinkTool::think("What's the best way to learn Rust programming?").unwrap();

        // Conclusion should be relevant and substantial
        assert!(!result.conclusion.is_empty());
        assert!(result.conclusion.len() > 50);
    }

    // Tests for code-specific functionality

    #[test]
    fn test_code_problem_classification() {
        assert_eq!(
            ThinkTool::classify_code_problem("Fix the null pointer bug"),
            CodeProblemType::BugFix
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Refactor the messy code"),
            CodeProblemType::Refactoring
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Implement user authentication"),
            CodeProblemType::Implementation
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Optimize database performance"),
            CodeProblemType::Performance
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Fix security vulnerability"),
            CodeProblemType::Security
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Write unit tests"),
            CodeProblemType::Testing
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Document the API"),
            CodeProblemType::Documentation
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Design system architecture"),
            CodeProblemType::Architecture
        );
        assert_eq!(
            ThinkTool::classify_code_problem("Review code quality"),
            CodeProblemType::CodeReview
        );
    }

    #[test]
    fn test_complexity_assessment() {
        // Simple case
        let simple =
            ThinkTool::assess_complexity("Fix variable name", Some("rust"), Some("main.rs"));
        assert_eq!(simple, Complexity::Simple);

        // Complex case
        let complex = ThinkTool::assess_complexity(
            "Implement distributed system with multiple microservices and database migration",
            Some("rust"),
            Some("src/services/complex/integration/handler.rs"),
        );
        assert!(matches!(complex, Complexity::Medium | Complexity::Complex));
    }

    #[test]
    fn test_think_about_code() {
        let result = ThinkTool::think_about_code(
            "Fix memory leak in C++ application",
            Some("c++"),
            Some("src/memory_manager.cpp"),
        )
        .unwrap();

        assert_eq!(result.problem_type, CodeProblemType::BugFix);
        assert!(result.steps.len() >= 3);
        assert!(!result.recommended_action.is_empty());
        assert_eq!(result.affected_files, vec!["src/memory_manager.cpp"]);
        assert!(result.confidence > 0.5);
        assert!(!result.conclusion.is_empty());
    }

    #[test]
    fn test_analyze_error() {
        let result =
            ThinkTool::analyze_error("NullPointerException at line 42 in UserService.java")
                .unwrap();

        assert_eq!(result.problem_type, CodeProblemType::BugFix);
        assert!(result.steps.len() >= 3);
        assert!(result.recommended_action.contains("null"));
        assert!(result.confidence >= 0.7); // Error analysis should have high confidence
        assert!(!result.affected_files.is_empty() || result.affected_files.is_empty()); // May or may not extract files
    }

    #[test]
    fn test_plan_refactoring() {
        let result =
            ThinkTool::plan_refactoring("Long method with too many responsibilities").unwrap();

        assert_eq!(result.problem_type, CodeProblemType::Refactoring);
        assert!(result.steps.len() >= 3);
        assert!(result.recommended_action.contains("refactor"));
        assert!(result.confidence >= 0.8); // Refactoring should have high confidence
        assert!(matches!(
            result.complexity,
            Complexity::Simple | Complexity::Medium
        ));
    }

    #[test]
    fn test_error_file_extraction() {
        let error_msg = "Error in /src/main.rs:42:10\nCompilation failed in /tests/unit_test.rs:15";
        let files = ThinkTool::extract_files_from_error(error_msg);

        assert!(
            files.contains(&"/src/main.rs".to_string())
                || files.contains(&"/tests/unit_test.rs".to_string())
        );
    }

    #[test]
    fn test_recommended_actions() {
        let bug_action =
            ThinkTool::determine_recommended_action(&CodeProblemType::BugFix, "null pointer");
        assert!(bug_action.contains("debug"));

        let perf_action =
            ThinkTool::determine_recommended_action(&CodeProblemType::Performance, "slow query");
        assert!(perf_action.contains("Profile") || perf_action.contains("profile"));

        let test_action =
            ThinkTool::determine_recommended_action(&CodeProblemType::Testing, "no tests");
        assert!(test_action.contains("test"));
    }

    #[test]
    fn test_code_confidence_calculation() {
        let steps = vec![ThinkStep {
            step_number: 1,
            thought: "test".to_string(),
            reasoning: "test reasoning".to_string(),
        }];

        // Bug fixes should have higher confidence
        let bug_confidence = ThinkTool::calculate_code_confidence(
            "Fix null pointer exception in authentication module",
            &steps,
            &CodeProblemType::BugFix,
        );
        assert!(bug_confidence > 0.7);

        // Architecture problems should have lower confidence (more contextual)
        let arch_confidence = ThinkTool::calculate_code_confidence(
            "Design system architecture",
            &steps,
            &CodeProblemType::Architecture,
        );
        assert!(arch_confidence < bug_confidence);
    }

    #[test]
    fn test_implementation_approach() {
        let rust_approach =
            ThinkTool::get_implementation_approach(&CodeProblemType::BugFix, Some("rust"));
        assert!(rust_approach.contains("ownership") || rust_approach.contains("borrowing"));

        let js_approach = ThinkTool::get_implementation_approach(
            &CodeProblemType::Implementation,
            Some("javascript"),
        );
        assert!(js_approach.contains("async") || js_approach.contains("type"));
    }

    #[test]
    fn test_affected_files_identification() {
        let files = ThinkTool::identify_affected_files(
            "Fix bug in src/auth.rs and update tests/auth_test.rs",
            Some("src/main.rs"),
        );

        assert!(!files.is_empty());
        // Should contain context file
        assert!(files.contains(&"src/main.rs".to_string()));
    }

    #[test]
    fn test_backward_compatibility() {
        // Ensure original think method still works
        let result = ThinkTool::think("How to implement authentication?").unwrap();

        assert!(result.steps.len() >= 3);
        assert!(!result.conclusion.is_empty());
        assert!(result.confidence > 0.0 && result.confidence <= 1.0);
    }

    // Sequential thinking capability tests

    #[test]
    fn test_sequential_thinking_basic() {
        // Test basic sequential thought processing with multiple problems
        let problems = [
            "How to debug a segmentation fault?",
            "What's the best approach for database optimization?",
            "How to implement user authentication securely?",
        ];

        let mut results = Vec::new();
        for problem in problems {
            let result = ThinkTool::think_about_code(problem, Some("rust"), None).unwrap();
            results.push(result);
        }

        // Verify each result has proper sequential structure
        for (i, result) in results.iter().enumerate() {
            assert!(
                result.steps.len() >= 3,
                "Problem {} should have at least 3 steps",
                i
            );

            // Verify steps are properly numbered sequentially
            for (step_idx, step) in result.steps.iter().enumerate() {
                assert_eq!(
                    step.step_number,
                    step_idx + 1,
                    "Step numbering should be sequential"
                );
                assert!(!step.thought.is_empty(), "Each thought should have content");
                assert!(
                    !step.reasoning.is_empty(),
                    "Each step should have reasoning"
                );
            }

            // Verify metadata consistency
            assert!(
                result.confidence >= 0.1 && result.confidence <= 0.95,
                "Confidence should be in valid range"
            );
            assert!(!result.conclusion.is_empty(), "Should have a conclusion");
            assert!(
                !result.recommended_action.is_empty(),
                "Should have recommended action"
            );
        }

        // Verify different problems produce different classifications
        let problem_types: Vec<_> = results.iter().map(|r| &r.problem_type).collect();
        assert!(problem_types.contains(&&CodeProblemType::BugFix));
        assert!(problem_types.contains(&&CodeProblemType::Performance));
        assert!(problem_types.contains(&&CodeProblemType::Implementation));
    }

    #[test]
    fn test_thought_revision() {
        // Test the capability to revise previous thoughts through re-analysis
        let initial_problem = "Fix the authentication bug";

        // Initial analysis
        let initial_result =
            ThinkTool::think_about_code(initial_problem, Some("rust"), None).unwrap();
        assert_eq!(initial_result.problem_type, CodeProblemType::BugFix);

        // More specific problem with additional context (simulating revision)
        let revised_problem =
            "Fix the authentication bug that causes null pointer exception in login validation";
        let revised_result =
            ThinkTool::think_about_code(revised_problem, Some("rust"), Some("src/auth.rs"))
                .unwrap();

        // Verify revision produces more detailed analysis
        assert!(
            revised_result.steps.len() >= initial_result.steps.len(),
            "Revised analysis should be as detailed or more"
        );
        assert!(
            !revised_result.affected_files.is_empty(),
            "Revised analysis should identify affected files"
        );
        assert!(
            revised_result.confidence >= initial_result.confidence - 0.1,
            "Confidence should not significantly decrease"
        );

        // Error-specific analysis should be more targeted
        let error_result =
            ThinkTool::analyze_error("NullPointerException in auth validation").unwrap();
        assert_eq!(error_result.problem_type, CodeProblemType::BugFix);
        assert!(
            error_result.recommended_action.contains("null")
                || error_result.recommended_action.contains("check")
        );

        // Test thought data revision capability
        let thought1 = ThoughtData::new("Initial analysis".to_string(), 1, 3);
        let thought2 = ThoughtData::new("Revised analysis".to_string(), 2, 3).as_revision(1);

        assert!(!thought1.is_revision);
        assert!(thought2.is_revision);
        assert_eq!(thought2.revises_thought, Some(1));
    }

    #[test]
    fn test_tool_recommendations() {
        // Test that appropriate tools are recommended for different problem types
        let test_cases = [
            (
                "Debug memory leak in C++ application",
                CodeProblemType::BugFix,
                "debug",
            ),
            (
                "Optimize slow database queries",
                CodeProblemType::Performance,
                "profile",
            ),
            (
                "Refactor messy legacy code",
                CodeProblemType::Refactoring,
                "refactor",
            ),
            (
                "Implement user registration feature",
                CodeProblemType::Implementation,
                "implement",
            ),
            (
                "Fix SQL injection vulnerability",
                CodeProblemType::Security,
                "security",
            ),
            (
                "Write comprehensive unit tests",
                CodeProblemType::Testing,
                "test",
            ),
            (
                "Document the REST API endpoints",
                CodeProblemType::Documentation,
                "documentation",
            ),
            (
                "Design microservices architecture",
                CodeProblemType::Architecture,
                "architecture",
            ),
            (
                "Review code for best practices",
                CodeProblemType::CodeReview,
                "review",
            ),
        ];

        for (problem, expected_type, expected_action_keyword) in test_cases {
            let result = ThinkTool::think_about_code(problem, Some("rust"), None).unwrap();

            assert_eq!(
                result.problem_type, expected_type,
                "Problem '{}' should be classified as {:?}",
                problem, expected_type
            );

            let action_lower = result.recommended_action.to_lowercase();
            assert!(
                action_lower.contains(expected_action_keyword),
                "Recommended action '{}' should contain '{}' for problem type {:?}",
                result.recommended_action,
                expected_action_keyword,
                expected_type
            );

            // Verify approach reasoning contains problem-specific guidance
            let has_relevant_step = result.steps.iter().any(|step| {
                let step_content = format!("{} {}", step.thought, step.reasoning).to_lowercase();
                step_content.contains(expected_action_keyword)
                    || step_content.contains(&format!("{:?}", expected_type).to_lowercase())
            });
            assert!(
                has_relevant_step,
                "Steps should contain problem-type specific reasoning for {:?}",
                expected_type
            );
        }

        // Test tool recommendation structure
        let tool_rec = ToolRecommendation {
            tool: "search".to_string(),
            confidence: 0.8,
            rationale: "Need to find relevant code".to_string(),
            priority: 1,
        };

        assert_eq!(tool_rec.tool, "search");
        assert!(tool_rec.confidence > 0.0 && tool_rec.confidence <= 1.0);
        assert!(!tool_rec.rationale.is_empty());
    }

    #[test]
    fn test_branch_management() {
        // Test branching thoughts for alternative approaches

        // Analyze from performance perspective
        let perf_result = ThinkTool::think_about_code(
            "Optimize performance bottlenecks in authentication system",
            Some("rust"),
            Some("src/auth.rs"),
        )
        .unwrap();

        // Analyze from security perspective
        let security_result = ThinkTool::think_about_code(
            "Fix security vulnerabilities in authentication system",
            Some("rust"),
            Some("src/auth.rs"),
        )
        .unwrap();

        // Verify different approaches are taken
        assert_eq!(perf_result.problem_type, CodeProblemType::Performance);
        assert_eq!(security_result.problem_type, CodeProblemType::Security);

        // Performance branch should focus on optimization
        let perf_actions = perf_result.recommended_action.to_lowercase();
        assert!(
            perf_actions.contains("profile")
                || perf_actions.contains("measure")
                || perf_actions.contains("performance")
        );

        // Security branch should focus on vulnerabilities
        let security_actions = security_result.recommended_action.to_lowercase();
        assert!(
            security_actions.contains("security")
                || security_actions.contains("vulnerability")
                || security_actions.contains("assessment")
        );

        // Both should identify the same affected files but different approaches
        assert_eq!(perf_result.affected_files, security_result.affected_files);
        assert_ne!(
            perf_result.recommended_action,
            security_result.recommended_action
        );

        // Test ThoughtData branching functionality
        let mut session = ThinkSession::new(100);
        session.add_thought(ThoughtData::new("Main thought".to_string(), 1, 3));

        // Create a branch from the first thought
        session.create_branch(0, "security-branch".to_string());
        assert!(session.branches.contains_key("security-branch"));

        // Test thought with branch data
        let branched_thought = ThoughtData::new("Security analysis".to_string(), 1, 2)
            .with_branch(1, "security-branch".to_string());

        assert_eq!(branched_thought.branch_from_thought, Some(1));
        assert_eq!(
            branched_thought.branch_id,
            Some("security-branch".to_string())
        );

        // Error analysis branching - different error types should produce different strategies
        let null_error = ThinkTool::analyze_error("NullPointerException in user service").unwrap();
        let memory_error =
            ThinkTool::analyze_error("Segmentation fault in buffer handling").unwrap();
        let network_error = ThinkTool::analyze_error("Connection timeout in API client").unwrap();

        // All should be bug fixes but with different approaches
        assert_eq!(null_error.problem_type, CodeProblemType::BugFix);
        assert_eq!(memory_error.problem_type, CodeProblemType::BugFix);
        assert_eq!(network_error.problem_type, CodeProblemType::BugFix);

        // But recommended actions should be different
        let actions = [
            &null_error.recommended_action,
            &memory_error.recommended_action,
            &network_error.recommended_action,
        ];
        assert!(actions.iter().all(|action| !action.is_empty()));
        // Actions should be unique (no two identical approaches)
        for (i, action1) in actions.iter().enumerate() {
            for (j, action2) in actions.iter().enumerate() {
                if i != j {
                    assert_ne!(
                        action1, action2,
                        "Different error types should have different recommended actions"
                    );
                }
            }
        }
    }

    #[test]
    fn test_session_history() {
        // Test that thought history is maintained correctly across multiple analyses
        let mut session = ThinkSession::new(100);

        // Simulate a debugging session with progressive problem refinement
        let problems = [
            "Application crashes on startup",
            "Application crashes when loading configuration",
            "Null pointer when parsing config file in src/config.rs line 45",
        ];

        let mut session_results = Vec::new();

        for (i, problem) in problems.iter().enumerate() {
            let context = if i == 2 { Some("src/config.rs") } else { None };
            let result = if i == 2 {
                ThinkTool::analyze_error(problem).unwrap()
            } else {
                ThinkTool::think_about_code(problem, Some("rust"), context).unwrap()
            };

            // Add thought to session
            let thought = ThoughtData::new(format!("Problem {}: {}", i + 1, problem), i + 1, 3)
                .with_confidence(result.confidence);
            session.add_thought(thought);

            session_results.push(result);
        }

        // Verify session history
        assert_eq!(session.thought_history.len(), 3);
        assert_eq!(session.total_thought_count, 3);

        // Verify progression in specificity and confidence
        assert!(session_results.len() == 3);

        // Later results should be more specific
        assert!(session_results[2].affected_files.len() >= session_results[0].affected_files.len());

        // Error analysis (last) should have high confidence
        assert!(
            session_results[2].confidence >= 0.7,
            "Error analysis should have high confidence"
        );

        // All should be related to debugging/bug fixing
        let problem_types: Vec<_> = session_results.iter().map(|r| &r.problem_type).collect();
        assert!(problem_types.iter().all(|&pt| matches!(
            pt,
            &CodeProblemType::BugFix | &CodeProblemType::Implementation
        )));

        // Recommended actions should become more specific
        let actions: Vec<_> = session_results
            .iter()
            .map(|r| &r.recommended_action)
            .collect();
        assert!(
            actions[2].len() >= actions[0].len(),
            "Later actions should be more detailed"
        );

        // Test session history access
        let current_history = session.current_history();
        assert_eq!(current_history.len(), 3);

        // Test session thought progression
        for (i, thought) in current_history.iter().enumerate() {
            assert_eq!(thought.thought_number, i + 1);
            assert!(!thought.thought.is_empty());
        }
    }

    #[test]
    fn test_tool_confidence_scores() {
        // Verify confidence scores are appropriate for each problem type
        let test_cases = [
            // High confidence cases (well-defined, specific problems)
            (
                "Fix NullPointerException in UserService.java line 42",
                CodeProblemType::BugFix,
                0.7,
                0.95,
            ),
            (
                "Write unit tests for calculateTax method",
                CodeProblemType::Testing,
                0.7,
                0.95,
            ),
            (
                "Document the getUserById API endpoint",
                CodeProblemType::Documentation,
                0.7,
                0.95,
            ),
            // Medium confidence cases (moderately complex)
            (
                "Implement user authentication with JWT",
                CodeProblemType::Implementation,
                0.5,
                0.8,
            ),
            (
                "Refactor the large UserService class",
                CodeProblemType::Refactoring,
                0.5,
                0.8,
            ),
            // Lower confidence cases (complex, contextual)
            (
                "Design scalable microservices architecture",
                CodeProblemType::Architecture,
                0.4,
                0.7,
            ),
            (
                "Optimize overall system performance",
                CodeProblemType::Performance,
                0.4,
                0.7,
            ),
            (
                "Review codebase for security issues",
                CodeProblemType::Security,
                0.4,
                0.7,
            ),
        ];

        for (problem, expected_type, min_confidence, max_confidence) in test_cases {
            let result = ThinkTool::think_about_code(problem, Some("java"), None).unwrap();

            assert_eq!(
                result.problem_type, expected_type,
                "Problem type should match for: {}",
                problem
            );

            assert!(
                result.confidence >= min_confidence && result.confidence <= max_confidence,
                "Confidence {:.2} should be between {:.2} and {:.2} for {:?} problem: '{}'",
                result.confidence,
                min_confidence,
                max_confidence,
                expected_type,
                problem
            );
        }

        // Test confidence with technical specificity
        let vague_problem = "Fix something that's broken";
        let specific_problem =
            "Fix memory leak in ArrayList resize operation in Java HashMap implementation";

        let vague_result = ThinkTool::think_about_code(vague_problem, Some("java"), None).unwrap();
        let specific_result =
            ThinkTool::think_about_code(specific_problem, Some("java"), None).unwrap();

        assert!(
            specific_result.confidence > vague_result.confidence,
            "Specific problems should have higher confidence than vague ones"
        );

        // Test confidence with language context
        let rust_result =
            ThinkTool::think_about_code("Implement memory-safe linked list", Some("rust"), None)
                .unwrap();
        let assembly_result = ThinkTool::think_about_code(
            "Implement memory-safe linked list",
            Some("assembly"),
            None,
        )
        .unwrap();

        // Assembly should have lower confidence due to complexity
        assert!(
            rust_result.confidence >= assembly_result.confidence,
            "Complex languages should not increase confidence unnecessarily"
        );

        // Test error analysis confidence
        let error_result = ThinkTool::analyze_error(
            "java.lang.NullPointerException: Cannot invoke method on null object",
        )
        .unwrap();
        assert!(
            error_result.confidence >= 0.7,
            "Error analysis with clear error messages should have high confidence"
        );

        // Test refactoring confidence
        let refactoring_result = ThinkTool::plan_refactoring("Long method with 200 lines").unwrap();
        assert!(
            refactoring_result.confidence >= 0.8,
            "Clear refactoring problems should have high confidence"
        );

        // Test ThoughtData confidence
        let thought = ThoughtData::new("Test thought".to_string(), 1, 3).with_confidence(0.85);
        assert_eq!(thought.confidence, 0.85);

        // Test confidence bounds
        let bounded_thought = ThoughtData::new("Test".to_string(), 1, 1).with_confidence(1.5); // Should be clamped to 1.0
        assert_eq!(bounded_thought.confidence, 1.0);
    }

    #[test]
    fn test_sequential_thinking_step_quality() {
        // Test the quality and coherence of sequential thinking steps
        let complex_problem = "Design and implement a secure, scalable user authentication system with OAuth2 integration";

        let result =
            ThinkTool::think_about_code(complex_problem, Some("typescript"), Some("src/auth/"))
                .unwrap();

        // Should generate multiple detailed steps for complex problems
        assert!(
            result.steps.len() >= 4,
            "Complex problems should generate more detailed analysis"
        );

        // Verify step progression makes logical sense
        let step_keywords = result
            .steps
            .iter()
            .map(|step| format!("{} {}", step.thought, step.reasoning).to_lowercase())
            .collect::<Vec<_>>();

        // Early steps should focus on understanding/analysis
        assert!(
            step_keywords[0].contains("context")
                || step_keywords[0].contains("understand")
                || step_keywords[0].contains("analyz")
        );

        // Later steps should focus on implementation/solution
        let last_steps = &step_keywords[step_keywords.len().saturating_sub(2)..];
        assert!(last_steps.iter().any(|step| {
            step.contains("implement") || step.contains("solution") || step.contains("strategy")
        }));

        // Verify step content quality
        for (i, step) in result.steps.iter().enumerate() {
            assert!(
                step.thought.len() >= 20,
                "Step {} thought should be substantial",
                i + 1
            );
            assert!(
                step.reasoning.len() >= 30,
                "Step {} reasoning should be detailed",
                i + 1
            );

            // Steps should not be repetitive
            for (j, other_step) in result.steps.iter().enumerate() {
                if i != j {
                    assert_ne!(
                        step.thought, other_step.thought,
                        "Steps should not be identical"
                    );
                }
            }
        }

        // Verify conclusion incorporates the analysis
        assert!(
            result.conclusion.len() >= 50,
            "Conclusion should be comprehensive"
        );
        assert!(
            result.conclusion.contains("auth")
                || result.conclusion.contains("security")
                || result.conclusion.contains("OAuth")
        );

        // Verify recommended action is specific and actionable
        assert!(
            result.recommended_action.len() >= 30,
            "Recommended action should be detailed"
        );
        assert!(
            !result.recommended_action.contains("TODO")
                && !result.recommended_action.contains("...")
        );

        // Test step description structure
        let step_desc = StepDescription {
            description: "Analyze authentication requirements".to_string(),
            expected_outcome: "Clear understanding of auth needs".to_string(),
            next_conditions: vec!["Security requirements defined".to_string()],
        };

        assert!(!step_desc.description.is_empty());
        assert!(!step_desc.expected_outcome.is_empty());
        assert!(!step_desc.next_conditions.is_empty());
    }

    #[test]
    fn test_thinking_consistency() {
        // Test that repeated analysis of the same problem produces consistent results
        let problem = "Optimize database query performance for user search";

        let results: Vec<_> = (0..3)
            .map(|_| {
                ThinkTool::think_about_code(problem, Some("sql"), Some("queries/user_search.sql"))
                    .unwrap()
            })
            .collect();

        // All results should have same classification
        let problem_types: Vec<_> = results.iter().map(|r| &r.problem_type).collect();
        assert!(
            problem_types
                .iter()
                .all(|&pt| pt == &CodeProblemType::Performance)
        );

        // Confidence should be consistent (within reasonable variance)
        let confidences: Vec<_> = results.iter().map(|r| r.confidence).collect();
        let confidence_variance = {
            let mean = confidences.iter().sum::<f32>() / confidences.len() as f32;
            confidences.iter().map(|c| (c - mean).abs()).sum::<f32>() / confidences.len() as f32
        };
        assert!(
            confidence_variance < 0.1,
            "Confidence should be consistent across runs"
        );

        // Step count should be consistent
        let step_counts: Vec<_> = results.iter().map(|r| r.steps.len()).collect();
        let min_steps = *step_counts.iter().min().unwrap();
        let max_steps = *step_counts.iter().max().unwrap();
        assert!(
            max_steps - min_steps <= 1,
            "Step count should be consistent"
        );

        // Recommended actions should be similar (same problem type)
        let actions: Vec<_> = results.iter().map(|r| &r.recommended_action).collect();
        assert!(actions.iter().all(|action| {
            action.to_lowercase().contains("profile")
                || action.to_lowercase().contains("measure")
                || action.to_lowercase().contains("performance")
        }));

        // Test basic ThinkResult with thought_data
        let basic_result = ThinkTool::think("How to solve this?").unwrap();
        assert!(basic_result.steps.len() >= 3);
        assert!(!basic_result.conclusion.is_empty());
        assert!(basic_result.confidence > 0.0);
        // thought_data should be None for basic thinking
        assert!(basic_result.thought_data.is_none());
    }
}