File size: 47,776 Bytes
c68a3b2
 
 
 
 
 
 
 
 
 
0474efa
 
 
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
0474efa
 
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
 
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
 
c68a3b2
 
0474efa
c68a3b2
0474efa
c68a3b2
 
 
0474efa
 
c68a3b2
 
 
0474efa
 
 
 
 
c68a3b2
 
0474efa
 
 
c68a3b2
0474efa
 
 
 
 
c68a3b2
0474efa
 
c68a3b2
0474efa
 
 
 
 
c68a3b2
 
 
 
 
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
0474efa
 
c68a3b2
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
0474efa
 
 
 
c68a3b2
0474efa
 
 
c68a3b2
0474efa
 
 
c68a3b2
 
 
 
 
 
 
0474efa
c68a3b2
 
 
0474efa
 
c68a3b2
 
0474efa
 
c68a3b2
 
 
 
 
 
 
 
 
0474efa
 
c68a3b2
 
 
 
 
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
0474efa
 
 
c68a3b2
0474efa
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
0474efa
c68a3b2
 
 
 
0474efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
0474efa
 
 
 
 
 
 
 
 
 
 
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
c68a3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0474efa
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
import streamlit as st
import pdfplumber
import re
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from datetime import datetime, timedelta
import sqlite3
import hashlib
import tempfile
import PyPDF2
import os
import webbrowser
import threading
import uuid
import subprocess
import time
from http.server import HTTPServer, SimpleHTTPRequestHandler

# Advanced ML and NLP imports
try:
    import xgboost as xgb
    from sklearn.ensemble import RandomForestClassifier, IsolationForest
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.cluster import KMeans
    import nltk
    from textblob import TextBlob
except ImportError as e:
    st.warning(f"Some advanced features may not be available. Missing: {str(e)}")

# Configuration
st.set_page_config(
    page_title='Universal Bank Statement Analyzer',
    page_icon='๐Ÿฆ',
    layout='wide',
    initial_sidebar_state='expanded'
)

# Initialize session state
if 'user_profile' not in st.session_state:
    st.session_state.user_profile = None
if 'transactions_df' not in st.session_state:
    st.session_state.transactions_df = None
if 'analysis_complete' not in st.session_state:
    st.session_state.analysis_complete = False
if 'detected_bank' not in st.session_state:
    st.session_state.detected_bank = None

class DatabaseManager:
    """Handles all database operations for user profiles and financial data"""
    def __init__(self):
        self.db_path = 'financial_analysis.db'
        self.init_database()

    def init_database(self):
        """Initialize database tables"""
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()

            # Test database integrity first
            cursor.execute("PRAGMA integrity_check;")
            integrity_result = cursor.fetchone()

            if integrity_result[0] != "ok":
                conn.close()
                raise sqlite3.DatabaseError("Database integrity check failed")

        except sqlite3.DatabaseError as e:
            # Handle corrupted database by removing it and creating fresh
            import os
            if os.path.exists(self.db_path):
                os.remove(self.db_path)
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()

        # User profiles table
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS user_profiles (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id TEXT UNIQUE NOT NULL,
                name TEXT NOT NULL,
                email TEXT UNIQUE NOT NULL,
                password_hash TEXT NOT NULL,
                financial_goals TEXT,
                risk_tolerance TEXT,
                monthly_income REAL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ''')

        # Financial data table
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS financial_data (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id TEXT NOT NULL,
                bank_name TEXT,
                date TEXT NOT NULL,
                description TEXT,
                amount REAL,
                category TEXT,
                balance REAL,
                analysis_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (user_id) REFERENCES user_profiles (user_id)
            )
        ''')

        # Recommendations table
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS recommendations (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id TEXT NOT NULL,
                bank_name TEXT,
                recommendation_type TEXT,
                title TEXT,
                description TEXT,
                priority INTEGER,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (user_id) REFERENCES user_profiles (user_id)
            )
        ''')

        conn.commit()
        conn.close()

    def create_user(self, user_id, name, email, password, financial_goals="", risk_tolerance="moderate", monthly_income=0):
        """Create a new user profile"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        password_hash = hashlib.sha256(password.encode()).hexdigest()

        try:
            cursor.execute('''
                INSERT INTO user_profiles (user_id, name, email, password_hash, financial_goals, risk_tolerance, monthly_income)
                VALUES (?, ?, ?, ?, ?, ?, ?)
            ''', (user_id, name, email, password_hash, financial_goals, risk_tolerance, monthly_income))
            conn.commit()
            return True
        except sqlite3.IntegrityError:
            return False
        finally:
            conn.close()

    def authenticate_user(self, email, password):
        """Authenticate user login"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        password_hash = hashlib.sha256(password.encode()).hexdigest()
        cursor.execute('SELECT user_id, name FROM user_profiles WHERE email = ? AND password_hash = ?',
                      (email, password_hash))
        result = cursor.fetchone()
        conn.close()

        return result

    def get_user_profile(self, user_id):
        """Get user profile data"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()

        cursor.execute('SELECT * FROM user_profiles WHERE user_id = ?', (user_id,))
        result = cursor.fetchone()
        conn.close()

        if result:
            columns = ['id', 'user_id', 'name', 'email', 'password_hash', 'financial_goals', 
                      'risk_tolerance', 'monthly_income', 'created_at']
            return dict(zip(columns, result))
        return None

class PersonalizationEngine:
    """Advanced personalization and recommendation system"""
    def __init__(self, user_profile=None):
        self.user_profile = user_profile
        self.scaler = StandardScaler()

    def analyze_spending_patterns(self, df):
        """Analyze user spending patterns using advanced ML"""
        if df.empty:
            return {}

        # Calculate advanced metrics
        spending_by_category = df.groupby('Category')['Amount'].agg(['sum', 'mean', 'count', 'std'])

        # Time-based analysis
        df['Date'] = pd.to_datetime(df['Date'])
        df['DayOfWeek'] = df['Date'].dt.dayofweek
        df['Month'] = df['Date'].dt.month
        df['IsWeekend'] = df['DayOfWeek'].isin([5, 6])

        patterns = {
            'spending_by_category': spending_by_category.to_dict(),
            'weekend_vs_weekday': {
                'weekend_avg': df[df['IsWeekend']]['Amount'].mean(),
                'weekday_avg': df[~df['IsWeekend']]['Amount'].mean()
            },
            'monthly_trends': df.groupby('Month')['Amount'].mean().to_dict(),
            'transaction_frequency': len(df) / max(1, (df['Date'].max() - df['Date'].min()).days),
            'balance_trend': df['Balance'].iloc[-1] - df['Balance'].iloc[0] if len(df) > 1 else 0
        }

        return patterns

    def generate_personalized_recommendations(self, df, patterns):
        """Generate personalized financial recommendations"""
        recommendations = []

        if df.empty:
            return recommendations

        # Budget recommendations
        total_spending = abs(df[df['Amount'] < 0]['Amount'].sum())

        if self.user_profile and self.user_profile.get('monthly_income', 0) > 0:
            spending_ratio = total_spending / self.user_profile['monthly_income']

            if spending_ratio > 0.8:
                recommendations.append({
                    'type': 'budget',
                    'priority': 'high',
                    'title': 'Reduce Monthly Spending',
                    'description': f'Your spending is {spending_ratio:.1%} of your income. Consider reducing expenses by 20%.'
                })
            elif spending_ratio < 0.5:
                recommendations.append({
                    'type': 'savings',
                    'priority': 'medium',
                    'title': 'Increase Savings',
                    'description': f'Great job! You\'re only spending {spending_ratio:.1%} of your income. Consider increasing your savings rate.'
                })

        # Category-specific recommendations
        for category, data in patterns.get('spending_by_category', {}).items():
            if data.get('sum', 0) < 0 and abs(data['sum']) > 1000:  # Significant spending categories
                recommendations.append({
                    'type': 'category',
                    'priority': 'medium',
                    'title': f'Optimize {category} Spending',
                    'description': f'You spent R{abs(data["sum"]):.2f} on {category}. Consider reviewing these expenses.'
                })

        # Investment recommendations
        if patterns.get('balance_trend', 0) > 5000:
            recommendations.append({
                'type': 'investment',
                'priority': 'medium',
                'title': 'Consider Investment Options',
                'description': 'Your balance is growing steadily. Consider investing excess funds for better returns.'
            })

        return recommendations

    def calculate_financial_health_score(self, df, patterns):
        """Calculate comprehensive financial health score"""
        if df.empty:
            return 0

        score_components = {
            'income_stability': 0,
            'spending_control': 0,
            'savings_rate': 0,
            'transaction_diversity': 0,
            'balance_growth': 0
        }

        # Income stability (based on regular credits)
        credits = df[df['Amount'] > 0]
        if len(credits) > 0:
            credit_std = credits['Amount'].std()
            credit_mean = credits['Amount'].mean()
            stability = max(0, 1 - (credit_std / credit_mean if credit_mean > 0 else 1))
            score_components['income_stability'] = min(stability * 25, 25)

        # Spending control (consistent spending patterns)
        debits = df[df['Amount'] < 0]
        if len(debits) > 0:
            spending_consistency = 1 - (debits['Amount'].std() / abs(debits['Amount'].mean()) if debits['Amount'].mean() != 0 else 1)
            score_components['spending_control'] = max(0, spending_consistency * 20)

        # Savings rate
        if self.user_profile and self.user_profile.get('monthly_income', 0) > 0:
            total_spending = abs(df[df['Amount'] < 0]['Amount'].sum())
            savings_rate = 1 - (total_spending / self.user_profile['monthly_income'])
            score_components['savings_rate'] = max(0, min(savings_rate * 25, 25))

        # Transaction diversity
        unique_categories = df['Category'].nunique()
        diversity_score = min(unique_categories / 8 * 15, 15)  # Max 15 points for 8+ categories
        score_components['transaction_diversity'] = diversity_score

        # Balance growth
        balance_trend = patterns.get('balance_trend', 0)
        if balance_trend > 0:
            score_components['balance_growth'] = min(balance_trend / 5000 * 15, 15)

        total_score = sum(score_components.values())
        return min(100, max(0, total_score)), score_components

class AdvancedAnalytics:
    """Advanced analytics and ML models for financial analysis"""
    def __init__(self):
        self.models = {}
        self.scaler = StandardScaler()

    def detect_anomalies(self, df):
        """Detect unusual transactions using Isolation Forest"""
        if len(df) < 10:
            return df

        # Prepare features for anomaly detection
        features = df[['Amount']].copy()
        features['DayOfWeek'] = pd.to_datetime(df['Date']).dt.dayofweek
        features['IsWeekend'] = features['DayOfWeek'].isin([5, 6]).astype(int)

        # Scale features
        features_scaled = self.scaler.fit_transform(features)

        # Detect anomalies
        iso_forest = IsolationForest(contamination=0.1, random_state=42)
        anomalies = iso_forest.fit_predict(features_scaled)

        df_copy = df.copy()
        df_copy['IsAnomaly'] = anomalies == -1

        return df_copy

    def predict_future_spending(self, df, days_ahead=30):
        """Predict future spending patterns"""
        if len(df) < 10:
            return None

        # Prepare time series data
        df_daily = df.groupby(pd.to_datetime(df['Date']).dt.date)['Amount'].sum().reset_index()
        df_daily['Date'] = pd.to_datetime(df_daily['Date'])
        df_daily = df_daily.sort_values('Date')

        # Simple linear trend prediction
        x = np.arange(len(df_daily))
        y = df_daily['Amount'].values

        coeffs = np.polyfit(x, y, 1)

        # Predict future values
        future_x = np.arange(len(df_daily), len(df_daily) + days_ahead)
        future_predictions = np.polyval(coeffs, future_x)

        future_dates = [df_daily['Date'].max() + timedelta(days=i+1) for i in range(days_ahead)]

        return pd.DataFrame({
            'Date': future_dates,
            'Predicted_Amount': future_predictions
        })

    def enhanced_loan_prediction(self, df, bank_name):
        """Enhanced loan eligibility prediction"""
        try:
            # Load training data
            training_data = pd.read_csv(f'{bank_name.lower().replace(" ", "_")}.csv')

            # Prepare features from the transaction data
            total_credits = df[df['Amount'] > 0]['Amount'].sum()
            total_debits = abs(df[df['Amount'] < 0]['Amount'].sum())
            num_transactions = len(df)
            avg_balance = df['Balance'].mean()
            closing_balance = df['Balance'].iloc[-1] if len(df) > 0 else 0

            # Prepare feature vector
            features = pd.DataFrame({
                'Total_Credits': [total_credits],
                'Total_Debits': [total_debits],
                'Average_Balance': [avg_balance],
                'Num_Transactions': [num_transactions],
                'Closing_Balance': [closing_balance]
            })

            # Ensure we only use columns that exist in training data
            available_columns = [col for col in features.columns if col in training_data.columns]
            features = features[available_columns]

            # Train model
            model = RandomForestClassifier(n_estimators=100, random_state=42)
            X_train = training_data[available_columns]
            y_train = training_data['Eligibility']
            model.fit(X_train, y_train)

            prediction = model.predict(features)[0]
            prediction_proba = model.predict_proba(features)[0]

            return {
                'eligible': bool(prediction),
                'confidence': float(max(prediction_proba)),
                'model_type': 'Random Forest'
            }

        except Exception as e:
            st.error(f"Error in loan prediction: {str(e)}")
            return {'eligible': False, 'confidence': 0.0, 'model_type': 'Error'}

def extract_text_from_pdf(file):
    """Extract text from PDF file"""
    try:
        if isinstance(file, str):
            with open(file, 'rb') as f:
                reader = PyPDF2.PdfReader(f)
                text = ''
                for page in reader.pages:
                    text += page.extract_text()
        else:
            reader = PyPDF2.PdfReader(file)
            text = ''
            for page in reader.pages:
                text += page.extract_text()
        return text
    except Exception as e:
        st.error(f"Error extracting text from PDF: {str(e)}")
        return ""

def identify_bank_from_text(text):
    """Identify bank from statement text"""
    text_lower = text.lower()
    bank_keywords = {
        'FNB': ['fnb', 'first national bank'],
        'Standard Bank': ['standard bank'],
        'Nedbank': ['nedbank'],
        'ABSA': ['absa'],
        'Capitec Bank': ['capitec']
    }

    for bank, keywords in bank_keywords.items():
        if any(keyword in text_lower for keyword in keywords):
            return bank
    return None

def extract_bank_statement_metadata(text, bank_name):
    """Extract metadata from bank statement based on bank"""
    if bank_name == 'FNB':
        # Extract account holder name
        name_match = re.search(r"(MR|MRS)\s+([A-Z\s]+)", text)
        account_holder_name = name_match.group(0) if name_match else "Name not found"

        # Extract closing balance
        closing_balance_match = re.search(r"Closing Balance\s+([\d,]+\.?\d*)", text)
        closing_balance = float(closing_balance_match.group(1).replace(',', '')) if closing_balance_match else 0.0

        return account_holder_name, closing_balance

    elif bank_name == 'Standard Bank':
        name_match = re.search(r"Account Holder:\s*(.+?)\n", text)
        account_holder_name = name_match.group(1).strip() if name_match else "Name not found"

        acc_match = re.search(r"Account Number:\s*(\d+)", text)
        account_number = acc_match.group(1) if acc_match else "Not found"

        period_match = re.search(r"Statement Period:\s*(.+?)\n", text)
        statement_period = period_match.group(1).strip() if period_match else "Not specified"

        balance_match = re.search(r"Closing Balance:\s*(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)", text)
        closing_balance = float(balance_match.group(1).replace(',', '').replace('R', '')) if balance_match else 0.0

        return account_holder_name, account_number, statement_period, closing_balance

    elif bank_name == 'Nedbank':
        name_match = re.search(r"Account Holder:\s*(.+?)\n", text)
        account_holder_name = name_match.group(1).strip() if name_match else "Name not found"

        acc_match = re.search(r"Account Number:\s*(\d+)", text)
        account_number = acc_match.group(1) if acc_match else "Not found"

        period_match = re.search(r"Statement Period:\s*(.+?)\n", text)
        statement_period = period_match.group(1).strip() if period_match else "Not specified"

        balance_match = re.search(r"Closing Balance:\s*(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)", text)
        closing_balance = float(balance_match.group(1).replace(',', '').replace('R', '')) if balance_match else 0.0

        return account_holder_name, account_number, statement_period, closing_balance

    elif bank_name == 'ABSA':
        name_match = re.search(r"Account Holder:\s*(.+?)\n", text)
        account_holder_name = name_match.group(1).strip() if name_match else "Name not found"

        acc_match = re.search(r"Account Number:\s*(\d+)", text)
        account_number = acc_match.group(1) if acc_match else "Not found"

        period_match = re.search(r"Statement Period:\s*(.+?)\n", text)
        statement_period = period_match.group(1).strip() if period_match else "Not specified"

        balance_match = re.search(r"Closing Balance:\s*(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)", text)
        closing_balance = float(balance_match.group(1).replace(',', '').replace('R', '')) if balance_match else 0.0

        return account_holder_name, account_number, statement_period, closing_balance

    else:  # Default for other banks
        name_match = re.search(r"Account Holder:\s*(.+?)\n", text)
        account_holder_name = name_match.group(1).strip() if name_match else "Name not found"

        balance_match = re.search(r"Closing Balance:\s*(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)", text)
        closing_balance = float(balance_match.group(1).replace(',', '').replace('R', '')) if balance_match else 0.0

        return account_holder_name, closing_balance

def parse_pdf_enhanced(file):
    """Enhanced PDF parsing with better text extraction"""
    try:
        with pdfplumber.open(file) as pdf:
            text = ''
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + '\n'
        return text
    except Exception as e:
        st.error(f"Error parsing PDF: {str(e)}")
        return ""

def process_text_to_df_enhanced(text, bank_name):
    """Process text to DataFrame based on bank format"""
    transactions = []
    
    if bank_name == 'FNB':
        transaction_pattern = re.compile(
            r'(\d{2} \w{3})\s+'  # Date (e.g., "02 Apr")
            r'(.+?)\s+'  # Description
            r'(-?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)(Cr|Dr)?\s*'  # Amount
            r'(-?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)(Cr|Dr)?'  # Balance
        )
        
        for line in text.split('\n'):
            line = line.strip()
            if not line or 'Transactions in RAND' in line or 'Date Description' in line:
                continue

            match = transaction_pattern.search(line)
            if match:
                try:
                    groups = match.groups()
                    date_str, description, amount_str, cr_dr1, balance_str, _ = groups

                    # Convert date to standard format
                    current_year = datetime.now().year
                    date_obj = datetime.strptime(f"{date_str} {current_year}", "%d %b %Y")
                    date_str = date_obj.strftime("%Y-%m-%d")

                    # Clean and convert amounts
                    amount = float(amount_str.replace(',', ''))
                    if cr_dr1 == 'Cr':
                        amount = -amount  # Credits are negative in our system

                    balance = float(balance_str.replace(',', ''))

                    transactions.append([date_str, description.strip(), amount, balance])
                except (ValueError, AttributeError):
                    continue
                    
    elif bank_name == 'Standard Bank':
        transaction_pattern = re.compile(
            r'(\d{2} \w{3} \d{2})\s+'  # Date (e.g., "02 Apr 22")
            r'(.+?)\s+'  # Description
            r'(-?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)\s+'  # Amount
            r'(-?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)'  # Balance
        )
        
        for line in text.split('\n'):
            line = line.strip()
            if not line or 'Date Description' in line or 'Transactions in RAND' in line:
                continue

            match = transaction_pattern.search(line)
            if match:
                try:
                    date_str, description, amount_str, balance_str = match.groups()

                    # Convert date to standard format
                    date_obj = datetime.strptime(date_str, '%d %b %y')
                    date_str = date_obj.strftime('%Y-%m-%d')

                    # Clean and convert amounts
                    amount = float(amount_str.replace(',', ''))
                    balance = float(balance_str.replace(',', ''))

                    transactions.append([date_str, description.strip(), amount, balance])
                except (ValueError, AttributeError):
                    continue
                    
    elif bank_name == 'Nedbank':
        transaction_pattern = re.compile(
            r'(\d{2}/\d{2}/\d{4})\s+'  # Date (e.g., "02/04/2022")
            r'(.+?)\s+'  # Description
            r'(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)\s+'  # Amount
            r'(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)'  # Balance
        )
        
        for line in text.split('\n'):
            line = line.strip()
            if not line or 'Date Description' in line or 'Transactions in RAND' in line:
                continue

            match = transaction_pattern.search(line)
            if match:
                try:
                    date_str, description, amount_str, balance_str = match.groups()

                    # Clean and convert amounts
                    amount = float(amount_str.replace(',', '').replace('R', '').replace(' ', ''))
                    balance = float(balance_str.replace(',', '').replace('R', '').replace(' ', ''))

                    transactions.append([date_str, description.strip(), amount, balance])
                except (ValueError, AttributeError):
                    continue
                    
    else:  # Default pattern for other banks
        transaction_pattern = re.compile(
            r'(\d{4}-\d{2}-\d{2})\s+'  # Date
            r'(.+?)\s+'  # Description
            r'(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)\s+'  # Amount
            r'(-?R?\d{1,3}(?:,\d{3})*(?:\.\d{2})?)'  # Balance
        )
        
        for line in text.split('\n'):
            line = line.strip()
            if not line:
                continue

            match = transaction_pattern.search(line)
            if match:
                try:
                    date_str, description, amount_str, balance_str = match.groups()

                    # Clean and convert amounts
                    amount = float(amount_str.replace(',', '').replace('R', '').replace(' ', ''))
                    balance = float(balance_str.replace(',', '').replace('R', '').replace(' ', ''))

                    transactions.append([date_str, description.strip(), amount, balance])
                except (ValueError, AttributeError):
                    continue

    if not transactions:
        return pd.DataFrame(columns=['Date', 'Description', 'Amount', 'Balance'])

    df = pd.DataFrame(transactions, columns=['Date', 'Description', 'Amount', 'Balance'])
    df['Date'] = pd.to_datetime(df['Date'])
    df = df.sort_values('Date').reset_index(drop=True)
    df['Bank'] = bank_name  # Add bank name column

    return df

def categorize_expense_enhanced(description, bank_name):
    """Enhanced expense categorization with bank-specific rules"""
    description_lower = description.lower()

    # Common categories across all banks
    common_categories = {
        'Salary/Income': ['salary', 'wage', 'income', 'payroll', 'refund'],
        'Groceries': ['grocery', 'supermarket', 'food', 'spar', 'checkers', 'woolworths'],
        'Transport': ['fuel', 'petrol', 'uber', 'taxi', 'transport', 'car payment'],
        'Utilities': ['electricity', 'water', 'municipal', 'rates', 'internet', 'phone'],
        'Entertainment': ['restaurant', 'movie', 'entertainment', 'netflix', 'spotify'],
        'Healthcare': ['medical', 'doctor', 'hospital', 'pharmacy', 'health'],
        'Shopping': ['retail', 'clothing', 'amazon', 'takealot', 'mall'],
        'Investments': ['investment', 'shares', 'unit trust', 'retirement'],
        'Insurance': ['insurance', 'medical aid', 'life cover', 'short term'],
        'Bank Charges': ['fees', 'charge', 'service', 'cost', 'monthly account fee'],
        'Cash Transactions': ['atm', 'cash', 'withdrawal', 'deposit'],
        'Cellular': ['airtime', 'data', 'vodacom', 'mtn', 'cell c'],
        'Interest': ['interest'],
        'Failed Transactions': ['unsuccessful', 'declined', 'failed']
    }

    # Bank-specific categories
    bank_specific = {
        'FNB': {
            'POS Purchases': ['pos purchase', 'card purchase', 'debit order'],
            'Payments': ['payment to', 'fnb app rtc pmt', 'internet pmt', 'debit order']
        },
        'Standard Bank': {
            'Payments': ['payment', 'transfer', 'debit order', 'immediate trf', 'digital payment']
        },
        'Nedbank': {
            'POS Purchases': ['pos purchase', 'card purchase', 'debit order', 'cashsend'],
            'Payments': ['payment to', 'immediate trf', 'digital payment', 'pmt to'],
            'Loans': ['loan payment', 'nedloan', 'personal loan']
        },
        'ABSA': {
            'POS Purchases': ['cashsend mobile', 'pos purchase'],
            'Payments': ['immediate trf', 'digital payment', 'payment']
        }
    }

    # Combine common and bank-specific categories
    category_keywords = {**common_categories, **(bank_specific.get(bank_name, {}))}

    # Check for specific keywords
    for category, keywords in category_keywords.items():
        if any(keyword in description_lower for keyword in keywords):
            return category

    # Try NLP-based categorization if available
    try:
        if 'TextBlob' in globals():
            blob = TextBlob(description)
            # Simple sentiment and keyword analysis could be added here
            pass
    except:
        pass

    return 'Other'

def create_advanced_visualizations(df, patterns, recommendations):
    """Create advanced interactive visualizations"""
    # 1. Financial Health Dashboard
    col1, col2, col3, col4 = st.columns(4)

    total_income = df[df['Amount'] > 0]['Amount'].sum()
    total_expenses = abs(df[df['Amount'] < 0]['Amount'].sum())
    net_flow = total_income - total_expenses
    transaction_count = len(df)

    with col1:
        st.metric("Total Income", f"R{total_income:,.2f}", delta=None)
    with col2:
        st.metric("Total Expenses", f"R{total_expenses:,.2f}", delta=None)
    with col3:
        st.metric("Net Cash Flow", f"R{net_flow:,.2f}",
                 delta=f"{'Positive' if net_flow > 0 else 'Negative'}")
    with col4:
        st.metric("Transactions", f"{transaction_count}", delta=None)

    # 2. Enhanced spending analysis
    fig_treemap = px.treemap(
        df.groupby('Category')['Amount'].sum().abs().reset_index(),
        path=['Category'],
        values='Amount',
        title='Spending Distribution by Category (Treemap)'
    )
    st.plotly_chart(fig_treemap, use_container_width=True)

    # 3. Time series analysis with predictions
    daily_spending = df.groupby(df['Date'].dt.date)['Amount'].sum().reset_index()
    daily_spending['Date'] = pd.to_datetime(daily_spending['Date'])

    fig_timeseries = go.Figure()
    fig_timeseries.add_trace(go.Scatter(
        x=daily_spending['Date'],
        y=daily_spending['Amount'],
        mode='lines+markers',
        name='Actual Spending',
        line=dict(color='blue')
    ))

    # Add trend line
    x_numeric = np.arange(len(daily_spending))
    z = np.polyfit(x_numeric, daily_spending['Amount'], 1)
    p = np.poly1d(z)
    fig_timeseries.add_trace(go.Scatter(
        x=daily_spending['Date'],
        y=p(x_numeric),
        mode='lines',
        name='Trend',
        line=dict(color='red', dash='dash')
    ))

    fig_timeseries.update_layout(title='Daily Spending Trend with Projection')
    st.plotly_chart(fig_timeseries, use_container_width=True)

    # 4. Category-wise monthly analysis
    df['Month'] = df['Date'].dt.to_period('M')
    monthly_category = df.groupby(['Month', 'Category'])['Amount'].sum().abs().reset_index()
    monthly_category['Month'] = monthly_category['Month'].astype(str)

    fig_monthly = px.bar(
        monthly_category,
        x='Month',
        y='Amount',
        color='Category',
        title='Monthly Spending by Category'
    )
    st.plotly_chart(fig_monthly, use_container_width=True)

def main():
    """Main application function"""
    # Initialize database
    db = DatabaseManager()

    # Sidebar for user authentication
    with st.sidebar:
        st.title("๐Ÿฆ Universal Bank Analyzer")

        if st.session_state.user_profile is None:
            tab1, tab2 = st.tabs(["Login", "Sign Up"])

            with tab1:
                st.subheader("Login")
                email = st.text_input("Email", key="login_email")
                password = st.text_input("Password", type="password", key="login_password")

                if st.button("Login", key="login_btn"):
                    user_data = db.authenticate_user(email, password)
                    if user_data:
                        user_id, name = user_data
                        st.session_state.user_profile = db.get_user_profile(user_id)
                        st.success(f"Welcome back, {name}!")
                        st.rerun()
                    else:
                        st.error("Invalid credentials")

            with tab2:
                st.subheader("Create Account")
                new_name = st.text_input("Full Name", key="signup_name")
                new_email = st.text_input("Email", key="signup_email")
                new_password = st.text_input("Password", type="password", key="signup_password")
                monthly_income = st.number_input("Monthly Income (R)", min_value=0.0, key="signup_income")
                risk_tolerance = st.selectbox("Risk Tolerance", ["conservative", "moderate", "aggressive"], key="signup_risk")
                financial_goals = st.text_area("Financial Goals", key="signup_goals")

                if st.button("Create Account", key="signup_btn"):
                    if new_name and new_email and new_password:
                        user_id = hashlib.md5(new_email.encode()).hexdigest()[:8]
                        if db.create_user(user_id, new_name, new_email, new_password, financial_goals, risk_tolerance, monthly_income):
                            st.success("Account created successfully! Please login.")
                        else:
                            st.error("Email already exists")
                    else:
                        st.error("Please fill all required fields")
                        st.rerun()

        else:
            st.success(f"Welcome, {st.session_state.user_profile['name']}!")
            if st.button("Logout"):
                st.session_state.user_profile = None
                st.session_state.transactions_df = None
                st.session_state.analysis_complete = False
                st.session_state.detected_bank = None
                st.experimental_rerun()

    # Main content area
    if st.session_state.user_profile is None:
        st.markdown("""
        # ๐Ÿฆ Universal Bank Statement Analysis

        ### Welcome to the next generation of financial analysis for all major banks!

        **Supported Banks:**
        - FNB (First National Bank)
        - Standard Bank
        - Nedbank
        - ABSA
        - Capitec Bank

        **Key Features:**
        - ๐Ÿค– **AI-Powered Insights**: Advanced machine learning for personalized recommendations
        - ๐Ÿ“Š **Comprehensive Analytics**: Deep dive into your spending patterns
        - ๐ŸŽฏ **Goal Tracking**: Set and monitor your financial objectives
        - ๐Ÿ”ฎ **Predictive Analysis**: Forecast future spending trends
        - ๐Ÿ›ก๏ธ **Anomaly Detection**: Identify unusual transactions
        - ๐Ÿ’ก **Smart Recommendations**: Personalized financial advice
        - ๐Ÿ’ฐ **Loan Eligibility**: Check your loan eligibility instantly

        **Please login or create an account to get started.**
        """)
        return

    # Main analysis interface
    st.title(f"๐Ÿฆ Universal Bank Analysis Dashboard - {st.session_state.user_profile['name']}")

    # File upload section
    st.markdown("### ๐Ÿ“„ Upload Your Bank Statement")
    uploaded_file = st.file_uploader(
        "Choose a PDF bank statement",
        type="pdf",
        help="Upload your bank statement in PDF format for analysis"
    )

    if uploaded_file is not None:
        try:
            # Parse PDF and identify bank
            with st.spinner("๐Ÿ” Analyzing bank statement..."):
                text = extract_text_from_pdf(uploaded_file)
                bank_name = identify_bank_from_text(text)
                
                if not bank_name:
                    st.error("Could not identify bank from statement. Please ensure it's from a supported bank.")
                    return
                
                st.session_state.detected_bank = bank_name
                st.success(f"Detected Bank: {bank_name}")

                # Extract metadata based on bank
                metadata = extract_bank_statement_metadata(text, bank_name)
                
                # Parse transactions
                df = process_text_to_df_enhanced(text, bank_name)

            if df.empty:
                st.warning("โš ๏ธ No transactions found in the uploaded statement. Please check the file format.")
                return

            # Store in session state
            st.session_state.transactions_df = df

            # Enhance data with categories
            df['Category'] = df['Description'].apply(lambda x: categorize_expense_enhanced(x, bank_name))

            # Initialize analytics engines
            personalization = PersonalizationEngine(st.session_state.user_profile)
            analytics = AdvancedAnalytics()

            # Perform analysis
            with st.spinner("๐Ÿง  Analyzing your financial data..."):
                patterns = personalization.analyze_spending_patterns(df)
                recommendations = personalization.generate_personalized_recommendations(df, patterns)
                health_score, score_components = personalization.calculate_financial_health_score(df, patterns)
                loan_prediction = analytics.enhanced_loan_prediction(df, bank_name)
                df_with_anomalies = analytics.detect_anomalies(df)

            st.session_state.analysis_complete = True

            # Display account info
            if bank_name == 'FNB':
                account_holder_name, closing_balance = metadata
                st.success(f"Account Holder: {account_holder_name}")
                st.info(f"Closing Balance: R{closing_balance:,.2f}")
            else:
                account_holder_name, account_number, statement_period, closing_balance = metadata
                st.success(f"Account Holder: {account_holder_name}")
                st.info(f"Account Number: {account_number} | Statement Period: {statement_period}")
                st.info(f"Closing Balance: R{closing_balance:,.2f}")

            # Display results in tabs
            tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
                "๐Ÿ“Š Overview", "๐Ÿ’ก Recommendations", "๐Ÿฅ Health Score",
                "๐Ÿ” Detailed Analysis", "๐Ÿšจ Anomalies", "๐Ÿ’ฐ Loan Eligibility"
            ])

            with tab1:
                st.markdown("### ๐Ÿ“ˆ Financial Overview")
                create_advanced_visualizations(df, patterns, recommendations)

                # Transaction table with enhanced features
                st.markdown("### ๐Ÿ“‹ Transaction History")
                st.dataframe(
                    df[['Date', 'Description', 'Category', 'Amount', 'Balance']],
                    use_container_width=True
                )

            with tab2:
                st.markdown("### ๐Ÿ’ก Personalized Recommendations")

                if recommendations:
                    for i, rec in enumerate(recommendations):
                        priority_color = {
                            'high': '๐Ÿ”ด',
                            'medium': '๐ŸŸก',
                            'low': '๐ŸŸข'
                        }.get(rec['priority'], 'โšช')

                        st.markdown(f"""
                        **{priority_color} {rec['title']}**

                        {rec['description']}

                        *Category: {rec['type'].title()} | Priority: {rec['priority'].title()}*
                        """)
                        st.divider()
                else:
                    st.info("๐Ÿ’ซ Great job! Your financial habits look healthy. Keep up the good work!")

            with tab3:
                st.markdown("### ๐Ÿฅ Financial Health Score")

                # Display overall score
                col1, col2 = st.columns([1, 2])

                with col1:
                    # Create gauge chart for health score
                    fig_gauge = go.Figure(go.Indicator(
                        mode = "gauge+number+delta",
                        value = health_score,
                        domain = {'x': [0, 1], 'y': [0, 1]},
                        title = {'text': "Financial Health Score"},
                        delta = {'reference': 75},
                        gauge = {
                            'axis': {'range': [None, 100]},
                            'bar': {'color': "darkblue"},
                            'steps': [
                                {'range': [0, 25], 'color': "lightgray"},
                                {'range': [25, 50], 'color': "gray"},
                                {'range': [50, 75], 'color': "lightgreen"},
                                {'range': [75, 100], 'color': "green"}
                            ],
                            'threshold': {
                                'line': {'color': "red", 'width': 4},
                                'thickness': 0.75,
                                'value': 90
                            }
                        }
                    ))
                    fig_gauge.update_layout(height=300)
                    st.plotly_chart(fig_gauge, use_container_width=True)

                with col2:
                    st.markdown("#### Score Breakdown")
                    for component, score in score_components.items():
                        component_name = component.replace('_', ' ').title()
                        st.progress(score/25, text=f"{component_name}: {score:.1f}/25")

                # Health recommendations
                if health_score < 60:
                    st.warning("โš ๏ธ Your financial health needs attention. Consider the recommendations above.")
                elif health_score < 80:
                    st.info("๐Ÿ’ก Good financial health! A few improvements could boost your score.")
                else:
                    st.success("๐ŸŽ‰ Excellent financial health! You're doing great!")

            with tab4:
                st.markdown("### ๐Ÿ” Detailed Financial Analysis")

                # Advanced metrics
                col1, col2 = st.columns(2)

                with col1:
                    st.markdown("#### Spending Patterns")
                    weekend_avg = patterns.get('weekend_vs_weekday', {}).get('weekend_avg', 0)
                    weekday_avg = patterns.get('weekend_vs_weekday', {}).get('weekday_avg', 0)

                    st.write(f"Weekend Average: R{weekend_avg:.2f}")
                    st.write(f"Weekday Average: R{weekday_avg:.2f}")
                    st.write(f"Transaction Frequency: {patterns.get('transaction_frequency', 0):.2f} per day")

                with col2:
                    st.markdown("#### Monthly Trends")
                    monthly_trends = patterns.get('monthly_trends', {})
                    for month, avg_spending in monthly_trends.items():
                        month_name = pd.to_datetime(f"2023-{month:02d}-01").strftime("%B")
                        st.write(f"{month_name}: R{avg_spending:.2f}")

                # Category analysis
                st.markdown("#### Category Analysis")
                category_data = []
                for category, data in patterns.get('spending_by_category', {}).items():
                    if isinstance(data, dict):
                        category_data.append({
                            'Category': category,
                            'Total': data.get('sum', 0),
                            'Average': data.get('mean', 0),
                            'Count': data.get('count', 0),
                            'Std Dev': data.get('std', 0)
                        })

                if category_data:
                    category_df = pd.DataFrame(category_data)
                    st.dataframe(category_df, use_container_width=True)

            with tab5:
                st.markdown("### ๐Ÿšจ Anomaly Detection")

                anomalies = df_with_anomalies[df_with_anomalies['IsAnomaly']]

                if not anomalies.empty:
                    st.warning(f"โš ๏ธ Found {len(anomalies)} unusual transactions:")
                    st.dataframe(
                        anomalies[['Date', 'Description', 'Amount', 'Category']],
                        use_container_width=True
                    )

                    # Visualization
                    fig_anomaly = px.scatter(
                        df_with_anomalies,
                        x='Date',
                        y='Amount',
                        color='IsAnomaly',
                        title='Transaction Anomalies',
                        color_discrete_map={True: 'red', False: 'blue'}
                    )
                    st.plotly_chart(fig_anomaly, use_container_width=True)
                else:
                    st.success("โœ… No unusual transactions detected. Your spending patterns look normal!")

            with tab6:
                st.markdown("### ๐Ÿ’ฐ Loan Eligibility Assessment")

                # Display loan prediction results
                if loan_prediction['eligible']:
                    st.success(f"โœ… **Congratulations!** You are eligible for a loan.")
                else:
                    st.error(f"โŒ **Unfortunately,** you are not currently eligible for a loan.")

                col1, col2 = st.columns(2)
                with col1:
                    st.metric("Confidence Score", f"{loan_prediction['confidence']:.1%}")
                with col2:
                    st.metric("Model Used", loan_prediction['model_type'])

                # Detailed loan analysis
                st.markdown("#### Loan Assessment Factors")

                total_credits = df[df['Amount'] > 0]['Amount'].sum()
                total_debits = abs(df[df['Amount'] < 0]['Amount'].sum())
                debt_to_income = total_debits / total_credits if total_credits > 0 else float('inf')

                factors = {
                    "Total Income": f"R{total_credits:,.2f}",
                    "Total Expenses": f"R{total_debits:,.2f}",
                    "Debt-to-Income Ratio": f"{debt_to_income:.2%}",
                    "Net Cash Flow": f"R{total_credits - total_debits:,.2f}",
                    "Transaction Count": str(len(df)),
                    "Account Balance Trend": f"R{patterns.get('balance_trend', 0):,.2f}"
                }

                for factor, value in factors.items():
                    st.write(f"**{factor}:** {value}")

                # Improvement suggestions for loan eligibility
                if not loan_prediction['eligible']:
                    st.markdown("#### ๐Ÿ’ก How to Improve Your Loan Eligibility")
                    st.markdown("""
                    - **Increase Income**: Look for ways to boost your monthly income
                    - **Reduce Expenses**: Cut down on non-essential spending
                    - **Build Savings**: Maintain a higher account balance
                    - **Regular Transactions**: Show consistent financial activity
                    - **Improve Cash Flow**: Ensure more money comes in than goes out
                    """)

        except Exception as e:
            st.error(f"โŒ An error occurred while processing your statement: {str(e)}")
            st.info("Please ensure your PDF is a valid bank statement and try again.")

if __name__ == "__main__":
    main()

# Custom CSS for better styling
st.markdown("""
<style>
    .metric-card {
        background-color: #f0f2f6;
        padding: 1rem;
        border-radius: 0.5rem;
        border-left: 4px solid #1f77b4;
    }

    .recommendation-card {
        background-color: #f8f9fa;
        padding: 1rem;
        border-radius: 0.5rem;
        margin: 0.5rem 0;
        border-left: 4px solid #28a745;
    }

    .stTabs [data-baseweb="tab-list"] {
        gap: 24px;
    }

    .stTabs [data-baseweb="tab"] {
        height: 50px;
        white-space: pre-wrap;
        background-color: #f0f2f6;
        border-radius: 4px 4px 0px 0px;
        gap: 1px;
        padding-top: 10px;
        padding-bottom: 10px;
    }

    .stTabs [aria-selected="true"] {
        background-color: #1f77b4;
        color: white;
    }
</style>
""", unsafe_allow_html=True)