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70a7
bbfa3ce
 
 
f0b70a7
bbfa3ce
 
 
 
 
f0b70a7
 
 
 
 
 
 
bbfa3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b70a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbfa3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11f5c5f
 
bbfa3ce
 
 
 
 
11f5c5f
 
 
 
 
 
 
 
bbfa3ce
 
 
4ffd7eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0b70a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ffd7eb
 
 
 
 
303bb41
 
4ffd7eb
 
 
303bb41
 
 
 
 
 
f0b70a7
 
 
 
 
 
 
 
 
 
4ffd7eb
 
21efa49
bbfa3ce
4ffd7eb
 
303bb41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
import os
import streamlit as st
import pandas as pd
import json
from datetime import datetime, timedelta
import plotly.express as px
import numpy as np
from collections import Counter
import pytz
from pymongo import MongoClient
import schedule
import threading
import time

# Try to import Google Generative AI, but handle it gracefully if not installed
try:
    import google.generativeai as genai
    GENAI_AVAILABLE = True
except ImportError:
    GENAI_AVAILABLE = False

from apify_client import ApifyClient
from dotenv import load_dotenv

# Set page config to wide mode with a custom title and icon
st.set_page_config(
    page_title="Twitter Scraper",
    page_icon="🐦",
    layout="wide",
    initial_sidebar_state="collapsed"
)

# Load environment variables from .env.local file specifically
load_dotenv(dotenv_path=".env.local")

# Setup MongoDB connection
MONGODB_URI = os.getenv("MONGODB_URI", "mongodb+srv://datacollector:43HTpLfqPAjFCLL@cluster0.mongodb.net/?retryWrites=true&w=majority")

# Try to connect to MongoDB, but continue if it fails
try:
    mongo_client = MongoClient(MONGODB_URI, serverSelectionTimeoutMS=5000)
    # Test the connection
    mongo_client.admin.command('ping')
    mongo_db = mongo_client["DataCollector"]
    tweets_collection = mongo_db["tweets"]
    scheduler_users_collection = mongo_db["scheduler_users"]
    MONGODB_AVAILABLE = True
    print("βœ… MongoDB connected successfully")
except Exception as e:
    print(f"⚠️ MongoDB connection failed: {e}")
    print("πŸ”„ Running in offline mode - data will not be stored")
    MONGODB_AVAILABLE = False
    # Create dummy collections for offline mode
    class DummyCollection:
        def update_one(self, *args, **kwargs):
            pass
        def find(self, *args, **kwargs):
            return []
    tweets_collection = DummyCollection()
    scheduler_users_collection = DummyCollection()

# Initialize the ApifyClient with your API token
client = ApifyClient(os.getenv("APIFY_API_KEY"))

# Initialize Gemini API if available and the key is available
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
if GENAI_AVAILABLE and GEMINI_API_KEY:
    genai.configure(api_key=GEMINI_API_KEY)

# Function to get summary from Gemini
def get_gemini_summary(tweets_data, context=""):
    try:
        if not GENAI_AVAILABLE:
            return "Error: Google Generative AI package not installed. Run 'pip install google-generativeai' to install it."
        
        if not GEMINI_API_KEY:
            return "Error: GEMINI_API_KEY not found. Please add it to your .env.local file."
        
        # Format the tweets data into a readable text
        tweets_text = []
        for i, tweet in enumerate(tweets_data.itertuples(), 1):
            tweet_str = f"{i}. @{tweet.Username}: {tweet.Text} (Likes: {tweet.Likes}, Retweets: {tweet.Retweets})"
            tweets_text.append(tweet_str)
        
        all_tweets = "\n\n".join(tweets_text)
        
        # Create a prompt for Gemini with enhanced analysis requirements
        prompt = f"""
        {context}
        
        Here are the tweets to analyze:
        
        {all_tweets}
        
        Please provide a comprehensive analysis of these tweets, including:
        
        1. Main themes and topics discussed
        2. Overall sentiment
        3. Key insights or patterns
        4. Most engaging content
        
        Additionally, please provide these specific analyses:
        
        5. Political/Brand Affiliation Analysis: Analyze which party or brand the reply tweeters belong to. Identify if there are instances where people from the same party/brand are tweeting negatively about their own party/brand.
        
        6. Top 10 Positive Tweets: List the most positive tweets with their tweet numbers and brief explanation.
        
        7. Top 10 Negative Tweets: List the most negative tweets with their tweet numbers and brief explanation.
        
        8. Top 10 Recommendations: Provide specific suggestions and recommendations to help the party or brand improve their messaging, engagement, or content strategy based on the tweet analysis.
        
        Format the analysis in a clear, structured way with bullet points where appropriate and clear section headings.
        """
        
        # Generate summary using Gemini
        model = genai.GenerativeModel('gemini-2.5-flash-preview-04-17')
        response = model.generate_content(prompt)
        
        return response.text
    except Exception as e:
        return f"Error generating summary: {str(e)}"

# Function to extract account details from API response
def extract_account_details(author_data):
    """Extract comprehensive account details from author data"""
    # If no data provided (None), return empty dict
    if author_data is None:
        return {}
    
    # Create account details with defaults for all fields
    account_details = {
        "user_id": author_data.get("id", ""),
        "name": author_data.get("name", ""),
        "username": author_data.get("userName", ""),
        "bio": author_data.get("description", author_data.get("biography", "")),
        "location": author_data.get("location", ""),
        "website": author_data.get("url", ""),
        "followers_count": author_data.get("followersCount", author_data.get("followers_count", author_data.get("followers", 0))),
        "following_count": author_data.get("followingCount", author_data.get("following_count", author_data.get("following", 0))),
        "tweet_count": author_data.get("statusesCount", author_data.get("tweet_count", 0)),
        "listed_count": author_data.get("listedCount", author_data.get("listed_count", 0)),
        "verified": author_data.get("verified", author_data.get("isVerified", author_data.get("isBlueVerified", False))),
        "protected": author_data.get("protected", False),
        "profile_image_url": author_data.get("profileImageUrl", author_data.get("profile_image_url", "")),
        "profile_banner_url": author_data.get("profileBannerUrl", author_data.get("profile_banner_url", "")),
        "created_at": author_data.get("createdAt", author_data.get("created_at", "")),
        "favourites_count": author_data.get("favouritesCount", author_data.get("favourites_count", 0)),
        "media_count": author_data.get("mediaCount", author_data.get("media_count", 0))
    }
    
    return account_details

def run_apify_comment_analysis(input):
    # Prepare the Actor input with exact format for Comment Analysis
    id = input["id"]
    since_date = input["since"]
    until_date = input.get("until", datetime.now().strftime("%Y-%m-%d"))  # NEW: Add until date
    
    # ENHANCED: Improved query parameters for better comment capture
    run_input = {
        "@": id,
        "filter:blue_verified": False,
        "filter:consumer_video": False,
        "filter:has_engagement": False,  # Always False to get more comments
        "filter:hashtags": False,
        "filter:images": False,
        "filter:links": False,
        "filter:media": False,
        "filter:mentions": False,
        "filter:native_video": False,
        "filter:nativeretweets": False,
        "filter:news": False,
        "filter:pro_video": False,
        "filter:quote": False,
        "filter:replies": False,  # Keep false to get actual comments
        "filter:safe": False,
        "filter:spaces": False,
        "filter:twimg": False,
        "filter:verified": False,
        "filter:videos": False,
        "filter:vine": False,
        "include:nativeretweets": False,
        "since": since_date + "_00:00:00_UTC",
        "to": id,
        "until": until_date + "_23:59:59_UTC",
        "queryType": "Latest",
        "min_retweets": 0,
        "min_faves": 0,
        "min_replies": 0,
        "-min_retweets": 0,
        "-min_faves": 0,
        "-min_replies": 0,
        "sort": "time"  # ADDED: Sort by time for chronological order
    }   

    # Show loading state
    with st.spinner(f"Fetching comments from {since_date} to {until_date}..."):
        # Run the Actor and wait for it to finish
        run = client.actor("CJdippxWmn9uRfooo").call(run_input=run_input)
        
        # Fetch ALL data from the run's dataset (no maxItems limit)
        data = list(client.dataset(run["defaultDatasetId"]).iterate_items())
        
        # ENHANCED: Log query details for debugging
        st.info(f"πŸ” Query Details: to:@{id} since:{since_date} until:{until_date} | Raw results: {len(data)} comments")
    
    return data, run["defaultDatasetId"]

def run_apify_account_analysis(input, disable_engagement_filters=True):
    # Prepare the Actor input with exact format for Account Analysis
    username = input["username"]
    since_date = input["since"]
    until_date = input.get("until", datetime.now().strftime("%Y-%m-%d"))  # NEW: Add until date
    min_faves = input.get("min_faves", 0)  # NEW: Configurable engagement
    min_retweets = input.get("min_retweets", 0)  # NEW: Configurable engagement
    min_replies = input.get("min_replies", 0)  # NEW: Configurable engagement
    
    # ENHANCED: More comprehensive query parameters for better accuracy
    run_input = {
        "filter:blue_verified": False,
        "filter:consumer_video": False,
        "filter:has_engagement": False,  # Always False for maximum tweet capture
        "filter:hashtags": False,
        "filter:images": False,
        "filter:links": False,
        "filter:media": False,
        "filter:mentions": False,
        "filter:native_video": False,
        "filter:nativeretweets": False,  # Include retweets for accurate count
        "filter:news": False,
        "filter:pro_video": False,
        "filter:quote": False,
        "filter:replies": False,  # Include replies for accurate count
        "filter:safe": False,
        "filter:spaces": False,
        "filter:twimg": False,
        "filter:verified": False,
        "filter:videos": False,
        "filter:vine": False,
        "from": username,
        "include:nativeretweets": True,  # CHANGED: Include retweets to match Twitter counts
        "queryType": "Latest",
        "since": since_date + "_00:00:00_UTC",
        "until": until_date + "_23:59:59_UTC",
        "min_faves": min_faves,
        "min_retweets": min_retweets,
        "min_replies": min_replies,
        "-min_retweets": 0,
        "-min_faves": 0,
        "-min_replies": 0,
        "sort": "time"  # ADDED: Sort by time for chronological order
    }   

    # Show loading state
    with st.spinner(f"Fetching tweets from {since_date} to {until_date}..."):
        # Run the Actor and wait for it to finish
        run = client.actor("CJdippxWmn9uRfooo").call(run_input=run_input)

        # Fetch ALL data from the run's dataset (no maxItems limit)
        data = list(client.dataset(run["defaultDatasetId"]).iterate_items())
        
        # ENHANCED: Log query details for debugging
        st.info(f"πŸ” Query Details: from:{username} since:{since_date} until:{until_date} | Raw results: {len(data)} tweets")
    
    return data, run["defaultDatasetId"]

# Function to extract hashtags from tweet text
def extract_hashtags(text):
    if not text:
        return []
    
    # Simple extraction - split by spaces and filter for hashtags
    words = text.split()
    hashtags = [word[1:] for word in words if word.startswith('#')]
    return hashtags

# Function to extract mentions from tweet text
def extract_mentions(text):
    if not text:
        return []
    
    # Simple extraction - split by spaces and filter for mentions
    words = text.split()
    mentions = [word[1:] for word in words if word.startswith('@')]
    return mentions

# Function to convert UTC time to Indian Standard Time (IST)
def convert_to_ist(utc_datetime):
    if not utc_datetime:
        return None
    
    # Create timezone objects
    utc_tz = pytz.timezone('UTC')
    ist_tz = pytz.timezone('Asia/Kolkata')
    
    # If datetime is naive, make it timezone-aware with UTC
    if utc_datetime.tzinfo is None:
        utc_datetime = utc_tz.localize(utc_datetime)
    
    # Convert to IST
    ist_datetime = utc_datetime.astimezone(ist_tz)
    return ist_datetime

# Function to process tweet data and create dataframe - ENHANCED FOR ACCOUNT DETAILS
def process_tweet_data(data, extract_account_info=False):
    processed_data = []
    all_hashtags = []
    all_mentions = []
    mock_data_detected = False
    mock_data_signature = "From KaitoEasyAPI, a reminder:Our API pricing is based on the volume of data returned."
    account_details = {}

    for item in data:
        text = item.get("text", "")
        if mock_data_signature in text:
            mock_data_detected = True
            continue  # Skip this mock data tweet

        try:
            # Format date
            date_str = item.get("createdAt", "")
            try:
                # Try to parse the Twitter date format
                date_obj = datetime.strptime(date_str, "%a %b %d %H:%M:%S %z %Y")
                
                # Convert to IST
                ist_date_obj = convert_to_ist(date_obj)
                
                formatted_date = ist_date_obj.strftime("%Y-%m-%d %H:%M:%S")
                date_only = ist_date_obj.strftime("%Y-%m-%d")
                time_only = ist_date_obj.strftime("%H:%M")
                hour = ist_date_obj.hour
                day_of_week = ist_date_obj.strftime("%A")
            except:
                formatted_date = date_str
                date_only = ""
                time_only = ""
                hour = 0
                day_of_week = ""

            # Get author info
            author = item.get("author", {})
            
            # ENHANCED: Extract account details if requested
            if extract_account_info and not account_details and author:
                account_details = extract_account_details(author)
                # Debug: log what we found
                print(f"DEBUG: Extracted account details from author: {account_details}")
            elif extract_account_info and not author:
                print(f"DEBUG: No author data found in tweet item: {list(item.keys())}")
            
            # Check if media exists
            has_media = False
            if "extendedEntities" in item and "media" in item["extendedEntities"]:
                media = item["extendedEntities"]["media"]
                if len(media) > 0:
                    has_media = True
            
            # Get tweet text
            text = item.get("text", "")
            
            # Extract hashtags and mentions
            hashtags = extract_hashtags(text)
            mentions = extract_mentions(text)
            
            # Collect all hashtags and mentions for analysis
            all_hashtags.extend(hashtags)
            all_mentions.extend(mentions)
            
            # Calculate tweet length
            tweet_length = len(text) if text else 0
            
            # Get bookmarks count if available
            bookmarks = item.get("bookmarkCount", 0)
            
            processed_item = {
                "Date": formatted_date,
                "Date_Only": date_only,
                "Time_Only": time_only,
                "Hour": hour,
                "Day_of_Week": day_of_week,
                "ID": item.get("id", ""),
                "Author": author.get("name", ""),
                "Username": author.get("userName", ""),
                "Text": text,
                "Text_Length": tweet_length,
                "Likes": item.get("likeCount", 0),
                "Retweets": item.get("retweetCount", 0),
                "Replies": item.get("replyCount", 0),
                "Bookmarks": bookmarks,
                "Views": item.get("viewCount", 0),
                "URL": item.get("url", ""),
                "Is_Reply": item.get("isReply", False),
                "Has_Media": has_media,
                "Hashtag_Count": len(hashtags),
                "Mention_Count": len(mentions),
                "Hashtags": ", ".join(hashtags) if hashtags else "",
                "Mentions": ", ".join(mentions) if mentions else ""
            }
            processed_data.append(processed_item)
        except Exception as e:
            st.warning(f"Error processing tweet: {e}")
    
    # Create dataframe
    df = pd.DataFrame(processed_data)
    
    # Calculate additional metrics
    metrics = {
        "hashtags": all_hashtags,
        "mentions": all_mentions,
        "account_details": account_details  # ADDED: Include account details
    }
    
    return df, metrics, mock_data_detected

# Function to display a compact version of the analysis for comparison
def display_compact_analysis(df, metrics, username, dataset_id):
    st.subheader(f"@{username}")

    # ENHANCED: Display account details if available
    account_details = metrics.get("account_details", {})
    if account_details:
        st.markdown("##### πŸ‘€ Account Info")
        
        # Display followers and following in columns
        if account_details.get("followers_count") or account_details.get("following_count"):
            acc_col1, acc_col2 = st.columns(2)
            with acc_col1:
                if account_details.get("followers_count"):
                    st.metric("Followers", f"{account_details['followers_count']:,}")
            with acc_col2:
                if account_details.get("following_count"):
                    st.metric("Following", f"{account_details['following_count']:,}")
        
        # Show follower ratio and verification status
        if account_details.get("followers_count") and account_details.get("following_count"):
            ratio = account_details["followers_count"] / account_details["following_count"]
            st.metric("Follower Ratio", f"{ratio:.2f}:1")
        
        if account_details.get("verified"):
            st.success("βœ… Verified")

    # Calculate metrics for analysis
    total_tweets = len(df)
    total_likes = df["Likes"].sum()
    total_retweets = df["Retweets"].sum()
    total_replies = df["Replies"].sum()
    total_bookmarks = df["Bookmarks"].sum()
    total_views = df["Views"].sum()
    
    total_engagement = total_likes + total_retweets + total_replies + total_bookmarks
    avg_engagement_per_tweet = total_engagement / total_tweets if total_tweets > 0 else 0
    engagement_rate = (total_engagement / total_views * 100) if total_views > 0 else 0
    
    df["Engagement"] = df["Likes"] + df["Retweets"] + df["Replies"] + df["Bookmarks"]
    most_engaging_tweet = df.loc[df["Engagement"].idxmax()] if not df.empty else None
    
    media_tweets_pct = (df["Has_Media"].sum() / total_tweets * 100) if total_tweets > 0 else 0
    reply_tweets_pct = (df["Is_Reply"].sum() / total_tweets * 100) if total_tweets > 0 else 0
    avg_tweet_length = df["Text_Length"].mean() if not df.empty else 0
    
    hashtag_counts = Counter(metrics["hashtags"])
    top_hashtags = hashtag_counts.most_common(5)
    
    mention_counts = Counter(metrics["mentions"])
    top_mentions = mention_counts.most_common(5)

    st.markdown("##### πŸ“ˆ Key Metrics")
    st.metric("Total Tweets", f"{total_tweets:,}")
    st.metric("Total Likes", f"{total_likes:,}")
    st.metric("Total Retweets", f"{total_retweets:,}")
    st.metric("Total Replies", f"{total_replies:,}")
    st.metric("Total Bookmarks", f"{total_bookmarks:,}")
    st.metric("Total Views", f"{total_views:,}")
    
    st.markdown("##### ⚑ Engagement")
    st.metric("Avg. Engagement/Tweet", f"{avg_engagement_per_tweet:.1f}")
    st.metric("Engagement Rate", f"{engagement_rate:.2f}%")
    
    st.markdown("##### πŸ” Content")
    st.metric("Media Tweets", f"{media_tweets_pct:.1f}%")
    st.metric("Reply Tweets", f"{reply_tweets_pct:.1f}%")
    st.metric("Avg. Tweet Length", f"{avg_tweet_length:.0f} chars")

    if top_hashtags:
        st.markdown("##### πŸ” Top Hashtags")
        for tag, count in top_hashtags:
            st.write(f"#{tag}: {count}")
    
    if top_mentions:
        st.markdown("##### πŸ‘₯ Top Mentions")
        for user, count in top_mentions:
            st.write(f"@{user}: {count}")
            
    if most_engaging_tweet is not None:
        st.markdown("##### 🌟 Most Engaging")
        with st.container():
            st.write(f"**{most_engaging_tweet['Text']}**")
            st.write(f"πŸ’¬ {most_engaging_tweet['Replies']} πŸ”„ {most_engaging_tweet['Retweets']} ❀️ {most_engaging_tweet['Likes']} πŸ”– {most_engaging_tweet['Bookmarks']} πŸ‘οΈ {most_engaging_tweet['Views']}")
            st.write(f"[{most_engaging_tweet['Date']}]({most_engaging_tweet['URL']})")

    st.info(f"Dataset ID: {dataset_id}")
    
    csv = df.to_csv(index=False).encode('utf-8')
    st.download_button(
        f"πŸ“₯ Download @{username} CSV",
        csv,
        f"twitter_data_{username}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
        "text/csv",
        key=f"download-csv-compare-{username}",
        use_container_width=True
    )

# Function to analyze and display the tweet data
def analyze_and_display_data(data, dataset_id, analysis_type="Account"):
    raw_data = None
    if not isinstance(data, pd.DataFrame): # If raw data is passed
        # Store raw data for sentiment analysis
        raw_data = data
        # Process the data into a dataframe - ENHANCED: Extract account details
        df, metrics, _ = process_tweet_data(data, extract_account_info=True)
    else: # If DataFrame is already processed (e.g. after retry)
        df = data
        # Recalculate metrics if df might have changed (e.g. if mock data was removed before this call)
        all_hashtags_retry = []
        all_mentions_retry = []
        for _, row in df.iterrows():
            if pd.notna(row.get("Hashtags")) and row["Hashtags"]:
                all_hashtags_retry.extend(row["Hashtags"].split(", "))
            if pd.notna(row.get("Mentions")) and row["Mentions"]:
                all_mentions_retry.extend(row["Mentions"].split(", "))
        metrics = {"hashtags": all_hashtags_retry, "mentions": all_mentions_retry, "account_details": {}}

    if not df.empty:
        # Calculate additional metrics for analysis
        total_tweets = len(df)
        total_likes = df["Likes"].sum()
        total_retweets = df["Retweets"].sum()
        total_replies = df["Replies"].sum()
        total_bookmarks = df["Bookmarks"].sum()
        total_views = df["Views"].sum()
        
        # Engagement metrics
        total_engagement = total_likes + total_retweets + total_replies + total_bookmarks
        avg_engagement_per_tweet = total_engagement / total_tweets if total_tweets > 0 else 0
        engagement_rate = (total_engagement / total_views * 100) if total_views > 0 else 0
        
        # Find most engaging tweet
        df["Engagement"] = df["Likes"] + df["Retweets"] + df["Replies"] + df["Bookmarks"]
        most_engaging_tweet = df.loc[df["Engagement"].idxmax()] if not df.empty else None
        
        # Tweet type breakdown
        media_tweets_pct = (df["Has_Media"].sum() / total_tweets * 100) if total_tweets > 0 else 0
        reply_tweets_pct = (df["Is_Reply"].sum() / total_tweets * 100) if total_tweets > 0 else 0
        
        # Content analysis
        avg_tweet_length = df["Text_Length"].mean() if not df.empty else 0
        
        # Get top hashtags
        hashtag_counts = Counter(metrics["hashtags"])
        top_hashtags = hashtag_counts.most_common(5)
        
        # Get top mentions
        mention_counts = Counter(metrics["mentions"])
        top_mentions = mention_counts.most_common(5)
        
        # Temporal analysis by day
        df_by_day = df.groupby("Date_Only").size().reset_index(name="Count")
        df_by_hour = df.groupby("Hour").size().reset_index(name="Count")
        df_by_weekday = df.groupby("Day_of_Week").size().reset_index(name="Count")
        
        # Store DataFrame and metrics in session state
        st.session_state.processed_df = df
        # Note: Data is only stored to MongoDB during scheduled operations, not manual scraping
        
        # Generate Gemini summary if available
        gemini_summary = None
        if GENAI_AVAILABLE:
            with st.spinner("Generating AI summary with Gemini..."):
                context = f"The following are {analysis_type.lower()} for Twitter {'account' if analysis_type == 'Account' else 'comments to'}"
                gemini_summary = get_gemini_summary(df, context)
        
        # Two column layout for dashboard
        left_col, right_col = st.columns([1, 1])
        
        with left_col:
            # ENHANCED: Display account details if available
            account_details = metrics.get("account_details", {})
            # Debug: Show account details for troubleshooting
            with st.expander("πŸ” Debug Account Details"):
                st.write("Account details object:")
                st.json(account_details)
                if not account_details and hasattr(st.session_state, 'results') and st.session_state.results:
                    st.write("Sample raw API response (first item):")
                    sample_item = st.session_state.results[0] if st.session_state.results else {}
                    st.json({
                        "author": sample_item.get("author", "No author key"),
                        "available_keys": list(sample_item.keys()) if sample_item else []
                    })
            if account_details:
                st.subheader("πŸ‘€ Account Information")
                acc_col1, acc_col2, acc_col3 = st.columns(3)
                with acc_col1:
                    # Show followers count (even if 0)
                    followers_count = account_details.get("followers_count", 0)
                    st.metric("Followers", f"{followers_count:,}")
                    # Show following count (even if 0)
                    following_count = account_details.get("following_count", 0)
                    st.metric("Following", f"{following_count:,}")
                    # Calculate follower-to-following ratio
                    if followers_count > 0 and following_count > 0:
                        ratio = followers_count / following_count
                        st.metric("Follower Ratio", f"{ratio:.2f}:1")
                with acc_col2:
                    if account_details.get("tweet_count"):
                        st.metric("Total Tweets (All Time)", f"{account_details['tweet_count']:,}")
                    if account_details.get("listed_count"):
                        st.metric("Listed Count", f"{account_details['listed_count']:,}")
                with acc_col3:
                    if account_details.get("verified"):
                        st.success("βœ… Verified Account")
                    if account_details.get("bio"):
                        st.write(f"**Bio:** {account_details['bio']}")
                
                st.divider()
            
            st.subheader("πŸ“ˆ Key Metrics")
            
            # Basic stats
            metrics_section = st.container()
            
            col1, col2, col3 = metrics_section.columns(3)
            with col1:
                st.metric("Total Tweets", f"{total_tweets:,}")
                st.metric("Total Likes", f"{total_likes:,}")
            with col2:
                st.metric("Total Retweets", f"{total_retweets:,}")
                st.metric("Total Replies", f"{total_replies:,}")
            with col3:
                st.metric("Total Bookmarks", f"{total_bookmarks:,}")
                st.metric("Total Views", f"{total_views:,}")
            
            # Engagement metrics
            st.subheader("⚑ Engagement Analysis")
            engagement_cols = st.columns(2)
            with engagement_cols[0]:
                st.metric("Avg. Engagement per Tweet", f"{avg_engagement_per_tweet:.1f}")
            with engagement_cols[1]:
                st.metric("Engagement Rate", f"{engagement_rate:.2f}%")
            
            # Tweet type breakdown
            st.subheader("πŸ” Content Breakdown")
            type_cols = st.columns(3)
            with type_cols[0]:
                st.metric("Tweets with Media", f"{media_tweets_pct:.1f}%")
            with type_cols[1]:
                st.metric("Reply Tweets", f"{reply_tweets_pct:.1f}%")
            with type_cols[2]:
                st.metric("Avg. Tweet Length", f"{avg_tweet_length:.0f} chars")
            
            # Top hashtags
            if top_hashtags:
                st.subheader("πŸ” Top Hashtags")
                for tag, count in top_hashtags:
                    st.write(f"#{tag}: {count} times")
            
            # Top mentions
            if top_mentions:
                st.subheader("πŸ‘₯ Top Mentions")
                for user, count in top_mentions:
                    st.write(f"@{user}: {count} times")
            
            # Dataset info
            st.info(f"Dataset ID: {dataset_id}")
            
            # Download button
            csv = df.to_csv(index=False).encode('utf-8')
            st.download_button(
                "πŸ“₯ Download as CSV",
                csv,
                f"twitter_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
                "text/csv",
                key=f"download-csv-{analysis_type}",
                use_container_width=True
            )
        
        with right_col:
            # Display Gemini summary if available
            if gemini_summary:
                st.subheader("🧠 AI Summary")
                st.markdown(gemini_summary)
                st.divider()
            elif GENAI_AVAILABLE is False:
                st.info("πŸ’‘ AI Summary not available. Install the Google Generative AI package for automatic summaries. See sidebar for instructions.")
            
            # Most engaging tweet
            if most_engaging_tweet is not None:
                st.subheader("🌟 Most Engaging Tweet")
                with st.container():
                    st.write(f"**@{most_engaging_tweet['Username']}** β€’ {most_engaging_tweet['Author']} β€’ {most_engaging_tweet['Date']}")
                    st.write(most_engaging_tweet['Text'])
                    
                    # Display metrics in a row
                    cols = st.columns(5)
                    with cols[0]:
                        st.write(f"πŸ’¬ {most_engaging_tweet['Replies']}")
                    with cols[1]:
                        st.write(f"πŸ”„ {most_engaging_tweet['Retweets']}")
                    with cols[2]:
                        st.write(f"❀️ {most_engaging_tweet['Likes']}")
                    with cols[3]:
                        st.write(f"πŸ”– {most_engaging_tweet['Bookmarks']}")
                    with cols[4]:
                        st.write(f"πŸ‘οΈ {most_engaging_tweet['Views']}")
                    
                    # Link to original tweet
                    st.write(f"[View on Twitter]({most_engaging_tweet['URL']})")
                    st.divider()
            
            # Temporal analysis visualizations
            st.subheader("πŸ“… Posting Patterns")
            
            # Tweets by day
            if not df_by_day.empty and len(df_by_day) > 1:
                fig_by_day = px.line(df_by_day, x="Date_Only", y="Count", 
                                     title="Tweets by Day",
                                     labels={"Date_Only": "Date", "Count": "Number of Tweets"})
                st.plotly_chart(fig_by_day, use_container_width=True)
            
            # Tweets by hour of day
            if not df_by_hour.empty:
                fig_by_hour = px.bar(df_by_hour, x="Hour", y="Count", 
                                      title="Tweets by Hour of Day (Indian Time)",
                                      labels={"Hour": "Hour (24h format)", "Count": "Number of Tweets"})
                st.plotly_chart(fig_by_hour, use_container_width=True)
            
            # Tweets by day of week
            if not df_by_weekday.empty:
                # Sort by days of week properly
                days_order = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
                df_by_weekday["Day_of_Week"] = pd.Categorical(df_by_weekday["Day_of_Week"], categories=days_order, ordered=True)
                df_by_weekday = df_by_weekday.sort_values("Day_of_Week")
                
                fig_by_weekday = px.bar(df_by_weekday, x="Day_of_Week", y="Count", 
                                       title="Tweets by Day of Week",
                                       labels={"Day_of_Week": "Day of Week", "Count": "Number of Tweets"})
                st.plotly_chart(fig_by_weekday, use_container_width=True)
            
            # Advanced views in expandable sections
            with st.expander("View as Table"):
                st.dataframe(df, use_container_width=True)
            
            with st.expander("View Raw JSON"):
                st.json(data)
        
        # Display tweets list without pagination
        st.subheader("🐦 Tweets")
        display_tweet_list(df)
    else:
        st.warning("No results found. Try a different query or date range.")

# Function to handle tweet list display without pagination
def display_tweet_list(df):
    # Display all tweets from the dataframe
    st.write(f"Displaying all {len(df)} tweets:")

    # Add a toggle to show/hide tweets for better performance
    if len(df) > 50:
        show_all = st.checkbox("Show all tweets (may slow down the app)", value=False)
        display_count = len(df) if show_all else min(50, len(df))
        st.info(f"Showing {display_count} of {len(df)} tweets. {'' if show_all else 'Check the box above to see all tweets.'}")
        display_df = df.iloc[:display_count].copy()
    else:
        display_df = df
    
    # Display each tweet
    for i, row in display_df.iterrows():
        with st.container():
            st.write(f"**@{row['Username']}** β€’ {row['Author']} β€’ {row['Date']}")
            st.write(row['Text'])
            
            # Display metrics in a row
            cols = st.columns(5)
            with cols[0]:
                st.write(f"πŸ’¬ {row['Replies']}")
            with cols[1]:
                st.write(f"πŸ”„ {row['Retweets']}")
            with cols[2]:
                st.write(f"❀️ {row['Likes']}")
            with cols[3]:
                st.write(f"πŸ”– {row['Bookmarks']}")
            with cols[4]:
                st.write(f"πŸ‘οΈ {row['Views']}")
            
            # Indicate if tweet has media without showing it
            if row['Has_Media']:
                st.write("πŸ“· Contains media")
            
            # Link to original tweet
            st.write(f"[View on Twitter]({row['URL']})")
            st.divider()

# Function to display tweets in a compact format for comparison
def display_tweet_list_compact(df):
    # Limit to first 20 tweets for comparison view to avoid overwhelming the UI
    display_count = min(20, len(df))
    if len(df) > 20:
        st.info(f"Showing top {display_count} of {len(df)} tweets")
    
    display_df = df.iloc[:display_count].copy()
    
    # Display each tweet in compact format
    for i, row in display_df.iterrows():
        with st.container():
            # Compact header with date
            st.write(f"**{row['Date_Only']} {row['Time_Only']}**")
            
            # Tweet text (truncate if too long)
            text = row['Text']
            if len(text) > 200:
                text = text[:200] + "..."
            st.write(text)
            
            # Compact metrics in one line
            metrics_text = f"πŸ’¬ {row['Replies']} β€’ πŸ”„ {row['Retweets']} β€’ ❀️ {row['Likes']} β€’ πŸ”– {row['Bookmarks']} β€’ πŸ‘οΈ {row['Views']}"
            if row['Has_Media']:
                metrics_text += " β€’ πŸ“·"
            st.caption(metrics_text)
            
            # Small divider
            st.write("---")

# Function to store processed tweets into MongoDB (upsert by tweet ID) - ENHANCED FOR RAW DATA
def store_to_mongodb(df, analysis_type="Account", ai_summary=None, raw_data=None, account_details=None):
    if df.empty:
        return
    if not MONGODB_AVAILABLE:
        print(f"⚠️ MongoDB unavailable - {len(df)} tweets not stored")
        return
    
    # Group by username and store one document per account
    for username in df['Username'].unique():
        user_tweets = df[df['Username'] == username]
        
        # Calculate aggregated metrics (convert to native Python types for MongoDB)
        # Handle missing columns gracefully
        total_tweets = int(len(user_tweets))
        total_likes = int(user_tweets.get("Likes", pd.Series([0])).sum()) if "Likes" in user_tweets.columns else 0
        total_retweets = int(user_tweets.get("Retweets", pd.Series([0])).sum()) if "Retweets" in user_tweets.columns else 0
        total_replies = int(user_tweets.get("Replies", pd.Series([0])).sum()) if "Replies" in user_tweets.columns else 0
        total_bookmarks = int(user_tweets.get("Bookmarks", pd.Series([0])).sum()) if "Bookmarks" in user_tweets.columns else 0
        total_views = int(user_tweets.get("Views", pd.Series([0])).sum()) if "Views" in user_tweets.columns else 0
        total_engagement = total_likes + total_retweets + total_replies + total_bookmarks
        avg_engagement = float(total_engagement / total_tweets) if total_tweets > 0 else 0.0
        
        # Get all tweets as a list
        tweets_list = user_tweets.to_dict("records")
        
        # ENHANCED: Create account document with raw data and account details
        account_doc = {
            "username": username,
            "analysis_type": analysis_type,
            "last_updated": datetime.now().isoformat(),
            "total_tweets": total_tweets,
            "total_likes": total_likes,
            "total_retweets": total_retweets,
            "total_replies": total_replies,
            "total_bookmarks": total_bookmarks,
            "total_views": total_views,
            "total_engagement": total_engagement,
            "avg_engagement_per_tweet": avg_engagement,
            "tweets": tweets_list,
            "ai_summary": ai_summary,
            "raw_tweets": raw_data if raw_data else [],  # ADDED: Store raw data for sentiment analysis
            "account_details": account_details if account_details else {}  # ADDED: Store account details
        }
        
        # Upsert by username - one document per account
        tweets_collection.update_one(
            {"username": username}, 
            {"$set": account_doc}, 
            upsert=True
        )

# --- Scheduler utilities ---

def fetch_and_store(username, since, until):
    """Helper to fetch tweets for a username and store them in MongoDB."""
    try:
        results, _ = run_apify_account_analysis({
            "username": username,
            "since": since,
            "until": until,
            "min_faves": 0,
            "min_retweets": 0,
            "min_replies": 0
        })
        df, metrics, _ = process_tweet_data(results, extract_account_info=True)
        
        # Generate AI summary if available
        ai_summary = None
        if not df.empty and GENAI_AVAILABLE and GEMINI_API_KEY:
            try:
                context = f"The following are account tweets for Twitter account @{username}"
                ai_summary = get_gemini_summary(df, context)
            except Exception as e:
                print(f"AI summary generation failed for @{username}: {e}")
        
        # ENHANCED: Store with raw data and account details
        account_details = metrics.get("account_details", {})
        store_to_mongodb(df, "Account", ai_summary, raw_data=results, account_details=account_details)
    except Exception as e:
        print(f"Scheduler error fetching @{username}: {e}")


def schedule_fetch(usernames, since, until):
    for user in usernames:
        fetch_and_store(user, since, until)


def _run_schedule_loop():
    """Background thread that keeps the schedule running."""
    while True:
        schedule.run_pending()
        time.sleep(30)

# --- End Scheduler utilities ---

# --- Scheduler DB helpers ---

def get_scheduler_usernames():
    if not MONGODB_AVAILABLE:
        return []
    return [doc["username"] for doc in scheduler_users_collection.find()]


def save_scheduler_usernames(usernames):
    if not MONGODB_AVAILABLE:
        print("⚠️ MongoDB unavailable - usernames not stored")
        return
    for u in usernames:
        scheduler_users_collection.update_one({"username": u}, {"$set": {"username": u}}, upsert=True)

def remove_scheduler_username(username):
    if not MONGODB_AVAILABLE:
        print("⚠️ MongoDB unavailable - username not removed")
        return
    scheduler_users_collection.delete_one({"username": username})

def clear_all_scheduler_usernames():
    if not MONGODB_AVAILABLE:
        print("⚠️ MongoDB unavailable - usernames not cleared")
        return
    scheduler_users_collection.delete_many({})

def clear_all_tweets_data():
    if not MONGODB_AVAILABLE:
        print("⚠️ MongoDB unavailable - tweets data not cleared")
        return
    result = tweets_collection.delete_many({})
    return result.deleted_count

# --- End Scheduler DB helpers ---

def run_apify_followers_analysis(input):
    """
    Fetch followers/following data using Apify actor
    """
    username = input["username"]
    relationship_type = input.get("relationship_type", "followers")  # "followers" or "following"
    max_items = input.get("max_items", 100)
    
    # Try the followers actor first
    try:
        if relationship_type == "followers":
            run_input = {
                "twitterHandles": [username],
                "maxItems": max_items,
                "getFollowers": True,
                "getFollowing": False,
                "getRetweeters": False,
                "includeUnavailableUsers": False,
            }
        else:  # following
            run_input = {
                "twitterHandles": [username],
                "maxItems": max_items,
                "getFollowers": False,
                "getFollowing": True,
                "getRetweeters": False,
                "includeUnavailableUsers": False,
            }
        
        with st.spinner(f"Fetching {relationship_type} for @{username}..."):
            # Try the actor you specified
            run = client.actor("V38PZzpEgOfeeWvZY").call(run_input=run_input)
            data = list(client.dataset(run["defaultDatasetId"]).iterate_items())
            
            if data:
                return data, run["defaultDatasetId"]
            else:
                # Fallback: Use alternative followers scraper
                return run_apify_followers_fallback(input)
                
    except Exception as e:
        st.warning(f"Primary followers actor failed: {e}")
        # Fallback to alternative scraper
        return run_apify_followers_fallback(input)

def run_apify_followers_fallback(input):
    """
    Fallback method using alternative followers scraper
    """
    username = input["username"]
    relationship_type = input.get("relationship_type", "followers")
    max_items = input.get("max_items", 100)
    
    try:
        # Use curious_coder/twitter-scraper as fallback
        run_input = {
            "profileUrl": f"https://twitter.com/{username}",
            "friendshipType": relationship_type,  # "followers" or "following"
            "count": max_items,
            "minDelay": 1,
            "maxDelay": 3
        }
        
        with st.spinner(f"Fetching {relationship_type} for @{username} (fallback method)..."):
            run = client.actor("curious_coder/twitter-scraper").call(run_input=run_input)
            data = list(client.dataset(run["defaultDatasetId"]).iterate_items())
            return data, run["defaultDatasetId"]
            
    except Exception as e:
        st.error(f"All followers scrapers failed: {e}")
        return [], None

def process_followers_data(data, relationship_type="followers"):
    """
    Process followers/following data into a structured format
    """
    processed_data = []
    
    for item in data:
        # Handle different data structures from different actors
        username = item.get('username', item.get('screen_name', item.get('userName', '')))
        name = item.get('name', item.get('displayName', ''))
        
        processed_item = {
            "Username": username,
            "Name": name,
            "Bio": item.get('description', item.get('bio', '')),
            "Location": item.get('location', ''),
            "Followers": item.get('followers_count', item.get('followersCount', item.get('followers', 0))),
            "Following": item.get('following_count', item.get('followingCount', item.get('following', 0))),
            "Tweets": item.get('tweet_count', item.get('statusesCount', item.get('statuses_count', 0))),
            "Verified": item.get('verified', item.get('isVerified', False)),
            "Profile_Image": item.get('profile_image_url', item.get('profileImageUrl', '')),
            "Created_At": item.get('created_at', item.get('createdAt', '')),
            "URL": item.get('url', f"https://twitter.com/{username}"),
            "Relationship_Type": relationship_type
        }
        processed_data.append(processed_item)
    
    return pd.DataFrame(processed_data)

# App header
st.title("🐦 Twitter Scraper")

# Initialize session state variables if they don't exist
if 'username' not in st.session_state:
    st.session_state.username = ""
if 'id' not in st.session_state:
    st.session_state.id = ""
if 'since' not in st.session_state:
    st.session_state.since = "2025-01-01"
if 'until' not in st.session_state:
    st.session_state.until = datetime.now().strftime("%Y-%m-%d")
if 'min_faves' not in st.session_state:
    st.session_state.min_faves = 0
if 'min_retweets' not in st.session_state:
    st.session_state.min_retweets = 0
if 'min_replies' not in st.session_state:
    st.session_state.min_replies = 0
if 'results' not in st.session_state:
    st.session_state.results = None
if 'dataset_id' not in st.session_state:
    st.session_state.dataset_id = None
if 'active_tab' not in st.session_state:
    st.session_state.active_tab = 0
if 'processed_df' not in st.session_state:
    st.session_state.processed_df = None
if 'username1' not in st.session_state:
    st.session_state.username1 = ""
if 'username2' not in st.session_state:
    st.session_state.username2 = ""
if 'compare_since' not in st.session_state:
    st.session_state.compare_since = "2025-01-01"
if 'compare_until' not in st.session_state:
    st.session_state.compare_until = datetime.now().strftime("%Y-%m-%d")

# Create tabs
tabs = st.tabs(["πŸ“Š Account Analysis", "πŸ’¬ Comment Analysis", "πŸ†š Compare", "⏰ Scheduler"])

# Account Analysis tab
with tabs[0]:
    # Create a container for inputs
    with st.container():
        st.header("Account Analysis")
        st.write("Analyze tweets from a specific Twitter account")
        
        # Input fields in a cleaner layout
        col1, col2, col3 = st.columns([3, 2, 2])
        with col1:
            st.session_state.username = st.text_input("Enter Twitter username (without @)", 
                                               value=st.session_state.username, 
                                               key="account_username",
                                               placeholder="e.g. elonmusk")
        with col2:
            st.session_state.since = st.date_input("Start date", 
                                                 value=datetime.strptime(st.session_state.since, "%Y-%m-%d") 
                                                 if isinstance(st.session_state.since, str) 
                                                 else st.session_state.since, 
                                                 key="account_since")
        with col3:
            st.session_state.until = st.date_input("End date", 
                                                 value=datetime.strptime(st.session_state.until, "%Y-%m-%d") 
                                                 if isinstance(st.session_state.until, str) 
                                                 else st.session_state.until, 
                                                 key="account_until")
        
        # Optional engagement filters
        with st.expander("βš™οΈ Advanced Filters (Optional)", expanded=False):
            st.info("All filters are set to 0 by default to capture maximum tweets. Increase values to filter for more engaging content.")
            col1, col2, col3 = st.columns(3)
            with col1:
                st.session_state.min_faves = st.number_input("Minimum Likes", 
                                                      min_value=0, 
                                                      max_value=10000, 
                                                      value=st.session_state.min_faves,
                                                      step=10,
                                                      key="account_min_faves")
            with col2:
                st.session_state.min_retweets = st.number_input("Minimum Retweets", 
                                                         min_value=0, 
                                                         max_value=1000, 
                                                         value=st.session_state.min_retweets,
                                                         step=5,
                                                         key="account_min_retweets")
            with col3:
                st.session_state.min_replies = st.number_input("Minimum Replies", 
                                                        min_value=0, 
                                                        max_value=1000, 
                                                        value=st.session_state.min_replies,
                                                        step=5,
                                                        key="account_min_replies")
        
        # Convert dates to string format
        if not isinstance(st.session_state.since, str):
            st.session_state.since = st.session_state.since.strftime("%Y-%m-%d")
        if not isinstance(st.session_state.until, str):
            st.session_state.until = st.session_state.until.strftime("%Y-%m-%d")
        
        # Run button
        run_button = st.button("πŸ” Analyze Account Tweets", key="run_account", use_container_width=True)
    
    # Run analysis when button is clicked
    if run_button:
        if st.session_state.username:
            # Validate date range
            if st.session_state.since > st.session_state.until:
                st.error("Start date must be before end date.")
            else:
                st.session_state.results, st.session_state.dataset_id = run_apify_account_analysis({
                    "username": st.session_state.username, 
                    "since": st.session_state.since,
                    "until": st.session_state.until,
                    "min_faves": st.session_state.min_faves,
                    "min_retweets": st.session_state.min_retweets,
                    "min_replies": st.session_state.min_replies
                })

                # Process results to check for mock data
                processed_df, metrics, mock_data_detected = process_tweet_data(st.session_state.results, extract_account_info=True)

                if mock_data_detected:
                    st.warning("Mock data detected in the response, indicating limited results. This may be due to strict filters or no tweets in the date range.")
                
                if not processed_df.empty:
                    date_range = f"{st.session_state.since} to {st.session_state.until}"
                    st.success(f"Analysis complete! Found {len(processed_df)} tweets from {date_range}.")
                    st.balloons()
                    # Pass raw data to preserve account details
                    analyze_and_display_data(st.session_state.results, st.session_state.dataset_id, "Account")
                else:
                    st.warning("No results found. Try a different date range or reduce the engagement filters.")
        else:
            st.error("Please enter a Twitter username")

# Comment Analysis tab
with tabs[1]:
    with st.container():
        st.header("Comment Analysis")
        st.write("Analyze comments directed at a specific Twitter account")
        
        # Input fields in a cleaner layout
        col1, col2, col3 = st.columns([3, 2, 2])
        with col1:
            tweet_id = st.text_input("Enter Twitter ID", 
                                    key="comment_id", 
                                    placeholder="e.g. YSJaganTrends")
        with col2:
            comment_since = st.date_input("Start date", 
                                        value=datetime.strptime(st.session_state.since, "%Y-%m-%d") 
                                        if isinstance(st.session_state.since, str) 
                                        else st.session_state.since,
                                        key="comment_since")
        with col3:
            comment_until = st.date_input("End date", 
                                        value=datetime.strptime(st.session_state.until, "%Y-%m-%d") 
                                        if isinstance(st.session_state.until, str) 
                                        else st.session_state.until,
                                        key="comment_until")
        
        # Run button
        comment_button = st.button("πŸ” Analyze Comments", key="run_comment", use_container_width=True)
    
    # Run analysis when button is clicked
    if comment_button:
        if tweet_id:
            # Validate date range
            if comment_since > comment_until:
                st.error("Start date must be before end date.")
            else:
                raw_results, dataset_id = run_apify_comment_analysis({
                    "id": tweet_id, 
                    "since": comment_since.strftime("%Y-%m-%d"),
                    "until": comment_until.strftime("%Y-%m-%d")
                })
                
                # Process data to remove mock tweets and get the actual count
                processed_df, _, mock_data_detected = process_tweet_data(raw_results)
                
                if not processed_df.empty:
                    date_range = f"{comment_since.strftime('%Y-%m-%d')} to {comment_until.strftime('%Y-%m-%d')}"
                    st.success(f"Analysis complete! Found {len(processed_df)} actual comments from {date_range}.")
                    st.balloons()
                    # Display the results using the processed DataFrame
                    analyze_and_display_data(processed_df, dataset_id, "Comment")
                elif mock_data_detected and processed_df.empty:
                    st.warning("Mock data was returned by the API, indicating no specific comments were found for your query. Please try adjusting your date range.")
                else: # No mock data, but still empty (or raw_results was empty)
                    st.warning("No results found. Try a different query or date range.")
        else:
            st.error("Please enter a Twitter ID")

# Compare Accounts tab
with tabs[2]:
    with st.container():
        st.header("Compare Accounts")
        st.write("Analyze two Twitter accounts side-by-side")
        
        # Input fields
        col1, col2 = st.columns(2)
        with col1:
            st.session_state.username1 = st.text_input(
                "Enter first Twitter username (without @)", 
                value=st.session_state.username1, 
                key="compare_username1",
                placeholder="e.g. narendramodi"
            )
        with col2:
            st.session_state.username2 = st.text_input(
                "Enter second Twitter username (without @)", 
                value=st.session_state.username2, 
                key="compare_username2",
                placeholder="e.g. RahulGandhi"
            )
        
        # Shared settings
        col1, col2 = st.columns([1, 1])
        with col1:
            # Use a different key for the date input to avoid conflicts
            compare_since_date = st.date_input(
                "Start date", 
                value=datetime.strptime(st.session_state.compare_since, "%Y-%m-%d"),
                key="compare_since_dateinput"
            )
            st.session_state.compare_since = compare_since_date.strftime("%Y-%m-%d")
        with col2:
            compare_until_date = st.date_input(
                "End date", 
                value=datetime.strptime(st.session_state.compare_until, "%Y-%m-%d"),
                key="compare_until_dateinput"
            )
            st.session_state.compare_until = compare_until_date.strftime("%Y-%m-%d")
        
        compare_button = st.button("βš–οΈ Compare Accounts", key="run_compare", use_container_width=True)

    if compare_button:
        if st.session_state.username1 and st.session_state.username2:
            # Validate date range
            if st.session_state.compare_since > st.session_state.compare_until:
                st.error("Start date must be before end date.")
            else:
                def fetch_and_process_user_data(username, since, until):
                    date_range = f"{since} to {until}"
                    with st.spinner(f"Fetching tweets for @{username} from {date_range}..."):
                        results, dataset_id = run_apify_account_analysis({
                            "username": username, 
                            "since": since,
                            "until": until,
                            "min_faves": 0,
                            "min_retweets": 0,
                            "min_replies": 0
                        })
                        processed_df, metrics, mock_data = process_tweet_data(results, extract_account_info=True)

                        if mock_data:
                            st.warning(f"Mock data detected for @{username}, indicating limited results in the date range.")
                        
                        if not processed_df.empty:
                            account_details = metrics.get("account_details", {})
                            followers_info = f" | {account_details.get('followers_count', 'N/A')} followers" if account_details.get('followers_count') else ""
                            following_info = f" | {account_details.get('following_count', 'N/A')} following" if account_details.get('following_count') else ""
                            st.success(f"Found {len(processed_df)} tweets for @{username} from {date_range}{followers_info}{following_info}.")
                            
                            # ENHANCED: Debug mode for account details
                            if account_details:
                                with st.expander(f"πŸ” Debug Account Info for @{username}"):
                                    st.json(account_details)
                        else:
                            st.warning(f"No results for @{username} in the specified date range.")

                        return processed_df, metrics, dataset_id

                col1, col2 = st.columns(2)
                
                with col1:
                    df1, metrics1, dsid1 = fetch_and_process_user_data(
                        st.session_state.username1, 
                        st.session_state.compare_since, 
                        st.session_state.compare_until
                    )
                    if not df1.empty:
                        display_compact_analysis(df1, metrics1, st.session_state.username1, dsid1)

                with col2:
                    df2, metrics2, dsid2 = fetch_and_process_user_data(
                        st.session_state.username2, 
                        st.session_state.compare_since, 
                        st.session_state.compare_until
                    )
                    if not df2.empty:
                        display_compact_analysis(df2, metrics2, st.session_state.username2, dsid2)
            
            # Display tweets side by side after the analysis
            if not df1.empty or not df2.empty:
                st.divider()
                st.subheader("🐦 Tweets Comparison")
                
                col1, col2 = st.columns(2)
                
                with col1:
                    if not df1.empty:
                        st.markdown(f"### @{st.session_state.username1} Tweets")
                        display_tweet_list_compact(df1)
                    else:
                        st.info(f"No tweets found for @{st.session_state.username1}")
                
                with col2:
                    if not df2.empty:
                        st.markdown(f"### @{st.session_state.username2} Tweets")
                        display_tweet_list_compact(df2)
                    else:
                        st.info(f"No tweets found for @{st.session_state.username2}")

        else:
            st.error("Please enter both Twitter usernames to compare.")



# Scheduler tab
with tabs[3]:
    st.header("⏰ Daily Scheduler")
    st.write("Configure daily automatic fetching of tweets and storage to MongoDB.")

    # Existing stored usernames
    existing_users = get_scheduler_usernames()
    if existing_users:
        st.markdown("**Current usernames:** " + ", ".join(existing_users))
        
        # Remove usernames section
        st.subheader("πŸ—‘οΈ Manage Usernames")
        col1, col2 = st.columns([3, 1])
        with col1:
            username_to_remove = st.selectbox("Select username to remove", [""] + existing_users, key="username_to_remove")
        with col2:
            st.write("")  # Empty space for alignment
            if st.button("πŸ—‘οΈ Remove", key="remove_username_btn"):
                if username_to_remove:
                    remove_scheduler_username(username_to_remove)
                    st.success(f"@{username_to_remove} removed from scheduler.")
                    st.rerun()
                else:
                    st.error("Please select a username to remove.")
        
        # Clear all button
        if st.button("πŸ—‘οΈ Clear All Usernames", key="clear_all_btn", type="secondary"):
            clear_all_scheduler_usernames()
            st.success("All usernames cleared from scheduler.")
            st.rerun()
        
        # Clear database button
        st.divider()
        st.subheader("πŸ—„οΈ Database Management")
        st.warning("⚠️ This will permanently delete all stored tweet data and AI summaries!")
        if st.button("πŸ—‘οΈ Clear All Tweet Data", key="clear_db_btn", type="secondary"):
            if MONGODB_AVAILABLE:
                deleted_count = clear_all_tweets_data()
                if deleted_count > 0:
                    st.success(f"βœ… Cleared {deleted_count} account records from database.")
                else:
                    st.info("Database was already empty.")
            else:
                st.error("MongoDB not available - cannot clear database.")
    else:
        st.info("No usernames stored yet.")

    # Add single username
    st.subheader("βž• Add Username")
    new_user = st.text_input("Add a new Twitter username", key="sched_single_add")
    if st.button("βž• Add Username", key="sched_add_btn", use_container_width=True):
        if new_user.strip():
            save_scheduler_usernames([new_user.strip()])
            st.success(f"@{new_user.strip()} added to scheduler list.")
            st.rerun()
        else:
            st.error("Enter a valid username.")

    st.divider()
    
    # Scheduler configuration
    st.subheader("βš™οΈ Scheduler Configuration")
    usernames_input = st.text_area("Usernames to schedule (one per line)", value="\n".join(existing_users), key="sched_usernames")
    
    col1, col2, col3 = st.columns(3)
    with col1:
        sched_since = st.date_input("Start date", value=(datetime.now() - timedelta(days=30)).date(), key="sched_since")
    with col2:
        sched_until = st.date_input("End date", value=datetime.now().date(), key="sched_until")
    with col3:
        sched_time = st.time_input("Run at (24h format)", datetime.now().replace(hour=2, minute=0, second=0, microsecond=0).time(), key="sched_time")

    # Buttons row
    col1, col2 = st.columns(2)
    with col1:
        if st.button("▢️ Start Scheduler", key="start_scheduler", use_container_width=True):
            usernames = [u.strip() for u in usernames_input.split("\n") if u.strip()]
            if usernames:
                # Validate date range
                if sched_since > sched_until:
                    st.error("Start date must be before end date.")
                else:
                    # Save/update usernames in DB
                    save_scheduler_usernames(usernames)
                    
                    # Clear existing jobs with tag
                    schedule.clear('tweet_jobs')

                    def scheduled_job():
                        schedule_fetch(usernames, sched_since.strftime("%Y-%m-%d"), sched_until.strftime("%Y-%m-%d"))

                    schedule.every().day.at(sched_time.strftime("%H:%M")).tag('tweet_jobs').do(scheduled_job)
                    date_range = f"{sched_since.strftime('%Y-%m-%d')} to {sched_until.strftime('%Y-%m-%d')}"
                    st.success(f"Scheduler started for {len(usernames)} accounts daily at {sched_time.strftime('%H:%M')} for date range {date_range}.")

                    # Launch scheduler loop thread if not already running
                    if 'scheduler_thread' not in st.session_state:
                        thread = threading.Thread(target=_run_schedule_loop, daemon=True)
                        thread.start()
                        st.session_state.scheduler_thread = thread
            else:
                st.error("Please input at least one username.")
    
    with col2:
        if st.button("πŸš€ Run Now", key="run_now_btn", use_container_width=True, type="secondary"):
            usernames = [u.strip() for u in usernames_input.split("\n") if u.strip()]
            if usernames:
                # Validate date range
                if sched_since > sched_until:
                    st.error("Start date must be before end date.")
                else:
                    date_range = f"{sched_since.strftime('%Y-%m-%d')} to {sched_until.strftime('%Y-%m-%d')}"
                    with st.spinner(f"Scraping tweets for {len(usernames)} accounts from {date_range}..."):
                        try:
                            total_tweets = 0
                            for username in usernames:
                                with st.spinner(f"Scraping @{username} from {date_range}..."):
                                    results, _ = run_apify_account_analysis({
                                        "username": username,
                                        "since": sched_since.strftime("%Y-%m-%d"),
                                        "until": sched_until.strftime("%Y-%m-%d"),
                                        "min_faves": 0,
                                        "min_retweets": 0,
                                        "min_replies": 0
                                    })
                                    df, metrics, _ = process_tweet_data(results, extract_account_info=True)
                                    if not df.empty:
                                        # Generate AI summary
                                        ai_summary = None
                                        if GENAI_AVAILABLE and GEMINI_API_KEY:
                                            with st.spinner(f"Generating AI summary for @{username}..."):
                                                try:
                                                    context = f"The following are account tweets for Twitter account @{username}"
                                                    ai_summary = get_gemini_summary(df, context)
                                                except Exception as e:
                                                    st.warning(f"AI summary generation failed for @{username}: {e}")
                                        
                                        # ENHANCED: Store with raw data and account details
                                        account_details = metrics.get("account_details", {})
                                        store_to_mongodb(df, "Account", ai_summary, raw_data=results, account_details=account_details)
                                        total_tweets += len(df)
                                        summary_status = " (with AI summary)" if ai_summary else ""
                                        account_info = f" | Followers: {account_details.get('followers_count', 'N/A')}" if account_details.get('followers_count') else ""
                                        st.success(f"βœ… @{username}: {len(df)} tweets scraped and stored from {date_range}{summary_status}{account_info}")
                                    else:
                                        st.warning(f"⚠️ @{username}: No tweets found in the specified date range")
                            
                            if total_tweets > 0:
                                st.success(f"πŸŽ‰ Successfully scraped and stored {total_tweets} tweets from {len(usernames)} accounts in date range {date_range}!")
                                st.info("Data has been stored in your MongoDB DataCollector database.")
                            else:
                                st.warning("No tweets were found for any of the accounts in the specified date range.")
                        except Exception as e:
                            st.error(f"❌ Error during scraping: {str(e)}")
            else:
                st.error("Please input at least one username.")

    # Display currently scheduled jobs
    jobs = schedule.get_jobs('tweet_jobs')
    if jobs:
        st.subheader("πŸ“… Scheduled Jobs")
        for job in jobs:
            st.write(str(job))
        st.info(f"Next run at: {jobs[0].next_run.strftime('%Y-%m-%d %H:%M:%S')}")
    
    # Stop scheduler button
    if jobs:
        if st.button("⏹️ Stop Scheduler", key="stop_scheduler", type="secondary"):
            schedule.clear('tweet_jobs')
            st.success("Scheduler stopped. All scheduled jobs cleared.")
            st.rerun()

# ENHANCED: Show API limitations and setup instructions
st.sidebar.title("πŸ“‹ API Notes & Features")
st.sidebar.info(
    """
    **New Features:**
    
    βœ… **Date Range Fetching:** All tweets between start and end dates are fetched (no max limit)
    
    βœ… **Account Analysis:** Comprehensive account details shown in all analysis views
    
    βœ… **Zero Engagement Filters:** Default engagement filters set to 0 for maximum tweet capture
    
    βš™οΈ **Optional Filters:** Users can set custom engagement thresholds if desired
    
    **Known Limitations:**
    
    🚫 **Tweet-level comment replies** are not available due to Twitter API restrictions. Only direct comments to the main account are fetched.
    
    ⚠️ **Tweet count discrepancies** may occur due to:
    - Private/protected tweets
    - Deleted tweets  
    - API rate limiting
    - Account restrictions
    - Language filtering (now disabled by default)
    - Time zone differences (API uses UTC, display shows IST)
    
    πŸ’‘ **Tips for better results:**
    - Use appropriate date ranges
    - Keep engagement filters at 0 (default) for maximum capture
    - Use broader time periods for more comprehensive data
    - Check the debug info shown with query results
    - Compare against multiple time ranges for consistency
    
    πŸ”§ **Troubleshooting discrepancies:**
    - Twitter's web interface may include/exclude different content types
    - Retweets are now included by default for better accuracy
    - Language filter removed to capture all tweets
    - Check the raw results count vs processed count
    """
)

# Show instructions for setting up Gemini
if not GENAI_AVAILABLE or not GEMINI_API_KEY:
    st.sidebar.title("Setup Gemini API")
    
    if not GENAI_AVAILABLE:
        st.sidebar.error(
            """
            The Google Generative AI package is not installed.
            
            Install it by running:
            ```
            pip install google-generativeai
            ```
            Then restart the application.
            """
        )
    
    if GENAI_AVAILABLE and not GEMINI_API_KEY:
        st.sidebar.info(
            """
            To enable the Gemini summarization feature:
            1. Get an API key from [Google AI Studio](https://aistudio.google.com/)
            2. Add the key to your .env.local file as:
            ```
            GEMINI_API_KEY=your_api_key_here
            ```
            3. Restart the application
            """
        )

# Show MongoDB status
st.sidebar.title("Database Status")
if MONGODB_AVAILABLE:
    st.sidebar.success("βœ… MongoDB Connected")
else:
    st.sidebar.error("⚠️ MongoDB Offline")
    st.sidebar.info(
        """
        Running in offline mode. 
        Data will not be stored to database.
        
        To connect to MongoDB:
        1. Check your internet connection
        2. Verify MongoDB Atlas cluster is running
        3. Check MONGODB_URI in .env.local
            """
        )

# Update requirements.txt file if it exists and does not contain the package
try:
    with open("requirements.txt", "r") as f:
        requirements = f.read()
    
    updated_requirements = False
    
    if "google-generativeai" not in requirements:
        with open("requirements.txt", "a") as f:
            f.write("\ngoogle-generativeai>=0.3.0\n")
            updated_requirements = True
    
    if "pytz" not in requirements:
        with open("requirements.txt", "a") as f:
            f.write("\npytz\n")
            updated_requirements = True
    
    if "pymongo" not in requirements:
        with open("requirements.txt", "a") as f:
            f.write("\npymongo>=4.6.0\n")
            updated_requirements = True
    
    if "schedule" not in requirements:
        with open("requirements.txt", "a") as f:
            f.write("\nschedule\n")
            updated_requirements = True
except:
    pass


# Footer with attribution
st.divider()
st.caption("Powered by Apify Twitter Scraper API β€’ Created with Streamlit β€’ AI Summaries by Google Gemini β€’ Times in Indian Standard Time (IST)")