File size: 52,927 Bytes
9d3939c
a442f53
 
f70d963
4980e0d
c9bac47
 
 
 
 
 
a442f53
f70d963
 
4980e0d
f70d963
4980e0d
a442f53
f70d963
8b5a437
4980e0d
 
 
 
8b5a437
4980e0d
 
a442f53
4980e0d
 
 
 
 
 
 
 
 
a442f53
4980e0d
 
d5effd8
a442f53
49bd39d
 
9c0039a
7c6c379
9d3939c
7c6c379
9d3939c
7c6c379
c9bac47
9d3939c
c9bac47
 
9d3939c
 
 
c9bac47
9d3939c
1e5f089
 
9d3939c
c9bac47
9d3939c
c9bac47
 
 
 
 
9d3939c
 
c9bac47
9d3939c
 
c9bac47
 
 
9d3939c
c9bac47
 
 
9d3939c
c9bac47
9d3939c
 
c9bac47
 
9d3939c
c9bac47
 
 
9d3939c
c9bac47
 
 
 
 
 
 
 
9d3939c
 
c9bac47
9d3939c
 
c9bac47
 
 
9d3939c
c9bac47
 
 
 
 
 
 
 
 
7c6c379
 
42effba
c9bac47
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
 
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
49bd39d
c9bac47
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49bd39d
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d3939c
 
 
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e5f089
9d3939c
 
 
c9bac47
 
 
9d3939c
 
 
 
 
 
 
 
 
 
c9bac47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a442f53
 
 
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
import os
import streamlit as st
import pandas as pd
import google.generativeai as genai
from google.generativeai.types import GenerationConfig
import datetime
import uuid
import json
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Configuration
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your_key_here')
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

# Load and prepare data
@st.cache_data
def load_scholarships():
    try:
        df = pd.read_csv("scholarships_data.csv", quotechar='"', quoting=1)
        df = df.map(lambda x: x.strip() if isinstance(x, str) else x)
        df.rename(columns=lambda x: x.strip(), inplace=True)
        return df
    except Exception as e:
        st.error(f"Error loading data: {str(e)}")
        st.stop()

# Convert CSV to RAG context
def create_rag_context(df):
    context = "Scholarship Database:\n\n"
    for _, row in df.iterrows():
        context += f"""Scholarship Name: {row['Scholarship Name']}
Eligibility: {row['Eligibility']}
Deadline: {row['Deadline']}
Link: {row['Link']}\n\n"""
    return context

# Initialize Gemini model
def get_rag_model():
    return genai.GenerativeModel('gemini-1.5-pro')

# Custom CSS for styling
def load_css():
    st.markdown("""
    <style>
    /* Custom color for results */
    .stMarkdown p, .stMarkdown ul, .stMarkdown ol {
        color: rgb(1, 27, 29); /* Custom color for results */
    }
    
    /* Card styling */
    .scholarship-card {
        background-color: #f8f9fa;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        border-left: 5px solid #4CAF50;
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    /* Tabs styling */
    .stTabs [data-baseweb="tab-list"] {
        gap: 8px;
    }
    
    .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: #4CAF50;
        color: white;
    }
    
    /* Button styling */
    .stButton>button {
        border-radius: 20px;
        font-weight: 500;
    }
    
    /* Progress indicator */
    .progress-container {
        display: flex;
        justify-content: space-between;
        margin-bottom: 30px;
    }
    
    .progress-step {
        text-align: center;
        flex: 1;
    }
    
    .progress-step-number {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background-color: #ddd;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 10px auto;
    }
    
    .progress-step.active .progress-step-number {
        background-color: #4CAF50;
    }
    
    .progress-step-title {
        font-size: 0.8rem;
    }
    </style>
    """, unsafe_allow_html=True)

# User management functions
def load_users():
    try:
        with open("users.json", "r") as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return {}

def save_users(users):
    with open("users.json", "w") as f:
        json.dump(users, f)

def authenticate(username, password):
    users = load_users()
    if username in users and users[username]["password"] == password:
        return True
    return False

def register_user(username, password, email):
    users = load_users()
    if username in users:
        return False
    
    users[username] = {
        "password": password,
        "email": email,
        "profile": {},
        "saved_scholarships": [],
        "applications": {},
        "created_at": datetime.datetime.now().isoformat()
    }
    save_users(users)
    return True

# Saved scholarship functions
def save_scholarship(username, scholarship_name, deadline):
    users = load_users()
    if username in users:
        # Check if already saved
        if scholarship_name not in [s["name"] for s in users[username]["saved_scholarships"]]:
            users[username]["saved_scholarships"].append({
                "name": scholarship_name,
                "deadline": deadline,
                "saved_at": datetime.datetime.now().isoformat(),
                "status": "Saved"
            })
            save_users(users)
            return True
    return False

def get_saved_scholarships(username):
    users = load_users()
    if username in users:
        return users[username]["saved_scholarships"]
    return []

def update_scholarship_status(username, scholarship_name, status):
    users = load_users()
    if username in users:
        for scholarship in users[username]["saved_scholarships"]:
            if scholarship["name"] == scholarship_name:
                scholarship["status"] = status
                save_users(users)
                return True
    return False

# Application tracking functions
def track_application(username, scholarship_name, status="Started"):
    users = load_users()
    if username in users:
        if scholarship_name not in users[username]["applications"]:
            users[username]["applications"][scholarship_name] = {
                "status": status,
                "progress": 0,
                "started_at": datetime.datetime.now().isoformat(),
                "form_data": {}
            }
            save_users(users)
            return True
    return False

def update_application(username, scholarship_name, form_data, progress):
    users = load_users()
    if username in users and scholarship_name in users[username]["applications"]:
        users[username]["applications"][scholarship_name]["form_data"] = form_data
        users[username]["applications"][scholarship_name]["progress"] = progress
        users[username]["applications"][scholarship_name]["updated_at"] = datetime.datetime.now().isoformat()
        save_users(users)
        return True
    return False

def get_application_progress(username, scholarship_name):
    users = load_users()
    if username in users and scholarship_name in users[username]["applications"]:
        return users[username]["applications"][scholarship_name]
    return None

# Email notification function
def send_deadline_notification(email, scholarship_name, deadline_date):
    try:
        sender_email = os.getenv('APP_EMAIL', '[email protected]')
        password = os.getenv('APP_EMAIL_PASSWORD', 'app_password')
        
        msg = MIMEMultipart()
        msg['From'] = sender_email
        msg['To'] = email
        msg['Subject'] = f"Deadline Reminder: {scholarship_name}"
        
        body = f"""
        <html>
        <body>
        <h2>Deadline Reminder</h2>
        <p>Hello,</p>
        <p>This is a reminder that the application deadline for <b>{scholarship_name}</b> is approaching.</p>
        <p>Deadline: <b>{deadline_date}</b></p>
        <p>Please log in to the AI Scholarship Advisor to complete your application.</p>
        <p>Best of luck!</p>
        </body>
        </html>
        """
        
        msg.attach(MIMEText(body, 'html'))
        
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(sender_email, password)
        server.send_message(msg)
        server.quit()
        return True
    except Exception as e:
        st.error(f"Failed to send email: {str(e)}")
        return False

# Profile management
def update_user_profile(username, profile_data):
    users = load_users()
    if username in users:
        users[username]["profile"] = profile_data
        save_users(users)
        return True
    return False

def get_user_profile(username):
    users = load_users()
    if username in users:
        return users[username]["profile"]
    return {}

# Forum functions
def load_forum_posts():
    try:
        with open("forum_posts.json", "r") as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return []

def save_forum_posts(posts):
    with open("forum_posts.json", "w") as f:
        json.dump(posts, f)

def add_forum_post(username, title, content, category):
    posts = load_forum_posts()
    post_id = str(uuid.uuid4())
    posts.append({
        "id": post_id,
        "username": username,
        "title": title,
        "content": content,
        "category": category,
        "created_at": datetime.datetime.now().isoformat(),
        "replies": []
    })
    save_forum_posts(posts)
    return post_id

def add_forum_reply(post_id, username, content):
    posts = load_forum_posts()
    for post in posts:
        if post["id"] == post_id:
            reply_id = str(uuid.uuid4())
            post["replies"].append({
                "id": reply_id,
                "username": username,
                "content": content,
                "created_at": datetime.datetime.now().isoformat()
            })
            save_forum_posts(posts)
            return reply_id
    return None

# Create profile completion wizard
def render_profile_wizard(username):
    st.markdown("### Complete Your Profile")
    
    # Progress indicator
    st.markdown("""
    <div class="progress-container">
        <div class="progress-step active">
            <div class="progress-step-number">1</div>
            <div class="progress-step-title">Personal Info</div>
        </div>
        <div class="progress-step">
            <div class="progress-step-number">2</div>
            <div class="progress-step-title">Academic Info</div>
        </div>
        <div class="progress-step">
            <div class="progress-step-number">3</div>
            <div class="progress-step-title">Financial Info</div>
        </div>
        <div class="progress-step">
            <div class="progress-step-number">4</div>
            <div class="progress-step-title">Preferences</div>
        </div>
    </div>
    """, unsafe_allow_html=True)
    
    profile = get_user_profile(username)
    step = st.session_state.get("profile_step", 1)
    
    if step == 1:
        with st.form("personal_info"):
            st.markdown("#### Step 1: Personal Information")
            first_name = st.text_input("First Name", profile.get("first_name", ""))
            last_name = st.text_input("Last Name", profile.get("last_name", ""))
            dob = st.date_input("Date of Birth", value=datetime.datetime.strptime(profile.get("dob", "2000-01-01"), "%Y-%m-%d").date() if "dob" in profile else datetime.date(2000, 1, 1))
            gender = st.selectbox("Gender", ["Male", "Female", "Other", "Prefer not to say"], index=["Male", "Female", "Other", "Prefer not to say"].index(profile.get("gender", "Prefer not to say")) if "gender" in profile else 3)
            citizenship = st.selectbox("Citizenship", ["India", "Other"], index=["India", "Other"].index(profile.get("citizenship", "India")) if "citizenship" in profile else 0)
            
            if st.form_submit_button("Next"):
                profile.update({
                    "first_name": first_name,
                    "last_name": last_name,
                    "dob": dob.strftime("%Y-%m-%d"),
                    "gender": gender,
                    "citizenship": citizenship
                })
                update_user_profile(username, profile)
                st.session_state.profile_step = 2
                st.rerun()
    
    elif step == 2:
        st.markdown("""
        <div class="progress-container">
            <div class="progress-step active">
                <div class="progress-step-number">1</div>
                <div class="progress-step-title">Personal Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">2</div>
                <div class="progress-step-title">Academic Info</div>
            </div>
            <div class="progress-step">
                <div class="progress-step-number">3</div>
                <div class="progress-step-title">Financial Info</div>
            </div>
            <div class="progress-step">
                <div class="progress-step-number">4</div>
                <div class="progress-step-title">Preferences</div>
            </div>
        </div>
        """, unsafe_allow_html=True)
        
        with st.form("academic_info"):
            st.markdown("#### Step 2: Academic Information")
            education = st.selectbox("Education Level", 
                                   ["High School", "Undergraduate", "Postgraduate", "PhD"],
                                   index=["High School", "Undergraduate", "Postgraduate", "PhD"].index(profile.get("education", "Undergraduate")) if "education" in profile else 1)
            
            institution = st.text_input("Current Institution", profile.get("institution", ""))
            field_of_study = st.text_input("Field of Study", profile.get("field_of_study", ""))
            
            cgpa = st.number_input("CGPA (out of 10)", 0.0, 10.0, float(profile.get("cgpa", 8.0)) if "cgpa" in profile else 8.0, 0.1)
            
            achievements = st.text_area("Academic Achievements", profile.get("achievements", ""))
            
            col1, col2 = st.columns(2)
            with col1:
                if st.form_submit_button("Previous"):
                    st.session_state.profile_step = 1
                    st.rerun()
            
            with col2:
                if st.form_submit_button("Next"):
                    profile.update({
                        "education": education,
                        "institution": institution,
                        "field_of_study": field_of_study,
                        "cgpa": cgpa,
                        "achievements": achievements
                    })
                    update_user_profile(username, profile)
                    st.session_state.profile_step = 3
                    st.rerun()
    
    elif step == 3:
        st.markdown("""
        <div class="progress-container">
            <div class="progress-step active">
                <div class="progress-step-number">1</div>
                <div class="progress-step-title">Personal Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">2</div>
                <div class="progress-step-title">Academic Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">3</div>
                <div class="progress-step-title">Financial Info</div>
            </div>
            <div class="progress-step">
                <div class="progress-step-number">4</div>
                <div class="progress-step-title">Preferences</div>
            </div>
        </div>
        """, unsafe_allow_html=True)
        
        with st.form("financial_info"):
            st.markdown("#### Step 3: Financial Information")
            
            income = st.number_input("Annual Family Income (β‚Ή)", 0, 10000000, int(profile.get("income", 300000)) if "income" in profile else 300000, 10000)
            
            category = st.selectbox("Category", 
                                  ["General", "OBC", "SC", "ST", "EWS", "Minority"],
                                  index=["General", "OBC", "SC", "ST", "EWS", "Minority"].index(profile.get("category", "General")) if "category" in profile else 0)
            
            financial_need = st.slider("Financial Need Level", 1, 10, int(profile.get("financial_need", 5)) if "financial_need" in profile else 5)
            
            col1, col2 = st.columns(2)
            with col1:
                if st.form_submit_button("Previous"):
                    st.session_state.profile_step = 2
                    st.rerun()
            
            with col2:
                if st.form_submit_button("Next"):
                    profile.update({
                        "income": income,
                        "category": category,
                        "financial_need": financial_need
                    })
                    update_user_profile(username, profile)
                    st.session_state.profile_step = 4
                    st.rerun()
    
    elif step == 4:
        st.markdown("""
        <div class="progress-container">
            <div class="progress-step active">
                <div class="progress-step-number">1</div>
                <div class="progress-step-title">Personal Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">2</div>
                <div class="progress-step-title">Academic Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">3</div>
                <div class="progress-step-title">Financial Info</div>
            </div>
            <div class="progress-step active">
                <div class="progress-step-number">4</div>
                <div class="progress-step-title">Preferences</div>
            </div>
        </div>
        """, unsafe_allow_html=True)
        
        with st.form("preferences"):
            st.markdown("#### Step 4: Preferences")
            
            preferred_locations = st.multiselect(
                "Preferred Study Locations", 
                ["India", "USA", "UK", "Canada", "Australia", "Germany", "Japan", "Singapore", "Other"],
                profile.get("preferred_locations", ["India"])
            )
            
            study_interests = st.multiselect(
                "Fields of Interest",
                ["Engineering", "Medicine", "Law", "Business", "Arts", "Science", "Humanities", "Social Sciences", "Other"],
                profile.get("study_interests", ["Engineering"])
            )
            
            career_goals = st.text_area("Career Goals", profile.get("career_goals", ""))
            
            extracurricular = st.text_area("Extracurricular Activities", profile.get("extracurricular", ""))
            
            notification_preference = st.checkbox("Receive Email Notifications", value=profile.get("notification_preference", True))
            
            col1, col2 = st.columns(2)
            with col1:
                if st.form_submit_button("Previous"):
                    st.session_state.profile_step = 3
                    st.rerun()
            
            with col2:
                if st.form_submit_button("Complete Profile"):
                    profile.update({
                        "preferred_locations": preferred_locations,
                        "study_interests": study_interests,
                        "career_goals": career_goals,
                        "extracurricular": extracurricular,
                        "notification_preference": notification_preference,
                        "profile_completed": True,
                        "completed_at": datetime.datetime.now().isoformat()
                    })
                    update_user_profile(username, profile)
                    st.session_state.profile_complete = True
                    st.success("Profile completed successfully!")
                    st.rerun()

# Auto-fill application form
def render_application_form(username, scholarship_name, df):
    st.markdown(f"### Application Form: {scholarship_name}")
    
    # Get scholarship details
    scholarship_row = df[df['Scholarship Name'] == scholarship_name].iloc[0]
    deadline = scholarship_row['Deadline']
    eligibility = scholarship_row['Eligibility']
    link = scholarship_row['Link']
    
    st.info(f"Deadline: {deadline}")
    st.markdown(f"Eligibility: {eligibility}")
    
    # Get user profile for auto-fill
    profile = get_user_profile(username)
    
    # Get existing application data if any
    app_data = get_application_progress(username, scholarship_name)
    form_data = app_data["form_data"] if app_data and "form_data" in app_data else {}
    
    with st.form("application_form"):
        st.markdown("#### Personal Information")
        col1, col2 = st.columns(2)
        
        with col1:
            first_name = st.text_input("First Name", value=form_data.get("first_name", profile.get("first_name", "")))
            last_name = st.text_input("Last Name", value=form_data.get("last_name", profile.get("last_name", "")))
        
        with col2:
            email = st.text_input("Email", value=form_data.get("email", profile.get("email", "")))
            phone = st.text_input("Phone Number", value=form_data.get("phone", ""))
        
        st.markdown("#### Academic Information")
        col1, col2 = st.columns(2)
        
        with col1:
            institution = st.text_input("Institution", value=form_data.get("institution", profile.get("institution", "")))
            education = st.selectbox("Education Level", 
                                   ["High School", "Undergraduate", "Postgraduate", "PhD"],
                                   index=["High School", "Undergraduate", "Postgraduate", "PhD"].index(form_data.get("education", profile.get("education", "Undergraduate"))) if "education" in form_data or "education" in profile else 1)
        
        with col2:
            field_of_study = st.text_input("Field of Study", value=form_data.get("field_of_study", profile.get("field_of_study", "")))
            cgpa = st.number_input("CGPA", 0.0, 10.0, float(form_data.get("cgpa", profile.get("cgpa", 8.0))) if "cgpa" in form_data or "cgpa" in profile else 8.0, 0.1)
        
        st.markdown("#### Essay Questions")
        why_deserve = st.text_area("Why do you deserve this scholarship? (200-300 words)", value=form_data.get("why_deserve", ""))
        career_goals = st.text_area("Describe your career goals (200-300 words)", value=form_data.get("career_goals", profile.get("career_goals", "")))
        
        st.markdown("#### Additional Documents")
        st.file_uploader("Upload Resume/CV (PDF)", type=["pdf"])
        st.file_uploader("Upload Transcripts (PDF)", type=["pdf"])
        st.file_uploader("Upload Recommendation Letter (PDF)", type=["pdf"])
        
        # Form actions
        col1, col2, col3 = st.columns(3)
        
        with col1:
            save_draft = st.form_submit_button("Save as Draft")
        
        with col2:
            submit_later = st.form_submit_button("Submit Later")
        
        with col3:
            submit_app = st.form_submit_button("Submit Application")
    
    # Handle form submission
    if save_draft or submit_later or submit_app:
        new_form_data = {
            "first_name": first_name,
            "last_name": last_name,
            "email": email,
            "phone": phone,
            "institution": institution,
            "education": education,
            "field_of_study": field_of_study,
            "cgpa": cgpa,
            "why_deserve": why_deserve,
            "career_goals": career_goals
        }
        
        progress = 25  # Initial progress
        
        # Calculate form completion progress
        if first_name and last_name and email and phone:
            progress += 25
        
        if institution and field_of_study:
            progress += 25
        
        if why_deserve and len(why_deserve) > 100:
            progress += 15
        
        if career_goals and len(career_goals) > 100:
            progress += 10
        
        # Update application status
        status = "Draft"
        if submit_app:
            status = "Submitted"
            progress = 100
        elif submit_later:
            status = "In Progress"
        
        # Create or update application tracking
        if app_data:
            update_application(username, scholarship_name, new_form_data, progress)
        else:
            track_application(username, scholarship_name, status)
            update_application(username, scholarship_name, new_form_data, progress)
        
        # Update scholarship status in saved list
        update_scholarship_status(username, scholarship_name, status)
        
        if submit_app:
            st.success("Application submitted successfully!")
        elif save_draft:
            st.info("Draft saved successfully.")
        elif submit_later:
            st.info("Application saved. You can continue later.")
        
        # Only refresh if not exiting
        if not submit_app:
            st.rerun()

# Forum feature
def render_forum(username):
    st.markdown("### Scholarship Community Forum")
    
    tab1, tab2 = st.tabs(["Browse Discussions", "Create New Post"])
    
    with tab1:
        posts = load_forum_posts()
        
        # Filter options
        categories = ["All Categories", "Application Tips", "Scholarship Advice", "University Selection", "Financial Aid", "Study Abroad", "Career Guidance"]
        filter_category = st.selectbox("Filter by Category", categories)
        
        # Sort options
        sort_option = st.selectbox("Sort by", ["Newest First", "Oldest First", "Most Replies"])
        
        # Apply filters and sorting
        if filter_category != "All Categories":
            filtered_posts = [post for post in posts if post["category"] == filter_category]
        else:
            filtered_posts = posts
            
        if sort_option == "Newest First":
            filtered_posts.sort(key=lambda x: x["created_at"], reverse=True)
        elif sort_option == "Oldest First":
            filtered_posts.sort(key=lambda x: x["created_at"])
        elif sort_option == "Most Replies":
            filtered_posts.sort(key=lambda x: len(x["replies"]), reverse=True)
        
        # Display posts
        if not filtered_posts:
            st.info("No discussions found. Be the first to start a conversation!")
        
        for post in filtered_posts:
            with st.expander(f"{post['title']} - by {post['username']} - {post['category']}"):
                st.markdown(f"**{post['title']}**")
                st.markdown(post['content'])
                st.markdown(f"*Posted on: {datetime.datetime.fromisoformat(post['created_at']).strftime('%Y-%m-%d %H:%M')}*")
                
                st.divider()
                
                # Display replies
                if post["replies"]:
                    st.markdown("#### Replies")
                    for reply in post["replies"]:
                        st.markdown(f"**{reply['username']}**: {reply['content']}")
                        st.markdown(f"*{datetime.datetime.fromisoformat(reply['created_at']).strftime('%Y-%m-%d %H:%M')}*")
                        st.divider()
                
                # Reply form
                with st.form(f"reply_form_{post['id']}"):
                    reply_content = st.text_area("Reply to this post", key=f"reply_{post['id']}")
                    if st.form_submit_button("Post Reply"):
                        if reply_content:
                            add_forum_reply(post["id"], username, reply_content)
                            st.success("Reply posted successfully!")
                            st.rerun()
    
    with tab2:
        with st.form("new_post_form"):
            st.markdown("#### Create New Discussion")
            post_title = st.text_input("Title")
            post_category = st.selectbox("Category", categories[1:])  # Exclude "All Categories"
            post_content = st.text_area("Content", height=200)
            
            if st.form_submit_button("Post Discussion"):
                if post_title and post_content:
                    add_forum_post(username, post_title, post_content, post_category)
                    st.success("Discussion posted successfully!")
                    st.rerun()
                else:
                    st.error("Please fill in all required fields.")

# Mentorship and skill development (future scope preview)
def render_mentorship_preview():
    st.markdown("### πŸš€ Coming Soon: Mentorship & Skill Development")
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("""
        #### πŸ‘¨β€πŸ« Mentorship Program
        - Connect with experienced mentors in your field
        - Get personalized guidance for scholarship applications
        - Weekly 1:1 mentorship sessions
        - Career planning and academic advice
        """)
    
    with col2:
        st.markdown("""
        #### 🧠 Skill Development
        - Free courses tailored to scholarship requirements
        - Interview preparation workshops
        - Essay writing masterclasses
        - Language proficiency training
        """)
    
    st.info("These features are coming soon! Join our waitlist to be notified when they launch.")
    
    with st.form("waitlist_form"):
        waitlist_email = st.text_input("Email address")
        interests = st.multiselect("I'm interested in", ["Mentorship", "Skill Development", "Both"])
        
        if st.form_submit_button("Join Waitlist"):
            if waitlist_email:
                st.success("Thank you for joining our waitlist! We'll notify you when these features launch.")
            else:
                st.error("Please enter your email address.")

# Financial planning feature (future scope preview)
def render_financial_planning_preview():
    st.markdown("### πŸ’° Financial Planning Tools")
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("""
        #### πŸ“Š Scholarship Budget Planner
        - Calculate total education expenses
        - Track scholarship applications and potential funding
        - Gap analysis between costs and available funding
        - Prioritize applications based on financial need
        """)
    
    with col2:
        st.markdown("""
        #### πŸ’Έ Financial Literacy Resources
        - Educational content on managing scholarship funds
        - Tax implications of scholarships
        - Combining scholarships with student loans
        - Building credit history as a student
        """)
    
    st.info("These financial planning tools are coming soon! Join our waitlist to be notified when they launch.")
    
    # Sample financial planning calculator preview
    st.markdown("#### πŸ” Education Cost Calculator Preview")
    
    with st.form("cost_calculator"):
        tuition = st.number_input("Estimated Annual Tuition (β‚Ή)", 0, 2000000, 100000, 10000)
        living = st.number_input("Annual Living Expenses (β‚Ή)", 0, 1000000, 120000, 10000)
        books = st.number_input("Books and Supplies (β‚Ή)", 0, 100000, 15000, 1000)
        travel = st.number_input("Travel Expenses (β‚Ή)", 0, 100000, 20000, 1000)
        years = st.number_input("Program Duration (years)", 1, 6, 4, 1)
        
        if st.form_submit_button("Calculate Total Cost"):
            annual_cost = tuition + living + books + travel
            total_cost = annual_cost * years
            
            st.markdown(f"**Annual Cost:** β‚Ή{annual_cost:,}")
            st.markdown(f"**Total Program Cost:** β‚Ή{total_cost:,}")
            
            # Display funding gap visualization
            st.markdown("#### Funding Gap Analysis")
            st.progress(0.2)
            st.markdown("20% of costs currently covered by identified scholarships")

# Career alignment feature (future scope preview)
def render_career_alignment_preview():
    st.markdown("### 🎯 Career Alignment Tools")
    
    st.markdown("""
    Our upcoming Career Alignment feature will help you connect your scholarship journey with your long-term career goals.
    """)
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("""
        #### 🧭 Career Pathway Mapping
        - Personalized career roadmap based on your profile
        - Identify skills gaps and development opportunities
        - Connect with industry professionals
        - Internship and job placement assistance
        """)
    
    with col2:
        st.markdown("""
        #### πŸ† Post-Scholarship Success
        - Resume and portfolio building workshops
        - Interview preparation specifically for scholars
        - Alumni network access
        - Scholarship to job transition guidance
        """)
    
    # AI Career Recommendation Preview
    st.markdown("#### πŸ€– AI Career Recommendation (Preview)")
    
    with st.form("career_recommendation"):
        interests = st.multiselect(
            "Select your interests",
            ["Technology", "Healthcare", "Business", "Arts", "Sciences", "Education", "Law", "Engineering"]
        )
        
        skills = st.multiselect(
            "Select your top skills",
            ["Programming", "Data Analysis", "Writing", "Research", "Design", "Leadership", "Problem-Solving", "Communication"]
        )
        
        values = st.multiselect(
            "What do you value in a career?",
            ["Work-Life Balance", "High Income", "Making an Impact", "Creativity", "Recognition", "Stability", "Independence", "Growth"]
        )
        
        if st.form_submit_button("Get AI Career Recommendations"):
            if interests and skills and values:
                st.info("Based on your profile, we recommend exploring these career paths:")
                
                if "Technology" in interests and "Programming" in skills:
                    st.markdown("1. **Software Development Engineer** - Aligned with your technical interests and problem-solving skills")
                elif "Healthcare" in interests:
                    st.markdown("1. **Healthcare Researcher** - Combines your interest in healthcare with analytical skills")
                elif "Business" in interests:
                    st.markdown("1. **Business Analyst** - Leverages your data analysis skills in a business context")
                else:
                    st.markdown("1. **Research Specialist** - Aligns with your analytical mindset and subject interests")
                
                st.markdown("*Full career alignment features coming soon!*")
            else:
                st.error("Please fill in all fields to get personalized recommendations.")

# Streamlit app
def main():
    # Load custom CSS
    load_css()
    
    # Initialize session state
    if "authenticated" not in st.session_state:
        st.session_state.authenticated = False
    if "username" not in st.session_state:
        st.session_state.username = None
    if "profile_complete" not in st.session_state:
        st.session_state.profile_complete = False
    
    # Hero Section
    st.markdown("""
    <div style="text-align: center; padding: 50px 0;">
        <h1 style="color: #2c3e50; font-size: 3rem;">πŸŽ“ AI Scholarship Advisor</h1>
        <p style="color: #34495e; font-size: 1.2rem;">Find the best scholarships tailored just for you!</p>
    </div>
    """, unsafe_allow_html=True)
    
    # User Authentication
    if not st.session_state.authenticated:
        tab1, tab2 = st.tabs(["Login", "Register"])
        
        with tab1:
            with st.form("login_form"):
                username = st.text_input("Username")
                password = st.text_input("Password", type="password")
                if st.form_submit_button("Login"):
                    if authenticate(username, password):
                        st.session_state.authenticated = True
                        st.session_state.username = username
                        
                        # Check if profile is complete
                        profile = get_user_profile(username)
                        if profile and profile.get("profile_completed", False):
                            st.session_state.profile_complete = True
                        
                        st.rerun()
                    else:
                        st.error("Invalid username or password")
        
        with tab2:
            with st.form("register_form"):
                new_username = st.text_input("Username", key="reg_username")
                new_password = st.text_input("Password", type="password", key="reg_password")
                confirm_password = st.text_input("Confirm Password", type="password")
                email = st.text_input("Email Address")
                
                if st.form_submit_button("Register"):
                    if new_password != confirm_password:
                        st.error("Passwords do not match")
                    elif not new_username or not new_password or not email:
                        st.error("All fields are required")
                    else:
                        if register_user(new_username, new_password, email):
                            st.success("Registration successful! Please login.")
                        else:
                            st.error("Username already exists")
    
    else:
        # User is authenticated
        username = st.session_state.username
        
        # Check profile completion
        profile = get_user_profile(username)
        profile_complete = profile.get("profile_completed", False)
        
        if not profile_complete and not st.session_state.profile_complete:
            # User profile needs to be completed
            render_profile_wizard(username)
        else:
            # Main application tabs
            tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs([
                "πŸ’‘ Recommendations", 
                "πŸ“‹ Saved Scholarships", 
                "πŸ“ Applications", 
                "πŸ’¬ Community Forum",
                "🧠 Skill Development",
                "πŸ’° Financial Planning"
            ])
            
            # Load data
            df = load_scholarships()
            
            with tab1:
                st.markdown("### 🎯 Get Personalized Recommendations")
                
                # Get user profile for recommendations
                profile = get_user_profile(username)
                
                # User can modify certain profile elements for this search
                with st.form("profile_form"):
                    st.markdown("### πŸ“ Customize Search Criteria")
                    col1, col2 = st.columns(2)
                    with col1:
                        age = st.number_input("Age", 16, 50, int(profile.get("age", 20)) if "age" in profile else 20)
                        citizenship = st.selectbox(
                            "Citizenship", 
                            ["India", "Other"],
                            index=0 if profile.get("citizenship", "India") == "India" else 1
                        )
                        income = st.number_input(
                            "Annual Family Income (β‚Ή)", 
                            0, 10000000, 
                            int(profile.get("income", 300000)) if "income" in profile else 300000
                        )
                    with col2:
                        education = st.selectbox(
                            "Education Level", 
                            ["High School", "Undergraduate", "Postgraduate", "PhD"],
                            index=["High School", "Undergraduate", "Postgraduate", "PhD"].index(profile.get("education", "Undergraduate")) if "education" in profile else 1
                        )
                        category = st.selectbox(
                            "Category", 
                            ["General", "OBC", "SC", "ST", "EWS", "Minority"],
                            index=["General", "OBC", "SC", "ST", "EWS", "Minority"].index(profile.get("category", "General")) if "category" in profile else 0
                        )
                    
                    submitted = st.form_submit_button("πŸš€ Get Recommendations")
                
                if submitted:
                    # Create user profile
                    user_profile = f"""
                    Student Profile:
                    - Age: {age}
                    - Citizenship: {citizenship}
                    - Annual Income: β‚Ή{income}
                    - Education Level: {education}
                    - Category: {category}
                    """
                    
                    # Create RAG context
                    rag_context = create_rag_context(df)
                    
                    # Generate response using RAG
                    model = get_rag_model()
                    prompt = f"""
                    {rag_context}
                    {user_profile}
                    Task: 
                    1. Analyze the student profile against all scholarships
                    2. Identify top 5 most relevant scholarships with priority order
                    3. For each scholarship:
                       - List matching eligibility criteria
                       - Explain why it's a good match
                       - Provide direct application link
                    4. Format response with markdown headers and bullet points
                    Important: 
                    - Be specific about eligibility matches
                    - Highlight deadlines if available
                    - Never invent scholarships not in the database
                    """
                    
                    with st.spinner("πŸ” Analyzing 50+ scholarships..."):
                        response = model.generate_content(
                            prompt,
                            generation_config=GenerationConfig(
                                temperature=0.3,
                                top_p=0.95,
                                max_output_tokens=2000
                            )
                        )
                    
                    # Display recommendations
                    st.markdown("### πŸŽ‰ Personalized Recommendations")
                    
                    # Process the response to add save buttons
                    recommendations_text = response.text
                    
                    # Split the recommendations into sections
                    scholarship_sections = recommendations_text.split("##")[1:]  # Skip the first split which is empty
                    
                    for section in scholarship_sections:
                        if not section.strip():
                            continue
                            
                        # Extract scholarship name from the header
                        lines = section.strip().split("\n")
                        scholarship_name = lines[0].strip()
                        
                        # Find deadline if mentioned
                        deadline = "Not specified"
                        for line in lines:
                            if "deadline" in line.lower():
                                deadline = line.split(":", 1)[1].strip() if ":" in line else line
                                break
                        
                        # Create a card for this scholarship
                        st.markdown(f"""
                        <div class="scholarship-card">
                            <h3>{scholarship_name}</h3>
                            {"".join([f"<p>{line}</p>" for line in lines[1:]])}
                        </div>
                        """, unsafe_allow_html=True)
                        
                        # Add a button to save this scholarship
                        if st.button(f"Save this scholarship", key=f"save_{scholarship_name}"):
                            if save_scholarship(username, scholarship_name, deadline):
                                st.success(f"Saved {scholarship_name} to your list!")
                            else:
                                st.info("This scholarship is already in your saved list.")
                    
                    # Show raw data for transparency
                    with st.expander("πŸ“Š View Full Scholarship Database"):
                        st.dataframe(df)
            
            with tab2:
                st.markdown("### πŸ“‹ Your Saved Scholarships")
                
                saved = get_saved_scholarships(username)
                
                if not saved:
                    st.info("You haven't saved any scholarships yet. Go to the Recommendations tab to find and save scholarships.")
                else:
                    # Group by status
                    saved_by_status = {}
                    for s in saved:
                        status = s.get("status", "Saved")
                        if status not in saved_by_status:
                            saved_by_status[status] = []
                        saved_by_status[status].append(s)
                    
                    # Create tabs for different statuses
                    status_tabs = st.tabs(list(saved_by_status.keys()))
                    
                    for i, (status, scholarships) in enumerate(saved_by_status.items()):
                        with status_tabs[i]:
                            for scholarship in scholarships:
                                with st.expander(f"{scholarship['name']} (Deadline: {scholarship['deadline']})"):
                                    st.markdown(f"**{scholarship['name']}**")
                                    st.markdown(f"Deadline: {scholarship['deadline']}")
                                    st.markdown(f"Saved on: {datetime.datetime.fromisoformat(scholarship['saved_at']).strftime('%Y-%m-%d')}")
                                    
                                    # Find full details in the dataframe
                                    scholarship_details = df[df['Scholarship Name'] == scholarship['name']]
                                    if not scholarship_details.empty:
                                        st.markdown(f"Eligibility: {scholarship_details.iloc[0]['Eligibility']}")
                                        st.markdown(f"Link: {scholarship_details.iloc[0]['Link']}")
                                    
                                    col1, col2 = st.columns(2)
                                    
                                    with col1:
                                        if st.button("Start Application", key=f"apply_{scholarship['name']}"):
                                            track_application(username, scholarship['name'])
                                            update_scholarship_status(username, scholarship['name'], "Started")
                                            st.session_state.current_application = scholarship['name']
                                            st.rerun()
                                    
                                    with col2:
                                        if st.button("Set Reminder", key=f"remind_{scholarship['name']}"):
                                            # Get user email
                                            users = load_users()
                                            email = users[username]["email"]
                                            
                                            # Send notification
                                            if send_deadline_notification(email, scholarship['name'], scholarship['deadline']):
                                                st.success("Reminder email sent!")
                                            else:
                                                st.error("Failed to send reminder email.")
            
            with tab3:
                st.markdown("### πŸ“ Your Applications")
                
                # Check if a specific application is being viewed
                if "current_application" in st.session_state:
                    render_application_form(username, st.session_state.current_application, df)
                    
                    if st.button("Back to All Applications"):
                        del st.session_state.current_application
                        st.rerun()
                else:
                    # Get user's applications
                    users = load_users()
                    applications = users[username]["applications"] if username in users else {}
                    
                    if not applications:
                        st.info("You haven't started any applications yet. Save a scholarship and click 'Start Application' to begin.")
                    else:
                        # Group by status/progress
                        in_progress = []
                        completed = []
                        drafts = []
                        
                        for name, app in applications.items():
                            if app["progress"] == 100:
                                completed.append((name, app))
                            elif app["status"] == "Draft":
                                drafts.append((name, app))
                            else:
                                in_progress.append((name, app))
                        
                        # Create tabs for different statuses
                        app_tabs = st.tabs(["In Progress", "Drafts", "Completed"])
                        
                        with app_tabs[0]:
                            if not in_progress:
                                st.info("No applications in progress.")
                            else:
                                for name, app in in_progress:
                                    col1, col2 = st.columns([3, 1])
                                    
                                    with col1:
                                        st.markdown(f"**{name}**")
                                        st.progress(app["progress"] / 100)
                                        st.markdown(f"Progress: {app['progress']}% complete")
                                    
                                    with col2:
                                        if st.button("Continue", key=f"continue_{name}"):
                                            st.session_state.current_application = name
                                            st.rerun()
                        
                        with app_tabs[1]:
                            if not drafts:
                                st.info("No draft applications.")
                            else:
                                for name, app in drafts:
                                    col1, col2 = st.columns([3, 1])
                                    
                                    with col1:
                                        st.markdown(f"**{name}**")
                                        st.markdown(f"Last updated: {datetime.datetime.fromisoformat(app.get('updated_at', app['started_at'])).strftime('%Y-%m-%d')}")
                                    
                                    with col2:
                                        if st.button("Edit Draft", key=f"edit_{name}"):
                                            st.session_state.current_application = name
                                            st.rerun()
                        
                        with app_tabs[2]:
                            if not completed:
                                st.info("No completed applications.")
                            else:
                                for name, app in completed:
                                    st.markdown(f"**{name}**")
                                    st.markdown(f"Submitted on: {datetime.datetime.fromisoformat(app.get('updated_at', app['started_at'])).strftime('%Y-%m-%d')}")
                                    st.success("Application completed and submitted!")
            
            with tab4:
                render_forum(username)
            
            with tab5:
                render_mentorship_preview()
            
            with tab6:
                st.markdown("### Financial Planning Tools")
                
                tab1, tab2 = st.tabs(["Education Financing", "Career Planning"])
                
                with tab1:
                    render_financial_planning_preview()
                
                with tab2:
                    render_career_alignment_preview()
        
        # Logout button
        if st.sidebar.button("Logout"):
            st.session_state.authenticated = False
            st.session_state.username = None
            st.session_state.profile_complete = False
            if "current_application" in st.session_state:
                del st.session_state.current_application
            if "profile_step" in st.session_state:
                del st.session_state.profile_step
            st.rerun()

if __name__ == "__main__":
    main()