semantex-core 1.0.0

Core library for semantex semantic code search (indexing, embeddings, search)
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
use crate::chunking::Chunker;
use crate::chunking::call_graph;
use crate::chunking::structured_meta::{
    ImplRelation, StructuredChunkMeta, TypeRef, TypeRefContext,
};
use crate::chunking::text_chunker::TextChunker;
use crate::file::detector::FileType;
use crate::types::{AstNodeKind, Chunk, ChunkType};
use anyhow::{Result, anyhow};
use std::path::Path;
use tree_sitter::{Language, Node, Parser};

/// Approximate characters per token
const CHARS_PER_TOKEN: usize = 4;

pub struct AstChunker {
    chunk_size: usize,
    chunk_overlap: usize,
    fallback: TextChunker,
}

impl AstChunker {
    pub fn new(chunk_size: usize, chunk_overlap: usize) -> Self {
        Self {
            chunk_size,
            chunk_overlap,
            fallback: TextChunker::new(chunk_size, chunk_overlap),
        }
    }
}

impl Chunker for AstChunker {
    #[allow(clippy::too_many_lines)]
    fn chunk(&self, path: &Path, content: &str) -> Result<Vec<Chunk>> {
        let file_type = FileType::detect(path);

        let Some(lang_fn) = get_language(path, file_type) else {
            return self.fallback.chunk(path, content);
        };

        let Some(definition_kinds) = definition_node_kinds(file_type) else {
            return self.fallback.chunk(path, content);
        };

        let mut parser = Parser::new();
        parser
            .set_language(&lang_fn)
            .map_err(|e| anyhow!("Failed to set language: {e}"))?;

        let tree = parser
            .parse(content, None)
            .ok_or_else(|| anyhow!("tree-sitter parse failed for {}", path.display()))?;

        let source = content.as_bytes();

        // Extract file-level imports (for attaching to chunks)
        let language_name_str = file_type.language_name();
        let file_imports = crate::chunking::import_resolver::extract_imports(
            &tree.root_node(),
            source,
            language_name_str,
        );
        // Keep up to 8 most relevant imports per chunk
        let truncated_imports: Vec<String> = file_imports.into_iter().take(8).collect();

        let mut ast_spans: Vec<AstSpan> = Vec::new();
        collect_definitions(tree.root_node(), source, definition_kinds, &mut ast_spans);

        // Strip type_refs from generated code (Dart codegen) — too noisy for cross-file resolution
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
        if file_name.ends_with(".freezed.dart")
            || file_name.ends_with(".g.dart")
            || file_name.ends_with(".pb.dart")
        {
            for span in &mut ast_spans {
                span.meta.type_refs.clear();
            }
        }

        // Sort by start byte to process gaps in order
        ast_spans.sort_by_key(|s| s.start_byte);

        // Remove overlapping spans (keep the first / outermost one)
        let mut deduped: Vec<AstSpan> = Vec::new();
        for span in ast_spans {
            if let Some(last) = deduped.last()
                && span.start_byte < last.end_byte
            {
                continue; // nested inside previous span, skip
            }
            deduped.push(span);
        }

        // Attach file-level imports to each span's metadata
        for span in &mut deduped {
            span.meta.resolved_imports.clone_from(&truncated_imports);
        }

        let language_name = file_type.language_name().to_string();
        let max_node_chars = self.chunk_size * CHARS_PER_TOKEN * 4;
        let mut chunks: Vec<Chunk> = Vec::new();
        // Track which chunk indices carry structured_meta (for call graph post-pass)
        let mut meta_indices: Vec<(usize, String, StructuredChunkMeta)> = Vec::new();
        let mut window_index = 0u32;
        let mut cursor = 0usize; // byte offset tracking position in content

        for span in &deduped {
            // Emit gap chunk(s) for content before this AST node
            if span.start_byte > cursor {
                let gap = &content[cursor..span.start_byte];
                if !gap.trim().is_empty() {
                    let gap_start_line = byte_offset_to_line(content, cursor);
                    let gap_end_line =
                        byte_offset_to_line(content, span.start_byte.saturating_sub(1));
                    chunks.push(Chunk {
                        id: 0,
                        file_path: path.to_path_buf(),
                        start_line: gap_start_line,
                        end_line: gap_end_line.max(gap_start_line),
                        content: gap.to_string(),
                        chunk_type: ChunkType::TextWindow { window_index },
                    });
                    window_index += 1;
                }
            }

            let node_text_str = &content[span.start_byte..span.end_byte];
            // tree-sitter rows are 0-based, Chunk lines are 1-based
            let start_line = span.start_row + 1;
            let end_line = span.end_row + 1;

            if node_text_str.len() > max_node_chars {
                // Split oversized AST nodes with sliding window.
                // Attach metadata only to the first sub-chunk.
                let sub_chunks = split_large_node(
                    path,
                    node_text_str,
                    start_line,
                    &span.name,
                    &span.kind,
                    &language_name,
                    self.chunk_size,
                    self.chunk_overlap,
                    &span.meta,
                );
                chunks.extend(sub_chunks);
            } else {
                let chunk_idx = chunks.len();
                chunks.push(Chunk {
                    id: 0,
                    file_path: path.to_path_buf(),
                    start_line: start_line as u32,
                    end_line: end_line as u32,
                    content: node_text_str.to_string(),
                    chunk_type: ChunkType::AstNode {
                        name: span.name.clone(),
                        kind: span.kind.clone(),
                        language: language_name.clone(),
                        structured_meta: None, // filled after call graph post-pass
                    },
                });
                meta_indices.push((chunk_idx, span.name.clone(), span.meta.clone()));
            }

            cursor = span.end_byte;
        }

        // Emit trailing gap
        if cursor < content.len() {
            let gap = &content[cursor..];
            if !gap.trim().is_empty() {
                let gap_start_line = byte_offset_to_line(content, cursor);
                let gap_end_line = byte_offset_to_line(content, content.len().saturating_sub(1));
                chunks.push(Chunk {
                    id: 0,
                    file_path: path.to_path_buf(),
                    start_line: gap_start_line,
                    end_line: gap_end_line.max(gap_start_line),
                    content: gap.to_string(),
                    chunk_type: ChunkType::TextWindow { window_index },
                });
            }
        }

        // If we found no AST nodes at all, fall back to text chunking
        if chunks.is_empty() {
            return self.fallback.chunk(path, content);
        }

        // Call graph post-pass: build bidirectional caller/callee relationships
        if !meta_indices.is_empty() {
            let mut chunks_with_meta: Vec<(String, StructuredChunkMeta)> = meta_indices
                .iter()
                .map(|(_, name, meta)| (name.clone(), meta.clone()))
                .collect();

            call_graph::build_call_graph(&mut chunks_with_meta);

            // Generate NL summaries (after called_by is populated)
            for (_, meta) in &mut chunks_with_meta {
                meta.generate_nl_summary();
            }

            // Store metadata back into chunks
            for (i, (chunk_idx, _, _)) in meta_indices.iter().enumerate() {
                if let ChunkType::AstNode {
                    ref mut structured_meta,
                    ..
                } = chunks[*chunk_idx].chunk_type
                {
                    *structured_meta = Some(Box::new(chunks_with_meta[i].1.clone()));
                }
            }
        }

        Ok(chunks)
    }
}

/// A span extracted from the AST, with pre-extracted structured metadata.
struct AstSpan {
    start_byte: usize,
    end_byte: usize,
    start_row: usize,
    end_row: usize,
    name: String,
    kind: AstNodeKind,
    meta: StructuredChunkMeta,
}

// ---------------------------------------------------------------------------
// Tree-sitter helpers
// ---------------------------------------------------------------------------

/// Get UTF-8 text for a tree-sitter node.
fn ts_node_text<'a>(node: Node<'a>, source: &'a [u8]) -> &'a str {
    node.utf8_text(source).unwrap_or("")
}

/// Find the first direct child of `node` whose `kind()` equals `kind`.
fn find_child_by_type<'a>(node: Node<'a>, kind: &str) -> Option<Node<'a>> {
    let mut cursor = node.walk();
    node.children(&mut cursor)
        .find(|child| child.kind() == kind)
}

// ---------------------------------------------------------------------------
// 5-layer metadata extraction
// ---------------------------------------------------------------------------

/// Extract structured metadata (layers 1-4) from a tree-sitter AST node.
///
/// Layer 1 (AST): name, signature, params, return type, docstring
/// Layer 2 (Call Graph): outgoing calls (called_by filled in post-pass)
/// Layer 3 (Control Flow): complexity, branches, loops, error handling
/// Layer 4 (Data Flow): local variables, state mutations
fn extract_structured_meta(node: Node, source: &[u8]) -> StructuredChunkMeta {
    let mut meta = StructuredChunkMeta::default();

    // Layer 1: AST identity
    if let Some(name_node) = node.child_by_field_name("name") {
        meta.name = Some(ts_node_text(name_node, source).to_string());
    } else if node.kind() == "export_statement" {
        // export function foo() / export class Foo — name is in nested declaration
        let mut inner_cursor = node.walk();
        for child in node.children(&mut inner_cursor) {
            if let Some(inner_name) = child.child_by_field_name("name") {
                meta.name = Some(ts_node_text(inner_name, source).to_string());
                break;
            }
        }
    }

    // Signature: text up to first '{' or ':'
    let full_text = ts_node_text(node, source);
    if let Some(brace_pos) = full_text.find('{').or_else(|| full_text.find(':')) {
        let sig = full_text[..brace_pos].trim();
        if !sig.is_empty() {
            meta.signature = Some(sig.to_string());
        }
    }

    // Parameters
    let params_node = find_child_by_type(node, "formal_parameters")
        .or_else(|| find_child_by_type(node, "parameters"))
        .or_else(|| find_child_by_type(node, "parameter_list"));
    if let Some(pn) = params_node {
        let mut cursor = pn.walk();
        for child in pn.children(&mut cursor) {
            if child.kind().contains("parameter") || child.kind() == "identifier" {
                let param_text = ts_node_text(child, source).to_string();
                if !param_text.is_empty() && param_text.len() < 100 {
                    meta.params.push(param_text);
                }
            }
        }
    }

    // Return type
    if let Some(ret_node) = node
        .child_by_field_name("return_type")
        .or_else(|| find_child_by_type(node, "type_annotation"))
    {
        meta.return_type = Some(ts_node_text(ret_node, source).to_string());
    }

    // Layer 2: Outgoing calls
    walk_for_calls(node, source, &mut meta.calls);

    // Layer 1 (cont): Preceding docstring/comment
    #[allow(clippy::collapsible_if)]
    if let Some(prev) = node.prev_sibling() {
        if prev.kind() == "comment" || prev.kind().contains("doc") {
            let doc_text = ts_node_text(prev, source).to_string();
            if !doc_text.is_empty() {
                meta.docstring = Some(doc_text);
            }
        }
    }

    // Layer 3: Control flow
    extract_control_flow(node, source, &mut meta);

    // Layer 4: Data flow
    extract_data_flow(node, source, &mut meta);

    // Layer 5 enhanced: Type references
    meta.type_refs = extract_type_refs(node, source);

    // Layer 5 enhanced: Implementation relationships
    meta.implements = extract_implementations(node, source);

    // Layer 1 enhanced: Structured docstring tags
    if let Some(ref docstring) = meta.docstring {
        let lang = infer_language_from_node(node);
        meta.doc_tags = crate::chunking::doc_parser::parse_doc_tags(docstring, lang);
    }

    meta
}

/// Recursively collect function/method call targets from a tree-sitter subtree.
fn walk_for_calls(node: Node, source: &[u8], calls: &mut Vec<String>) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        let callee_text: Option<String> = if child.kind() == "call_expression"
            || child.kind() == "method_invocation"
            || child.kind() == "call"
        {
            // Regular function / method call: target is in the `function` or
            // `name` field, falling back to the first child (e.g. Elixir).
            child
                .child_by_field_name("function")
                .or_else(|| child.child_by_field_name("name"))
                .or_else(|| child.child(0))
                .map(|n| ts_node_text(n, source).to_string())
        } else if child.kind() == "new_expression" {
            // TypeScript / JavaScript: `new Foo(...)` or `new ns.Foo(...)`.
            // The `constructor` field holds the constructed type expression.
            // We record the raw text (e.g. "Foo" or "ns.Foo"); the call-graph
            // post-pass uses rsplit('.') to strip any namespace prefix when
            // resolving to a named chunk.
            child
                .child_by_field_name("constructor")
                .map(|n| ts_node_text(n, source).to_string())
        } else if child.kind() == "object_creation_expression" {
            // Java / C#: `new Foo(...)`.
            // The `type` field holds the constructed type name.
            child
                .child_by_field_name("type")
                .map(|n| ts_node_text(n, source).to_string())
        } else {
            None
        };

        if let Some(text) = callee_text
            && !text.is_empty()
            && text.len() < 100
        {
            calls.push(text);
        }

        walk_for_calls(child, source, calls);
    }
}

/// Extract control flow information (Layer 3).
fn extract_control_flow(node: Node, source: &[u8], meta: &mut StructuredChunkMeta) {
    let mut complexity: u32 = 1; // base complexity
    walk_for_control_flow(node, source, &mut complexity, meta);
    meta.complexity = complexity;
}

fn walk_for_control_flow(
    node: Node,
    source: &[u8],
    complexity: &mut u32,
    meta: &mut StructuredChunkMeta,
) {
    match node.kind() {
        "if_statement"
        | "if_expression"
        | "conditional_expression"
        | "match_expression"
        | "switch_statement"
        | "case_clause" => {
            *complexity += 1;
            meta.has_branches = true;
        }
        "for_statement" | "for_expression" | "while_statement" | "loop_expression"
        | "for_in_statement" | "for_of_statement" => {
            *complexity += 1;
            meta.has_loops = true;
        }
        "try_statement" | "catch_clause" | "rescue" => {
            meta.has_error_handling = true;
        }
        "match_arm" => {
            let arm_text = ts_node_text(node, source);
            if arm_text.contains("Err(") || arm_text.contains("None") {
                meta.has_error_handling = true;
            }
        }
        _ => {}
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_for_control_flow(child, source, complexity, meta);
    }
}

/// Extract data flow information (Layer 4).
fn extract_data_flow(node: Node, source: &[u8], meta: &mut StructuredChunkMeta) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "let_declaration"
            | "variable_declaration"
            | "variable_declarator"
            | "assignment_statement"
            | "let_statement" => {
                if let Some(name_node) = child
                    .child_by_field_name("name")
                    .or_else(|| child.child_by_field_name("pattern"))
                    .or_else(|| child.child(0))
                {
                    let var_name = ts_node_text(name_node, source).to_string();
                    if !var_name.is_empty() && var_name.len() < 50 {
                        meta.local_vars.push(var_name);
                    }
                }
            }
            "assignment_expression" => {
                if let Some(left) = child.child_by_field_name("left").or_else(|| child.child(0)) {
                    let lhs = ts_node_text(left, source);
                    if lhs.contains("self.") || lhs.contains("this.") || lhs.contains("->") {
                        meta.state_mutations.push(lhs.to_string());
                    }
                }
            }
            _ => {}
        }
        extract_data_flow(child, source, meta);
    }
}

// ---------------------------------------------------------------------------
// Layer 5 enhanced: Type reference extraction
// ---------------------------------------------------------------------------

/// Primitive type names to exclude from type references.
const PRIMITIVE_TYPES: &[&str] = &[
    "str",
    "string",
    "String",
    "int",
    "i8",
    "i16",
    "i32",
    "i64",
    "i128",
    "u8",
    "u16",
    "u32",
    "u64",
    "u128",
    "f32",
    "f64",
    "float",
    "double",
    "bool",
    "boolean",
    "void",
    "None",
    "null",
    "undefined",
    "number",
    "usize",
    "isize",
    "char",
    "byte",
    "self",
    "Self",
];

/// Check if a type name is a primitive that should be filtered out.
fn is_primitive_type(name: &str) -> bool {
    PRIMITIVE_TYPES.contains(&name)
}

/// Generic container types with no cross-file resolution value.
const GENERIC_CONTAINER_TYPES: &[&str] = &[
    "List",
    "Map",
    "Vec",
    "Array",
    "Set",
    "Option",
    "Result",
    "Optional",
    "Promise",
    "Future",
    "Stream",
    "Observable",
    "HashMap",
    "HashSet",
    "BTreeMap",
    "BTreeSet",
    "Dictionary",
];

/// Check if a type name looks user-defined (starts with uppercase or contains `::` / `.`).
fn is_user_type(name: &str) -> bool {
    if name.is_empty() || is_primitive_type(name) {
        return false;
    }
    if GENERIC_CONTAINER_TYPES.contains(&name) {
        return false;
    }
    // Must start with uppercase letter, or contain a path separator
    name.starts_with(|c: char| c.is_uppercase()) || name.contains("::") || name.contains('.')
}

/// Extract type references from a tree-sitter AST node.
///
/// Looks for user-defined types in parameter annotations, return types, and field types.
fn extract_type_refs(node: Node, source: &[u8]) -> Vec<TypeRef> {
    let mut refs = Vec::new();

    // Extract from parameter type annotations
    let params_node = find_child_by_type(node, "formal_parameters")
        .or_else(|| find_child_by_type(node, "parameters"))
        .or_else(|| find_child_by_type(node, "parameter_list"));
    if let Some(pn) = params_node {
        collect_type_identifiers(pn, source, TypeRefContext::Param, &mut refs);
    }

    // Extract from return type
    if let Some(ret_node) = node
        .child_by_field_name("return_type")
        .or_else(|| find_child_by_type(node, "type_annotation"))
    {
        collect_type_identifiers(ret_node, source, TypeRefContext::Return, &mut refs);
    }

    // Extract from struct/class field types
    let node_kind = node.kind();
    if node_kind == "struct_item"
        || node_kind == "struct_specifier"
        || node_kind == "class_declaration"
        || node_kind == "class_definition"
        || node_kind == "class_specifier"
        || node_kind == "struct_declaration"
    {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            let ck = child.kind();
            if ck == "field_declaration"
                || ck == "field_definition"
                || ck == "field_declaration_list"
                || ck == "class_body"
            {
                collect_type_identifiers(child, source, TypeRefContext::Field, &mut refs);
            }
        }
    }

    refs
}

/// Walk a subtree and collect type identifier nodes as `TypeRef`s.
fn collect_type_identifiers(
    node: Node,
    source: &[u8],
    context: TypeRefContext,
    refs: &mut Vec<TypeRef>,
) {
    let kind = node.kind();

    if kind == "type_identifier" || kind == "scoped_type_identifier" {
        let name = ts_node_text(node, source);
        if is_user_type(name) && name.len() < 100 {
            refs.push(TypeRef {
                type_name: name.to_string(),
                context,
            });
        }
        return; // Don't recurse into children of type identifiers
    }

    if kind == "generic_type" {
        // Extract the base type from generic_type (e.g. `Vec<T>` -> look for type_identifier child)
        if let Some(base) = node.child(0) {
            let base_kind = base.kind();
            if base_kind == "type_identifier" || base_kind == "scoped_type_identifier" {
                let name = ts_node_text(base, source);
                if is_user_type(name) && name.len() < 100 {
                    refs.push(TypeRef {
                        type_name: name.to_string(),
                        context,
                    });
                }
            }
        }
        // Also check type arguments for user types
        if let Some(type_args) = find_child_by_type(node, "type_arguments") {
            collect_type_identifiers(type_args, source, context, refs);
        }
        return;
    }

    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_type_identifiers(child, source, context, refs);
    }
}

// ---------------------------------------------------------------------------
// Layer 5 enhanced: Trait/impl extraction
// ---------------------------------------------------------------------------

/// Extract implementation relationships from a tree-sitter AST node.
///
/// Handles Rust `impl Trait for Type`, TS/Java `class X implements Y`,
/// and Python class inheritance.
fn extract_implementations(node: Node, source: &[u8]) -> Vec<ImplRelation> {
    let mut relations = Vec::new();
    let node_kind = node.kind();

    match node_kind {
        // Rust: `impl Trait for Type { ... }`
        "impl_item" => {
            let trait_node = node.child_by_field_name("trait");
            let type_node = node.child_by_field_name("type");
            if let (Some(tn), Some(ty)) = (trait_node, type_node) {
                let trait_name = ts_node_text(tn, source).to_string();
                let implementor = ts_node_text(ty, source).to_string();
                if !trait_name.is_empty() && !implementor.is_empty() {
                    relations.push(ImplRelation {
                        implementor,
                        trait_name,
                    });
                }
            }
        }
        // Java/TS/Dart/C# class declarations with implements/interfaces clause
        "class_declaration" | "class_definition" => {
            let implementor_name = node
                .child_by_field_name("name")
                .map(|n| ts_node_text(n, source).to_string())
                .unwrap_or_default();

            if implementor_name.is_empty() {
                return relations;
            }

            // Look for implements/interfaces clause
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                let ck = child.kind();
                if ck == "implements_clause"
                    || ck == "interfaces"
                    || ck == "super_interfaces"
                    || ck == "implements"
                {
                    let mut inner_cursor = child.walk();
                    for iface in child.children(&mut inner_cursor) {
                        if iface.kind() == "type_identifier"
                            || iface.kind() == "scoped_type_identifier"
                        {
                            let trait_name = ts_node_text(iface, source).to_string();
                            if !trait_name.is_empty() {
                                relations.push(ImplRelation {
                                    implementor: implementor_name.clone(),
                                    trait_name,
                                });
                            }
                        }
                    }
                }

                // Python: class Foo(Base1, Base2) — argument_list holds base classes
                if ck == "argument_list" {
                    let mut inner_cursor = child.walk();
                    for arg in child.children(&mut inner_cursor) {
                        if arg.kind() == "identifier" || arg.kind() == "attribute" {
                            let base_name = ts_node_text(arg, source).to_string();
                            if !base_name.is_empty() && base_name != "object" {
                                relations.push(ImplRelation {
                                    implementor: implementor_name.clone(),
                                    trait_name: base_name,
                                });
                            }
                        }
                    }
                }

                // extends clause (TS/Java)
                if ck == "extends_clause" || ck == "superclass" {
                    let mut inner_cursor = child.walk();
                    for base in child.children(&mut inner_cursor) {
                        if base.kind() == "type_identifier"
                            || base.kind() == "identifier"
                            || base.kind() == "scoped_type_identifier"
                        {
                            let base_name = ts_node_text(base, source).to_string();
                            if !base_name.is_empty() {
                                relations.push(ImplRelation {
                                    implementor: implementor_name.clone(),
                                    trait_name: base_name,
                                });
                            }
                        }
                    }
                }
            }
        }
        _ => {}
    }

    relations
}

// ---------------------------------------------------------------------------
// Language inference helper
// ---------------------------------------------------------------------------

/// Infer a language name from a tree-sitter node by walking up to the root
/// and checking the grammar name. Returns a best-effort language string.
fn infer_language_from_node(node: Node) -> &'static str {
    // Walk to root and check tree language via node kind patterns
    let kind = node.kind();
    if kind == "function_item"
        || kind == "impl_item"
        || kind == "struct_item"
        || kind == "enum_item"
        || kind == "trait_item"
        || kind == "mod_item"
    {
        return "rust";
    }
    if kind == "function_definition" || kind == "class_definition" {
        // Could be Python — check for `def` or `class` keywords
        // Python function_definition and class_definition are distinct from other languages
        // that use the same names (e.g., C). We use a heuristic: presence of `parameters` child.
        if find_child_by_type(node, "parameters").is_some() {
            return "python";
        }
    }
    if kind.contains("method_declaration") || kind.contains("interface_declaration") {
        return "java";
    }
    // Default fallback — generic tag parsing still works
    ""
}

// ---------------------------------------------------------------------------
// Language / grammar support
// ---------------------------------------------------------------------------

/// Get the tree-sitter language for a file, based on its type and extension
fn get_language(path: &Path, file_type: FileType) -> Option<Language> {
    match file_type {
        FileType::Rust => Some(tree_sitter_rust::LANGUAGE.into()),
        FileType::Python => Some(tree_sitter_python::LANGUAGE.into()),
        FileType::JavaScript => Some(tree_sitter_javascript::LANGUAGE.into()),
        FileType::TypeScript => {
            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if ext == "tsx" {
                Some(tree_sitter_typescript::LANGUAGE_TSX.into())
            } else {
                Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())
            }
        }
        FileType::Go => Some(tree_sitter_go::LANGUAGE.into()),
        FileType::Java => Some(tree_sitter_java::LANGUAGE.into()),
        FileType::C => Some(tree_sitter_c::LANGUAGE.into()),
        FileType::Cpp => Some(tree_sitter_cpp::LANGUAGE.into()),
        FileType::Ruby => Some(tree_sitter_ruby::LANGUAGE.into()),
        FileType::Dart => Some(tree_sitter_dart_orchard::LANGUAGE.into()),
        FileType::CSharp => Some(tree_sitter_c_sharp::LANGUAGE.into()),
        FileType::Scala => Some(tree_sitter_scala::LANGUAGE.into()),
        FileType::Php => Some(tree_sitter_php::LANGUAGE_PHP.into()),
        FileType::Lua => Some(tree_sitter_lua::LANGUAGE.into()),
        FileType::Haskell => Some(tree_sitter_haskell::LANGUAGE.into()),
        FileType::OCaml => {
            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
            if ext == "mli" {
                Some(tree_sitter_ocaml::LANGUAGE_OCAML_INTERFACE.into())
            } else {
                Some(tree_sitter_ocaml::LANGUAGE_OCAML.into())
            }
        }
        FileType::Zig => Some(tree_sitter_zig::LANGUAGE.into()),
        FileType::R => Some(tree_sitter_r::LANGUAGE.into()),
        FileType::Html => Some(tree_sitter_html::LANGUAGE.into()),
        FileType::Swift => Some(tree_sitter_swift::LANGUAGE.into()),
        FileType::Elixir => Some(tree_sitter_elixir::LANGUAGE.into()),
        FileType::Svelte => Some(tree_sitter_svelte_next::LANGUAGE.into()),
        FileType::Vue => Some(tree_sitter_vue_next::LANGUAGE.into()),
        FileType::Kotlin => Some(tree_sitter_kotlin_ng::LANGUAGE.into()),
        FileType::Sql => Some(tree_sitter_sequel::LANGUAGE.into()),
        _ => None,
    }
}

/// Get the set of AST node kind strings that represent definitions for a language
fn definition_node_kinds(file_type: FileType) -> Option<&'static [&'static str]> {
    match file_type {
        FileType::Rust => Some(&[
            "function_item",
            "impl_item",
            "struct_item",
            "enum_item",
            "mod_item",
            "trait_item",
        ]),
        FileType::Python => Some(&["function_definition", "class_definition"]),
        FileType::JavaScript | FileType::TypeScript => Some(&[
            "function_declaration",
            "class_declaration",
            "method_definition",
            "lexical_declaration",
            "export_statement",
        ]),
        FileType::Go => Some(&[
            "function_declaration",
            "method_declaration",
            "type_declaration",
        ]),
        FileType::Java => Some(&[
            "class_declaration",
            "method_declaration",
            "interface_declaration",
            "enum_declaration",
        ]),
        FileType::C => Some(&["function_definition", "struct_specifier"]),
        FileType::Cpp => Some(&["function_definition", "class_specifier", "struct_specifier"]),
        FileType::Ruby => Some(&["method", "class", "module"]),
        FileType::Dart => Some(&[
            "class_definition",
            "enum_declaration",
            "mixin_declaration",
            "extension_declaration",
            "extension_type_declaration",
            "function_signature",
            "getter_signature",
            "setter_signature",
            "constructor_signature",
            "type_alias",
        ]),
        FileType::CSharp => Some(&[
            "class_declaration",
            "method_declaration",
            "interface_declaration",
            "enum_declaration",
            "struct_declaration",
            "namespace_declaration",
            "record_declaration",                // C# 9+ records
            "file_scoped_namespace_declaration", // C# 10+ file-scoped namespaces
        ]),
        FileType::Scala => Some(&[
            "class_definition",
            "object_definition",
            "trait_definition",
            "function_definition",
            "val_definition",
            "given_definition",     // Scala 3 contextual instances
            "extension_definition", // Scala 3 extension methods
            "enum_definition",      // Scala 3 enums (distinct from Java/TS enum_declaration)
            "type_definition",      // type aliases
        ]),
        FileType::Php => Some(&[
            "class_declaration",
            "method_declaration",
            "function_definition",
            "interface_declaration",
            "trait_declaration",
        ]),
        FileType::Lua => Some(&["function_declaration", "local_function_declaration"]),
        FileType::Haskell => Some(&[
            "function",
            "type_alias",
            "newtype",
            "adt",
            "class",
            "instance",
        ]),
        FileType::OCaml => Some(&[
            "value_definition",
            "type_definition",
            "module_definition",
            "class_definition",
            // Top-level `external name : type = "c_func"` — OCaml grammar
            // emits node kind `external` at the structure_item level (not
            // `external_declaration`, which is only used inside type defs).
            "external",
            "external_declaration",
        ]),
        FileType::Zig => Some(&["function_declaration", "container_declaration"]),
        FileType::R => Some(&["function_definition", "left_assignment"]),
        FileType::Html | FileType::Svelte => Some(&["element", "script_element", "style_element"]),
        FileType::Vue => Some(&["script_element", "template_element", "style_element"]),
        FileType::Swift => Some(&[
            "class_declaration",
            "function_declaration",
            "protocol_declaration",
            "struct_declaration",
            "enum_declaration",
        ]),
        FileType::Elixir => Some(&["call"]),
        FileType::Kotlin => Some(&[
            "class_declaration",
            "function_declaration",
            "object_declaration",
            "interface_declaration",
        ]),
        // tree-sitter-sequel wraps every top-level statement in a `statement`
        // node; `collect_definitions` recurses through it regardless, so we
        // list the inner DDL node kinds directly. Only CREATE statements are
        // treated as definitions (mirrors other languages chunking
        // declarations, not usages) — ALTER/DROP/DML are left as gap text.
        FileType::Sql => Some(&[
            "create_table",
            "create_view",
            "create_materialized_view",
            "create_function",
            "create_trigger",
            "create_index",
            "create_type",
            "create_sequence",
            "create_schema",
            "create_role",
            "create_database",
            "create_extension",
        ]),
        _ => None,
    }
}

/// For Dart: `function_signature`, `getter_signature`, and `setter_signature` nodes
/// only cover the declaration line — the body is a separate sibling `function_body`
/// node. Walk forward through siblings to find and include it so that the full
/// function implementation is captured in the chunk.
fn extend_to_function_body(node: &Node) -> Option<(usize, usize)> {
    let mut cursor = node.next_sibling();
    while let Some(sib) = cursor {
        match sib.kind() {
            "function_body" => {
                return Some((sib.end_byte(), sib.end_position().row));
            }
            // Skip anonymous/punctuation siblings (whitespace tokens, "native" keyword, etc.)
            k if !sib.is_named() || k == "native" => {
                cursor = sib.next_sibling();
            }
            // Hit a different named node — stop searching
            _ => break,
        }
    }
    None
}

/// Node kinds whose span must be extended to include a following `function_body`
/// sibling. Applies only to Dart, where signature and body are separate grammar
/// nodes with no named parent wrapper.
const DART_SIGNATURE_KINDS: &[&str] =
    &["function_signature", "getter_signature", "setter_signature"];

/// Recursively walk the AST and collect definition nodes with structured metadata.
fn collect_definitions(node: Node, source: &[u8], kinds: &[&str], out: &mut Vec<AstSpan>) {
    let node_kind = node.kind();

    if kinds.contains(&node_kind) {
        let name = extract_name(&node, source).unwrap_or_else(|| "<anonymous>".to_string());
        let kind = classify_node_kind(node_kind);
        let node_text = ts_node_text(node, source);
        let mut meta = extract_structured_meta(node, source);
        meta.kind = Some(kind.label().to_string());

        // Layer 6: Semantic role (after all other metadata is available)
        meta.semantic_role =
            crate::chunking::semantic_role::classify_semantic_role(&meta, node_text);

        // Dart: signature nodes don't include the function body — extend span.
        let (end_byte, end_row) = if DART_SIGNATURE_KINDS.contains(&node_kind) {
            extend_to_function_body(&node).unwrap_or((node.end_byte(), node.end_position().row))
        } else {
            (node.end_byte(), node.end_position().row)
        };

        out.push(AstSpan {
            start_byte: node.start_byte(),
            end_byte,
            start_row: node.start_position().row,
            end_row,
            name,
            kind,
            meta,
        });
        // Continue recursing — nested definitions (e.g. methods in classes,
        // declarations in export_statement) are collected separately and
        // deduplicated later by the overlap-removal pass.
    }

    for i in 0..node.child_count() {
        if let Some(child) = node.child(i as u32) {
            collect_definitions(child, source, kinds, out);
        }
    }
}

/// Try to extract a name from a definition node
fn extract_name(node: &Node, source: &[u8]) -> Option<String> {
    // Try common field names first
    for field in &["name", "identifier"] {
        if let Some(child) = node.child_by_field_name(field) {
            let text = &source[child.start_byte()..child.end_byte()];
            return Some(String::from_utf8_lossy(text).to_string());
        }
    }

    // First pass: canonical identifier-like child kinds shared across grammars.
    // These win whenever they exist on the node.
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i as u32) {
            match child.kind() {
                "identifier" | "name" | "property_identifier" | "type_identifier" => {
                    let text = &source[child.start_byte()..child.end_byte()];
                    return Some(String::from_utf8_lossy(text).to_string());
                }
                _ => {}
            }
        }
    }

    // Second pass: language-specific identifier-like child kinds. Checked AFTER
    // the canonical kinds so that any grammar emitting both still picks the
    // canonical name first. Currently:
    //   - `value_name`: OCaml-specific. Emitted as a named child of `external`,
    //     `value_specification`, `value_path`, and `alias_pattern`. Of those,
    //     only `external` is in our definition_node_kinds for OCaml.
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i as u32)
            && child.kind() == "value_name"
        {
            let text = &source[child.start_byte()..child.end_byte()];
            return Some(String::from_utf8_lossy(text).to_string());
        }
    }

    // Third pass: `object_reference` (tree-sitter-sequel/SQL-specific). Most
    // `create_*` statement nodes don't carry a direct `name` field or bare
    // `identifier` child — the target object name is one level down, inside
    // an `object_reference` child that itself has a `name` field (e.g.
    // `create_table` -> `object_reference` -> name: `identifier`). Statements
    // with more than one `object_reference` (e.g. `create_trigger`, which also
    // references the table and function it attaches to) emit the defined
    // object's reference first, so the first match wins.
    for i in 0..node.child_count() {
        if let Some(child) = node.child(i as u32)
            && child.kind() == "object_reference"
            && let Some(name_node) = child.child_by_field_name("name")
        {
            let text = &source[name_node.start_byte()..name_node.end_byte()];
            return Some(String::from_utf8_lossy(text).to_string());
        }
    }

    None
}

/// Map tree-sitter node kind strings to AstNodeKind
fn classify_node_kind(kind: &str) -> AstNodeKind {
    match kind {
        "function_item"
        | "function_definition"
        | "function_declaration"
        | "function_signature"
        | "local_function_declaration"
        | "function"
        | "getter_signature"
        | "setter_signature"
        | "external"
        | "external_declaration"
        | "create_function" => AstNodeKind::Function,
        "method_definition"
        | "method_declaration"
        | "method"
        | "method_signature"
        | "constructor_signature" => AstNodeKind::Method,
        "class_definition" | "class_declaration" | "class_specifier" | "class" => {
            AstNodeKind::Class
        }
        // `create_table` (SQL): a table's columns are conceptually fields, so
        // it maps to Struct like other languages' record types.
        "struct_item" | "struct_specifier" | "struct_declaration" | "record_declaration"
        | "create_table" => AstNodeKind::Struct,
        "enum_item" | "enum_declaration" | "enum_definition" => AstNodeKind::Enum,
        "interface_declaration"
        | "trait_item"
        | "protocol_declaration"
        | "trait_definition"
        | "trait_declaration" => AstNodeKind::Interface,
        "mod_item"
        | "module"
        | "mixin_declaration"
        | "extension_declaration"
        | "namespace_declaration"
        | "file_scoped_namespace_declaration"
        | "module_definition"
        | "object_definition"
        | "create_schema" => AstNodeKind::Module,
        // `type_alias` (Dart, Haskell): always a true alias (`typedef Foo = Bar`).
        // `type_definition` (Scala 3, OCaml): umbrella for type-level declarations
        //   that may or may not be aliases — Scala 3 emits it for `type X = Y` and
        //   abstract type members; OCaml emits it for records (`type r = { x: int }`),
        //   sum types (`type t = A | B`), and aliases. Folding both into a single
        //   `type_alias` label mislabels OCaml records/variants and Scala 3 abstract
        //   types as aliases, so keep them distinct.
        "type_alias" => AstNodeKind::Other("type_alias".to_string()),
        "type_definition" => AstNodeKind::Other("type_definition".to_string()),
        "impl_item" => AstNodeKind::Other("impl".to_string()),
        "type_declaration" | "create_type" => AstNodeKind::Other("type".to_string()),
        "given_definition" => AstNodeKind::Other("given".to_string()),
        "extension_definition" => AstNodeKind::Other("extension".to_string()),
        // SQL DDL (tree-sitter-sequel): views/materialized views/indexes/etc.
        // have no close analogue in the shared AstNodeKind set, so they stay
        // Other(_). (create_table/create_function/create_schema/create_type
        // are folded into the shared arms above.)
        "create_view" | "create_materialized_view" => AstNodeKind::Other("view".to_string()),
        "create_trigger" => AstNodeKind::Other("trigger".to_string()),
        "create_index" => AstNodeKind::Other("index".to_string()),
        "create_sequence" => AstNodeKind::Other("sequence".to_string()),
        "create_role" => AstNodeKind::Other("role".to_string()),
        "create_database" => AstNodeKind::Other("database".to_string()),
        "create_extension" => AstNodeKind::Other("extension_stmt".to_string()),
        other => AstNodeKind::Other(other.to_string()),
    }
}

/// Convert a byte offset in content to a 1-based line number
fn byte_offset_to_line(content: &str, byte_offset: usize) -> u32 {
    let capped = byte_offset.min(content.len());
    content[..capped].bytes().filter(|&b| b == b'\n').count() as u32 + 1
}

/// Split a large AST node into smaller chunks using a sliding window.
/// Attaches structured metadata to the first sub-chunk only.
#[allow(clippy::too_many_arguments)]
fn split_large_node(
    path: &Path,
    text: &str,
    base_line: usize,
    name: &str,
    kind: &AstNodeKind,
    language: &str,
    chunk_size: usize,
    chunk_overlap: usize,
    meta: &StructuredChunkMeta,
) -> Vec<Chunk> {
    let chunk_chars = chunk_size * CHARS_PER_TOKEN;
    let overlap_chars = chunk_overlap * CHARS_PER_TOKEN;
    let step = if chunk_chars > overlap_chars {
        chunk_chars - overlap_chars
    } else {
        chunk_chars
    };

    let mut chunks = Vec::new();
    let mut offset = 0usize;
    let mut part = 0u32;
    let bytes = text.as_bytes();

    while offset < text.len() {
        let end = text.floor_char_boundary((offset + chunk_chars).min(text.len()));

        // Try to break at a line boundary
        let split_at = if end < text.len() {
            let search_start = if end > overlap_chars {
                text.floor_char_boundary(end - overlap_chars)
            } else {
                offset
            };
            let mut best = end;
            for i in (search_start..end).rev() {
                if bytes[i] == b'\n' {
                    best = i + 1;
                    break;
                }
            }
            best
        } else {
            end
        };

        let chunk_text = &text[offset..split_at];
        if !chunk_text.trim().is_empty() {
            let lines_before = text[..offset].bytes().filter(|&b| b == b'\n').count();
            let lines_in = chunk_text.bytes().filter(|&b| b == b'\n').count();
            let start_line = (base_line + lines_before) as u32;
            let end_line = (base_line + lines_before + lines_in) as u32;

            let part_name = if part == 0 {
                name.to_string()
            } else {
                format!("{name}[part {part}]")
            };

            // First sub-chunk gets full metadata; continuations get lightweight identity copy
            let chunk_meta = Some(Box::new(if part == 0 {
                meta.clone()
            } else {
                StructuredChunkMeta {
                    name: meta.name.clone(),
                    kind: meta.kind.clone(),
                    semantic_role: meta.semantic_role,
                    ..Default::default()
                }
            }));

            chunks.push(Chunk {
                id: 0,
                file_path: path.to_path_buf(),
                start_line,
                end_line: end_line.max(start_line),
                content: chunk_text.to_string(),
                chunk_type: ChunkType::AstNode {
                    name: part_name,
                    kind: kind.clone(),
                    language: language.to_string(),
                    structured_meta: chunk_meta,
                },
            });
            part += 1;
        }

        let new_offset = text.floor_char_boundary(offset + step.max(1));
        offset = if split_at > new_offset {
            if split_at > overlap_chars {
                text.floor_char_boundary(split_at - overlap_chars)
            } else {
                split_at
            }
        } else {
            new_offset
        };
    }

    chunks
}

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

    #[test]
    fn test_rust_function_chunking() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"
fn hello() {
    println!("hello");
}

fn world() {
    println!("world");
}
"#;
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        // Should find at least the two functions
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        assert_eq!(ast_chunks.len(), 2);
    }

    #[test]
    fn test_python_class_chunking() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"
class Greeter:
    def greet(self):
        print("hello")

def standalone():
    pass
"#;
        let chunks = chunker.chunk(Path::new("test.py"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        // class_definition contains the method, and standalone function
        assert!(ast_chunks.len() >= 2);
    }

    #[test]
    fn test_unsupported_extension_falls_back() {
        let chunker = AstChunker::new(256, 64);
        let content = "some yaml content\nkey: value\n";
        let chunks = chunker.chunk(Path::new("config.yaml"), content).unwrap();
        assert!(!chunks.is_empty());
        assert!(matches!(chunks[0].chunk_type, ChunkType::TextWindow { .. }));
    }

    #[test]
    fn test_ast_node_name_extraction() {
        let chunker = AstChunker::new(256, 64);
        let content = "fn my_function() { }\n";
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        let ast_chunk = chunks
            .iter()
            .find(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .unwrap();
        match &ast_chunk.chunk_type {
            ChunkType::AstNode { name, .. } => assert_eq!(name, "my_function"),
            _ => panic!("Expected AstNode"),
        }
    }

    #[test]
    fn test_dart_ast_chunking() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  final String title;

  @override
  Widget build(BuildContext context) {
    return Container();
  }

  void _helper() {
    print('hello');
  }
}

enum Color { red, green, blue }

void topLevelFunction() {
  print('top level');
}
";
        let chunks = chunker.chunk(Path::new("lib/main.dart"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        // Should find: class_definition, enum_declaration, function_signature
        assert!(
            ast_chunks.len() >= 3,
            "Expected at least 3 AST chunks, got {}: {:?}",
            ast_chunks.len(),
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );
        // Verify it produces AstNode chunks (not just TextWindow)
        let has_class = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => matches!(kind, AstNodeKind::Class),
            _ => false,
        });
        assert!(has_class, "Should find a class definition");
    }

    /// Verify that top-level Dart function bodies are included in the chunk text,
    /// not just the signature line. Regression test for the `function_signature`
    /// body-capture fix (extend_to_function_body).
    #[test]
    fn test_dart_function_body_captured() {
        let chunker = AstChunker::new(512, 64);
        let content = "void refreshToken(String userId) {\n  final token = fetchFromVault(userId);\n  return token;\n}\n\nString getSecret() {\n  return 'hunter2';\n}\n";
        let chunks = chunker.chunk(Path::new("lib/auth.dart"), content).unwrap();

        // Find the function chunk for refreshToken
        let fn_chunk = chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "refreshToken" && matches!(kind, AstNodeKind::Function)
            }
            _ => false,
        });
        assert!(
            fn_chunk.is_some(),
            "Should find refreshToken as an AstNode chunk"
        );
        // The chunk content must include the body, not just the signature
        assert!(
            fn_chunk.unwrap().content.contains("fetchFromVault"),
            "refreshToken chunk must contain body content, got: {}",
            fn_chunk.unwrap().content
        );

        // Same check for getSecret
        let secret_chunk = chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, .. } => name == "getSecret",
            _ => false,
        });
        assert!(
            secret_chunk.is_some(),
            "Should find getSecret as an AstNode chunk"
        );
        assert!(
            secret_chunk.unwrap().content.contains("hunter2"),
            "getSecret chunk must contain body, got: {}",
            secret_chunk.unwrap().content
        );
    }

    /// Verify setter_signature is captured with its body.
    #[test]
    fn test_dart_setter_captured() {
        let chunker = AstChunker::new(512, 64);
        let content = "class Cache {\n  int _size = 0;\n  set size(int val) {\n    _size = val.clamp(0, 1024);\n  }\n}\n";
        let chunks = chunker.chunk(Path::new("lib/cache.dart"), content).unwrap();
        // The class chunk should contain the setter body
        let class_chunk = chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => matches!(kind, AstNodeKind::Class),
            _ => false,
        });
        assert!(class_chunk.is_some(), "Should find Cache class");
        assert!(
            class_chunk.unwrap().content.contains("clamp"),
            "Class chunk should contain setter body"
        );
    }

    #[test]
    fn test_gap_content_becomes_text_window() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"// This is a file header comment
// with some info

use std::io;

fn hello() {
    println!("hello");
}
"#;
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        let has_text_window = chunks
            .iter()
            .any(|c| matches!(c.chunk_type, ChunkType::TextWindow { .. }));
        let has_ast_node = chunks
            .iter()
            .any(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }));
        assert!(has_text_window, "Should have TextWindow chunks for gaps");
        assert!(has_ast_node, "Should have AstNode chunks for functions");
    }

    #[test]
    fn test_structured_meta_extracted_for_rust_function() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
/// Adds two numbers.
fn add(a: i32, b: i32) -> i32 {
    a + b
}
";
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        let ast_chunk = chunks
            .iter()
            .find(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .unwrap();
        match &ast_chunk.chunk_type {
            ChunkType::AstNode {
                structured_meta: Some(meta),
                ..
            } => {
                assert_eq!(meta.name.as_deref(), Some("add"));
                assert!(meta.signature.is_some(), "Should have a signature");
                assert!(!meta.params.is_empty(), "Should have parameters");
                assert!(!meta.nl_summary.is_empty(), "Should have NL summary");
            }
            ChunkType::AstNode {
                structured_meta: None,
                ..
            } => panic!("Expected structured_meta to be Some"),
            _ => panic!("Expected AstNode"),
        }
    }

    #[test]
    fn test_structured_meta_call_graph() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"
fn caller() {
    callee();
}

fn callee() {
    println!("done");
}
"#;
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        let callee_chunk = chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, .. } => name == "callee",
            _ => false,
        });
        if let Some(chunk) = callee_chunk {
            match &chunk.chunk_type {
                ChunkType::AstNode {
                    structured_meta: Some(meta),
                    ..
                } => {
                    assert!(
                        meta.called_by.contains(&"caller".to_string()),
                        "callee should be called_by caller, got: {:?}",
                        meta.called_by
                    );
                }
                _ => panic!("Expected AstNode with structured_meta"),
            }
        }
    }

    #[test]
    fn test_vue_sfc_chunks_script_template_style() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"<template>
  <div class="hello">{{ msg }}</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
const msg = ref('hello')
</script>

<style scoped>
.hello { color: red; }
</style>
"#;
        let chunks = chunker.chunk(Path::new("App.vue"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        // Expect at least one chunk per <script>, <template>, <style> block.
        assert!(
            ast_chunks.len() >= 3,
            "Expected at least 3 AST chunks (script/template/style), got {}: {:?}",
            ast_chunks.len(),
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ocaml_external_declaration() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"
external sqrt : float -> float = "sqrt_C_func"

let double x = x * 2
"#;
        let chunks = chunker.chunk(Path::new("test.ml"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        let has_sqrt = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "sqrt" && matches!(kind, AstNodeKind::Function)
            }
            _ => false,
        });
        assert!(
            has_sqrt,
            "Should find external sqrt as Function: {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );
    }

    /// Regression test for the `value_name` precedence-ordering fix.
    ///
    /// `value_name` is an OCaml-specific identifier-like node kind. The fix
    /// reorders `extract_name` so that the canonical identifier kinds
    /// (`identifier`, `name`, `property_identifier`, `type_identifier`) are
    /// checked BEFORE `value_name`. That way, if a future grammar emits both
    /// kinds under the same definition node, the canonical kind wins.
    ///
    /// We exercise the invariant from two directions using real grammars:
    /// 1. Rust `fn my_function`: canonical "name" field is set on
    ///    `function_item`. The first pass returns "my_function" — `value_name`
    ///    is never consulted (it doesn't exist in the Rust grammar anyway).
    /// 2. OCaml `external sqrt`: `external` has no "name" field and no
    ///    canonical identifier-kind children, only a `value_name` child. The
    ///    second pass picks it up.
    ///
    /// Together, these confirm the two-pass precedence works as intended.
    #[test]
    fn test_extract_name_canonical_kinds_take_precedence_over_value_name() {
        let chunker = AstChunker::new(256, 64);

        // (1) Canonical kinds (Rust uses "name" field) — first pass wins.
        let rust_content = "fn my_function() { }\n";
        let rust_chunks = chunker.chunk(Path::new("t.rs"), rust_content).unwrap();
        let rust_name = rust_chunks
            .iter()
            .find_map(|c| match &c.chunk_type {
                ChunkType::AstNode { name, .. } => Some(name.clone()),
                _ => None,
            })
            .expect("expected an AstNode chunk for Rust");
        assert_eq!(
            rust_name, "my_function",
            "Rust function name should come from canonical 'name' field"
        );

        // (2) `value_name` fallback (OCaml `external`) — second pass picks it up.
        let ocaml_content = "external sqrt : float -> float = \"sqrt_C_func\"\n";
        let ocaml_chunks = chunker.chunk(Path::new("t.ml"), ocaml_content).unwrap();
        let ocaml_name = ocaml_chunks
            .iter()
            .find_map(|c| match &c.chunk_type {
                ChunkType::AstNode { name, .. } if name != "<anonymous>" => Some(name.clone()),
                _ => None,
            })
            .expect("expected a named AstNode chunk for OCaml external");
        assert_eq!(
            ocaml_name, "sqrt",
            "OCaml external should fall back to value_name in the second pass"
        );
    }

    #[test]
    fn test_scala3_given_extension_enum_type() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
trait Show[A]:
  def show(a: A): String

given showInt: Show[Int] with
  def show(a: Int): String = a.toString

enum Color:
  case Red, Green, Blue

extension (s: String)
  def kebab: String = s.replace(' ', '-')

type StringMap = Map[String, String]
";
        let chunks = chunker.chunk(Path::new("test.scala"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        assert!(
            !ast_chunks.is_empty(),
            "Should produce AST chunks for Scala 3 file"
        );

        let has_enum_color = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "Color" && matches!(kind, AstNodeKind::Enum)
            }
            _ => false,
        });
        assert!(
            has_enum_color,
            "Should find enum Color classified as Enum: {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );

        // given_definition -> Other("given")
        let has_given = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => {
                matches!(kind, AstNodeKind::Other(s) if s == "given")
            }
            _ => false,
        });
        assert!(
            has_given,
            "Should find given_definition classified as Other(given): {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );

        // extension_definition -> Other("extension")
        let has_extension = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => {
                matches!(kind, AstNodeKind::Other(s) if s == "extension")
            }
            _ => false,
        });
        assert!(
            has_extension,
            "Should find extension_definition classified as Other(extension)"
        );
    }

    /// Regression test for the `type_definition` vs `type_alias` split.
    ///
    /// Previously `classify_node_kind` folded both grammar kinds into a single
    /// `Other("type_alias")` label. That misrepresents:
    ///   - OCaml `type r = { x: int }` and `type t = A | B` (records and sum
    ///     types, both emitted as `type_definition`, not aliases).
    ///   - Scala 3 abstract type members (also `type_definition`).
    ///
    /// The fix keeps the two kinds distinct: `type_alias` -> `Other("type_alias")`
    /// (true aliases — Dart/Haskell), `type_definition` -> `Other("type_definition")`
    /// (umbrella term; covers Scala 3 + OCaml).
    ///
    /// tree-sitter-scala 0.26's grammar.js emits `type_definition` for any
    /// `type X = ...` (alias) AND for abstract type members. tree-sitter-ocaml
    /// 0.25 emits `type_definition` for records, variants, and aliases alike.
    #[test]
    fn test_type_definition_kept_distinct_from_type_alias() {
        let chunker = AstChunker::new(256, 64);

        // Scala 3 `type X = ...` — grammar emits `type_definition`, not `type_alias`.
        let scala_content = "type StringMap = Map[String, String]\n";
        let scala_chunks = chunker.chunk(Path::new("t.scala"), scala_content).unwrap();
        let scala_kind = scala_chunks.iter().find_map(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } if name == "StringMap" => Some(kind.clone()),
            _ => None,
        });
        assert!(
            matches!(scala_kind, Some(AstNodeKind::Other(ref s)) if s == "type_definition"),
            "Scala 3 `type X = ...` should chunk as Other(\"type_definition\"), got: {scala_kind:?}"
        );

        // OCaml record `type r = { x: int }` — grammar emits `type_definition`
        // (NOT `type_alias`); this is a record declaration, not an alias.
        let ocaml_content = "type point = { x : int; y : int }\n";
        let ocaml_chunks = chunker.chunk(Path::new("t.ml"), ocaml_content).unwrap();
        let ocaml_kind = ocaml_chunks.iter().find_map(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => match kind {
                AstNodeKind::Other(s) if s == "type_definition" || s == "type_alias" => {
                    Some(kind.clone())
                }
                _ => None,
            },
            _ => None,
        });
        assert!(
            matches!(ocaml_kind, Some(AstNodeKind::Other(ref s)) if s == "type_definition"),
            "OCaml `type point = {{ ... }}` (a record) must NOT be labeled \
             type_alias; expected Other(\"type_definition\"), got: {ocaml_kind:?}"
        );
    }

    #[test]
    fn test_csharp_record_and_file_scoped_namespace() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
namespace Foo.Bar;

public record Person(string Name, int Age);

public class Other {
    public void Method() { }
}
";
        let chunks = chunker.chunk(Path::new("test.cs"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        // Expect at least: namespace, record, class, method
        assert!(
            ast_chunks.len() >= 3,
            "Expected at least 3 AST chunks (namespace, record, class), got {}: {:?}",
            ast_chunks.len(),
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );

        let has_record = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "Person" && matches!(kind, AstNodeKind::Struct)
            }
            _ => false,
        });
        assert!(has_record, "Should find record Person classified as Struct");

        let has_file_scoped_ns = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { kind, .. } => matches!(kind, AstNodeKind::Module),
            _ => false,
        });
        assert!(
            has_file_scoped_ns,
            "Should find file-scoped namespace as Module"
        );
    }

    #[test]
    fn test_structured_meta_control_flow() {
        let chunker = AstChunker::new(256, 64);
        let content = r#"
fn complex(x: i32) -> i32 {
    if x > 0 {
        for i in 0..x {
            println!("{}", i);
        }
        x
    } else {
        0
    }
}
"#;
        let chunks = chunker.chunk(Path::new("test.rs"), content).unwrap();
        let ast_chunk = chunks
            .iter()
            .find(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .unwrap();
        match &ast_chunk.chunk_type {
            ChunkType::AstNode {
                structured_meta: Some(meta),
                ..
            } => {
                assert!(meta.has_branches, "Should detect branches");
                assert!(meta.has_loops, "Should detect loops");
                assert!(meta.complexity >= 3, "Complexity should be >= 3");
            }
            _ => panic!("Expected AstNode with structured_meta"),
        }
    }

    /// Regression test: constructor invocations (`new Foo()`) must produce
    /// call-graph edges so that callers of `new`-instantiated types are found
    /// by the structural route.
    ///
    /// Covers:
    ///   - `new_expression` node kind (TypeScript/JavaScript)
    ///   - `object_creation_expression` node kind (Java / C#) — same code
    ///     path, covered by the Java sub-test below.
    #[test]
    fn test_constructor_call_edges_new_expression() {
        let chunker = AstChunker::new(512, 64);

        // --- TypeScript: `new Foo()` inside a function ---
        let ts_content = r#"
class Foo {
  greet(): string { return "hi"; }
}

function bar(): Foo {
  return new Foo();
}
"#;
        let ts_chunks = chunker.chunk(Path::new("test.ts"), ts_content).unwrap();

        // The `Foo` class chunk should record that `bar` calls it.
        let foo_chunk = ts_chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, .. } => name == "Foo",
            _ => false,
        });
        assert!(foo_chunk.is_some(), "Should find a Foo class chunk in TS");
        if let Some(chunk) = foo_chunk {
            match &chunk.chunk_type {
                ChunkType::AstNode {
                    structured_meta: Some(meta),
                    ..
                } => {
                    assert!(
                        meta.called_by.contains(&"bar".to_string()),
                        "Foo should be called_by bar (via new Foo()), got called_by: {:?}",
                        meta.called_by
                    );
                }
                _ => panic!("Expected AstNode with structured_meta for Foo"),
            }
        }

        // --- TypeScript: qualified `new ns.Cache()` — rightmost ident matches ---
        let ts_qualified = r"
class Cache {
  get(k: string): string { return k; }
}

function init(): Cache {
  return new Cache();
}
";
        let ts_q_chunks = chunker.chunk(Path::new("cache.ts"), ts_qualified).unwrap();
        let cache_chunk = ts_q_chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, .. } => name == "Cache",
            _ => false,
        });
        assert!(cache_chunk.is_some(), "Should find Cache class chunk");
        if let Some(chunk) = cache_chunk {
            match &chunk.chunk_type {
                ChunkType::AstNode {
                    structured_meta: Some(meta),
                    ..
                } => {
                    assert!(
                        meta.called_by.contains(&"init".to_string()),
                        "Cache should be called_by init (via new Cache()), got: {:?}",
                        meta.called_by
                    );
                }
                _ => panic!("Expected AstNode with structured_meta for Cache"),
            }
        }

        // --- Java: `new` uses `object_creation_expression` ---
        // The Java chunker keeps the outermost span (class_declaration wins
        // over nested method_declaration), so `App` is the chunk that contains
        // `new Widget()` and gets recorded as the caller.
        let java_content = r"
class Widget {
    void draw() {}
}

class App {
    Widget makeWidget() {
        return new Widget();
    }
}
";
        let java_chunks = chunker.chunk(Path::new("App.java"), java_content).unwrap();
        let widget_chunk = java_chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, .. } => name == "Widget",
            _ => false,
        });
        assert!(
            widget_chunk.is_some(),
            "Should find Widget class chunk in Java"
        );
        if let Some(chunk) = widget_chunk {
            match &chunk.chunk_type {
                ChunkType::AstNode {
                    structured_meta: Some(meta),
                    ..
                } => {
                    assert!(
                        !meta.called_by.is_empty(),
                        "Widget should have at least one caller via new Widget(), got called_by: {:?}",
                        meta.called_by
                    );
                    // The caller is App (outermost Java class chunk; method_declaration
                    // is nested and deduped out). Verify the edge exists, not absent.
                    assert!(
                        meta.called_by.contains(&"App".to_string()),
                        "Widget should be called_by App (contains new Widget()), got: {:?}",
                        meta.called_by
                    );
                }
                _ => panic!("Expected AstNode with structured_meta for Widget"),
            }
        }
    }

    /// Regression test for the SQL grammar wiring: `tree-sitter-sequel`
    /// (see Cargo.toml) provides the LANGUAGE compatible with our
    /// `tree-sitter 0.26.9`. This asserts SQL files produce real `AstNode`
    /// chunks (CREATE TABLE, CREATE FUNCTION, ...), not a silent TextWindow
    /// fallback.
    #[test]
    fn test_sql_ast_chunking_not_text_fallback() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE
);

CREATE FUNCTION add_numbers(a INTEGER, b INTEGER) RETURNS INTEGER AS $$
BEGIN
    RETURN a + b;
END;
$$ LANGUAGE plpgsql;

CREATE VIEW active_users AS
SELECT * FROM users WHERE active = true;

CREATE INDEX idx_users_email ON users (email);

SELECT * FROM users;
";
        let chunks = chunker.chunk(Path::new("schema.sql"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();
        assert!(
            !ast_chunks.is_empty(),
            "SQL must produce AstNode chunks, not fall back to text chunking"
        );

        let table_chunk = ast_chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "users" && matches!(kind, AstNodeKind::Struct)
            }
            _ => false,
        });
        assert!(
            table_chunk.is_some(),
            "Should find CREATE TABLE users as a Struct-kind AstNode: {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );
        assert!(
            table_chunk.unwrap().content.contains("PRIMARY KEY"),
            "Table chunk should contain its column definitions"
        );

        let function_chunk = ast_chunks.iter().find(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "add_numbers" && matches!(kind, AstNodeKind::Function)
            }
            _ => false,
        });
        assert!(
            function_chunk.is_some(),
            "Should find CREATE FUNCTION add_numbers as a Function-kind AstNode"
        );
        assert!(
            function_chunk.unwrap().content.contains("RETURN a + b"),
            "Function chunk should contain the function body"
        );

        let view_chunk = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "active_users" && matches!(kind, AstNodeKind::Other(s) if s == "view")
            }
            _ => false,
        });
        assert!(
            view_chunk,
            "Should find CREATE VIEW active_users as Other(\"view\")"
        );

        let index_chunk = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "idx_users_email" && matches!(kind, AstNodeKind::Other(s) if s == "index")
            }
            _ => false,
        });
        assert!(
            index_chunk,
            "Should find CREATE INDEX idx_users_email as Other(\"index\")"
        );

        // The bare trailing `SELECT * FROM users;` is not a definition — it
        // should surface as gap text, not a spurious AstNode.
        let has_text_window = chunks
            .iter()
            .any(|c| matches!(c.chunk_type, ChunkType::TextWindow { .. }));
        assert!(
            has_text_window,
            "The standalone SELECT statement should remain a TextWindow gap chunk"
        );
    }

    /// Additional SQL DDL coverage beyond the core CREATE TABLE/FUNCTION case:
    /// CREATE SCHEMA (-> Module) and CREATE TRIGGER, whose `object_reference`
    /// name-extraction must pick the trigger's own name, not the table or
    /// function it references.
    #[test]
    fn test_sql_schema_and_trigger_naming() {
        let chunker = AstChunker::new(256, 64);
        let content = r"
CREATE SCHEMA analytics;

CREATE TRIGGER audit_trigger AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION log_change();
";
        let chunks = chunker.chunk(Path::new("schema.sql"), content).unwrap();
        let ast_chunks: Vec<_> = chunks
            .iter()
            .filter(|c| matches!(c.chunk_type, ChunkType::AstNode { .. }))
            .collect();

        let has_schema = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "analytics" && matches!(kind, AstNodeKind::Module)
            }
            _ => false,
        });
        assert!(
            has_schema,
            "Should find CREATE SCHEMA analytics as a Module-kind AstNode: {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );

        let has_trigger = ast_chunks.iter().any(|c| match &c.chunk_type {
            ChunkType::AstNode { name, kind, .. } => {
                name == "audit_trigger" && matches!(kind, AstNodeKind::Other(s) if s == "trigger")
            }
            _ => false,
        });
        assert!(
            has_trigger,
            "Trigger name extraction must pick the trigger's own name (first \
             object_reference), not the referenced table/function: {:?}",
            ast_chunks.iter().map(|c| &c.chunk_type).collect::<Vec<_>>()
        );
    }
}