cxpak 1.0.1

Spends CPU cycles so you don't spend tokens. The LLM gets a briefing packet instead of a flashlight in a dark room.
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
// SQL column-level extraction, CQL, Cypher regex, Elasticsearch JSON pattern

use crate::schema::{
    ColumnSchema, DbFunctionSchema, ForeignKeyRef, OrmFieldSchema, OrmFramework, OrmModelSchema,
    TableSchema, ViewSchema,
};
use regex::Regex;

// ---------------------------------------------------------------------------
// Task 5 – SQL column-level extraction
// ---------------------------------------------------------------------------

/// Extract the content between the first `(` and its matching `)`, tracking
/// paren depth so nested parens are handled correctly.
fn extract_paren_body(sql: &str) -> Option<&str> {
    let start = sql.find('(')?;
    let bytes = sql.as_bytes();
    let mut depth = 0usize;
    let mut end = None;
    for (i, &b) in bytes.iter().enumerate().skip(start) {
        match b {
            b'(' => depth += 1,
            b')' => {
                depth -= 1;
                if depth == 0 {
                    end = Some(i);
                    break;
                }
            }
            _ => {}
        }
    }
    let end = end?;
    Some(&sql[start + 1..end])
}

/// Split a comma-separated list of column/constraint definitions at the **top
/// paren level only**, so `DECIMAL(10,2)` and `CHECK(a,b)` are kept intact.
fn split_top_level_commas(body: &str) -> Vec<&str> {
    let mut result = Vec::new();
    let mut depth = 0usize;
    let mut start = 0;
    let bytes = body.as_bytes();
    for (i, &b) in bytes.iter().enumerate() {
        match b {
            b'(' => depth += 1,
            b')' => {
                depth = depth.saturating_sub(1);
            }
            b',' if depth == 0 => {
                result.push(&body[start..i]);
                start = i + 1;
            }
            _ => {}
        }
    }
    result.push(&body[start..]);
    result
}

/// Constraint keywords that terminate the column type token stream.
const CONSTRAINT_KEYWORDS: &[&str] = &[
    "NOT",
    "NULL",
    "UNIQUE",
    "PRIMARY",
    "DEFAULT",
    "REFERENCES",
    "CHECK",
    "CONSTRAINT",
    "COLLATE",
    "GENERATED",
    "AUTO_INCREMENT",
];

/// Return true if the token (upper-cased) is a constraint keyword.
fn is_constraint_kw(tok: &str) -> bool {
    let up = tok.to_uppercase();
    CONSTRAINT_KEYWORDS.contains(&up.as_str())
}

/// Consume tokens that form a type, including any paren group that follows
/// (e.g. `VARCHAR(255)`, `DECIMAL(10,2)`).  Advances `idx` past consumed
/// tokens and returns the collected type string.
fn collect_type(tokens: &[&str], idx: &mut usize) -> String {
    let mut parts: Vec<String> = Vec::new();

    while *idx < tokens.len() {
        let tok = tokens[*idx];
        if is_constraint_kw(tok) {
            break;
        }
        // Accumulate this token into the type string.
        // If the token itself contains an opening paren we must also absorb
        // the matching closing paren (it may span several tokens due to spaces
        // inside parens being a separate token — but in practice SQL types keep
        // the paren-group in one token like `VARCHAR(255)` after trimming).
        parts.push(tok.to_string());
        *idx += 1;

        // If the last part we added contains an open paren but no matching
        // close paren, keep consuming until we close it.
        let joined = parts.join(" ");
        let open = joined.chars().filter(|&c| c == '(').count();
        let close = joined.chars().filter(|&c| c == ')').count();
        if open > close {
            // Absorb tokens until parens balance.
            while *idx < tokens.len() {
                let next = tokens[*idx];
                parts.push(next.to_string());
                *idx += 1;
                let j2 = parts.join(" ");
                let o2 = j2.chars().filter(|&c| c == '(').count();
                let c2 = j2.chars().filter(|&c| c == ')').count();
                if o2 == c2 {
                    break;
                }
            }
        }
    }
    parts.join(" ")
}

/// Parse a REFERENCES clause starting at `idx` (which points to the token
/// *after* `REFERENCES`).  Returns a `ForeignKeyRef`.
fn parse_references(tokens: &[&str], idx: &mut usize) -> ForeignKeyRef {
    if *idx >= tokens.len() {
        return ForeignKeyRef {
            target_table: String::new(),
            target_column: String::new(),
        };
    }
    // The next token is `table_name` or `schema.table_name` or
    // `schema.table_name(col_name)` or `table_name(col_name)`.
    let raw = tokens[*idx];
    *idx += 1;

    // Strip any trailing ON DELETE / ON UPDATE that may have been lumped in.
    // Split at '(' to separate table from column.
    if let Some(paren_pos) = raw.find('(') {
        let table = raw[..paren_pos].to_string();
        // Extract column up to closing ')'
        let rest = &raw[paren_pos + 1..];
        let col = rest.trim_end_matches(')').to_string();
        ForeignKeyRef {
            target_table: table,
            target_column: col,
        }
    } else {
        // No paren – either `table` or `table` followed by `(col)` as a
        // separate token.
        if *idx < tokens.len() && tokens[*idx].starts_with('(') {
            let col_tok = tokens[*idx];
            *idx += 1;
            let col = col_tok
                .trim_start_matches('(')
                .trim_end_matches(')')
                .to_string();
            ForeignKeyRef {
                target_table: raw.to_string(),
                target_column: col,
            }
        } else {
            ForeignKeyRef {
                target_table: raw.to_string(),
                target_column: String::new(),
            }
        }
    }
}

/// Capture the DEFAULT expression (could be a bare token or a paren-wrapped
/// expression).
fn capture_default(tokens: &[&str], idx: &mut usize) -> String {
    if *idx >= tokens.len() {
        return String::new();
    }
    let tok = tokens[*idx];
    *idx += 1;

    // If it starts with '(' we need to absorb until balanced.
    if tok.starts_with('(') {
        let mut expr = tok.to_string();
        let mut open = expr.chars().filter(|&c| c == '(').count();
        let mut close = expr.chars().filter(|&c| c == ')').count();
        while open > close && *idx < tokens.len() {
            let next = tokens[*idx];
            *idx += 1;
            expr.push(' ');
            expr.push_str(next);
            open = expr.chars().filter(|&c| c == '(').count();
            close = expr.chars().filter(|&c| c == ')').count();
        }
        expr
    } else {
        tok.to_string()
    }
}

/// Parse a single column definition line into a `ColumnSchema`.
/// Returns `None` if the line is a table-level constraint that should be
/// skipped.
fn parse_column_def(line: &str) -> Option<ColumnSchema> {
    let trimmed = line.trim();
    if trimmed.is_empty() {
        return None;
    }
    let upper = trimmed.to_uppercase();

    // Skip table-level constraint lines.
    for kw in &[
        "PRIMARY KEY",
        "FOREIGN KEY",
        "CHECK",
        "CONSTRAINT",
        "UNIQUE",
    ] {
        if upper.starts_with(kw) {
            return None;
        }
    }

    // Tokenise by whitespace (we keep paren-groups together only if they're
    // already in one token, which is typical for `VARCHAR(255)`).
    // We need a smarter tokeniser that keeps paren groups attached to their
    // preceding token when there's no space before the paren.
    let tokens: Vec<&str> = trimmed.split_whitespace().collect();
    if tokens.is_empty() {
        return None;
    }

    let mut idx = 0usize;

    // Column name – possibly quoted.
    let raw_name = tokens[idx];
    idx += 1;
    let col_name = raw_name.trim_matches('"').to_string();

    // Column type.
    let data_type = collect_type(&tokens, &mut idx);

    // Remaining tokens are constraint keywords.
    let mut nullable = true;
    let mut unique = false;
    let mut default: Option<String> = None;
    let mut foreign_key: Option<ForeignKeyRef> = None;
    let mut constraints: Vec<String> = Vec::new();
    let mut is_pk = false;

    while idx < tokens.len() {
        let tok_upper = tokens[idx].to_uppercase();
        match tok_upper.as_str() {
            "NOT" => {
                idx += 1;
                if idx < tokens.len() && tokens[idx].to_uppercase() == "NULL" {
                    nullable = false;
                    idx += 1;
                }
            }
            "NULL" => {
                nullable = true;
                idx += 1;
            }
            "UNIQUE" => {
                unique = true;
                constraints.push("UNIQUE".to_string());
                idx += 1;
            }
            "PRIMARY" => {
                idx += 1;
                if idx < tokens.len() && tokens[idx].to_uppercase() == "KEY" {
                    is_pk = true;
                    constraints.push("PRIMARY KEY".to_string());
                    idx += 1;
                }
            }
            "DEFAULT" => {
                idx += 1;
                let val = capture_default(&tokens, &mut idx);
                default = Some(val);
            }
            "REFERENCES" => {
                idx += 1;
                foreign_key = Some(parse_references(&tokens, &mut idx));
                // Skip trailing ON DELETE / ON UPDATE clauses.
                while idx < tokens.len() {
                    let t = tokens[idx].to_uppercase();
                    if t == "ON" {
                        idx += 1; // consume ON
                        if idx < tokens.len() {
                            idx += 1; // consume DELETE/UPDATE
                        }
                        if idx < tokens.len() {
                            idx += 1; // consume CASCADE/SET/RESTRICT etc.
                        }
                        // Handle SET NULL (two words)
                        if idx < tokens.len()
                            && (tokens[idx].to_uppercase() == "NULL"
                                || tokens[idx].to_uppercase() == "DEFAULT")
                        {
                            idx += 1;
                        }
                    } else {
                        break;
                    }
                }
            }
            "CHECK" | "CONSTRAINT" | "COLLATE" | "GENERATED" | "AUTO_INCREMENT" => {
                // Skip the rest – these are terminal for column parsing.
                idx += 1;
            }
            _ => {
                idx += 1;
            }
        }
    }

    if unique && !is_pk {
        // Already pushed "UNIQUE" above.
    }

    Some(ColumnSchema {
        name: col_name,
        data_type,
        nullable,
        default,
        constraints,
        foreign_key,
        // is_pk stored separately; caller sets primary_key on TableSchema
        // by checking constraints or the "PRIMARY KEY" table-level clause.
        // We use a temporary field trick: add pk marker to constraints so
        // caller can extract it.  Actually we already push "PRIMARY KEY" to
        // constraints when is_pk is true.
    })
}

/// Extract a `TableSchema` from the body text of a CREATE TABLE statement.
///
/// `body` is the full SQL from (and including) the opening `(`.
pub fn extract_table_schema(
    body: &str,
    table_name: &str,
    file_path: &str,
    start_line: usize,
) -> TableSchema {
    let inner = match extract_paren_body(body) {
        Some(s) => s,
        None => {
            return TableSchema {
                name: table_name.to_string(),
                columns: Vec::new(),
                primary_key: None,
                indexes: Vec::new(),
                file_path: file_path.to_string(),
                start_line,
            }
        }
    };

    let defs = split_top_level_commas(inner);

    let mut columns: Vec<ColumnSchema> = Vec::new();
    let mut table_pk: Option<Vec<String>> = None;
    // (table_name, col_name, target_table, target_col) for table-level FK
    let mut table_fks: Vec<(String, String, String)> = Vec::new();

    for def in &defs {
        let trimmed = def.trim();
        let upper = trimmed.to_uppercase();

        if upper.starts_with("PRIMARY KEY") {
            // PRIMARY KEY (col1, col2, ...)
            if let Some(pk_body) = extract_paren_body(trimmed) {
                let pk_cols: Vec<String> = pk_body
                    .split(',')
                    .map(|c| c.trim().trim_matches('"').to_string())
                    .filter(|c| !c.is_empty())
                    .collect();
                if !pk_cols.is_empty() {
                    table_pk = Some(pk_cols);
                }
            }
            continue;
        }

        if upper.starts_with("FOREIGN KEY") {
            // FOREIGN KEY (col) REFERENCES target_table(target_col)
            let fk_col = extract_paren_body(trimmed)
                .map(|s| s.trim().trim_matches('"').to_string())
                .unwrap_or_default();
            // Find REFERENCES keyword after the first ')'.
            let after_paren = trimmed.find(')').map(|i| &trimmed[i + 1..]).unwrap_or("");
            let upper_after = after_paren.to_uppercase();
            if let Some(ref_pos) = upper_after.find("REFERENCES") {
                let ref_part = after_paren[ref_pos + "REFERENCES".len()..].trim();
                let tokens: Vec<&str> = ref_part.split_whitespace().collect();
                let mut idx = 0usize;
                let fk_ref = parse_references(&tokens, &mut idx);
                table_fks.push((fk_col, fk_ref.target_table, fk_ref.target_column));
            }
            continue;
        }

        if upper.starts_with("CHECK")
            || upper.starts_with("CONSTRAINT")
            || upper.starts_with("UNIQUE")
        {
            continue;
        }

        if let Some(col) = parse_column_def(trimmed) {
            columns.push(col);
        }
    }

    // Determine primary_key: prefer table-level, else gather from column constraints.
    let pk = if table_pk.is_some() {
        table_pk
    } else {
        let pk_cols: Vec<String> = columns
            .iter()
            .filter(|c| c.constraints.contains(&"PRIMARY KEY".to_string()))
            .map(|c| c.name.clone())
            .collect();
        if pk_cols.is_empty() {
            None
        } else {
            Some(pk_cols)
        }
    };

    // Mark pk columns as not-nullable (implicit for primary keys).
    if let Some(ref pk_cols) = pk {
        for col in &mut columns {
            if pk_cols.contains(&col.name) {
                col.nullable = false;
            }
        }
    }

    // Apply table-level foreign keys to matching columns.
    for (fk_col, target_table, target_column) in table_fks {
        if let Some(col) = columns.iter_mut().find(|c| c.name == fk_col) {
            col.foreign_key = Some(ForeignKeyRef {
                target_table,
                target_column,
            });
        }
    }

    TableSchema {
        name: table_name.to_string(),
        columns,
        primary_key: pk,
        indexes: Vec::new(),
        file_path: file_path.to_string(),
        start_line,
    }
}

/// Extract a `ViewSchema` from a SELECT body, collecting referenced table names.
pub fn extract_view_schema(body: &str, view_name: &str, file_path: &str) -> ViewSchema {
    // Words that appear immediately after FROM or JOIN are table names.
    let re = Regex::new(r"(?i)\b(?:FROM|JOIN)\s+([a-zA-Z_][a-zA-Z0-9_.]*)").unwrap();
    let mut source_tables: Vec<String> = Vec::new();
    for cap in re.captures_iter(body) {
        let tbl = cap[1].to_string();
        if !source_tables.contains(&tbl) {
            source_tables.push(tbl);
        }
    }
    ViewSchema {
        name: view_name.to_string(),
        source_tables,
        file_path: file_path.to_string(),
    }
}

/// Extract a `DbFunctionSchema` from function body text.
pub fn extract_function_schema(body: &str, func_name: &str, file_path: &str) -> DbFunctionSchema {
    let re =
        Regex::new(r"(?i)\b(?:FROM|JOIN|INTO|UPDATE|TABLE)\s+([a-zA-Z_][a-zA-Z0-9_.]*)").unwrap();
    let mut referenced_tables: Vec<String> = Vec::new();
    for cap in re.captures_iter(body) {
        let tbl = cap[1].to_string();
        if !referenced_tables.contains(&tbl) {
            referenced_tables.push(tbl);
        }
    }
    DbFunctionSchema {
        name: func_name.to_string(),
        referenced_tables,
        file_path: file_path.to_string(),
    }
}

// ---------------------------------------------------------------------------
// Task 6 – CQL, Cypher, Elasticsearch extraction
// ---------------------------------------------------------------------------

// CQL: CREATE TABLE is SQL-compatible; the WITH clause lives after the closing
// `)` so it is already ignored by `extract_table_schema`.
// No additional CQL-specific code is needed.

/// An entry extracted from a Cypher schema file.
#[derive(Debug, Clone, PartialEq)]
pub struct CypherEntry {
    pub labels: Vec<String>,
    pub entry_type: String, // "constraint", "index", "node", "relationship"
}

impl CypherEntry {
    pub fn contains_label(&self, label: &str) -> bool {
        self.labels.iter().any(|l| l == label)
    }
}

/// Extract Cypher schema entries from file content.
pub fn extract_cypher_schema(content: &str, _file_path: &str) -> Vec<CypherEntry> {
    let mut entries: Vec<CypherEntry> = Vec::new();

    // CREATE CONSTRAINT ... FOR (alias:Label)
    let constraint_re =
        Regex::new(r"(?i)CREATE\s+CONSTRAINT\s+\w+\s+(?:ON|FOR)\s+\(\w+:(\w+)\)").unwrap();
    for cap in constraint_re.captures_iter(content) {
        entries.push(CypherEntry {
            labels: vec![cap[1].to_string()],
            entry_type: "constraint".to_string(),
        });
    }

    // CREATE INDEX ... FOR (alias:Label)
    let index_re = Regex::new(r"(?i)CREATE\s+INDEX\s+\w+\s+(?:ON|FOR)\s+\(\w+:(\w+)\)").unwrap();
    for cap in index_re.captures_iter(content) {
        entries.push(CypherEntry {
            labels: vec![cap[1].to_string()],
            entry_type: "index".to_string(),
        });
    }

    // Bare node label patterns: (alias:Label) — not already captured above.
    let node_re = Regex::new(r"\(\w+:(\w+)\)").unwrap();
    let mut seen_labels: Vec<String> = entries
        .iter()
        .flat_map(|e| e.labels.iter().cloned())
        .collect();
    for cap in node_re.captures_iter(content) {
        let label = cap[1].to_string();
        if !seen_labels.contains(&label) {
            seen_labels.push(label.clone());
            entries.push(CypherEntry {
                labels: vec![label],
                entry_type: "node".to_string(),
            });
        }
    }

    // Relationship types: -[:TYPE]->
    let rel_re = Regex::new(r"-\[:(\w+)\]->").unwrap();
    for cap in rel_re.captures_iter(content) {
        entries.push(CypherEntry {
            labels: vec![cap[1].to_string()],
            entry_type: "relationship".to_string(),
        });
    }

    entries
}

/// A field extracted from an Elasticsearch mapping.
#[derive(Debug, Clone, PartialEq)]
pub struct ElasticsearchField {
    pub name: String,
    pub field_type: String,
}

/// An Elasticsearch index mapping schema.
#[derive(Debug, Clone, PartialEq)]
pub struct ElasticsearchSchema {
    pub fields: Vec<ElasticsearchField>,
}

/// Parse an Elasticsearch JSON mapping document.
///
/// Looks for `mappings.properties`, then recursively flattens nested
/// properties using dot notation.  Returns `None` for non-ES JSON.
pub fn extract_elasticsearch_schema(
    content: &str,
    _file_path: &str,
) -> Option<ElasticsearchSchema> {
    let v: serde_json::Value = serde_json::from_str(content).ok()?;

    // Navigate: { "mappings": { "properties": { … } } }
    // Also accept top-level { "properties": { … } } for shorthand mappings.
    let properties = v
        .get("mappings")
        .and_then(|m| m.get("properties"))
        .or_else(|| v.get("properties"))?;

    if !properties.is_object() {
        return None;
    }

    let mut fields = Vec::new();
    collect_es_fields(properties, "", &mut fields);

    Some(ElasticsearchSchema { fields })
}

/// Recursively collect Elasticsearch fields, flattening nested properties.
fn collect_es_fields(
    properties: &serde_json::Value,
    prefix: &str,
    fields: &mut Vec<ElasticsearchField>,
) {
    let map = match properties.as_object() {
        Some(m) => m,
        None => return,
    };

    for (key, value) in map {
        let full_name = if prefix.is_empty() {
            key.clone()
        } else {
            format!("{}.{}", prefix, key)
        };

        let field_type = value
            .get("type")
            .and_then(|t| t.as_str())
            .unwrap_or("object")
            .to_string();

        fields.push(ElasticsearchField {
            name: full_name.clone(),
            field_type,
        });

        // Recurse into nested properties.
        if let Some(nested) = value.get("properties") {
            collect_es_fields(nested, &full_name, fields);
        }
    }
}

// ---------------------------------------------------------------------------
// Task 7 – Prisma schema enrichment
// ---------------------------------------------------------------------------

/// Scalar types built into Prisma – anything else is a relation model.
const PRISMA_SCALAR_TYPES: &[&str] = &[
    "String",
    "Boolean",
    "Int",
    "BigInt",
    "Float",
    "Decimal",
    "DateTime",
    "Json",
    "Bytes",
    "Unsupported",
];

/// Parse a Prisma model body and return an `OrmModelSchema`.
pub fn extract_prisma_schema(
    body: &str,
    model_name: &str,
    file_path: &str,
    start_line: usize,
) -> OrmModelSchema {
    // Determine table name: check for @@map("...") directive.
    let map_re = Regex::new(r#"@@map\("([^"]+)"\)"#).unwrap();
    let table_name = if let Some(cap) = map_re.captures(body) {
        cap[1].to_string()
    } else {
        model_name.to_lowercase()
    };

    let mut fields: Vec<OrmFieldSchema> = Vec::new();

    for line in body.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with("//") || trimmed.starts_with("@@") {
            continue;
        }

        // Each field line: `name  Type  [@modifiers ...]`
        // Use split_whitespace to handle multiple spaces between tokens.
        let tokens: Vec<&str> = trimmed.split_whitespace().collect();
        if tokens.len() < 2 {
            continue;
        }

        let field_name = tokens[0];
        let raw_type = tokens[1];
        // Everything from token 2 onwards is modifiers / annotations.
        let modifiers = if tokens.len() > 2 {
            tokens[2..].join(" ")
        } else {
            String::new()
        };

        // Strip `?` (optional) and `[]` (array/relation) suffixes for analysis.
        let is_array = raw_type.ends_with("[]");
        let base_type = raw_type.trim_end_matches('?').trim_end_matches("[]");

        // Determine if it's a relation.
        let is_scalar = PRISMA_SCALAR_TYPES.contains(&base_type);
        let is_relation =
            is_array || (!is_scalar && base_type.chars().next().is_some_and(|c| c.is_uppercase()));

        let related_model = if is_relation {
            Some(base_type.to_string())
        } else {
            None
        };

        // Check for @id, @unique, @default, @relation.
        let mut constraints: Vec<String> = Vec::new();
        if modifiers.contains("@id") {
            constraints.push("PRIMARY KEY".to_string());
        }
        if modifiers.contains("@unique") {
            constraints.push("UNIQUE".to_string());
        }
        // @default(...) – presence is all we need for OrmFieldSchema.
        // @relation(...) – already captured via is_relation.

        fields.push(OrmFieldSchema {
            name: field_name.to_string(),
            field_type: raw_type.to_string(),
            is_relation,
            related_model,
        });
    }

    // Suppress the unused start_line warning.
    let _ = start_line;

    OrmModelSchema {
        class_name: model_name.to_string(),
        table_name,
        framework: OrmFramework::Prisma,
        file_path: file_path.to_string(),
        fields,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -----------------------------------------------------------------------
    // Task 5 – SQL column-level extraction
    // -----------------------------------------------------------------------

    #[test]
    fn test_basic_columns() {
        let sql = "(id INTEGER, name TEXT)";
        let schema = extract_table_schema(sql, "users", "schema.sql", 1);
        assert_eq!(schema.name, "users");
        assert_eq!(schema.columns.len(), 2);
        assert_eq!(schema.columns[0].name, "id");
        assert_eq!(schema.columns[0].data_type, "INTEGER");
        assert_eq!(schema.columns[1].name, "name");
        assert_eq!(schema.columns[1].data_type, "TEXT");
    }

    #[test]
    fn test_inline_primary_key() {
        let sql = "(id SERIAL PRIMARY KEY, name TEXT)";
        let schema = extract_table_schema(sql, "users", "schema.sql", 1);
        assert_eq!(schema.primary_key, Some(vec!["id".to_string()]));
        // pk column is implicitly not nullable
        let id_col = schema.columns.iter().find(|c| c.name == "id").unwrap();
        assert!(!id_col.nullable);
    }

    #[test]
    fn test_table_level_primary_key() {
        let sql = "(id INTEGER, name TEXT, PRIMARY KEY (id))";
        let schema = extract_table_schema(sql, "orders", "schema.sql", 1);
        assert_eq!(schema.primary_key, Some(vec!["id".to_string()]));
    }

    #[test]
    fn test_table_level_composite_pk() {
        let sql = "(user_id INTEGER, role TEXT, PRIMARY KEY (user_id, role))";
        let schema = extract_table_schema(sql, "user_roles", "schema.sql", 1);
        assert_eq!(
            schema.primary_key,
            Some(vec!["user_id".to_string(), "role".to_string()])
        );
    }

    #[test]
    fn test_inline_foreign_key_with_column() {
        let sql = "(id INTEGER, user_id INTEGER REFERENCES users(id))";
        let schema = extract_table_schema(sql, "orders", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "user_id").unwrap();
        let fk = col.foreign_key.as_ref().unwrap();
        assert_eq!(fk.target_table, "users");
        assert_eq!(fk.target_column, "id");
    }

    #[test]
    fn test_inline_foreign_key_without_column() {
        let sql = "(id INTEGER, user_id INTEGER REFERENCES users)";
        let schema = extract_table_schema(sql, "orders", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "user_id").unwrap();
        let fk = col.foreign_key.as_ref().unwrap();
        assert_eq!(fk.target_table, "users");
        assert_eq!(fk.target_column, "");
    }

    #[test]
    fn test_table_level_foreign_key() {
        let sql = "(id INTEGER, user_id INTEGER, FOREIGN KEY (user_id) REFERENCES users(id))";
        let schema = extract_table_schema(sql, "orders", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "user_id").unwrap();
        let fk = col.foreign_key.as_ref().unwrap();
        assert_eq!(fk.target_table, "users");
        assert_eq!(fk.target_column, "id");
    }

    #[test]
    fn test_not_null_and_unique() {
        let sql = "(id INTEGER NOT NULL, email TEXT NOT NULL UNIQUE)";
        let schema = extract_table_schema(sql, "users", "schema.sql", 1);
        let id_col = schema.columns.iter().find(|c| c.name == "id").unwrap();
        assert!(!id_col.nullable);
        let email_col = schema.columns.iter().find(|c| c.name == "email").unwrap();
        assert!(!email_col.nullable);
        assert!(email_col.constraints.contains(&"UNIQUE".to_string()));
    }

    #[test]
    fn test_default_value() {
        let sql = "(status TEXT DEFAULT 'active', count INTEGER DEFAULT 0)";
        let schema = extract_table_schema(sql, "items", "schema.sql", 1);
        let status_col = schema.columns.iter().find(|c| c.name == "status").unwrap();
        assert_eq!(status_col.default, Some("'active'".to_string()));
        let count_col = schema.columns.iter().find(|c| c.name == "count").unwrap();
        assert_eq!(count_col.default, Some("0".to_string()));
    }

    #[test]
    fn test_postgresql_types() {
        let sql = "(data JSONB, tags TEXT[], seq SERIAL)";
        let schema = extract_table_schema(sql, "items", "schema.sql", 1);
        let data_col = schema.columns.iter().find(|c| c.name == "data").unwrap();
        assert_eq!(data_col.data_type, "JSONB");
        let tags_col = schema.columns.iter().find(|c| c.name == "tags").unwrap();
        assert_eq!(tags_col.data_type, "TEXT[]");
    }

    #[test]
    fn test_multiword_type_timestamp() {
        let sql = "(created_at TIMESTAMP WITH TIME ZONE NOT NULL)";
        let schema = extract_table_schema(sql, "events", "schema.sql", 1);
        let col = schema
            .columns
            .iter()
            .find(|c| c.name == "created_at")
            .unwrap();
        assert_eq!(col.data_type, "TIMESTAMP WITH TIME ZONE");
        assert!(!col.nullable);
    }

    #[test]
    fn test_multiword_type_double_precision() {
        let sql = "(amount DOUBLE PRECISION)";
        let schema = extract_table_schema(sql, "payments", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "amount").unwrap();
        assert_eq!(col.data_type, "DOUBLE PRECISION");
    }

    #[test]
    fn test_type_with_parens_decimal() {
        let sql = "(price DECIMAL(10,2) NOT NULL)";
        let schema = extract_table_schema(sql, "products", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "price").unwrap();
        assert_eq!(col.data_type, "DECIMAL(10,2)");
        assert!(!col.nullable);
    }

    #[test]
    fn test_type_with_parens_varchar() {
        let sql = "(name VARCHAR(255) NOT NULL)";
        let schema = extract_table_schema(sql, "users", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "name").unwrap();
        assert_eq!(col.data_type, "VARCHAR(255)");
        assert!(!col.nullable);
    }

    #[test]
    fn test_references_with_on_delete() {
        let sql = "(id INTEGER, owner_id INTEGER REFERENCES users(id) ON DELETE CASCADE)";
        let schema = extract_table_schema(sql, "docs", "schema.sql", 1);
        let col = schema
            .columns
            .iter()
            .find(|c| c.name == "owner_id")
            .unwrap();
        let fk = col.foreign_key.as_ref().unwrap();
        assert_eq!(fk.target_table, "users");
        assert_eq!(fk.target_column, "id");
    }

    #[test]
    fn test_quoted_identifiers() {
        let sql = r#"("id" INTEGER, "user_name" TEXT)"#;
        let schema = extract_table_schema(sql, "users", "schema.sql", 1);
        assert_eq!(schema.columns[0].name, "id");
        assert_eq!(schema.columns[1].name, "user_name");
    }

    #[test]
    fn test_table_level_check_skipped() {
        let sql = "(id INTEGER, age INTEGER, CHECK (age > 0))";
        let schema = extract_table_schema(sql, "people", "schema.sql", 1);
        // CHECK constraint definition should not be parsed as a column.
        assert_eq!(schema.columns.len(), 2);
    }

    #[test]
    fn test_schema_qualified_reference() {
        let sql = "(id INTEGER, user_id INTEGER REFERENCES public.users(id))";
        let schema = extract_table_schema(sql, "orders", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "user_id").unwrap();
        let fk = col.foreign_key.as_ref().unwrap();
        assert_eq!(fk.target_table, "public.users");
        assert_eq!(fk.target_column, "id");
    }

    #[test]
    fn test_empty_table() {
        let schema = extract_table_schema("", "empty", "schema.sql", 1);
        assert_eq!(schema.columns.len(), 0);
        assert!(schema.primary_key.is_none());
    }

    #[test]
    fn test_mysql_auto_increment() {
        let sql = "(id INTEGER AUTO_INCREMENT PRIMARY KEY, name TEXT)";
        let schema = extract_table_schema(sql, "items", "schema.sql", 1);
        let col = schema.columns.iter().find(|c| c.name == "id").unwrap();
        // AUTO_INCREMENT stops type collection; PRIMARY KEY handled.
        assert_eq!(col.data_type, "INTEGER");
        assert!(schema.primary_key.is_some());
    }

    #[test]
    fn test_view_extraction() {
        let sql = "SELECT u.id, o.total FROM users u JOIN orders o ON u.id = o.user_id";
        let view = extract_view_schema(sql, "user_orders", "schema.sql");
        assert!(view.source_tables.contains(&"users".to_string()));
        assert!(view.source_tables.contains(&"orders".to_string()));
    }

    #[test]
    fn test_function_extraction() {
        let body = "BEGIN SELECT * FROM orders; UPDATE inventory SET count = count - 1; END";
        let func = extract_function_schema(body, "process_order", "funcs.sql");
        assert!(func.referenced_tables.contains(&"orders".to_string()));
        assert!(func.referenced_tables.contains(&"inventory".to_string()));
    }

    #[test]
    fn test_multiple_foreign_keys() {
        let sql = "(id INTEGER PRIMARY KEY, author_id INTEGER REFERENCES users(id), post_id INTEGER REFERENCES posts(id))";
        let schema = extract_table_schema(sql, "comments", "schema.sql", 1);
        let author = schema
            .columns
            .iter()
            .find(|c| c.name == "author_id")
            .unwrap();
        assert_eq!(author.foreign_key.as_ref().unwrap().target_table, "users");
        let post = schema.columns.iter().find(|c| c.name == "post_id").unwrap();
        assert_eq!(post.foreign_key.as_ref().unwrap().target_table, "posts");
    }

    // -----------------------------------------------------------------------
    // Task 6 – CQL, Cypher, Elasticsearch
    // -----------------------------------------------------------------------

    #[test]
    fn test_cql_basic() {
        // CQL uses same SQL syntax; WITH clause ignored.
        let body = "(id UUID PRIMARY KEY, name TEXT, created_at TIMESTAMP) WITH CLUSTERING ORDER BY (created_at DESC)";
        let schema = extract_table_schema(body, "events", "schema.cql", 1);
        assert_eq!(schema.columns.len(), 3);
        assert_eq!(schema.primary_key, Some(vec!["id".to_string()]));
    }

    #[test]
    fn test_cql_with_clause_ignored() {
        // Ensures WITH clause outside the parens does not produce spurious columns.
        let body = "(pk UUID PRIMARY KEY)";
        let schema = extract_table_schema(body, "t", "schema.cql", 1);
        assert_eq!(schema.columns.len(), 1);
    }

    #[test]
    fn test_cypher_constraint() {
        let content = "CREATE CONSTRAINT person_id FOR (p:Person) ASSERT p.id IS UNIQUE;";
        let entries = extract_cypher_schema(content, "schema.cypher");
        let constraint = entries.iter().find(|e| e.entry_type == "constraint");
        assert!(constraint.is_some());
        assert!(constraint.unwrap().contains_label("Person"));
    }

    #[test]
    fn test_cypher_index() {
        let content = "CREATE INDEX movie_title FOR (m:Movie) ON (m.title);";
        let entries = extract_cypher_schema(content, "schema.cypher");
        let index = entries.iter().find(|e| e.entry_type == "index");
        assert!(index.is_some());
        assert!(index.unwrap().contains_label("Movie"));
    }

    #[test]
    fn test_cypher_node_labels() {
        let content = "MATCH (u:User)-[:FOLLOWS]->(other:User) RETURN u;";
        let entries = extract_cypher_schema(content, "schema.cypher");
        let user_entry = entries.iter().find(|e| e.contains_label("User"));
        assert!(user_entry.is_some());
        let rel_entry = entries.iter().find(|e| e.entry_type == "relationship");
        assert!(rel_entry.is_some());
        assert!(rel_entry.unwrap().contains_label("FOLLOWS"));
    }

    #[test]
    fn test_elasticsearch_mappings() {
        let json = r#"{
            "mappings": {
                "properties": {
                    "title": { "type": "text" },
                    "price": { "type": "float" },
                    "in_stock": { "type": "boolean" }
                }
            }
        }"#;
        let schema = extract_elasticsearch_schema(json, "index.json").unwrap();
        assert!(schema
            .fields
            .iter()
            .any(|f| f.name == "title" && f.field_type == "text"));
        assert!(schema
            .fields
            .iter()
            .any(|f| f.name == "price" && f.field_type == "float"));
        assert!(schema
            .fields
            .iter()
            .any(|f| f.name == "in_stock" && f.field_type == "boolean"));
    }

    #[test]
    fn test_elasticsearch_nested_properties() {
        let json = r#"{
            "mappings": {
                "properties": {
                    "address": {
                        "type": "object",
                        "properties": {
                            "city": { "type": "keyword" },
                            "zip": { "type": "keyword" }
                        }
                    }
                }
            }
        }"#;
        let schema = extract_elasticsearch_schema(json, "index.json").unwrap();
        assert!(schema.fields.iter().any(|f| f.name == "address"));
        assert!(schema
            .fields
            .iter()
            .any(|f| f.name == "address.city" && f.field_type == "keyword"));
        assert!(schema.fields.iter().any(|f| f.name == "address.zip"));
    }

    #[test]
    fn test_non_es_json() {
        let json = r#"{"name": "John", "age": 30}"#;
        let result = extract_elasticsearch_schema(json, "data.json");
        assert!(result.is_none());
    }

    // -----------------------------------------------------------------------
    // Task 7 – Prisma schema enrichment
    // -----------------------------------------------------------------------

    #[test]
    fn test_prisma_basic_model() {
        let body = r#"
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
"#;
        let model = extract_prisma_schema(body, "User", "schema.prisma", 1);
        assert_eq!(model.class_name, "User");
        assert_eq!(model.table_name, "user");
        let id_field = model.fields.iter().find(|f| f.name == "id").unwrap();
        assert!(id_field.field_type.contains("Int"));
        assert!(!id_field.is_relation);
        let email_field = model.fields.iter().find(|f| f.name == "email").unwrap();
        assert!(email_field.field_type.contains("String"));
    }

    #[test]
    fn test_prisma_map_override() {
        let body = r#"
  id   Int    @id
  name String
  @@map("my_users")
"#;
        let model = extract_prisma_schema(body, "User", "schema.prisma", 1);
        assert_eq!(model.table_name, "my_users");
    }

    #[test]
    fn test_prisma_relation_field() {
        let body = r#"
  id      Int      @id
  posts   Post[]   @relation("UserPosts")
  profile Profile? @relation("UserProfile")
"#;
        let model = extract_prisma_schema(body, "User", "schema.prisma", 1);
        let posts = model.fields.iter().find(|f| f.name == "posts").unwrap();
        assert!(posts.is_relation);
        assert_eq!(posts.related_model, Some("Post".to_string()));
        let profile = model.fields.iter().find(|f| f.name == "profile").unwrap();
        assert!(profile.is_relation);
        assert_eq!(profile.related_model, Some("Profile".to_string()));
    }
}