memvid-core 2.0.139

Core library for Memvid v2, a crash-safe, deterministic, single-file AI memory.
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
// Safe unwrap: Float comparisons and known-valid iterator operations.
#![allow(clippy::unwrap_used)]
//! PDF table extraction using Lattice and Stream detection.
//!
//! This module implements two complementary table detection strategies:
//! - **Lattice**: Detects tables with visible grid lines (ruled tables)
//! - **Stream**: Infers tables from text alignment and whitespace

use std::collections::HashMap;
use std::time::Instant;

use super::layout::{PageLayout, TextBox, cluster_values, extract_pdf_layout};
use super::types::{
    DetectionMode, ExtractedTable, ExtractionMode, TableCell, TableExtractionOptions,
    TableExtractionResult, TableQuality, TableRow,
};
use crate::error::Result;
use regex::Regex;

/// Minimum line length to consider for grid detection (in points).
const MIN_LINE_LENGTH: f32 = 20.0;

/// Minimum number of grid intersections to consider a valid lattice table.
const MIN_GRID_INTERSECTIONS: usize = 4;

/// Extract tables from a PDF document.
///
/// # Arguments
/// * `bytes` - Raw PDF bytes
/// * `source_file` - Original filename for metadata
/// * `options` - Extraction configuration
///
/// # Returns
/// A result containing all extracted tables and diagnostics.
pub fn extract_tables_from_pdf(
    bytes: &[u8],
    source_file: &str,
    options: &TableExtractionOptions,
) -> Result<TableExtractionResult> {
    let start = Instant::now();

    // Extract page layouts
    let layouts = extract_pdf_layout(bytes, options.max_pages)?;
    let pages_processed = u32::try_from(layouts.len()).unwrap_or(0);

    let mut all_tables = Vec::new();
    let mut warnings = Vec::new();

    // Try Lattice detection first (more reliable when applicable)
    if options.mode != ExtractionMode::StreamOnly {
        let lattice_tables = extract_lattice_tables(&layouts, source_file, options);
        for table in lattice_tables {
            if passes_quality_filter(&table, options) {
                all_tables.push(table);
            }
        }
    }

    // Then try Stream detection for pages without Lattice tables
    if options.mode != ExtractionMode::LatticeOnly {
        let pages_with_lattice: std::collections::HashSet<u32> = all_tables
            .iter()
            .flat_map(|t| t.page_start..=t.page_end)
            .collect();

        let stream_layouts: Vec<&PageLayout> = layouts
            .iter()
            .filter(|l| !pages_with_lattice.contains(&l.page_number))
            .collect();

        let stream_tables = extract_stream_tables(&stream_layouts, source_file, options);
        for table in stream_tables {
            if passes_quality_filter(&table, options) {
                all_tables.push(table);
            }
        }
    }

    // Fallback: Line-based detection when Stream fails (for lopdf linearized text)
    // This detects tables from consecutive lines with key-value patterns
    if all_tables.is_empty() && options.mode != ExtractionMode::LatticeOnly {
        let line_tables = extract_line_based_tables(bytes, source_file, options);
        for table in line_tables {
            if passes_quality_filter(&table, options) {
                all_tables.push(table);
            }
        }
    }

    // Merge multi-page tables if enabled
    if options.merge_multi_page && all_tables.len() > 1 {
        all_tables = super::multi_page::merge_multi_page_tables(all_tables, options);
    }

    // Sort by page order
    all_tables.sort_by_key(|t| (t.page_start, t.page_end));

    // Generate unique IDs
    for (i, table) in all_tables.iter_mut().enumerate() {
        if table.table_id.is_empty() {
            table.table_id = format!("tbl_{}_{}", source_file.replace('.', "_"), i + 1);
        }
    }

    let total_ms = start.elapsed().as_millis().try_into().unwrap_or(u64::MAX);

    if all_tables.is_empty() && pages_processed > 0 {
        warnings.push("No tables detected in document".to_string());
    }

    Ok(TableExtractionResult {
        tables: all_tables,
        pages_processed,
        total_ms,
        warnings,
    })
}

/// Check if a table passes the quality filter.
fn passes_quality_filter(table: &ExtractedTable, options: &TableExtractionOptions) -> bool {
    // Check minimum dimensions
    if table.n_rows < options.min_rows || table.n_cols < options.min_cols {
        return false;
    }

    // Check quality threshold
    match (table.quality, options.min_quality) {
        (TableQuality::High, _) => true,
        (TableQuality::Medium, TableQuality::High) => false,
        (TableQuality::Medium, _) => true,
        (TableQuality::Low, TableQuality::Low) => true,
        (TableQuality::Low, _) => {
            // In aggressive mode, allow low quality with warnings
            options.mode == ExtractionMode::Aggressive
        }
    }
}

// ============================================================================
// Lattice Detection (Ruled Tables)
// ============================================================================

/// A grid cell defined by boundary lines.
#[derive(Debug, Clone)]
struct GridCell {
    row: usize,
    col: usize,
    x_min: f32,
    x_max: f32,
    y_min: f32,
    y_max: f32,
}

/// Extract tables using grid line detection (Lattice mode).
fn extract_lattice_tables(
    layouts: &[PageLayout],
    source_file: &str,
    options: &TableExtractionOptions,
) -> Vec<ExtractedTable> {
    let mut tables = Vec::new();

    for layout in layouts {
        // Get horizontal and vertical lines
        let h_lines: Vec<f32> = layout
            .horizontal_lines(options.row_clustering_threshold)
            .iter()
            .filter(|l| l.length() >= MIN_LINE_LENGTH)
            .map(|l| l.y_coord())
            .collect();

        let v_lines: Vec<f32> = layout
            .vertical_lines(options.col_clustering_threshold)
            .iter()
            .filter(|l| l.length() >= MIN_LINE_LENGTH)
            .map(|l| l.x_coord())
            .collect();

        if h_lines.len() < 2 || v_lines.len() < 2 {
            continue;
        }

        // Cluster lines to find grid boundaries
        let h_clusters = cluster_values(&h_lines, options.row_clustering_threshold);
        let v_clusters = cluster_values(&v_lines, options.col_clustering_threshold);

        if h_clusters.len() < 2 || v_clusters.len() < 2 {
            continue;
        }

        // Build grid cells
        let grid = build_grid_cells(&h_clusters, &v_clusters);
        if grid.len() < MIN_GRID_INTERSECTIONS {
            continue;
        }

        // Assign text to cells
        let cell_contents = assign_text_to_cells(&layout.text_boxes, &grid);

        // Build table from grid
        if let Some(table) = build_table_from_grid(
            &grid,
            &cell_contents,
            layout.page_number,
            source_file,
            options,
        ) {
            tables.push(table);
        }
    }

    tables
}

/// Build grid cells from clustered line positions.
fn build_grid_cells(h_clusters: &[f32], v_clusters: &[f32]) -> Vec<GridCell> {
    let mut cells = Vec::new();

    // Sort clusters (Y descending for PDF coordinates, X ascending)
    let mut h_sorted: Vec<f32> = h_clusters.to_vec();
    h_sorted.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

    let mut v_sorted: Vec<f32> = v_clusters.to_vec();
    v_sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    for (row_idx, h_pair) in h_sorted.windows(2).enumerate() {
        for (col_idx, v_pair) in v_sorted.windows(2).enumerate() {
            cells.push(GridCell {
                row: row_idx,
                col: col_idx,
                x_min: v_pair[0],
                x_max: v_pair[1],
                y_min: h_pair[1], // Lower Y (remember PDF coords)
                y_max: h_pair[0], // Higher Y
            });
        }
    }

    cells
}

/// Assign text boxes to grid cells based on position.
fn assign_text_to_cells(
    text_boxes: &[TextBox],
    grid: &[GridCell],
) -> HashMap<(usize, usize), String> {
    let mut cell_text: HashMap<(usize, usize), Vec<String>> = HashMap::new();

    for tbox in text_boxes {
        let center_x = tbox.center_x();
        let center_y = tbox.center_y();

        for cell in grid {
            if center_x >= cell.x_min
                && center_x <= cell.x_max
                && center_y >= cell.y_min
                && center_y <= cell.y_max
            {
                cell_text
                    .entry((cell.row, cell.col))
                    .or_default()
                    .push(tbox.text.trim().to_string());
                break;
            }
        }
    }

    // Join text fragments in each cell
    cell_text
        .into_iter()
        .map(|(k, v)| (k, v.join(" ")))
        .collect()
}

/// Build an `ExtractedTable` from grid data.
fn build_table_from_grid(
    grid: &[GridCell],
    cell_contents: &HashMap<(usize, usize), String>,
    page: u32,
    source_file: &str,
    _options: &TableExtractionOptions,
) -> Option<ExtractedTable> {
    if grid.is_empty() {
        return None;
    }

    let max_row = grid.iter().map(|c| c.row).max().unwrap_or(0);
    let max_col = grid.iter().map(|c| c.col).max().unwrap_or(0);

    let n_rows = max_row + 1;
    let n_cols = max_col + 1;

    // Build rows
    let mut rows = Vec::with_capacity(n_rows);
    for row_idx in 0..n_rows {
        let mut cells = Vec::with_capacity(n_cols);
        for col_idx in 0..n_cols {
            let text = cell_contents
                .get(&(row_idx, col_idx))
                .cloned()
                .unwrap_or_default();
            cells.push(TableCell::new(text, col_idx));
        }
        rows.push(TableRow::new(row_idx, page, cells));
    }

    // Detect headers (first row with content)
    let mut headers = Vec::new();
    if let Some(first_row) = rows.first() {
        headers = first_row
            .cell_texts()
            .iter()
            .map(|s| (*s).to_string())
            .collect();
        if headers.iter().any(|h| !h.is_empty()) {
            if let Some(row) = rows.first_mut() {
                *row = std::mem::take(row).as_header();
            }
        }
    }

    let mut table = ExtractedTable::new(String::new(), source_file);
    table.page_start = page;
    table.page_end = page;
    table.headers = headers;
    table.n_cols = n_cols;
    table.n_rows = rows.iter().filter(|r| !r.is_header_row).count();
    table.rows = rows;
    table.detection_mode = DetectionMode::Lattice;
    table.quality = TableQuality::High; // Lattice detection is most reliable
    table.confidence_score = 0.9;

    Some(table)
}

// ============================================================================
// Stream Detection (Whitespace-Aligned Tables)
// ============================================================================

/// Extract tables using text alignment inference (Stream mode).
fn extract_stream_tables(
    layouts: &[&PageLayout],
    source_file: &str,
    options: &TableExtractionOptions,
) -> Vec<ExtractedTable> {
    let mut tables = Vec::new();

    for layout in layouts {
        if layout.text_boxes.is_empty() {
            continue;
        }

        // Cluster text boxes into rows by Y-position
        let rows = cluster_into_rows(&layout.text_boxes, options.row_clustering_threshold);

        if rows.len() < options.min_rows {
            continue;
        }

        // Detect column boundaries from X-positions
        let col_boundaries = detect_column_boundaries(&rows, options.col_clustering_threshold);

        if col_boundaries.len() < options.min_cols + 1 {
            continue;
        }

        // Build table from detected structure
        if let Some(table) = build_stream_table(
            &rows,
            &col_boundaries,
            layout.page_number,
            source_file,
            options,
        ) {
            tables.push(table);
        }
    }

    tables
}

/// Cluster text boxes into rows by Y-position.
fn cluster_into_rows(text_boxes: &[TextBox], threshold: f32) -> Vec<Vec<&TextBox>> {
    if text_boxes.is_empty() {
        return Vec::new();
    }

    // Sort by Y-position (descending for PDF coordinates - top to bottom)
    let mut sorted: Vec<&TextBox> = text_boxes.iter().collect();
    sorted.sort_by(|a, b| b.y.partial_cmp(&a.y).unwrap_or(std::cmp::Ordering::Equal));

    let mut rows: Vec<Vec<&TextBox>> = Vec::new();
    let mut current_row = vec![sorted[0]];
    let mut current_y = sorted[0].y;

    for tbox in &sorted[1..] {
        if (current_y - tbox.y).abs() <= threshold {
            current_row.push(tbox);
        } else {
            // Sort row by X-position (left to right)
            current_row.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal));
            rows.push(current_row);
            current_row = vec![tbox];
            current_y = tbox.y;
        }
    }

    if !current_row.is_empty() {
        current_row.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal));
        rows.push(current_row);
    }

    rows
}

/// Detect column boundaries from consistent X-positions across rows.
fn detect_column_boundaries(rows: &[Vec<&TextBox>], threshold: f32) -> Vec<f32> {
    // Collect all X-positions (left edges and right edges)
    let mut x_positions: Vec<f32> = Vec::new();

    for row in rows {
        for tbox in row {
            x_positions.push(tbox.x);
            x_positions.push(tbox.right());
        }
    }

    // Cluster X-positions
    let candidates = cluster_values(&x_positions, threshold);

    // Filter to keep only boundaries that appear consistently
    let min_occurrences = rows.len() / 2;
    filter_consistent_boundaries(&candidates, rows, threshold, min_occurrences)
}

/// Filter column boundaries to keep only those appearing consistently.
fn filter_consistent_boundaries(
    candidates: &[f32],
    rows: &[Vec<&TextBox>],
    threshold: f32,
    min_occurrences: usize,
) -> Vec<f32> {
    candidates
        .iter()
        .filter(|&&boundary| {
            let occurrences = rows
                .iter()
                .filter(|row| {
                    row.iter().any(|tbox| {
                        (tbox.x - boundary).abs() <= threshold
                            || (tbox.right() - boundary).abs() <= threshold
                    })
                })
                .count();
            occurrences >= min_occurrences
        })
        .copied()
        .collect()
}

/// Build an `ExtractedTable` from stream-detected structure.
fn build_stream_table(
    text_rows: &[Vec<&TextBox>],
    col_boundaries: &[f32],
    page: u32,
    source_file: &str,
    _options: &TableExtractionOptions,
) -> Option<ExtractedTable> {
    if text_rows.is_empty() || col_boundaries.len() < 2 {
        return None;
    }

    let n_cols = col_boundaries.len() - 1;
    let mut rows = Vec::with_capacity(text_rows.len());

    for (row_idx, text_row) in text_rows.iter().enumerate() {
        let mut cells = vec![TableCell::new(String::new(), 0); n_cols];

        for tbox in text_row {
            let center_x = tbox.center_x();

            // Find which column this text belongs to
            for (col_idx, col_pair) in col_boundaries.windows(2).enumerate() {
                if center_x >= col_pair[0] && center_x <= col_pair[1] {
                    // Append text to this cell
                    if !cells[col_idx].text.is_empty() {
                        cells[col_idx].text.push(' ');
                    }
                    cells[col_idx].text.push_str(tbox.text.trim());
                    cells[col_idx].col_index = col_idx;
                    break;
                }
            }
        }

        rows.push(TableRow::new(row_idx, page, cells));
    }

    // Detect headers (first row)
    let mut headers = Vec::new();
    if let Some(first_row) = rows.first() {
        headers = first_row
            .cell_texts()
            .iter()
            .map(|s| (*s).to_string())
            .collect();
        // Check if first row looks like headers (non-empty, possibly different formatting)
        let non_empty_count = headers.iter().filter(|h| !h.is_empty()).count();
        if non_empty_count > n_cols / 2 {
            if let Some(row) = rows.first_mut() {
                *row = std::mem::take(row).as_header();
            }
        }
    }

    // Calculate quality based on consistency
    let (quality, confidence) = calculate_stream_quality(&rows, n_cols);

    let mut table = ExtractedTable::new(String::new(), source_file);
    table.page_start = page;
    table.page_end = page;
    table.headers = headers;
    table.n_cols = n_cols;
    table.n_rows = rows.iter().filter(|r| !r.is_header_row).count();
    table.rows = rows;
    table.detection_mode = DetectionMode::Stream;
    table.quality = quality;
    table.confidence_score = confidence;

    Some(table)
}

/// Calculate quality score for stream-detected tables.
fn calculate_stream_quality(rows: &[TableRow], expected_cols: usize) -> (TableQuality, f32) {
    if rows.is_empty() {
        return (TableQuality::Low, 0.0);
    }

    let mut score = 1.0f32;

    // Penalty for inconsistent column counts
    let col_counts: Vec<usize> = rows
        .iter()
        .map(|r| r.cells.iter().filter(|c| !c.text.is_empty()).count())
        .collect();

    let avg_cols = col_counts.iter().sum::<usize>() as f32 / col_counts.len() as f32;
    let variance: f32 = col_counts
        .iter()
        .map(|&c| (c as f32 - avg_cols).powi(2))
        .sum::<f32>()
        / col_counts.len() as f32;

    if variance > 1.0 {
        score -= 0.2 * variance.min(2.0);
    }

    // Penalty for many empty cells
    let total_cells = rows.len() * expected_cols;
    let empty_cells = rows
        .iter()
        .flat_map(|r| &r.cells)
        .filter(|c| c.text.is_empty())
        .count();
    let empty_ratio = empty_cells as f32 / total_cells.max(1) as f32;
    if empty_ratio > 0.3 {
        score -= 0.2 * empty_ratio;
    }

    // Penalty for few rows
    if rows.len() < 4 {
        score -= 0.1;
    }

    // Stream detection inherently less reliable than Lattice
    score -= 0.1;

    score = score.clamp(0.0, 1.0);

    let quality = if score >= 0.7 {
        TableQuality::High
    } else if score >= 0.4 {
        TableQuality::Medium
    } else {
        TableQuality::Low
    };

    (quality, score)
}

// ============================================================================
// Line-Based Detection (Fallback for linearized PDF text)
// ============================================================================

/// A detected table region from line patterns.
#[derive(Debug)]
struct LineBasedTableRegion {
    /// Header line (if detected)
    header: Option<String>,
    /// Data rows as raw lines
    data_lines: Vec<String>,
    /// Detected column count
    col_count: usize,
    /// Starting line index in the document (reserved for future use)
    #[allow(dead_code)]
    start_line: usize,
}

/// Extract tables from linearized PDF text using line patterns.
///
/// This fallback works when lopdf's text extraction returns each cell on
/// separate lines (common with wkhtmltopdf, some PDF generators).
/// It detects tables by identifying:
/// 1. Key-value patterns (Label followed by Value)
/// 2. Currency/numeric patterns common in invoices/pay stubs
/// 3. Section headers followed by consistent data rows
fn extract_line_based_tables(
    bytes: &[u8],
    source_file: &str,
    options: &TableExtractionOptions,
) -> Vec<ExtractedTable> {
    // Extract raw text from PDF
    let text = match extract_raw_text(bytes) {
        Some(t) => t,
        None => return Vec::new(),
    };

    let lines: Vec<&str> = text.lines().collect();
    if lines.len() < options.min_rows {
        return Vec::new();
    }

    let mut tables = Vec::new();

    // Detect table regions by identifying patterns
    let regions = detect_table_regions(&lines, options);

    for region in regions {
        if let Some(table) = build_line_based_table(region, source_file, options) {
            tables.push(table);
        }
    }

    tables
}

/// Extract raw text from PDF bytes using lopdf.
fn extract_raw_text(bytes: &[u8]) -> Option<String> {
    use lopdf::Document;

    let document = Document::load_mem(bytes).ok()?;
    let pages = document.get_pages();
    let mut all_text = String::new();

    for page_num in 1..=u32::try_from(pages.len()).unwrap_or(0) {
        if let Ok(text) = document.extract_text(&[page_num]) {
            all_text.push_str(&text);
            all_text.push('\n');
        }
    }

    if all_text.is_empty() {
        None
    } else {
        Some(all_text)
    }
}

/// Detect table regions from line patterns.
fn detect_table_regions<'a>(
    lines: &'a [&'a str],
    options: &TableExtractionOptions,
) -> Vec<LineBasedTableRegion> {
    let mut regions = Vec::new();

    // Patterns for detecting table-like content
    let currency_re = Regex::new(r"^\$?[\d,]+\.?\d*$").unwrap();
    let date_re = Regex::new(r"^\d{1,2}[/-]\d{1,2}[/-]\d{2,4}$").unwrap();
    let percent_re = Regex::new(r"^\d+\.?\d*%$").unwrap();
    let hours_re = Regex::new(r"^\d+\.?\d*\s*(hrs?|hours?)$").unwrap();

    // Common table section headers (case-insensitive patterns)
    let section_headers = [
        "earnings",
        "deductions",
        "taxes",
        "withheld",
        "summary",
        "totals",
        "gross",
        "net",
        "employee",
        "employer",
        "description",
        "amount",
        "rate",
        "hours",
        "pay",
        "date",
        "period",
        "ytd",
        "current",
        "item",
        "quantity",
        "price",
        "total",
        "subtotal",
        "invoice",
        "bill",
    ];

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

        // Skip empty lines
        if line.is_empty() {
            i += 1;
            continue;
        }

        // Check if this might be a table header
        let line_lower = line.to_lowercase();
        let is_potential_header = section_headers.iter().any(|h| line_lower.contains(h));

        if is_potential_header {
            // Look for data rows following this header
            let (region, consumed) = collect_table_region(
                lines,
                i,
                Some(line.to_string()),
                &currency_re,
                &date_re,
                &percent_re,
                &hours_re,
                options,
            );

            if let Some(r) = region {
                if r.data_lines.len() >= options.min_rows {
                    regions.push(r);
                }
            }

            i += consumed.max(1);
        } else {
            // Check if current line looks like tabular data
            let looks_like_data =
                is_tabular_data_line(line, &currency_re, &date_re, &percent_re, &hours_re);

            if looks_like_data {
                // Try to collect a table region without explicit header
                let (region, consumed) = collect_table_region(
                    lines,
                    i,
                    None,
                    &currency_re,
                    &date_re,
                    &percent_re,
                    &hours_re,
                    options,
                );

                if let Some(r) = region {
                    if r.data_lines.len() >= options.min_rows {
                        regions.push(r);
                    }
                }

                i += consumed.max(1);
            } else {
                i += 1;
            }
        }
    }

    regions
}

/// Check if a line looks like tabular data.
fn is_tabular_data_line(
    line: &str,
    currency_re: &Regex,
    date_re: &Regex,
    percent_re: &Regex,
    hours_re: &Regex,
) -> bool {
    let trimmed = line.trim();

    // Check for currency values
    if currency_re.is_match(trimmed) {
        return true;
    }

    // Check for dates
    if date_re.is_match(trimmed) {
        return true;
    }

    // Check for percentages
    if percent_re.is_match(trimmed) {
        return true;
    }

    // Check for hours
    if hours_re.is_match(trimmed) {
        return true;
    }

    // Check for numeric values
    if trimmed.parse::<f64>().is_ok() {
        return true;
    }

    false
}

/// Collect consecutive lines that form a table region.
fn collect_table_region(
    lines: &[&str],
    start: usize,
    header: Option<String>,
    currency_re: &Regex,
    date_re: &Regex,
    _percent_re: &Regex,
    _hours_re: &Regex,
    options: &TableExtractionOptions,
) -> (Option<LineBasedTableRegion>, usize) {
    let mut data_lines = Vec::new();
    let mut i = if header.is_some() { start + 1 } else { start };
    let mut consecutive_non_data = 0;
    let max_gap = 2; // Allow small gaps in data

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

        if line.is_empty() {
            consecutive_non_data += 1;
            i += 1;
            continue;
        }

        // Check if this is a new section header (end of current table)
        let line_lower = line.to_lowercase();
        let is_new_section = ["earnings", "deductions", "taxes", "summary", "totals"]
            .iter()
            .any(|h| line_lower.starts_with(h) && !data_lines.is_empty());

        if is_new_section {
            break;
        }

        // Collect the line as data
        data_lines.push(line.to_string());
        consecutive_non_data = 0;
        i += 1;
    }

    let consumed = i - start;

    if data_lines.len() < options.min_rows {
        return (None, consumed);
    }

    // Try to determine column structure
    let col_count = infer_column_count(&data_lines, currency_re, date_re);

    if col_count < options.min_cols {
        return (None, consumed);
    }

    (
        Some(LineBasedTableRegion {
            header,
            data_lines,
            col_count,
            start_line: start,
        }),
        consumed,
    )
}

/// Infer the number of columns from data patterns.
fn infer_column_count(lines: &[String], currency_re: &Regex, _date_re: &Regex) -> usize {
    // For key-value style tables (common in pay stubs), assume 2 columns
    // Look for patterns: Label on one line, Value on next

    let mut consecutive_label_value = 0;
    let mut i = 0;

    while i + 1 < lines.len() {
        let line1 = lines[i].trim();
        let line2 = lines[i + 1].trim();

        // Check if line1 is a label (text) and line2 is a value (numeric/currency)
        let line1_is_label =
            !line1.is_empty() && !currency_re.is_match(line1) && line1.parse::<f64>().is_err();

        let line2_is_value =
            currency_re.is_match(line2) || line2.parse::<f64>().is_ok() || line2.contains('$');

        if line1_is_label && line2_is_value {
            consecutive_label_value += 1;
        }

        i += 2;
    }

    // If we found many label-value pairs, it's a 2-column table
    if consecutive_label_value >= 3 {
        return 2;
    }

    // Default: single column (or need more sophisticated analysis)
    // Check if lines contain tab or multiple spaces for column detection
    let mut max_parts = 1;
    for line in lines {
        let parts: Vec<&str> = line.split('\t').collect();
        if parts.len() > max_parts {
            max_parts = parts.len();
        }

        // Also check for "  " (double space) separation
        let space_parts: Vec<&str> = line.split("  ").filter(|s| !s.is_empty()).collect();
        if space_parts.len() > max_parts {
            max_parts = space_parts.len();
        }
    }

    max_parts.max(2) // Assume at least 2 columns for table detection
}

/// Build an `ExtractedTable` from a line-based region.
fn build_line_based_table(
    region: LineBasedTableRegion,
    source_file: &str,
    _options: &TableExtractionOptions,
) -> Option<ExtractedTable> {
    if region.data_lines.is_empty() {
        return None;
    }

    let mut rows = Vec::new();
    let mut headers = Vec::new();

    // If we have a header, use it
    if let Some(ref h) = region.header {
        headers = vec![h.clone(), "Value".to_string()];
        let header_row = TableRow::new(
            0,
            1,
            vec![
                TableCell::new(h.clone(), 0).as_header(),
                TableCell::new("Value", 1).as_header(),
            ],
        )
        .as_header();
        rows.push(header_row);
    }

    // Group lines into rows (label + value pairs for 2-col tables)
    let mut row_idx = rows.len();
    let currency_re = Regex::new(r"^\$?[\d,]+\.?\d*$").unwrap();

    let mut i = 0;
    while i < region.data_lines.len() {
        let line1 = region.data_lines[i].trim();

        // Try to detect label-value pair
        let is_label = !currency_re.is_match(line1) && line1.parse::<f64>().is_err();

        if is_label && i + 1 < region.data_lines.len() {
            let line2 = region.data_lines[i + 1].trim();
            let is_value =
                currency_re.is_match(line2) || line2.parse::<f64>().is_ok() || line2.contains('$');

            if is_value {
                // Create 2-column row
                let cells = vec![TableCell::new(line1, 0), TableCell::new(line2, 1)];
                rows.push(TableRow::new(row_idx, 1, cells));
                row_idx += 1;
                i += 2;
                continue;
            }
        }

        // Single cell row
        let cells = vec![TableCell::new(line1, 0)];
        rows.push(TableRow::new(row_idx, 1, cells));
        row_idx += 1;
        i += 1;
    }

    if rows.is_empty() {
        return None;
    }

    // Set default headers if not already set
    if headers.is_empty() {
        headers = vec!["Label".to_string(), "Value".to_string()];
    }

    let n_cols = region.col_count.max(2);
    let n_rows = rows.iter().filter(|r| !r.is_header_row).count();

    let mut table = ExtractedTable::new(String::new(), source_file);
    table.page_start = 1;
    table.page_end = 1;
    table.headers = headers;
    table.n_cols = n_cols;
    table.n_rows = n_rows;
    table.rows = rows;
    table.detection_mode = DetectionMode::LineBased;
    table.quality = TableQuality::Medium; // Lower confidence for line-based
    table.confidence_score = 0.6;

    Some(table)
}

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

    #[test]
    fn test_cluster_into_rows() {
        let boxes = vec![
            TextBox {
                text: "A".to_string(),
                x: 10.0,
                y: 100.0,
                width: 20.0,
                height: 10.0,
                font_size: 12.0,
                page: 1,
            },
            TextBox {
                text: "B".to_string(),
                x: 50.0,
                y: 100.0,
                width: 20.0,
                height: 10.0,
                font_size: 12.0,
                page: 1,
            },
            TextBox {
                text: "C".to_string(),
                x: 10.0,
                y: 80.0,
                width: 20.0,
                height: 10.0,
                font_size: 12.0,
                page: 1,
            },
        ];

        let rows = cluster_into_rows(&boxes, 5.0);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].len(), 2); // A and B on same row
        assert_eq!(rows[1].len(), 1); // C on second row
    }

    #[test]
    fn test_build_grid_cells() {
        let h_clusters = vec![100.0, 80.0, 60.0];
        let v_clusters = vec![10.0, 50.0, 90.0];

        let grid = build_grid_cells(&h_clusters, &v_clusters);
        assert_eq!(grid.len(), 4); // 2 rows × 2 cols
    }

    #[test]
    fn test_stream_quality_calculation() {
        let rows = vec![
            TableRow::new(
                0,
                1,
                vec![
                    TableCell::new("A", 0),
                    TableCell::new("B", 1),
                    TableCell::new("C", 2),
                ],
            ),
            TableRow::new(
                1,
                1,
                vec![
                    TableCell::new("1", 0),
                    TableCell::new("2", 1),
                    TableCell::new("3", 2),
                ],
            ),
        ];

        let (quality, score) = calculate_stream_quality(&rows, 3);
        assert!(score > 0.5);
        assert!(matches!(quality, TableQuality::Medium | TableQuality::High));
    }
}