rustledger-importer 0.17.4

Import framework for rustledger - extract transactions from bank files
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
//! CSV format auto-inference.
//!
//! Given raw CSV content, detects:
//! - Delimiter (`,`, `;`, `\t`, `|`)
//! - Whether headers are present
//! - Which column is the date (and its format)
//! - Which column(s) contain amounts
//! - Which column contains the description/narration
//! - Which column contains the payee (if separate)
//!
//! This enables zero-config import for ~80% of bank CSV exports.

use format_num_pattern::Locale;

use crate::config::{ColumnSpec, CsvConfig, SecondaryDate};

/// Result of CSV format inference.
///
/// Constructed only by [`infer_csv_config`]. Marked `#[non_exhaustive]` so that
/// *future* inferred fields can be added without breaking downstream consumers
/// of `rustledger-importer`. Adding the attribute is itself a one-time breaking
/// change for any external code that built this struct with a literal or matched
/// it exhaustively (there is none in this workspace — it is only constructed
/// here and consumed by field access via [`Self::to_csv_config`]).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct InferredCsvConfig {
    /// Detected field delimiter.
    pub delimiter: char,
    /// Whether the first row appears to be a header.
    pub has_header: bool,
    /// The date column.
    pub date_column: ColumnSpec,
    /// The detected date format (strftime-style).
    pub date_format: String,
    /// Single amount column (if amounts are in one column).
    pub amount_column: Option<ColumnSpec>,
    /// Debit column (if amounts are split debit/credit).
    pub debit_column: Option<ColumnSpec>,
    /// Credit column (if amounts are split debit/credit).
    pub credit_column: Option<ColumnSpec>,
    /// Description/narration column.
    pub narration_column: Option<ColumnSpec>,
    /// Payee column (if separate from narration).
    pub payee_column: Option<ColumnSpec>,
    /// Per-row currency column (detected from a header like "Currency"/"Ccy").
    pub currency_column: Option<ColumnSpec>,
    /// A second date column (e.g. a value date alongside the booking date),
    /// preserved as transaction metadata rather than discarded (#1623).
    pub secondary_date: Option<SecondaryDate>,
    /// Inferred amount locale (decimal/grouping separators). `Some(de_DE)` when
    /// the amounts look comma-decimal (e.g. `-54,23`, `2.500,00`); `None` when
    /// they look period-decimal or carry no signal (the parser defaults to
    /// POSIX). An explicit `--amount-locale` overrides this.
    pub amount_locale: Option<Locale>,
    /// Overall confidence in the inference (0.0 to 1.0).
    pub confidence: f64,
}

impl InferredCsvConfig {
    /// Convert to a [`CsvConfig`] for use with the CSV importer.
    #[must_use]
    pub fn to_csv_config(&self) -> CsvConfig {
        CsvConfig {
            date_column: self.date_column.clone(),
            date_format: self.date_format.clone(),
            narration_column: self.narration_column.clone(),
            payee_column: self.payee_column.clone(),
            currency_column: self.currency_column.clone(),
            amount_column: self.amount_column.clone(),
            debit_column: self.debit_column.clone(),
            credit_column: self.credit_column.clone(),
            amount_locale: self.amount_locale.or(Some(Locale::POSIX)),
            has_header: self.has_header,
            delimiter: self.delimiter,
            secondary_date: self.secondary_date.clone(),
            ..CsvConfig::default()
        }
    }
}

/// Infer the CSV format from file content.
///
/// Reads the first few rows to detect delimiter, headers, column types, and
/// date format. Returns `None` if the content doesn't look like a parseable CSV.
#[must_use]
pub fn infer_csv_config(content: &str) -> Option<InferredCsvConfig> {
    if content.trim().is_empty() {
        return None;
    }

    let delimiter = detect_delimiter(content);
    let rows = parse_rows(content, delimiter);

    if rows.len() < 2 {
        return None;
    }

    let has_header = detect_header(&rows);
    let headers: Vec<&str> = if has_header {
        rows[0].iter().map(String::as_str).collect()
    } else {
        vec![]
    };
    let data_rows: Vec<&Vec<String>> = if has_header {
        rows[1..].iter().collect()
    } else {
        rows.iter().collect()
    };

    // Sample up to 10 data rows for classification
    let sample: Vec<&Vec<String>> = data_rows.iter().take(10).copied().collect();
    if sample.is_empty() {
        return None;
    }

    let num_cols = rows[0].len();
    let mut confidence = 0.0;

    // Classify each column
    let date_col = find_date_column(&headers, &sample, num_cols);
    let date_col_idx = date_col.as_ref().map(|(i, _)| *i);
    let (amount_col, debit_col, credit_col) =
        find_amount_columns(&headers, &sample, num_cols, date_col_idx);
    // Detect the currency column first so text-column detection can exclude it
    // (otherwise a "Currency" column could be claimed as narration/payee when no
    // dedicated description column exists).
    let currency_col = if has_header {
        find_currency_column(&headers)
    } else {
        None
    };
    let (narration_col, payee_col) = find_text_columns(
        &headers,
        num_cols,
        date_col.as_ref().map(|(i, _)| *i),
        amount_col,
        debit_col,
        credit_col,
        currency_col,
    );

    // Build result
    let (date_column, date_format) = match date_col {
        Some((i, fmt)) => {
            confidence += 0.4;
            let col = if has_header && i < headers.len() {
                ColumnSpec::Name(headers[i].to_string())
            } else {
                ColumnSpec::Index(i)
            };
            (col, fmt)
        }
        None => return None, // Can't do anything without a date
    };

    // Preserve a second date column (e.g. a value date alongside the booking
    // date) as metadata instead of dropping it (#1623). Only when there is a
    // header, so the metadata key can be derived from the column name.
    let secondary_date = if has_header {
        date_col_idx.and_then(|primary| {
            find_secondary_date_column(&headers, &sample, num_cols, primary).map(|(i, fmt)| {
                SecondaryDate {
                    column: ColumnSpec::Name(headers[i].to_string()),
                    format: fmt,
                    meta_key: header_to_meta_key(headers[i]),
                }
            })
        })
    } else {
        None
    };

    let amount_column = amount_col.map(|i| {
        confidence += 0.3;
        if has_header && i < headers.len() {
            ColumnSpec::Name(headers[i].to_string())
        } else {
            ColumnSpec::Index(i)
        }
    });

    let debit_column = debit_col.map(|i| {
        confidence += 0.15;
        if has_header && i < headers.len() {
            ColumnSpec::Name(headers[i].to_string())
        } else {
            ColumnSpec::Index(i)
        }
    });

    let credit_column = credit_col.map(|i| {
        confidence += 0.15;
        if has_header && i < headers.len() {
            ColumnSpec::Name(headers[i].to_string())
        } else {
            ColumnSpec::Index(i)
        }
    });

    if amount_column.is_none() && debit_column.is_none() {
        return None; // Can't do anything without amounts
    }

    let narration_column = narration_col.map(|i| {
        confidence += 0.2;
        if has_header && i < headers.len() {
            ColumnSpec::Name(headers[i].to_string())
        } else {
            ColumnSpec::Index(i)
        }
    });

    let payee_column = payee_col.map(|i| {
        confidence += 0.1;
        if has_header && i < headers.len() {
            ColumnSpec::Name(headers[i].to_string())
        } else {
            ColumnSpec::Index(i)
        }
    });

    // Build the currency ColumnSpec from the index detected above (by header
    // name only; cell values are free-form 3-letter codes).
    let currency_column = currency_col.map(|i| {
        confidence += 0.05;
        ColumnSpec::Name(headers[i].to_string())
    });

    // Infer the decimal separator from the amount-bearing columns so that
    // comma-decimal exports (e.g. "-54,23", "2.500,00") parse correctly under
    // --auto instead of being read with a POSIX (period-decimal) locale.
    let amount_locale = infer_amount_locale(
        &sample,
        [amount_col, debit_col, credit_col].into_iter().flatten(),
        delimiter,
    );

    Some(InferredCsvConfig {
        delimiter,
        has_header,
        date_column,
        date_format,
        amount_column,
        debit_column,
        credit_column,
        narration_column,
        payee_column,
        currency_column,
        secondary_date,
        amount_locale,
        confidence: f64::min(confidence, 1.0),
    })
}

/// Find a per-row currency column by header name (case-insensitive). Matches
/// common spellings used by multi-currency exports; the column's values are
/// 3-letter currency codes, so we only key off the header, never the content.
fn find_currency_column(headers: &[&str]) -> Option<usize> {
    const NAMES: [&str; 6] = [
        "currency",
        "ccy",
        "curr",
        "cur",
        "currency code",
        "ccy code",
    ];
    headers.iter().position(|h| {
        let h = h.trim();
        NAMES.iter().any(|n| h.eq_ignore_ascii_case(n))
    })
}

/// Count the leading run of ASCII digits in `s` — i.e. the digits immediately
/// after a separator, ignoring any trailing currency symbol/suffix
/// (`looks_like_number` admits `$ € £ ¥` etc.), so `23€` counts as 2.
fn leading_digit_run(s: &str) -> usize {
    s.chars().take_while(char::is_ascii_digit).count()
}

/// Infer an amount [`Locale`] from the sampled values in the amount-bearing
/// columns. Returns `Some(de_DE)` when the values look comma-decimal, else
/// `None` (callers default to POSIX).
///
/// When a value has both `.` and `,`, the rightmost is taken as the decimal
/// separator (`2.500,00` -> comma-decimal, `1,234.56` -> period-decimal). A
/// lone comma or period counts as a decimal separator only when immediately
/// followed by 1-2 digits, so a thousands group like `1,234` / `1.234` stays
/// ambiguous while a decorated amount like `-54,23€` is still comma-decimal.
/// Each sampled cell across the amount-bearing columns votes; the majority wins.
///
/// # Limitation
///
/// A period-grouped integer (`1.234` meaning 1234) carries no local signal
/// distinguishing it from period-decimal `1.234` (= 1.234). When such ambiguous
/// grouping is the *only* signal, the decision falls back to `delimiter`: a
/// `;`-delimited export is read as comma-decimal (`de_DE`) — `;` is used
/// precisely because `,` is the decimal separator — while any other delimiter
/// returns `None` (POSIX). Pass `--amount-locale de_DE` to force the locale.
fn infer_amount_locale(
    sample: &[&Vec<String>],
    cols: impl Iterator<Item = usize>,
    delimiter: char,
) -> Option<Locale> {
    let cols: Vec<usize> = cols.collect();
    let mut comma_decimal = 0usize;
    let mut period_decimal = 0usize;
    // Values like `1.250` carry no local decimal signal: they're either EU
    // thousands-grouping (= 1250) or a US three-place decimal (= 1.25). Tracked
    // separately so a `;`-delimited export can break the tie (see #1441).
    let mut ambiguous_grouped = 0usize;
    for row in sample {
        for &col in &cols {
            let Some(cell) = row.get(col) else { continue };
            let v = cell.trim();
            if v.is_empty() {
                continue;
            }
            match (v.rfind(','), v.rfind('.')) {
                (Some(comma), Some(dot)) => {
                    if comma > dot {
                        comma_decimal += 1;
                    } else {
                        period_decimal += 1;
                    }
                }
                (Some(comma), None) => {
                    // 1-2 digits after a lone comma is a decimal (`-54,23`);
                    // otherwise it's thousands-grouping (`1,234`).
                    if (1..=2).contains(&leading_digit_run(&v[comma + 1..])) {
                        comma_decimal += 1;
                    } else {
                        period_decimal += 1;
                    }
                }
                (None, Some(dot)) => {
                    // Symmetric to the comma case: a lone period with 1-2
                    // trailing digits is a decimal point (`50.00`); a 3-digit
                    // group (`1.250`) is ambiguous.
                    if (1..=2).contains(&leading_digit_run(&v[dot + 1..])) {
                        period_decimal += 1;
                    } else {
                        ambiguous_grouped += 1;
                    }
                }
                (None, None) => {}
            }
        }
    }
    if comma_decimal > period_decimal {
        return Some(Locale::de_DE);
    }
    // Tie-breaker: a `;`-delimited export is almost always European — `;` is
    // chosen precisely because `,` is the decimal separator — so when the only
    // separator signal is ambiguous grouping (no firm period-decimal cell),
    // read it as comma-decimal rather than silently parsing `1.250` as 1.25.
    if period_decimal == 0 && ambiguous_grouped > 0 && delimiter == ';' {
        return Some(Locale::de_DE);
    }
    None
}

// ============================================================================
// Detection heuristics
// ============================================================================

/// Detect the most likely delimiter by trying each candidate and picking
/// the one that produces the most consistent column count.
fn detect_delimiter(content: &str) -> char {
    let candidates = [',', ';', '\t', '|'];
    let mut best_delimiter = ',';
    let mut best_score = f64::MAX;

    for &delim in &candidates {
        let counts: Vec<usize> = content
            .lines()
            .take(10)
            .filter(|l| !l.trim().is_empty())
            .map(|line| line.matches(delim).count())
            .collect();

        if counts.is_empty() || counts.iter().all(|&c| c == 0) {
            continue;
        }

        // Score = variance of column counts (lower is better)
        let mean = counts.iter().sum::<usize>() as f64 / counts.len() as f64;
        let variance = counts
            .iter()
            .map(|&c| (c as f64 - mean).powi(2))
            .sum::<f64>()
            / counts.len() as f64;

        // Prefer delimiters that produce more columns (break ties)
        let score = mean.mul_add(-0.01, variance);

        if score < best_score {
            best_score = score;
            best_delimiter = delim;
        }
    }

    best_delimiter
}

/// Parse content into rows of fields using the given delimiter.
fn parse_rows(content: &str, delimiter: char) -> Vec<Vec<String>> {
    let mut reader = csv::ReaderBuilder::new()
        .has_headers(false)
        .delimiter(delimiter as u8)
        .from_reader(content.as_bytes());

    reader
        .records()
        .take(20) // Only need first ~20 rows
        .filter_map(Result::ok)
        .map(|record| record.iter().map(String::from).collect())
        .collect()
}

/// Detect if the first row is a header by checking if it contains
/// common header keywords and/or looks different from data rows.
fn detect_header(rows: &[Vec<String>]) -> bool {
    if rows.is_empty() {
        return false;
    }

    let first_row = &rows[0];

    // Check for common header keywords
    let keywords = [
        "date",
        "amount",
        "description",
        "narration",
        "memo",
        "payee",
        "debit",
        "credit",
        "balance",
        "reference",
        "transaction",
        "type",
        "category",
        "account",
        "details",
        "particulars",
        "value",
        "posting",
        "merchant",
        "name",
        "note",
        "status",
        "check",
        "num",
        "ref",
    ];

    let keyword_matches = first_row
        .iter()
        .filter(|cell| {
            let lower = cell.to_lowercase();
            keywords.iter().any(|kw| lower.contains(kw))
        })
        .count();

    // If 2+ cells match keywords, it's likely a header
    if keyword_matches >= 2 {
        return true;
    }

    // Check if first row has no numbers but data rows do
    let first_has_numbers = first_row.iter().any(|cell| looks_like_number(cell));
    let second_has_numbers = rows
        .get(1)
        .is_some_and(|row| row.iter().any(|cell| looks_like_number(cell)));

    if !first_has_numbers && second_has_numbers {
        return true;
    }

    false
}

/// Common date formats to try, ordered by prevalence.
const DATE_FORMATS: &[&str] = &[
    "%Y-%m-%d",  // 2024-01-15
    "%m/%d/%Y",  // 01/15/2024
    "%d/%m/%Y",  // 15/01/2024
    "%Y/%m/%d",  // 2024/01/15
    "%m-%d-%Y",  // 01-15-2024
    "%d-%m-%Y",  // 15-01-2024
    "%d.%m.%Y",  // 15.01.2024
    "%m.%d.%Y",  // 01.15.2024
    "%Y.%m.%d",  // 2024.01.15
    "%b %d, %Y", // Jan 15, 2024
    "%d %b %Y",  // 15 Jan 2024
    "%B %d, %Y", // January 15, 2024
    "%d %B %Y",  // 15 January 2024
    "%m/%d/%y",  // 01/15/24
    "%d/%m/%y",  // 15/01/24
];

/// Whole-word keyword match against a (lowercased) CSV header.
///
/// A keyword matches only when it appears delimited by non-alphanumeric
/// characters or string boundaries, so short tokens like "in"/"out" no longer
/// match inside "running balance", "routing number", "beginning balance", etc.
/// (which made `--auto` steal a running-balance column as the credit/amount).
/// Multi-word keywords such as "transaction date" are matched as phrases.
fn header_matches(header_lower: &str, keywords: &[&str]) -> bool {
    keywords.iter().any(|kw| {
        header_lower.match_indices(kw).any(|(start, matched)| {
            let before = header_lower[..start].chars().next_back();
            let after = header_lower[start + matched.len()..].chars().next();
            before.is_none_or(|c| !c.is_alphanumeric())
                && after.is_none_or(|c| !c.is_alphanumeric())
        })
    })
}

/// Find the date column and its format.
fn find_date_column(
    headers: &[&str],
    sample: &[&Vec<String>],
    num_cols: usize,
) -> Option<(usize, String)> {
    // First, check headers for date keywords
    let date_keywords = [
        "date",
        "posted",
        "transaction date",
        "value date",
        "booking",
    ];
    let mut candidates: Vec<usize> = Vec::new();

    for (i, header) in headers.iter().enumerate() {
        let lower = header.to_lowercase();
        if header_matches(&lower, &date_keywords) {
            candidates.push(i);
        }
    }

    // If no header matches, try all columns
    if candidates.is_empty() {
        candidates = (0..num_cols).collect();
    }

    // Try each candidate column with each date format
    for &col_idx in &candidates {
        let values: Vec<&str> = sample
            .iter()
            .filter_map(|row| row.get(col_idx).map(String::as_str))
            .filter(|v| !v.trim().is_empty())
            .collect();

        if values.is_empty() {
            continue;
        }

        for &fmt in DATE_FORMATS {
            let parse_count = values
                .iter()
                .filter(|v| jiff::fmt::strtime::parse(fmt, v.trim()).is_ok())
                .count();

            // Require at least 80% of non-empty values to parse
            if parse_count > 0 && parse_count * 5 >= values.len() * 4 {
                return Some((col_idx, fmt.to_string()));
            }
        }
    }

    None
}

/// Find a SECOND header-named date column (distinct from `primary_idx`) whose
/// values parse as dates — e.g. a "Value Date" alongside a "Booking Date".
/// Only header-keyword-matched columns are considered, so a stray date-shaped
/// column isn't spuriously preserved. Returns `(column index, detected format)`.
/// See #1623: without this, the second date column is silently dropped.
fn find_secondary_date_column(
    headers: &[&str],
    sample: &[&Vec<String>],
    num_cols: usize,
    primary_idx: usize,
) -> Option<(usize, String)> {
    let date_keywords = [
        "date",
        "posted",
        "transaction date",
        "value date",
        "booking",
    ];
    for (i, header) in headers.iter().enumerate() {
        if i == primary_idx || i >= num_cols {
            continue;
        }
        if !header_matches(&header.to_lowercase(), &date_keywords) {
            continue;
        }
        let values: Vec<&str> = sample
            .iter()
            .filter_map(|row| row.get(i).map(String::as_str))
            .filter(|v| !v.trim().is_empty())
            .collect();
        if values.is_empty() {
            continue;
        }
        for &fmt in DATE_FORMATS {
            let parse_count = values
                .iter()
                .filter(|v| jiff::fmt::strtime::parse(fmt, v.trim()).is_ok())
                .count();
            if parse_count > 0 && parse_count * 5 >= values.len() * 4 {
                return Some((i, fmt.to_string()));
            }
        }
    }
    None
}

/// Slugify a CSV header into a beancount metadata key: lowercase, runs of
/// non-alphanumeric become a single `_`, trimmed, and prefixed if it doesn't
/// start with a letter. E.g. "Value Date" -> `value_date`, "Posted" -> `posted`.
fn header_to_meta_key(header: &str) -> String {
    let mut key = String::new();
    let mut last_underscore = false;
    for c in header.trim().to_lowercase().chars() {
        if c.is_ascii_alphanumeric() {
            key.push(c);
            last_underscore = false;
        } else if !last_underscore {
            key.push('_');
            last_underscore = true;
        }
    }
    let key = key.trim_matches('_').to_string();
    if key.chars().next().is_some_and(|c| c.is_ascii_lowercase()) {
        key
    } else {
        format!("date_{key}").trim_end_matches('_').to_string()
    }
}

/// Find amount column(s). Returns `(single_amount, debit, credit)`.
///
/// `date_col` is the already-detected date column index, which is skipped
/// during the fallback numeric-column scan (dates can look numeric).
fn find_amount_columns(
    headers: &[&str],
    sample: &[&Vec<String>],
    num_cols: usize,
    date_col: Option<usize>,
) -> (Option<usize>, Option<usize>, Option<usize>) {
    let amount_keywords = ["amount", "sum", "value", "total"];
    let debit_keywords = ["debit", "withdrawal", "out", "charge"];
    let credit_keywords = ["credit", "deposit", "in", "payment"];

    let mut amount_col = None;
    let mut debit_col = None;
    let mut credit_col = None;

    // Check headers first
    for (i, header) in headers.iter().enumerate() {
        let lower = header.to_lowercase();
        if header_matches(&lower, &debit_keywords) {
            debit_col = Some(i);
        } else if header_matches(&lower, &credit_keywords) {
            credit_col = Some(i);
        } else if header_matches(&lower, &amount_keywords) {
            amount_col = Some(i);
        }
    }

    // If we found debit/credit pair, prefer that
    if debit_col.is_some() && credit_col.is_some() {
        return (None, debit_col, credit_col);
    }

    // If we found a single amount column by header, verify it has numbers
    if let Some(col) = amount_col {
        let has_numbers = sample
            .iter()
            .filter_map(|row| row.get(col))
            .any(|v| looks_like_number(v));
        if has_numbers {
            return (Some(col), None, None);
        }
    }

    // Fall back to finding numeric columns
    for col_idx in 0..num_cols {
        // Skip the date column — dates can look numeric
        if date_col == Some(col_idx) {
            continue;
        }

        let values: Vec<&str> = sample
            .iter()
            .filter_map(|row| row.get(col_idx).map(String::as_str))
            .filter(|v| !v.trim().is_empty())
            .collect();

        if values.is_empty() {
            continue;
        }

        let number_count = values.iter().filter(|v| looks_like_number(v)).count();

        // If 80%+ look like numbers, this is probably an amount column
        if number_count * 5 >= values.len() * 4 && amount_col.is_none() {
            amount_col = Some(col_idx);
        }
    }

    (amount_col, None, None)
}

/// Find text columns for narration and payee.
fn find_text_columns(
    headers: &[&str],
    num_cols: usize,
    date_col: Option<usize>,
    amount_col: Option<usize>,
    debit_col: Option<usize>,
    credit_col: Option<usize>,
    currency_col: Option<usize>,
) -> (Option<usize>, Option<usize>) {
    let narration_keywords = [
        "description",
        "narration",
        "memo",
        "details",
        "particulars",
        "reference",
        "transaction",
        "text",
    ];
    let payee_keywords = [
        "payee",
        "merchant",
        "name",
        "vendor",
        "beneficiary",
        "recipient",
    ];

    let used_cols: Vec<usize> = [date_col, amount_col, debit_col, credit_col, currency_col]
        .iter()
        .filter_map(|c| *c)
        .collect();

    let mut narration_col = None;
    let mut payee_col = None;

    // Check headers
    for (i, header) in headers.iter().enumerate() {
        if used_cols.contains(&i) {
            continue;
        }
        let lower = header.to_lowercase();
        if header_matches(&lower, &payee_keywords) && payee_col.is_none() {
            payee_col = Some(i);
        } else if header_matches(&lower, &narration_keywords) && narration_col.is_none() {
            narration_col = Some(i);
        }
    }

    // If no narration found by header, pick the first unused text column
    if narration_col.is_none() {
        for i in 0..num_cols {
            if !used_cols.contains(&i) && payee_col != Some(i) {
                narration_col = Some(i);
                break;
            }
        }
    }

    (narration_col, payee_col)
}

/// Check if a string looks like a number (for amount/date detection).
fn looks_like_number(s: &str) -> bool {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return false;
    }
    // Strip currency symbols and parentheses
    let cleaned: String = trimmed
        .chars()
        .filter(|c| !matches!(c, '$' | '' | '£' | '¥' | '(' | ')'))
        .collect();
    let cleaned = cleaned.trim();
    if cleaned.is_empty() {
        return false;
    }
    // A numeric cell is digits plus grouping/decimal separators and an optional
    // leading sign. Repeated separators are allowed: a period-grouped value like
    // `1.234.567,00` carries several `.` yet is still a number — which separator
    // is the decimal point is resolved later by `infer_amount_locale`, not here.
    // Capping the dot count would drop the whole amount column for million-plus
    // European exports (see #1442).
    let mut has_digit = false;
    for (i, c) in cleaned.chars().enumerate() {
        match c {
            '0'..='9' => has_digit = true,
            '.' | ',' => {}
            '-' | '+' if i == 0 => {}
            _ => return false,
        }
    }
    has_digit
}

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

    #[test]
    fn detect_comma_delimiter() {
        let csv = "Date,Amount,Description\n2024-01-15,-50.00,Coffee\n2024-01-16,-12.00,Lunch\n";
        assert_eq!(detect_delimiter(csv), ',');
    }

    #[test]
    fn detect_semicolon_delimiter() {
        let csv = "Date;Amount;Description\n2024-01-15;-50.00;Coffee\n2024-01-16;-12.00;Lunch\n";
        assert_eq!(detect_delimiter(csv), ';');
    }

    #[test]
    fn detect_tab_delimiter() {
        let csv =
            "Date\tAmount\tDescription\n2024-01-15\t-50.00\tCoffee\n2024-01-16\t-12.00\tLunch\n";
        assert_eq!(detect_delimiter(csv), '\t');
    }

    #[test]
    fn detect_header_with_keywords() {
        let rows = vec![
            vec![
                "Date".to_string(),
                "Amount".to_string(),
                "Description".to_string(),
            ],
            vec![
                "2024-01-15".to_string(),
                "-50.00".to_string(),
                "Coffee".to_string(),
            ],
        ];
        assert!(detect_header(&rows));
    }

    #[test]
    fn detect_no_header() {
        let rows = vec![
            vec![
                "2024-01-15".to_string(),
                "-50.00".to_string(),
                "Coffee".to_string(),
            ],
            vec![
                "2024-01-16".to_string(),
                "-12.00".to_string(),
                "Lunch".to_string(),
            ],
        ];
        assert!(!detect_header(&rows));
    }

    #[test]
    fn looks_like_number_positive() {
        assert!(looks_like_number("50.00"));
        assert!(looks_like_number("-50.00"));
        assert!(looks_like_number("+50.00"));
        assert!(looks_like_number("1,234.56"));
        assert!(looks_like_number("$50.00"));
        assert!(looks_like_number("(50.00)"));
        // Period-grouped values with two+ separators (#1442): a million-plus
        // European amount must still register as numeric so its column is kept.
        assert!(looks_like_number("1.234.567,00"));
        assert!(looks_like_number("-2.000,00"));
        assert!(looks_like_number("1,234,567.00"));
    }

    #[test]
    fn looks_like_number_negative() {
        assert!(!looks_like_number("Coffee"));
        assert!(!looks_like_number("2024-01-15"));
        assert!(!looks_like_number(""));
        assert!(!looks_like_number("ABC123"));
    }

    #[test]
    fn infer_simple_csv() {
        let csv = "\
Date,Description,Amount
2024-01-15,Coffee shop,-5.50
2024-01-16,Grocery store,-42.00
2024-01-17,Salary,3000.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert_eq!(config.delimiter, ',');
        assert!(config.has_header);
        assert_eq!(config.date_format, "%Y-%m-%d");
        assert!(config.amount_column.is_some());
        assert!(config.narration_column.is_some());
        assert!(config.confidence > 0.5);
    }

    #[test]
    fn infer_us_date_format() {
        let csv = "\
Date,Description,Amount
01/15/2024,Coffee shop,-5.50
01/16/2024,Grocery store,-42.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert_eq!(config.date_format, "%m/%d/%Y");
    }

    #[test]
    fn infer_semicolon_csv() {
        let csv = "\
Date;Description;Amount
2024-01-15;Coffee shop;-5.50
2024-01-16;Grocery store;-42.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert_eq!(config.delimiter, ';');
    }

    #[test]
    fn infer_debit_credit_columns() {
        let csv = "\
Date,Description,Debit,Credit
2024-01-15,Coffee shop,5.50,
2024-01-16,Salary,,3000.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(config.debit_column.is_some());
        assert!(config.credit_column.is_some());
        assert!(config.amount_column.is_none());
    }

    #[test]
    fn infer_preserves_secondary_date_column() {
        // Booking Date is the primary (directive) date; Value Date is the second
        // date column and must be preserved rather than silently dropped (#1623).
        let csv = "\
Booking Date,Description,Value Date,Amount
2024-01-15,Coffee,2024-01-17,-5.00
2024-01-16,Salary,2024-01-16,3000.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            matches!(config.date_column, ColumnSpec::Name(ref n) if n == "Booking Date"),
            "primary date should be Booking Date, got {:?}",
            config.date_column
        );
        let sd = config
            .secondary_date
            .as_ref()
            .expect("Value Date should be preserved as a secondary date");
        assert!(matches!(sd.column, ColumnSpec::Name(ref n) if n == "Value Date"));
        assert_eq!(sd.meta_key, "value_date");
    }

    #[test]
    fn single_date_column_has_no_secondary() {
        let csv = "\
Date,Description,Amount
2024-01-15,Coffee,-5.00
2024-01-16,Salary,3000.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(config.secondary_date.is_none());
    }

    #[test]
    fn header_to_meta_key_slugifies() {
        assert_eq!(header_to_meta_key("Value Date"), "value_date");
        assert_eq!(header_to_meta_key("Booking Date"), "booking_date");
        assert_eq!(header_to_meta_key("Posted"), "posted");
        assert_eq!(header_to_meta_key("Settlement / Value"), "settlement_value");
        // Leading non-letter gets a `date_` prefix so the key stays valid.
        assert_eq!(header_to_meta_key("2nd date"), "date_2nd_date");
    }

    #[test]
    fn infer_detects_currency_column() {
        let csv = "\
Date,Description,Amount,Currency
2024-01-02,Coffee,-5.00,EUR
2024-01-05,Salary,2000.00,USD
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            matches!(config.currency_column, Some(ColumnSpec::Name(ref n)) if n == "Currency"),
            "expected Currency column to be detected, got {:?}",
            config.currency_column
        );
        // And it must not be misclassified as the amount column.
        assert!(matches!(config.amount_column, Some(ColumnSpec::Name(ref n)) if n == "Amount"));
    }

    #[test]
    fn infer_currency_column_not_stolen_as_narration() {
        // No "Description" column: the narration fallback must NOT grab the
        // currency column.
        let csv = "\
Date,Payee,Amount,Currency
2024-01-02,Cafe,-5.00,EUR
2024-01-05,Work,2000.00,USD
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(matches!(config.currency_column, Some(ColumnSpec::Name(ref n)) if n == "Currency"));
        let not_currency =
            |c: &Option<ColumnSpec>| !matches!(c, Some(ColumnSpec::Name(n)) if n == "Currency");
        assert!(
            not_currency(&config.narration_column),
            "currency stolen as narration"
        );
        assert!(
            not_currency(&config.payee_column),
            "currency stolen as payee"
        );
    }

    #[test]
    fn infer_does_not_steal_running_balance_as_credit() {
        // Regression: "Running Balance" must not be classified as the credit
        // column. The word "running" contains the letters "in", which the old
        // substring match treated as the `in` credit keyword, stealing the
        // running balance as the amount. With Debit + Credit + Running Balance,
        // the pair must be the real Debit/Credit columns.
        let csv = "\
Date,Description,Debit,Credit,Running Balance
2024-01-15,Deposit,,100.00,1100.00
2024-01-16,Withdraw,40.00,,1060.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        match &config.debit_column {
            Some(ColumnSpec::Name(n)) => assert_eq!(n, "Debit"),
            other => panic!("debit column should be 'Debit', got {other:?}"),
        }
        match &config.credit_column {
            Some(ColumnSpec::Name(n)) => assert_eq!(n, "Credit"),
            other => panic!("credit column should be 'Credit', got {other:?}"),
        }
    }

    #[test]
    fn infer_with_payee_column() {
        let csv = "\
Date,Payee,Description,Amount
2024-01-15,Starbucks,Morning coffee,-5.50
2024-01-16,Whole Foods,Groceries,-42.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(config.payee_column.is_some());
        assert!(config.narration_column.is_some());
    }

    #[test]
    fn infer_comma_decimal_locale() {
        // Comma-decimal exports (-54,23 ; 2.500,00) must infer a comma-decimal
        // locale so --auto parses them correctly instead of as POSIX (which
        // read -54,23 as -5423 and 2.500,00 as 2.50000).
        let csv = "\
Date;Description;Amount
2024-01-15;Coffee;-54,23
2024-01-16;Salary;2.500,00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            config.amount_locale.is_some(),
            "expected a comma-decimal locale, got {:?}",
            config.amount_locale
        );
    }

    #[test]
    fn infer_period_decimal_locale_is_none() {
        // Period-decimal amounts must not be mistaken for European.
        let csv = "\
Date,Description,Amount
2024-01-15,Coffee,-54.23
2024-01-16,Salary,2500.00
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(config.amount_locale.is_none());
    }

    #[test]
    fn infer_semicolon_grouped_integers_break_to_european() {
        // #1441: amounts that are period-grouped integers (1.250 = 1250) carry
        // no local decimal signal, but a `;` delimiter strongly implies a
        // European export, so the tie breaks to comma-decimal.
        let csv = "\
Date;Description;Amount
2024-01-15;Rent;1.250
2024-01-16;Salary;3.000
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            config.amount_locale.is_some(),
            "expected European tie-break for ;-delimited grouped integers, got {:?}",
            config.amount_locale
        );
    }

    #[test]
    fn infer_comma_delimited_grouped_integers_stay_posix() {
        // The same ambiguous grouping under a comma delimiter is a US export,
        // so it must NOT break to European (1.250 stays 1.25).
        let csv = "\
Date,Description,Amount
2024-01-15,Rent,1.250
2024-01-16,Salary,3.000
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            config.amount_locale.is_none(),
            "comma-delimited grouped integers must stay POSIX, got {:?}",
            config.amount_locale
        );
    }

    #[test]
    fn infer_semicolon_with_firm_period_decimal_stays_posix() {
        // A `;`-delimited file that DOES have a firm period-decimal cell
        // (50.00) is not overridden by the European tie-break.
        let csv = "\
Date;Description;Amount
2024-01-15;Coffee;50.00
2024-01-16;Big;1.250
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            config.amount_locale.is_none(),
            "a firm period-decimal cell must keep POSIX, got {:?}",
            config.amount_locale
        );
    }

    #[test]
    fn infer_thousands_comma_not_mistaken_for_decimal() {
        // A lone comma with 3 trailing digits is a thousands group, not a
        // decimal separator, so it must stay period-style.
        let csv = "\
Date;Description;Amount
2024-01-15;Coffee;1,234
2024-01-16;Salary;5,678
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(config.amount_locale.is_none());
    }

    #[test]
    fn infer_comma_decimal_with_currency_suffix() {
        // A trailing currency symbol must not defeat comma-decimal inference:
        // the digit count after the comma is what matters, not the byte length.
        let csv = "\
Date;Description;Amount
2024-01-15;Coffee;-54,23€
2024-01-16;Salary;1.500,00€
";
        let config = infer_csv_config(csv).expect("should infer config");
        assert!(
            config.amount_locale.is_some(),
            "currency-decorated comma-decimal should infer European, got {:?}",
            config.amount_locale
        );
    }

    #[test]
    fn infer_empty_content_returns_none() {
        assert!(infer_csv_config("").is_none());
        assert!(infer_csv_config("   \n  \n").is_none());
    }

    #[test]
    fn infer_single_row_returns_none() {
        assert!(infer_csv_config("Date,Amount\n").is_none());
    }

    #[test]
    fn inferred_to_csv_config() {
        let csv = "\
Date,Description,Amount
2024-01-15,Coffee,-5.50
2024-01-16,Lunch,-12.00
";
        let inferred = infer_csv_config(csv).expect("should infer");
        let config = inferred.to_csv_config();
        assert_eq!(config.delimiter, ',');
        assert!(config.has_header);
        assert_eq!(config.date_format, "%Y-%m-%d");
    }
}