File size: 55,650 Bytes
63f0c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efdc2ad
 
63f0c94
 
 
 
efdc2ad
63f0c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
from __future__ import annotations

import io
import re
from typing import Any

import pandas as pd

from tools.financial_calculator import calculate_profit, calculate_profit_margin
from tools.text_parser import parse_business_input


DOCUMENT_TYPES = [
    "Income Statement",
    "Cash Flow Record",
    "Sales Record",
    "Expense Record",
    "Bank Statement",
    "Mobile Money Statement",
    "Receipts",
    "Inventory/Stock Record",
    "Loan or Debt Record",
    "General Business Notes",
    "Other",
]

# ── LLM system prompt for document parsing ─────────────────────────────────────

_DOC_PARSE_SYSTEM = """You are a financial document parser for African small business records.
Your job is to extract clean financial totals from raw document content β€” without double-counting.

CRITICAL RULES:
- Identify TOTALS vs LINE ITEMS vs SUBTOTALS. Only extract the final TOTAL revenue and TOTAL expenses.
- In multi-sheet Excel files, summary sheets often repeat totals already captured in detail sheets β€” do NOT add them together.
- If a document has both an Income Statement and a Cash Flow Statement, prefer the Income Statement for revenue and expenses.
- Negative numbers in cash flow statements represent outflows (expenses/payments) β€” treat their absolute value as expenses.
- For Income Statements: total_expenses = ALL costs combined (cost of goods + operating expenses + any other costs). Do NOT split them.
- Opening/closing balances, averages, and running totals are NOT revenue or expenses β€” ignore them.
- Do NOT sum all numbers blindly. Read labels carefully to understand what each number represents.
- If a value is unclear or cannot be reliably determined, return null β€” do NOT guess.
- Auto-detect the real document type regardless of what the user labeled it.
- Amounts are in Zambian Kwacha (K / ZMW) unless another currency is clearly stated.
- IMPORTANT: Every value in the JSON must be a plain pre-computed number. NEVER write arithmetic expressions like "18600 + 46980" β€” compute the sum yourself and write only the result.
- CRITICAL: Extract numbers ONLY from the DOCUMENT CONTENT provided by the user. NEVER reuse example numbers or labels from the schema/template. If the document does not contain a value, return null β€” do NOT invent or carry over numbers from prior conversations."""

_DOC_PARSE_USER_TEMPLATE = """Filename: {filename}
User-labeled type: {doc_type}

DOCUMENT CONTENT:
{content}

Extract financial data and return ONLY a JSON object. No markdown fences, no extra text.
All numeric fields must be plain pre-computed numbers (never expressions like "1000 + 2000").

CRITICAL: The schema below uses <PLACEHOLDER> tokens to show the SHAPE of the JSON only.
You MUST replace every <PLACEHOLDER> with a value computed from the actual DOCUMENT CONTENT above.
NEVER copy these placeholder strings or any example values into your output β€” extract from the document.

{{
  "detected_document_type": "<one of: Income Statement | Sales Record | Cash Flow Record | Expense Record | Bank Statement | Other>",
  "currency": "<currency code from document, e.g. K>",
  "period_description": "<human readable period from document, or null>",
  "period_start": "<YYYY-MM-DD or null>",
  "period_end": "<YYYY-MM-DD or null>",
  "total_revenue": <number computed from document, or null>,
  "total_expenses": <number computed from document, or null>,
  "profit": <number computed from document, or null>,
  "debt_balance": <number from document, or null>,
  "expense_breakdown": [
    {{"category": "<expense category name from document>", "amount": <number from document>}}
  ],
  "product_breakdown": [
    {{"product": "<product name from document>", "units_sold": <number>, "unit_price": <number>, "revenue": <number>}}
  ],
  "monthly_breakdown": [{{"month": "<month label from document>", "revenue": <number>, "expenses": <number>, "profit": <number>}}],
  "key_observations": ["<short factual observation grounded in the document>"],
  "confidence": "<high | medium | low>",
  "warnings": []
}}

Rules for detected_document_type:
- If the document has Product/Item columns with Units Sold and/or Unit Price β†’ "Sales Record"
- If it has Revenue, Expenses, Profit rows β†’ "Income Statement"
- If it has Cash In / Cash Out / Bank transactions β†’ "Cash Flow Record" or "Bank Statement"
- Choose the most accurate type β€” do NOT default to "Income Statement".

Rules for expense_breakdown:
- List each distinct expense LINE ITEM (not subtotals). Return [] if none visible.
- Amounts must be totals across the full period (not per month).

Rules for product_breakdown:
- ONLY populate when detected_document_type is "Sales Record" or the document clearly lists products with quantities/prices.
- For each product row: "product" = name, "units_sold" = quantity, "unit_price" = price per unit, "revenue" = units Γ— price.
- If revenue per product is stated directly, use that value.
- Return [] for all other document types.

Now extract the actual data from the document above and return your JSON:"""


def _normalize_currency(raw: Any) -> str:
    """Force any LLM-returned currency string down to a short symbol like 'K'.
    Prevents 'Zambian Kwacha (K)' + amount = 'Zambian Kwacha (K)18,600' double-prefix.
    """
    if not raw:
        return "K"
    s = str(raw).strip()
    upper = s.upper()
    if "ZMW" in upper or "KWACHA" in upper or upper == "K":
        return "K"
    if upper in {"USD", "US$", "$", "EUR", "€", "GBP", "Β£"}:
        return "K"
    # Strip parentheticals like "Zambian Kwacha (K)" β†’ "K"
    m = re.search(r"\(([^)]{1,3})\)", s)
    if m:
        return m.group(1)
    return "K"


# ── Raw content extractors ─────────────────────────────────────────────────────

def _extract_excel_as_text(file_bytes: bytes) -> tuple[str, int]:
    """Read all sheets from an xlsx file and return as structured text + row count."""
    from openpyxl import load_workbook

    wb = load_workbook(io.BytesIO(file_bytes), data_only=True)
    sheets_text: list[str] = []
    total_rows = 0

    for sheet_name in wb.sheetnames:
        ws = wb[sheet_name]
        sheet_lines = [f"=== SHEET: {sheet_name} ==="]

        for row in ws.iter_rows(values_only=True):
            if all(cell is None or str(cell).strip() == "" for cell in row):
                continue
            row_text = " | ".join(str(cell) if cell is not None else "" for cell in row)
            if row_text.strip(" |"):
                sheet_lines.append(row_text)
                total_rows += 1

        if len(sheet_lines) > 1:
            sheets_text.append("\n".join(sheet_lines))

    return "\n\n".join(sheets_text), total_rows


def _parse_excel_by_sheets(file_bytes: bytes, filename: str = "") -> dict[str, Any] | None:
    """
    Sheet-aware structured extraction for multi-sheet Excel workbooks.
    Reads Income Statement / Cash Flow / Debt sheets independently to prevent mixing.
    Returns a complete result dict on success, or None to fall through to the LLM path.
    """
    try:
        from openpyxl import load_workbook
    except ImportError:
        return None

    try:
        wb = load_workbook(io.BytesIO(file_bytes), data_only=True)
    except Exception:
        return None

    names_lower = {s.lower().strip(): s for s in wb.sheetnames}

    def find_sheet(*keywords: str):
        for kw in keywords:
            for nl, original in names_lower.items():
                if kw in nl:
                    return wb[original]
        return None

    ws_income = find_sheet("income statement", "income", "profit & loss", "p&l", "pnl")
    ws_debt   = find_sheet("debt", "loan", "liabilit", "payable")
    ws_cf     = find_sheet("cash flow", "cashflow")

    # Single-sheet workbook: assume the only sheet is the income statement
    if ws_income is None and len(wb.sheetnames) == 1:
        ws_income = wb[wb.sheetnames[0]]

    if ws_income is None:
        return None  # No recognisable Income Statement β†’ let LLM handle it

    # ── cell β†’ float (handles parentheses negatives and K/ZMW prefixes) ─────────
    def to_num(v) -> float | None:
        if v is None:
            return None
        if isinstance(v, (int, float)):
            return float(v)
        s = re.sub(r"[KZMWkzmw\s,]", "", str(v))
        if s.startswith("(") and s.endswith(")"):
            s = "-" + s[1:-1]
        try:
            return float(s)
        except (ValueError, TypeError):
            return None

    def label_matches(label: str, keywords: list[str]) -> bool:
        nl = re.sub(r"[^a-z0-9/(). ]+", " ", label.lower()).strip()
        for kw in keywords:
            nk = re.sub(r"[^a-z0-9/(). ]+", " ", kw.lower()).strip()
            if nk == nl or nk in nl:
                return True
        return False

    def find_total_col_idx(ws) -> int | None:
        """
        Scan the first 5 rows (skipping column 0, which holds row labels) to find
        a column whose header contains 'total' or 'sum' but NOT 'average'.
        Returns the 0-based column index, or None if not found.
        """
        TOTAL_KWS = ("total", "sum", "full year", "6-month", "6 month", "period total")
        SKIP_KWS  = ("average", "avg", "mean")
        for row in ws.iter_rows(max_row=5, values_only=True):
            for idx, cell in enumerate(row):
                if idx == 0 or cell is None:
                    continue  # col 0 = row label column
                s = str(cell).strip().lower()
                if any(kw in s for kw in TOTAL_KWS) and not any(kw in s for kw in SKIP_KWS):
                    return idx
        return None

    def find_row_value(ws, keywords: list[str], total_col_idx: int | None = None) -> float | None:
        """
        Scan worksheet rows; return the period total of the best-matching row.

        Priority order:
        1. Cell in the explicitly identified Total column (most reliable).
        2. Sum all numeric cells in the row when the row has β‰₯ 4 numbers
           (handles multi-month statements that have no separate Total column).
        3. Last numeric cell in the row (single-value fallback).
        """
        candidates: list[tuple[float, bool]] = []
        for row in ws.iter_rows(values_only=True):
            label = next((str(c).strip() for c in row if c is not None and str(c).strip()), None)
            if not label or not label_matches(label, keywords):
                continue

            val: float | None = None

            # Strategy 1 β€” use the detected Total column
            if total_col_idx is not None and total_col_idx < len(row):
                val = to_num(row[total_col_idx])

            # Strategy 2 β€” derive total from numeric cells in the row
            if val is None:
                nums = [n for n in (to_num(c) for c in row) if n is not None]
                if nums:
                    # 4+ numbers β†’ multi-month layout; sum gives the period total
                    # 1-3 numbers β†’ single-value or 2-col row; take the last
                    val = sum(nums) if len(nums) >= 4 else nums[-1]

            if val is None:
                continue
            candidates.append((val, "total" in label.lower()))

        if not candidates:
            return None
        # Prefer rows whose label contains "total"
        for val, is_total in candidates:
            if is_total:
                return val
        return candidates[0][0]

    def classify_income_row(label: str) -> str | None:
        """Classify an income-statement line item as revenue/expense/ignore."""
        nl = re.sub(r"[^a-z0-9/(). ]+", " ", label.lower()).strip()
        if not nl:
            return None

        # Generic totals/subtotals often duplicate detail rows and should not be
        # included in derived sums.
        if "total" in nl or "subtotal" in nl or "grand total" in nl:
            return None

        ignore_tokens = (
            "line item",
            "key ratios",
            "analysis",
            "auditor",
            "notes",
            "margin",
            "ratio",
            "profit",
            "loss",
            "gross profit",
            "operating profit",
            "net profit",
            "total revenue",
            "total income",
            "total sales",
            "total expenses",
            "total operating expenses",
            "total other expenses",
            "total cost of goods sold",
            "total costs",
        )
        if any(tok in nl for tok in ignore_tokens):
            return None
        if nl in {"revenue", "operating expenses"}:
            return None

        revenue_tokens = ("sales", "revenue", "income", "turnover", "receipts")
        expense_tokens = (
            "expense",
            "expenses",
            "cost",
            "rent",
            "wage",
            "salary",
            "electricity",
            "water",
            "utility",
            "transport",
            "loan",
            "repayment",
            "airtime",
            "marketing",
            "maintenance",
            "repair",
            "interest",
            "fee",
            "freight",
            "delivery",
            "purchase",
            "supplier",
        )
        if any(tok in nl for tok in revenue_tokens):
            return "revenue"
        if any(tok in nl for tok in expense_tokens):
            return "expense"
        return None

    def row_period_value(row, total_col_idx: int | None = None) -> float | None:
        """Return one row's period value from total column or monthly cells."""
        value: float | None = None
        if total_col_idx is not None and total_col_idx < len(row):
            value = to_num(row[total_col_idx])
        if value is not None:
            return value

        nums = [n for n in (to_num(c) for c in row) if n is not None]
        if not nums:
            return None
        return sum(nums) if len(nums) >= 4 else nums[-1]

    def derive_totals_from_income_rows(
        ws, total_col_idx: int | None = None
    ) -> tuple[float | None, float | None, dict[str, float]]:
        """
        Derive revenue/expenses from detailed line items when explicit total rows
        are missing or formula cells are not available in data-only mode.
        """
        revenue_sum = 0.0
        expense_sum = 0.0
        seen_revenue = False
        seen_expense = False

        for row in ws.iter_rows(values_only=True):
            label = next((str(c).strip() for c in row if c is not None and str(c).strip()), None)
            if not label:
                continue
            kind = classify_income_row(label)
            if kind is None:
                continue

            val = row_period_value(row, total_col_idx)
            if val is None:
                continue
            val = abs(val)
            if kind == "revenue":
                revenue_sum += val
                seen_revenue = True
            elif kind == "expense":
                expense_sum += val
                seen_expense = True

        expense_breakdown: dict[str, float] = {}

        def normalize_expense_label(label: str) -> str:
            clean = re.sub(r"\s+", " ", label.strip())
            return clean.title() if clean.islower() else clean

        for row in ws.iter_rows(values_only=True):
            label = next((str(c).strip() for c in row if c is not None and str(c).strip()), None)
            if not label:
                continue
            if classify_income_row(label) != "expense":
                continue
            val = row_period_value(row, total_col_idx)
            if val is None:
                continue
            expense_breakdown[normalize_expense_label(label)] = round(abs(val), 2)

        return (
            revenue_sum if seen_revenue else None,
            expense_sum if seen_expense else None,
            expense_breakdown,
        )

    def infer_period_description(ws) -> str | None:
        """Infer period label like 'Nov 2025 – Apr 2026' from any leading metadata
        or column headers. Returns the chronological span (earliest – latest)."""
        month_pattern = re.compile(
            r"\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\s+\d{4}\b",
            flags=re.IGNORECASE,
        )
        month_order = {
            m: i + 1
            for i, m in enumerate(
                ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
            )
        }

        def sort_key(token: str) -> tuple[int, int]:
            mtxt, ytxt = token.split()
            return int(ytxt), month_order.get(mtxt[:3].lower(), 0)

        tokens: list[str] = []
        for row in ws.iter_rows(max_row=10, values_only=True):
            for cell in row:
                if cell is None:
                    continue
                text = str(cell)
                if not text.strip():
                    continue
                tokens.extend(m.group(0) for m in month_pattern.finditer(text))

        if not tokens:
            return None
        unique = sorted({t for t in tokens}, key=sort_key)
        if len(unique) == 1:
            return unique[0]
        return f"{unique[0]} – {unique[-1]}"

    def count_non_empty_rows(ws) -> int:
        count = 0
        for row in ws.iter_rows(values_only=True):
            if any(cell is not None and str(cell).strip() != "" for cell in row):
                count += 1
        return count

    # ── Income Statement ──────────────────────────────────────────────────────────
    # Detect Total column once; all row lookups use it so we never pick up a
    # monthly slice or an average column by accident.
    income_total_col = find_total_col_idx(ws_income)

    revenue = find_row_value(ws_income, [
        "total revenue", "total sales", "total income", "gross revenue",
    ], income_total_col)
    expenses = find_row_value(ws_income, [
        "total expenses", "total costs", "total operating expenses",
        "total expenditure", "total cost of sales",
    ], income_total_col)
    profit = find_row_value(ws_income, [
        "net profit / (loss)", "net profit/(loss)", "net profit", "net loss", "net income",
    ], income_total_col)
    if profit is None:
        # Fall back to operating profit only when net profit is unavailable.
        profit = find_row_value(ws_income, [
            "profit / (loss)", "profit/(loss)", "operating profit",
        ], income_total_col)

    # If formula-based total rows are blank in data-only mode, derive from detail rows.
    derived_revenue, derived_expenses, derived_expense_breakdown = derive_totals_from_income_rows(
        ws_income, income_total_col
    )
    if revenue is None and derived_revenue is not None:
        revenue = derived_revenue
    if expenses is None and derived_expenses is not None:
        expenses = derived_expenses

    if revenue is None:
        return None  # Can't extract without at least a revenue figure

    revenue = abs(revenue)
    if expenses is not None:
        expenses = abs(expenses)
    elif profit is not None:
        expenses = revenue - profit
    else:
        expenses = 0.0

    if profit is None:
        profit = revenue - expenses
    # profit keeps its sign (negative = loss)

    # Some statements expose "total operating expenses" (excluding COGS/finance)
    # while profit is net. Force accounting identity to avoid mixed subtotals.
    mismatch = abs((revenue - expenses) - profit)
    tolerance = max(1.0, abs(revenue) * 0.01)
    if mismatch > tolerance:
        recomputed_expenses = revenue - profit
        if recomputed_expenses >= 0:
            expenses = recomputed_expenses

    # ── Debt sheet ───────────────────────────────────────────────────────────────
    debt = 0.0
    if ws_debt is not None:
        total_debt = find_row_value(ws_debt, [
            "total debt", "total loans", "total liabilities", "total balance",
            "total", "grand total",
        ])
        if total_debt is not None:
            debt = abs(total_debt)
        else:
            # Sum individual data rows, skipping obvious header cells
            HEADER_LABELS = {
                "date", "lender", "creditor", "description", "type",
                "debt type", "loan type", "amount", "balance", "notes",
            }
            for row in ws_debt.iter_rows(values_only=True):
                label = next((str(c).strip() for c in row if c is not None and str(c).strip()), None)
                if not label or label.lower().strip() in HEADER_LABELS:
                    continue
                val = row_last_num(row)
                if val is not None and val > 0:
                    debt += val

    # ── Cash Flow (inflow/outflow only β€” do NOT override IS revenue/expenses) ────
    cash_inflow, cash_outflow = revenue, expenses
    if ws_cf is not None:
        cf_in = find_row_value(ws_cf, [
            "total cash inflows", "total inflows", "total receipts",
        ])
        cf_out = find_row_value(ws_cf, [
            "total cash outflows", "total outflows", "total payments",
        ])
        if cf_in is not None:
            cash_inflow = abs(cf_in)
        if cf_out is not None:
            cash_outflow = abs(cf_out)

    margin = calculate_profit_margin(revenue, profit)
    if derived_expense_breakdown:
        biggest_cats = [
            c for c, _ in sorted(derived_expense_breakdown.items(), key=lambda x: x[1], reverse=True)[:3]
        ]
    else:
        biggest_cats = []
    sheets_used = {
        "income": ws_income.title,
        "debt":   ws_debt.title if ws_debt else None,
        "cashflow": ws_cf.title if ws_cf else None,
    }
    period_description = infer_period_description(ws_income)
    rows_analyzed = count_non_empty_rows(ws_income)

    result = _blank_result("xlsx", filename, "Income Statement")
    result.update({
        "rows_analyzed":  rows_analyzed,
        "period_description": period_description,
        "date_range": period_description or "Unknown",
        "revenue":        round(revenue, 2),
        "expenses":       round(expenses, 2),
        "profit":         round(profit, 2),
        "debt":           round(debt, 2),
        "cash_inflow":    round(cash_inflow, 2),
        "cash_outflow":   round(cash_outflow, 2),
        "profit_margin":  round(margin, 2),
        "confidence":     "high",
        "used_llm":       False,
        "expenses_breakdown": derived_expense_breakdown,
        "biggest_expense_categories": biggest_cats,
        "summary": (
            f"Structured multi-sheet extraction from '{ws_income.title}' "
            f"(Revenue: K{revenue:,.2f}, Expenses: K{expenses:,.2f}, "
            f"Profit: K{profit:,.2f}, Margin: {margin:.1f}%)."
        ),
        "warnings": [],
        "metadata": {"sheets_used": sheets_used},
    })
    return result


def _extract_csv_as_text(file_bytes: bytes) -> tuple[str, int]:
    frame = pd.read_csv(io.BytesIO(file_bytes))
    return frame.to_string(max_rows=300), len(frame)


def _extract_pdf_as_text(file_bytes: bytes) -> tuple[str, int]:
    from pypdf import PdfReader
    reader = PdfReader(io.BytesIO(file_bytes))
    pages = [page.extract_text() or "" for page in reader.pages]
    return "\n\n".join(pages), len(pages)


def _truncate(text: str, max_chars: int = 10000) -> str:
    if len(text) <= max_chars:
        return text
    return text[:8000] + "\n\n... [content truncated] ...\n\n" + text[-2000:]


# ── LLM-based parser ───────────────────────────────────────────────────────────

def _sanitize_llm_json(text: str) -> str:
    """Replace arithmetic expressions like '18600 + 46980' with their computed value."""
    import re
    def _eval_expr(m: re.Match) -> str:
        try:
            val = eval(m.group(0), {"__builtins__": {}})  # noqa: S307 β€” safe: only digits/ops
            return str(val)
        except Exception:
            return m.group(0)
    # Match patterns like: 18600 + 46980  or  18600+46980  (digits, +, -, *, / only)
    return re.sub(r"\d[\d\s]*[\+\-\*\/][\d\s]+\d", _eval_expr, text)


def _llm_parse_document(raw_text: str, document_type: str, filename: str) -> dict[str, Any] | None:
    """Call the LLM to intelligently extract financial data from raw document text.

    Raises exceptions so callers can surface the real error rather than silently falling back.
    """
    from agents import get_chat_model, _extract_json_block
    from langchain_core.messages import HumanMessage, SystemMessage

    llm = get_chat_model()
    if llm is None:
        raise RuntimeError(
            "LLM not configured (set LLM_PROVIDER=amd with AMD_BASE_URL/AMD_MODEL "
            "or LLM_PROVIDER=openai with OPENAI_API_KEY, OPENAI_BASE_URL, MODEL_NAME)."
        )

    user_content = _DOC_PARSE_USER_TEMPLATE.format(
        filename=filename,
        doc_type=document_type,
        content=_truncate(raw_text),
    )

    response = llm.invoke(
        [SystemMessage(content=_DOC_PARSE_SYSTEM), HumanMessage(content=user_content)],
        temperature=0.1,
        max_tokens=1800,
    )
    raw_response = getattr(response, "content", None)
    if not isinstance(raw_response, str) or not raw_response.strip():
        raise ValueError("LLM returned an empty response.")

    sanitized = _sanitize_llm_json(raw_response)
    parsed = _extract_json_block(sanitized)
    if parsed is None:
        raise ValueError(f"Could not parse JSON from LLM response. First 300 chars: {sanitized[:300]!r}")
    return parsed


# ── Result schema ──────────────────────────────────────────────────────────────

def _blank_result(source_type: str, filename: str = "", document_type: str = "Other") -> dict[str, Any]:
    return {
        "document_type": document_type,
        "source_type": source_type,
        "file_name": filename,
        "rows_analyzed": 0,
        "pages_analyzed": 0,
        "characters_analyzed": 0,
        "revenue": 0.0,
        "expenses": 0.0,
        "profit": 0.0,
        "debt": 0.0,
        "cash_inflow": 0.0,
        "cash_outflow": 0.0,
        "profit_margin": 0.0,
        "date_range": "Unknown",
        "period_description": None,
        "detected_dates": [],
        "detected_columns": {},
        "summary": "",
        "warnings": [],
        "preview_rows": [],
        "expenses_breakdown": {},
        "biggest_expense_categories": [],
        "confidence": "low",
        "monthly_breakdown": None,
        "key_observations": [],
        "currency": "K",
        "used_llm": False,
        "metadata": {},
        # Sales-record specific fields (None when not a sales record)
        "product_breakdown": None,
        "top_product": None,
        "slowest_product": None,
    }


def _map_llm_result(
    llm: dict[str, Any],
    source_type: str,
    filename: str,
    rows_analyzed: int = 0,
) -> dict[str, Any]:
    """Map LLM JSON output to the standard document result schema."""
    revenue = float(llm.get("total_revenue") or 0)
    expenses = float(llm.get("total_expenses") or 0)
    profit_raw = llm.get("profit")
    profit = float(profit_raw) if profit_raw is not None else calculate_profit(revenue, expenses)
    debt = float(llm.get("debt_balance") or 0)

    detected_type = llm.get("detected_document_type") or "Other"
    period_desc = llm.get("period_description") or None
    currency = _normalize_currency(llm.get("currency"))

    start = llm.get("period_start")
    end = llm.get("period_end")
    if start and end:
        date_range = f"{start} to {end}"
    elif period_desc:
        date_range = period_desc
    else:
        date_range = "Unknown"

    margin = calculate_profit_margin(revenue, profit)
    confidence = llm.get("confidence", "low")
    warnings = list(llm.get("warnings") or [])
    observations = list(llm.get("key_observations") or [])

    if confidence == "low":
        warnings.insert(0, "Low confidence extraction β€” please verify figures manually before relying on them.")
    elif confidence == "medium":
        warnings.insert(0, "Partial extraction β€” some figures may need manual verification.")

    # Build expense breakdown dict (category β†’ amount) from LLM list output
    raw_breakdown_list = llm.get("expense_breakdown") or []
    expenses_breakdown: dict[str, float] = {}
    for item in raw_breakdown_list:
        if isinstance(item, dict):
            cat = str(item.get("category") or "").strip()
            amt = item.get("amount")
            if cat and amt is not None:
                try:
                    expenses_breakdown[cat] = round(float(amt), 2)
                except (TypeError, ValueError):
                    pass

    # Compute largest expense category (Python-side, not LLM guess)
    if expenses_breakdown:
        biggest_cats = [c for c, _ in sorted(expenses_breakdown.items(), key=lambda x: x[1], reverse=True)[:3]]
    else:
        biggest_cats = []

    # Product breakdown (Sales Record documents only)
    raw_products = llm.get("product_breakdown") or []
    product_breakdown: list[dict] | None = None
    top_product: str | None = None
    slowest_product: str | None = None

    if raw_products and isinstance(raw_products, list):
        cleaned: list[dict] = []
        for item in raw_products:
            if not isinstance(item, dict):
                continue
            name = str(item.get("product") or "").strip()
            if not name:
                continue
            units = float(item.get("units_sold") or 0)
            price = float(item.get("unit_price") or 0)
            # Prefer LLM-stated revenue; fall back to units Γ— price
            rev_stated = item.get("revenue")
            rev = float(rev_stated) if rev_stated is not None else round(units * price, 2)
            cleaned.append({"product": name, "units_sold": units, "unit_price": price, "revenue": rev})
        if cleaned:
            by_rev = sorted(cleaned, key=lambda x: x["revenue"], reverse=True)
            product_breakdown = by_rev
            top_product = by_rev[0]["product"]
            slowest_product = by_rev[-1]["product"] if len(by_rev) > 1 else None

    summary = (
        f"LLM-extracted {detected_type.lower()} covering {period_desc or 'an unknown period'}. "
        f"Revenue: {currency}{revenue:,.2f}, Expenses: {currency}{expenses:,.2f}, "
        f"Profit: {currency}{profit:,.2f} ({margin:.1f}% margin)."
    )

    result = _blank_result(source_type, filename, detected_type)
    result.update({
        "rows_analyzed": rows_analyzed,
        "revenue": round(revenue, 2),
        "expenses": round(expenses, 2),
        "profit": round(profit, 2),
        "debt": round(debt, 2),
        "cash_inflow": round(revenue, 2),
        "cash_outflow": round(expenses, 2),
        "profit_margin": round(margin, 2),
        "date_range": date_range,
        "period_description": period_desc,
        "summary": summary,
        "warnings": warnings,
        "expenses_breakdown": expenses_breakdown,
        "biggest_expense_categories": biggest_cats,
        "confidence": confidence,
        "monthly_breakdown": llm.get("monthly_breakdown"),
        "key_observations": observations,
        "currency": currency,
        "used_llm": True,
        "product_breakdown": product_breakdown,
        "top_product": top_product,
        "slowest_product": slowest_product,
    })
    return result


# ── Keyword-based fallback (kept for when LLM is unavailable) ─────────────────

def _normalize_name(value: str) -> str:
    return re.sub(r"[^a-z0-9]+", " ", str(value).strip().lower()).strip()


def _find_matching_column(columns: list[str], keywords: list[str]) -> str | None:
    normalized_map = {column: _normalize_name(column) for column in columns}
    for keyword in keywords:
        nk = _normalize_name(keyword)
        for original, normalized in normalized_map.items():
            if normalized == nk:
                return original
    for keyword in keywords:
        nk = _normalize_name(keyword)
        for original, normalized in normalized_map.items():
            if nk in normalized:
                return original
    return None


def _to_numeric(series: pd.Series) -> pd.Series:
    cleaned = (
        series.astype(str)
        .str.replace(",", "", regex=False)
        .str.replace(r"[^0-9.\-]", "", regex=True)
        .replace("", pd.NA)
    )
    return pd.to_numeric(cleaned, errors="coerce")


def _extract_date_range(frame: pd.DataFrame, date_column: str | None) -> tuple[str, list[str]]:
    if not date_column:
        return "Unknown", []
    dates = pd.to_datetime(frame[date_column], errors="coerce").dropna()
    if dates.empty:
        return "Unknown", []
    start = dates.min().date().isoformat()
    end = dates.max().date().isoformat()
    sample_dates = sorted({d.date().isoformat() for d in dates.head(5)})
    return f"{start} to {end}", sample_dates


def _document_type_columns(frame: pd.DataFrame, document_type: str) -> dict[str, str | None]:
    columns = frame.columns.tolist()
    return {
        "date": _find_matching_column(columns, ["date", "period", "day", "month"]),
        "description": _find_matching_column(columns, ["description", "details", "notes"]),
        "product": _find_matching_column(columns, ["product", "item", "product name", "item name", "service", "goods"]),
        "category": _find_matching_column(columns, ["category", "type", "transaction type", "class"]),
        "amount": _find_matching_column(columns, ["amount", "value", "total", "price"]),
        "revenue": _find_matching_column(columns, ["revenue", "sales", "income", "total sales"]),
        "expense": _find_matching_column(columns, ["expense", "expenses", "cost", "operating expense", "cash out"]),
        "profit": _find_matching_column(columns, ["profit", "net profit", "gross profit"]),
        "debt": _find_matching_column(columns, ["debt", "loan", "balance", "repayment"]),
        "cash_in": _find_matching_column(columns, ["cash in", "money in", "inflow", "credit"]),
        "cash_out": _find_matching_column(columns, ["cash out", "money out", "outflow", "debit"]),
        "quantity": _find_matching_column(columns, ["quantity", "qty", "units", "units sold", "qty sold"]),
        "unit_price": _find_matching_column(columns, ["unit price", "price per unit", "rate", "price"]),
    }


def _detect_is_sales_record(frame: pd.DataFrame) -> bool:
    """Return True when the DataFrame looks like a product sales breakdown.

    Signals: has a product/item name column AND at least one of units-sold or
    unit-price columns β€” the hallmark of a sales-by-product table rather than a
    P&L or cash-flow statement.
    """
    cols_lower = [c.lower().strip() for c in frame.columns]
    product_hit = any(
        kw in col
        for col in cols_lower
        for kw in ("product", "item name", "item", "goods", "service")
    )
    unit_hit = any(
        kw in col
        for col in cols_lower
        for kw in ("units sold", "qty sold", "quantity sold", "units", "qty")
    )
    price_hit = any(
        kw in col
        for col in cols_lower
        for kw in ("unit price", "price per unit", "price", "rate")
    )
    revenue_expense_hit = any(
        kw in col
        for col in cols_lower
        for kw in ("revenue", "expenses", "profit", "net profit")
    )
    # Only flag as sales record if there are product signals but NOT
    # a full income-statement layout (revenue + expenses + profit columns).
    return product_hit and (unit_hit or price_hit) and not revenue_expense_hit


REVENUE_KEYWORDS = ["revenue", "sales", "sale", "income", "money in", "inflow", "customer payment", "credit", "deposit"]
EXPENSE_KEYWORDS = ["expense", "expenses", "cost", "costs", "purchase", "purchases", "rent", "stock",
                    "transport", "supplier", "money out", "outflow", "debit", "fees", "utilities", "wages"]
DEBT_KEYWORDS = ["debt", "loan", "borrowed", "payable", "supplier debt", "owed", "repayment", "balance due"]


def _classify_from_text(text: str) -> str:
    lowered = text.lower()
    if any(k in lowered for k in DEBT_KEYWORDS):
        return "debt"
    if any(k in lowered for k in REVENUE_KEYWORDS):
        return "revenue"
    if any(k in lowered for k in EXPENSE_KEYWORDS):
        return "expense"
    return "unknown"


def classify_transaction_row(row: dict[str, Any], document_type: str) -> str:
    text = " ".join(str(v) for v in row.values() if v is not None and str(v).strip())
    base = _classify_from_text(text)
    document_type = document_type or "Other"
    if document_type == "Sales Record":
        return "revenue"
    if document_type == "Expense Record":
        return "expense"
    if document_type == "Receipts":
        return "expense" if "sales receipt" not in text.lower() else "revenue"
    if document_type == "Loan or Debt Record":
        return "debt"
    if document_type == "Inventory/Stock Record":
        return "expense"
    if document_type == "Cash Flow Record":
        return base
    if document_type in {"Bank Statement", "Mobile Money Statement"} and base == "unknown":
        tx_type = str(row.get("Transaction Type", row.get("Type", ""))).lower()
        if "credit" in tx_type or "deposit" in tx_type:
            return "revenue"
        if "debit" in tx_type or "withdraw" in tx_type or "cash-out" in tx_type:
            return "expense"
    return base


def _keyword_parse_frame(data: pd.DataFrame, document_type: str) -> dict[str, Any]:
    """Original keyword-based parser β€” used as fallback when LLM is unavailable."""
    result = _blank_result("table", document_type=document_type)
    if data.empty:
        result["summary"] = "The uploaded file did not contain any rows to analyze."
        result["warnings"].append("No rows were found in the uploaded document.")
        return result

    frame = data.copy()
    frame.columns = [str(c).strip() for c in frame.columns]
    detected = _document_type_columns(frame, document_type)
    result["detected_columns"] = {k: v for k, v in detected.items() if v}
    result["rows_analyzed"] = int(len(frame))
    result["preview_rows"] = frame.head(5).fillna("").to_dict(orient="records")

    date_range, detected_dates = _extract_date_range(frame, detected["date"])
    result["date_range"] = date_range
    result["detected_dates"] = detected_dates

    revenue = expenses = debt = cash_inflow = cash_outflow = 0.0
    warnings: list[str] = []

    if detected["revenue"] and detected["revenue"] != detected["cash_in"]:
        revenue += float(_to_numeric(frame[detected["revenue"]]).fillna(0).sum())
    if detected["expense"] and detected["expense"] != detected["cash_out"]:
        expenses += float(_to_numeric(frame[detected["expense"]]).fillna(0).sum())
    if detected["debt"]:
        debt += float(_to_numeric(frame[detected["debt"]]).fillna(0).sum())
    if detected["cash_in"]:
        cash_inflow += float(_to_numeric(frame[detected["cash_in"]]).fillna(0).sum())
        revenue += cash_inflow if document_type in {"Cash Flow Record", "Bank Statement", "Mobile Money Statement"} else 0
    if detected["cash_out"]:
        cash_outflow += float(_to_numeric(frame[detected["cash_out"]]).fillna(0).sum())
        expenses += cash_outflow if document_type in {"Cash Flow Record", "Bank Statement", "Mobile Money Statement"} else 0

    amount_column = detected["amount"]
    if document_type == "Expense Record" and amount_column and not detected["expense"]:
        expenses += float(_to_numeric(frame[amount_column]).fillna(0).sum())
    elif document_type == "Loan or Debt Record" and amount_column and not detected["debt"]:
        debt += float(_to_numeric(frame[amount_column]).fillna(0).sum())

    unclassified = 0
    expense_breakdown: dict[str, float] = {}
    if amount_column:
        amounts = _to_numeric(frame[amount_column]).fillna(0)
        for index, amount in amounts.items():
            if amount == 0:
                continue
            row_dict = frame.loc[index].to_dict()
            classification = classify_transaction_row(row_dict, document_type)
            if document_type in {"Income Statement", "Other"} and classification == "revenue" and not detected["revenue"]:
                revenue += abs(float(amount))
            elif document_type in {"Income Statement", "Other"} and classification == "expense" and not detected["expense"]:
                expenses += abs(float(amount))
            elif classification == "debt" and not detected["debt"]:
                debt += abs(float(amount))
            elif document_type in {"Bank Statement", "Mobile Money Statement", "Cash Flow Record"}:
                if classification == "revenue" and not detected["cash_in"]:
                    cash_inflow += abs(float(amount))
                    revenue += abs(float(amount))
                elif classification == "expense" and not detected["cash_out"]:
                    cash_outflow += abs(float(amount))
                    expenses += abs(float(amount))
                elif classification == "debt" and not detected["debt"]:
                    debt += abs(float(amount))
            elif document_type == "Sales Record" and not detected["revenue"]:
                revenue += abs(float(amount))
            elif document_type in {"Expense Record", "Receipts", "Inventory/Stock Record"} and not detected["expense"]:
                expenses += abs(float(amount))
            else:
                unclassified += 1

            category_column = detected["category"]
            if classification == "expense" and category_column and pd.notna(frame.at[index, category_column]):
                cat = str(frame.at[index, category_column]).strip()
                expense_breakdown[cat] = expense_breakdown.get(cat, 0.0) + abs(float(amount))

    if unclassified:
        warnings.append(f"{unclassified} row(s) could not be classified for this document type.")

    if document_type == "Sales Record" and revenue == 0 and detected["quantity"] and detected["unit_price"]:
        revenue = float((_to_numeric(frame[detected["quantity"]]) * _to_numeric(frame[detected["unit_price"]])).fillna(0).sum())

    if detected["profit"]:
        profit = float(_to_numeric(frame[detected["profit"]]).fillna(0).sum())
        warnings.append("Profit was read directly from the document.")
    else:
        profit = calculate_profit(revenue, expenses)

    margin = calculate_profit_margin(revenue, profit)
    biggest = [n for n, _ in sorted(expense_breakdown.items(), key=lambda x: x[1], reverse=True)[:3]]

    # ── Product-level extraction for Sales Record documents ───────────────────
    product_breakdown: list[dict] | None = None
    top_product: str | None = None
    slowest_product: str | None = None

    if document_type == "Sales Record" and detected.get("product"):
        prod_col  = detected["product"]
        units_col = detected.get("quantity")
        price_col = detected.get("unit_price")
        amt_col   = detected.get("amount")

        rows: list[dict] = []
        for _, row in frame.iterrows():
            name = str(row.get(prod_col, "")).strip()
            if not name or name.lower() in ("nan", "none", "total", "grand total"):
                continue
            units = (
                float(_to_numeric(pd.Series([row.get(units_col)])).fillna(0).iloc[0])
                if units_col else 0.0
            )
            price = (
                float(_to_numeric(pd.Series([row.get(price_col)])).fillna(0).iloc[0])
                if price_col else 0.0
            )
            rev = units * price if price > 0 and units > 0 else (
                abs(float(_to_numeric(pd.Series([row.get(amt_col)])).fillna(0).iloc[0]))
                if amt_col else 0.0
            )
            if rev > 0 or units > 0:
                rows.append({"product": name, "units_sold": units, "unit_price": price, "revenue": rev})

        if rows:
            by_rev = sorted(rows, key=lambda x: x["revenue"], reverse=True)
            product_breakdown = by_rev
            top_product = by_rev[0]["product"]
            slowest_product = by_rev[-1]["product"] if len(by_rev) > 1 else None

            # Derive total revenue from product rows if not yet captured
            if revenue == 0:
                revenue = round(sum(r["revenue"] for r in rows), 2)
                profit  = calculate_profit(revenue, expenses)
                margin  = calculate_profit_margin(revenue, profit)

    result.update({
        "revenue": round(revenue, 2),
        "expenses": round(expenses, 2),
        "profit": round(profit, 2),
        "debt": round(debt, 2),
        "cash_inflow": round(cash_inflow or revenue, 2),
        "cash_outflow": round(cash_outflow or expenses, 2),
        "profit_margin": round(margin, 2),
        "warnings": warnings,
        "biggest_expense_categories": biggest,
        "confidence": "low",
        "used_llm": False,
        "product_breakdown": product_breakdown,
        "top_product": top_product,
        "slowest_product": slowest_product,
    })
    result["summary"] = f"Keyword-based fallback parser used for this {document_type.lower()}. LLM unavailable."
    return result


# ── Public parse functions ─────────────────────────────────────────────────────

def _classify_llm_error(exc: Exception) -> str:
    """Return a user-friendly message for known LLM failure modes."""
    msg = str(exc)
    if "429" in msg or "rate_limit" in msg.lower() or "Rate limit" in msg:
        import re
        wait = re.search(r"try again in (\S+)", msg)
        wait_str = f" Please try again in {wait.group(1)}." if wait else " Please try again later."
        return (
            f"AI daily usage limit reached.{wait_str} "
            "Document parsing will use the keyword-based fallback method."
        )
    if "401" in msg or "auth" in msg.lower() or "api_key" in msg.lower():
        return "AI service key invalid or missing. Check your API key configuration."
    if "timeout" in msg.lower() or "connect" in msg.lower():
        return "LLM request timed out. Check your internet connection and try again."
    return f"LLM error: {msg[:200]}"


def parse_excel_file(uploaded_file: Any, document_type: str = "Other") -> dict[str, Any]:
    file_bytes = uploaded_file.getvalue()
    filename = uploaded_file.name
    extraction_error: str | None = None
    llm_error: str | None = None

    # Stage 0: sheet-aware structured extraction for multi-sheet workbooks
    try:
        structured = _parse_excel_by_sheets(file_bytes, filename)
        if structured is not None:
            return structured
    except Exception:
        pass  # Non-critical β€” fall through to LLM path

    # Stage 1: extract all sheets as structured text
    raw_text: str = ""
    rows: int = 0
    try:
        raw_text, rows = _extract_excel_as_text(file_bytes)
    except Exception as exc:
        extraction_error = str(exc)

    # Auto-correct document_type for product-level spreadsheets
    try:
        _xl_frame = pd.read_excel(io.BytesIO(file_bytes), nrows=5)
        if document_type in ("Income Statement", "Other") and _detect_is_sales_record(_xl_frame):
            document_type = "Sales Record"
    except Exception:
        pass

    # Stage 2: LLM parse β€” errors are surfaced, not swallowed
    if raw_text.strip():
        print("=== RAW EXCEL CONTENT BEING SENT TO LLM ===")
        print(raw_text[:2000])
        print("=== END RAW CONTENT ===")
        try:
            llm = _llm_parse_document(raw_text, document_type, filename)
            if llm:
                return _map_llm_result(llm, "xlsx", filename, rows)
        except Exception as exc:
            llm_error = _classify_llm_error(exc)

    # Fallback: read first sheet only with pandas keyword parser
    try:
        frame = pd.read_excel(io.BytesIO(file_bytes))
        if document_type in ("Income Statement", "Other") and _detect_is_sales_record(frame):
            document_type = "Sales Record"
        result = _keyword_parse_frame(frame, document_type)
        result["source_type"] = "xlsx"
        result["file_name"] = filename
        msgs: list[str] = []
        if llm_error:
            msgs.append(llm_error)
        if extraction_error:
            msgs.append(f"Sheet extraction error: {extraction_error}")
        if not llm_error and not extraction_error:
            msgs.append("Keyword-based fallback used β€” numbers may not be accurate.")
        result["warnings"] = msgs + result.get("warnings", [])
        return result
    except Exception as exc:
        result = _blank_result("xlsx", filename, document_type)
        if llm_error:
            result["warnings"].append(llm_error)
        if extraction_error:
            result["warnings"].append(f"Sheet extraction error: {extraction_error}")
        result["warnings"].append(f"Could not read Excel file: {exc}")
        return result


def parse_csv_file(uploaded_file: Any, document_type: str = "Other") -> dict[str, Any]:
    file_bytes = uploaded_file.getvalue()
    filename = uploaded_file.name

    try:
        frame = pd.read_csv(io.BytesIO(file_bytes))
        # Auto-correct to Sales Record when columns signal a product breakdown
        if document_type in ("Income Statement", "Other") and _detect_is_sales_record(frame):
            document_type = "Sales Record"
        raw_text = frame.to_string(max_rows=300)
        llm = _llm_parse_document(raw_text, document_type, filename)
        if llm:
            return _map_llm_result(llm, "csv", filename, len(frame))
    except Exception:
        pass

    try:
        frame = pd.read_csv(io.BytesIO(file_bytes))
        if document_type in ("Income Statement", "Other") and _detect_is_sales_record(frame):
            document_type = "Sales Record"
        result = _keyword_parse_frame(frame, document_type)
        result["source_type"] = "csv"
        result["file_name"] = filename
        result["warnings"].insert(0, "LLM parsing unavailable β€” used keyword-based fallback.")
        return result
    except Exception as exc:
        result = _blank_result("csv", filename, document_type)
        result["warnings"].append(f"Could not read CSV file: {exc}")
        return result


def parse_text_file(uploaded_file: Any, document_type: str = "Other") -> dict[str, Any]:
    content = uploaded_file.getvalue().decode("utf-8", errors="ignore")
    filename = uploaded_file.name

    try:
        llm = _llm_parse_document(content, document_type, filename)
        if llm:
            return _map_llm_result(llm, "txt", filename, content.count("\n"))
    except Exception:
        pass

    parsed = parse_business_input(content)
    result = _blank_result("txt", filename, document_type)
    result.update({
        "characters_analyzed": len(content),
        "revenue": parsed["revenue"],
        "expenses": parsed["expenses"],
        "profit": round(parsed["revenue"] - parsed["expenses"], 2),
        "debt": parsed["debt"],
        "cash_inflow": parsed["revenue"],
        "cash_outflow": parsed["expenses"],
        "summary": f"Text file interpreted as {document_type.lower()} using keyword extraction.",
        "metadata": {
            "location": parsed.get("location"),
            "business_type": parsed.get("business_type"),
        },
    })
    return result


def parse_pdf_file(uploaded_file: Any, document_type: str = "Other") -> dict[str, Any]:
    result = _blank_result("pdf", uploaded_file.name, document_type)
    result["warnings"].append("PDF support is experimental. For best results, upload CSV or Excel.")
    try:
        from pypdf import PdfReader
    except Exception:
        result["summary"] = "PDF parsing unavailable β€” pypdf not installed."
        result["warnings"].append("Install pypdf to enable PDF text extraction.")
        return result

    try:
        raw_text, page_count = _extract_pdf_as_text(uploaded_file.getvalue())
        result["pages_analyzed"] = page_count
        result["characters_analyzed"] = len(raw_text)

        llm = _llm_parse_document(raw_text, document_type, uploaded_file.name)
        if llm:
            mapped = _map_llm_result(llm, "pdf", uploaded_file.name, page_count)
            mapped["pages_analyzed"] = page_count
            mapped["characters_analyzed"] = len(raw_text)
            mapped["warnings"] = result["warnings"] + mapped["warnings"]
            return mapped

        # Fallback: keyword parse on extracted text
        parsed = parse_business_input(raw_text)
        result.update({
            "revenue": parsed["revenue"],
            "expenses": parsed["expenses"],
            "profit": round(parsed["revenue"] - parsed["expenses"], 2),
            "debt": parsed["debt"],
            "cash_inflow": parsed["revenue"],
            "cash_outflow": parsed["expenses"],
            "summary": f"PDF read as text and interpreted as {document_type.lower()}.",
            "metadata": {
                "location": parsed.get("location"),
                "business_type": parsed.get("business_type"),
            },
        })
    except Exception as exc:
        result["warnings"].append(f"PDF extraction failed: {exc}")
    return result


def parse_uploaded_file(uploaded_file: Any, document_type: str = "Other") -> dict[str, Any]:
    suffix = uploaded_file.name.lower().rsplit(".", maxsplit=1)[-1] if "." in uploaded_file.name else ""
    if suffix == "csv":
        return parse_csv_file(uploaded_file, document_type)
    if suffix in {"xlsx", "xlsm"}:
        return parse_excel_file(uploaded_file, document_type)
    if suffix == "txt":
        return parse_text_file(uploaded_file, document_type)
    if suffix == "pdf":
        return parse_pdf_file(uploaded_file, document_type)

    result = _blank_result(suffix or "unknown", uploaded_file.name, document_type)
    result["summary"] = "The uploaded file format is not supported for document analysis."
    result["warnings"].append("Please upload a CSV, Excel (.xlsx), TXT, or PDF file.")
    return result


def summarize_document_records(data: dict[str, Any], document_type: str | None = None) -> str:
    active_type = document_type or data.get("document_type", "Other")
    period = data.get("period_description") or data.get("date_range") or "unknown period"
    obs = (" Observations: " + "; ".join(data.get("key_observations", [])[:2])) if data.get("key_observations") else ""
    return (
        f"{data.get('summary', '').strip()} "
        f"Document type: {active_type}. Period: {period}. "
        f"Revenue: {data.get('revenue', 0):,.2f}. "
        f"Expenses: {data.get('expenses', 0):,.2f}. "
        f"Profit: {data.get('profit', 0):,.2f}. "
        f"Debt: {data.get('debt', 0):,.2f}."
        f"{obs}"
    ).strip()