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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3811fa5
1c1fed2
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
from fastapi import FastAPI, File, UploadFile, HTTPException, Form, WebSocket, WebSocketDisconnect, Request
from typing import Optional, List, Dict, Any
from fastapi.responses import FileResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
import uvicorn
import os
import tempfile
from pathlib import Path
import asyncio
import logging
import json
import base64
import requests
import re

from services.image_verifier import ImageVerifier
from services.video_verifier import VideoVerifier
from services.input_processor import InputProcessor
from services.text_fact_checker import TextFactChecker
from services.educational_content_generator import EducationalContentGenerator
from services.mongodb_service import MongoDBService
from services.websocket_service import connection_manager, initialize_mongodb_change_stream, cleanup_mongodb_change_stream
from services.razorpay_service import RazorpayService
import razorpay.errors
from utils.file_utils import save_upload_file, cleanup_temp_files
from config import config
from services.deepfake_checker import detect_audio_deepfake
from services.youtube_caption import get_youtube_transcript_ytdlp
import google.generativeai as genai

app = FastAPI(
    title="Visual Verification Service",
    description="A service to verify images/videos and generate visual counter-measures",
    version="1.0.0"
)

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Add CORS middleware
# Allow all origins for accessibility
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Mount static directory for local assets (e.g., extracted frames)
import os
os.makedirs("public/frames", exist_ok=True)
app.mount("/static", StaticFiles(directory="public"), name="static")
app.mount("/frames", StaticFiles(directory="public/frames"), name="frames")


# ---------- Tier configuration ----------

# Public-facing tiers used across the product
NORMALIZED_TIERS = ("Free", "Plus", "Pro")

# Map stored subscription_tier / plan_name values to normalized tiers.
# This keeps backward compatibility with any existing users whose tier
# might still be stored as \"Pro\" or \"Enterprise\".
SUBSCRIPTION_TIER_MAPPING = {
    "free": "Free",
    "plus": "Plus",
    "pro": "Plus",          # legacy Pro maps to Plus
    "enterprise": "Pro",    # highest tier maps to Pro
}

# Central limits per tier so they can be tuned in one place.
# These values are intentionally conservative to protect API costs.
TIER_LIMITS = {
    "Free": {
        "daily_verifications": 5,
        "monthly_verifications": 25,
        "max_chat_sessions": 1,
        "max_messages_per_session": 10,
    },
    "Plus": {
        "daily_verifications": 10,
        "monthly_verifications": 50,
        "max_chat_sessions": 5,
        "max_messages_per_session": 50,
    },
    "Pro": {
        "daily_verifications": 25,
        "monthly_verifications": 200,
        "max_chat_sessions": 20,
        "max_messages_per_session": 200,
    },
}


def get_normalized_tier(raw_tier: str | None) -> str:
    """
    Normalize any stored subscription_tier / plan_name to one of
    the public-facing tiers: Free, Plus, Pro.
    """
    if not raw_tier:
        return "Free"
    key = str(raw_tier).strip().lower()
    return SUBSCRIPTION_TIER_MAPPING.get(key, "Free")


def get_tier_limits(raw_tier: str | None) -> dict:
    """
    Return the limits dict for a given stored tier value.
    """
    normalized = get_normalized_tier(raw_tier)
    return TIER_LIMITS.get(normalized, TIER_LIMITS["Free"])


# Initialize verifiers and input processor
image_verifier = ImageVerifier()
video_verifier = VideoVerifier()
input_processor = InputProcessor()
text_fact_checker = TextFactChecker()
educational_generator = EducationalContentGenerator()

# Initialize MongoDB service
mongodb_service = None
try:
    mongodb_service = MongoDBService()
except Exception as e:
    print(f"Warning: MongoDB service initialization failed: {e}")

# Initialize Razorpay service
razorpay_service = None
try:
    razorpay_service = RazorpayService()
except Exception as e:
    print(f"Warning: Razorpay service initialization failed: {e}")

# Initialize MongoDB change service (will be set in startup event)
mongodb_change_service = None

async def initialize_subscription_plans():
    """Initialize subscription plans in Razorpay if they don't exist"""
    if not razorpay_service or not razorpay_service.client:
        logger.warning("⚠️ Razorpay service not available. Skipping plan initialization.")
        return
    
    # First, test Razorpay connection by trying to fetch account details or make a simple API call
    try:
        # Try to verify credentials work by attempting a simple operation
        # We'll skip listing plans if it fails and just try to create
        logger.info("πŸ” Testing Razorpay API connection...")
    except Exception as e:
        logger.error(f"❌ Razorpay API connection test failed: {e}")
        logger.warning("⚠️ Skipping plan initialization due to API connection issues")
        return
    
    try:
        # Try to list existing plans, but don't fail if it errors
        existing_plan_names = set()
        try:
            existing_plans = razorpay_service.list_plans(count=100)
            if existing_plans and existing_plans.get("items"):
                existing_plan_names = {
                    p.get("item", {}).get("name") 
                    for p in existing_plans.get("items", [])
                    if p.get("item", {}).get("name")
                }
                logger.info(f"πŸ“‹ Found {len(existing_plan_names)} existing plans")
        except Exception as list_error:
            error_msg = str(list_error).lower()
            if "not found" in error_msg or "404" in error_msg:
                logger.info("ℹ️ No existing plans found (this is normal for new accounts)")
            else:
                logger.warning(f"⚠️ Could not list existing plans: {list_error}")
            # Continue anyway - we'll try to create plans and handle duplicates
        
        plans_to_create = [
            {
                "name": "Plan 1",
                "amount": 100,  # 1 INR in paise
                "currency": "INR",
                "interval": 1,
                "period": "monthly",
                "description": "Plan 1 - Monthly Subscription (1 INR)"
            },
            {
                "name": "Plan 2",
                "amount": 200,  # 2 INR in paise
                "currency": "INR",
                "interval": 1,
                "period": "monthly",
                "description": "Plan 2 - Monthly Subscription (2 INR)"
            },
            {
                "name": "Plan 3",
                "amount": 300,  # 3 INR in paise
                "currency": "INR",
                "interval": 1,
                "period": "monthly",
                "description": "Plan 3 - Monthly Subscription (3 INR)"
            }
        ]
        
        created_count = 0
        skipped_count = 0
        error_count = 0
        
        for plan_data in plans_to_create:
            plan_name = plan_data["name"]
            
            # Check if plan already exists
            if plan_name in existing_plan_names:
                logger.info(f"⏭️ Plan {plan_name} already exists, skipping")
                skipped_count += 1
                continue
            
            try:
                logger.info(f"πŸ”„ Creating plan: {plan_name}...")
                plan = razorpay_service.create_plan(**plan_data)
                logger.info(f"βœ… Created subscription plan: {plan_name} (ID: {plan.get('id')})")
                created_count += 1
            except razorpay.errors.BadRequestError as e:
                error_msg = str(e).lower()
                # Check if error is due to plan already existing (duplicate)
                if "already exists" in error_msg or "duplicate" in error_msg:
                    logger.info(f"⏭️ Plan {plan_name} already exists (detected during creation), skipping")
                    skipped_count += 1
                else:
                    logger.error(f"❌ BadRequestError creating plan {plan_name}: {e}")
                    error_count += 1
            except Exception as e:
                error_msg = str(e).lower()
                # Check if error is due to plan already existing (duplicate)
                if "already exists" in error_msg or "duplicate" in error_msg:
                    logger.info(f"⏭️ Plan {plan_name} already exists (detected during creation), skipping")
                    skipped_count += 1
                elif "not found" in error_msg or "404" in error_msg:
                    logger.error(f"❌ API endpoint not found for plan {plan_name}. Check Razorpay credentials and API access.")
                    logger.error(f"   Error details: {e}")
                    error_count += 1
                else:
                    logger.error(f"❌ Failed to create plan {plan_name}: {e}")
                    error_count += 1
        
        if created_count > 0:
            logger.info(f"βœ… Successfully created {created_count} subscription plans")
        if skipped_count > 0:
            logger.info(f"⏭️ Skipped {skipped_count} plans (already exist)")
        if error_count > 0:
            logger.warning(f"⚠️ {error_count} plans failed to create. Check Razorpay credentials and API permissions.")
        if created_count == 0 and skipped_count == 0 and error_count > 0:
            logger.error("❌ All plan creation attempts failed. Please verify:")
            logger.error("   1. RAZORPAY_ID and RAZORPAY_KEY are correct")
            logger.error("   2. API keys have subscription/plan creation permissions")
            logger.error("   3. Razorpay account has subscriptions feature enabled")
            
    except Exception as e:
        logger.error(f"❌ Failed to initialize subscription plans: {e}")
        import traceback
        logger.error(traceback.format_exc())

@app.on_event("startup")
async def startup_event():
    """Initialize services on startup"""
    global mongodb_change_service
    try:
        mongodb_change_service = await initialize_mongodb_change_stream()
        # Initialize subscription plans
        await initialize_subscription_plans()
        logger.info("βœ… All services initialized successfully")
    except Exception as e:
        logger.error(f"❌ Failed to initialize services: {e}")

@app.on_event("shutdown")
async def shutdown_event():
    """Cleanup services on shutdown"""
    try:
        await cleanup_mongodb_change_stream()
        logger.info("🧹 All services cleaned up successfully")
    except Exception as e:
        logger.error(f"❌ Error during cleanup: {e}")

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    """WebSocket endpoint for real-time updates"""
    await connection_manager.connect(websocket, {"connected_at": asyncio.get_event_loop().time()})
    logger.info(f"βœ… WebSocket client connected. Total connections: {len(connection_manager.active_connections)}")
    
    try:
        # Send initial greeting to confirm connectivity
        await connection_manager.send_personal_message(
            json.dumps({"type": "hello", "message": "Connected to rumours stream"}),
            websocket
        )
        while True:
            try:
                # Wait for incoming messages with a timeout
                data = await asyncio.wait_for(websocket.receive_text(), timeout=30.0)
                
                # Echo back a response (optional)
                await connection_manager.send_personal_message(
                    json.dumps({"type": "pong", "message": "Connection active"}), 
                    websocket
                )
            except asyncio.TimeoutError:
                # Send a ping to keep connection alive
                await connection_manager.send_personal_message(
                    json.dumps({"type": "ping", "message": "Keep alive"}), 
                    websocket
                )
            except Exception as e:
                logger.error(f"❌ Error in WebSocket message handling: {e}")
                break
                
    except WebSocketDisconnect:
        logger.info("πŸ”Œ WebSocket client disconnected normally")
        connection_manager.disconnect(websocket)
    except Exception as e:
        logger.error(f"❌ WebSocket error: {e}")
        connection_manager.disconnect(websocket)

@app.get("/")
async def root():
    return {"message": "Visual Verification Service is running"}

@app.post("/verify/image")
async def verify_image(
    file: Optional[UploadFile] = File(None),
    image_url: Optional[str] = Form(None),
    claim_context: str = Form("Unknown context"),
    claim_date: str = Form("Unknown date")
):
    """
    Verify a single image and generate a visual counter-measure
    """
    try:
        # Save uploaded file temporarily
        temp_file_path = None
        if file is not None:
            temp_file_path = await save_upload_file(file)
        
        # Verify image
        result = await image_verifier.verify(
            image_path=temp_file_path,
            claim_context=claim_context,
            claim_date=claim_date,
            image_url=image_url
        )
        
        # Clean up temp file
        if temp_file_path:
            cleanup_temp_files([temp_file_path])
        
        return result
            
    except Exception as e:
        # Clean up on error
        if 'temp_file_path' in locals() and temp_file_path:
            cleanup_temp_files([temp_file_path])
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/verify/video")
async def verify_video(
    file: Optional[UploadFile] = File(None),
    video_url: Optional[str] = Form(None),
    claim_context: str = Form("Unknown context"),
    claim_date: str = Form("Unknown date")
):
    """
    Verify a video and generate a visual counter-measure video
    """
    try:
        # Save uploaded file temporarily
        temp_file_path = None
        if file is not None:
            temp_file_path = await save_upload_file(file)
        
        # Verify video
        result = await video_verifier.verify(
            video_path=temp_file_path,
            claim_context=claim_context,
            claim_date=claim_date,
            video_url=video_url
        )
        
        # Clean up temp file
        if temp_file_path:
            cleanup_temp_files([temp_file_path])
        
        return result
            
    except Exception as e:
        # Clean up on error
        if 'temp_file_path' in locals() and temp_file_path:
            cleanup_temp_files([temp_file_path])
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/verify/text")
async def verify_text(
    text_input: str = Form(...),
    claim_context: str = Form("Unknown context"),
    claim_date: str = Form("Unknown date")
):
    """
    Verify a textual claim using Google's Fact Check Tools API
    """
    try:
        # Verify text claim
        result = await text_fact_checker.verify(
            text_input=text_input,
            claim_context=claim_context,
            claim_date=claim_date
        )
        
        return result
            
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

async def _extract_media_from_url(url: str) -> Optional[Dict[str, Any]]:
    """
    Use yt-dlp to extract media from a URL and determine if it's an image or video.
    
    Returns:
        Dict with "type" ("image" or "video") and "path" (local file path), or None if fails
    """
    try:
        from shutil import which
        import subprocess
        import tempfile
        
        # Resolve yt-dlp binary
        ytdlp_bin = config.YTDLP_BIN or "yt-dlp"
        found = which(ytdlp_bin) or which("yt-dlp")
        if not found:
            print("[extract_media] yt-dlp not found")
            return None
        
        # Create temp directory
        temp_dir = tempfile.mkdtemp(prefix="media_extract_")
        
        # First, get info about the media
        info_cmd = [found, url, "--dump-json", "--no-playlist"]
        result = subprocess.run(
            info_cmd,
            capture_output=True,
            text=True,
            timeout=30
        )
        
        if result.returncode != 0:
            print(f"[extract_media] yt-dlp info failed: {result.stderr}")
            return None
        
        info = json.loads(result.stdout)
        
        # Determine media type
        ext = info.get("ext", "").lower()
        is_video = ext in ["mp4", "webm", "mkv", "avi", "mov", "flv", "m4v"]
        is_image = ext in ["jpg", "jpeg", "png", "gif", "webp", "bmp"]
        
        if not is_video and not is_image:
            # Check formats to determine type
            formats = info.get("formats", [])
            has_video_codec = any(f.get("vcodec") != "none" for f in formats)
            has_audio_codec = any(f.get("acodec") != "none" for f in formats)
            
            if has_video_codec:
                is_video = True
            elif not has_audio_codec and not has_video_codec:
                # Likely an image
                is_image = True
        
        media_type = "video" if is_video else "image"
        
        # Download the media
        output_template = os.path.join(temp_dir, f"media.%(ext)s")
        download_cmd = [
            found,
            url,
            "-o", output_template,
            "--no-playlist",
        ]
        
        # For images, prefer best quality; for videos, get best format
        if is_image:
            download_cmd.extend(["--format", "best"])
        else:
            download_cmd.extend(["--format", "best[ext=mp4]/best"])
        
        result = subprocess.run(
            download_cmd,
            capture_output=True,
            text=True,
            timeout=60
        )
        
        if result.returncode != 0:
            print(f"[extract_media] yt-dlp download failed: {result.stderr}")
            return None
        
        # Find the downloaded file
        downloaded_files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))]
        if not downloaded_files:
            print("[extract_media] No file downloaded")
            return None
        
        media_path = os.path.join(temp_dir, downloaded_files[0])
        
        return {
            "type": media_type,
            "path": media_path,
            "temp_dir": temp_dir  # Keep for cleanup
        }
        
    except Exception as e:
        print(f"[extract_media] Error: {e}")
        import traceback
        print(traceback.format_exc())
        return None


def _is_youtube_url(url: str) -> bool:
    """Check if URL is a YouTube URL"""
    url_lower = url.lower()
    youtube_domains = ['youtube.com', 'youtu.be', 'www.youtube.com', 'www.youtu.be', 'm.youtube.com']
    return any(domain in url_lower for domain in youtube_domains)


async def _generate_claims_summary(claim_results: List[Dict[str, Any]], gemini_model) -> str:
    """Generate a comprehensive summary of all claim verification results using Gemini"""
    try:
        # Prepare claims data for Gemini
        claims_data = []
        for i, result in enumerate(claim_results, 1):
            claims_data.append({
                "number": i,
                "claim": result.get("claim_text", ""),
                "verdict": result.get("verdict", "uncertain"),
                "explanation": result.get("message", "No explanation available")
            })
        
        prompt = f"""You are a fact-checking summary writer. Based on the following verified claims from a YouTube video, create a comprehensive, user-friendly summary.

CLAIM VERIFICATION RESULTS:
{json.dumps(claims_data, indent=2)}

Your task is to create a clear, concise summary that:
1. Lists each claim with its verdict (TRUE/FALSE/MIXED/UNCERTAIN)
2. Explains WHY each claim is true or false in simple terms
3. Highlights the most important findings
4. Provides an overall assessment of the video's factual accuracy

Format your response as a well-structured summary that is easy to read. Use clear sections and bullet points where appropriate.

IMPORTANT: 
- Be concise but thorough
- Explain the reasoning for each verdict
- Focus on the most significant false or misleading claims
- Keep the tone professional and informative
- Do NOT use markdown formatting, just plain text with clear structure

Return ONLY the summary text, no JSON or code blocks."""
        
        response = gemini_model.generate_content(prompt)
        response_text = response.text.strip()
        
        # Clean up response if needed
        if response_text.startswith('```'):
            response_text = re.sub(r'^```[a-z]*\n?', '', response_text, flags=re.IGNORECASE)
            response_text = re.sub(r'```$', '', response_text, flags=re.IGNORECASE).strip()
        
        print(f"βœ… Generated comprehensive summary")
        return response_text
        
    except Exception as e:
        print(f"❌ Error generating summary with Gemini: {e}")
        import traceback
        print(traceback.format_exc())
        # Fallback to simple concatenation
        summary_parts = []
        summary_parts.append(f"Analyzed {len(claim_results)} controversial claim(s) from the video transcript:\n")
        
        for i, result in enumerate(claim_results, 1):
            claim_text = result.get("claim_text", "")
            verdict = result.get("verdict", "uncertain")
            message = result.get("message", "No explanation available")
            
            claim_display = claim_text[:150] + "..." if len(claim_text) > 150 else claim_text
            
            verdict_label = {
                "true": "βœ… TRUE",
                "false": "❌ FALSE",
                "mixed": "⚠️ MIXED",
                "uncertain": "❓ UNCERTAIN",
                "error": "⚠️ ERROR"
            }.get(verdict, "❓ UNCERTAIN")
            
            summary_parts.append(f"\n{i}. {verdict_label}: {claim_display}")
            summary_parts.append(f"   Explanation: {message}")
        
        return "\n".join(summary_parts)


async def _extract_claims_from_captions(captions: str, gemini_model) -> List[str]:
    """Extract top 5 controversial claims from video captions using Gemini"""
    try:
        prompt = f"""You are a fact-checking assistant. Analyze the following video transcript and extract the TOP 5 MOST CONTROVERSIAL and verifiable claims that were mentioned in the video.

VIDEO TRANSCRIPT:
{captions}

Your task is to identify the 5 MOST controversial, factual claims that can be verified. Prioritize:
- Claims about events, statistics, or facts that are controversial or disputed
- Claims about people, organizations, or institutions that are potentially misleading
- Claims that are specific enough to be fact-checked and are likely to be false or disputed
- Claims that have significant impact or are widely discussed

Ignore:
- General opinions or subjective statements
- Questions or hypothetical scenarios
- Vague statements without specific claims
- Small talk or filler content

IMPORTANT: Return EXACTLY 5 claims (or fewer if the video doesn't contain 5 verifiable controversial claims). Rank them by controversy/importance.

Return ONLY a JSON object in this exact format:
{{
    "claims": [
        "Claim 1 text here (most controversial)",
        "Claim 2 text here",
        "Claim 3 text here",
        "Claim 4 text here",
        "Claim 5 text here"
    ]
}}

Return ONLY the JSON object, no other text or explanation."""
        
        response = gemini_model.generate_content(prompt)
        response_text = response.text.strip()
        
        # Clean up response if needed
        if response_text.startswith('```json'):
            response_text = response_text.replace('```json', '').replace('```', '').strip()
        elif response_text.startswith('```'):
            response_text = response_text.replace('```', '').strip()
        
        # Parse JSON response
        parsed = json.loads(response_text)
        claims = parsed.get("claims", [])
        
        # Filter out empty claims and limit to 5
        claims = [c.strip() for c in claims if c and c.strip()][:5]
        
        print(f"βœ… Extracted {len(claims)} claims from video captions")
        return claims
        
    except Exception as e:
        print(f"❌ Error extracting claims from captions: {e}")
        import traceback
        print(traceback.format_exc())
        return []


async def _verify_youtube_video(url: str, claim_context: str, claim_date: str) -> Dict[str, Any]:
    """Verify a YouTube video by extracting captions, extracting claims, and verifying each claim"""
    import tempfile
    import asyncio
    
    try:
        print(f"πŸŽ₯ Starting YouTube video verification for: {url}")
        
        # Step 1: Extract captions
        print(f"πŸ“ Extracting captions from YouTube video...")
        # Create a temporary file for the transcript output
        with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as temp_file:
            temp_output_file = temp_file.name
        
        # Run the synchronous function in an executor to avoid blocking
        loop = asyncio.get_event_loop()
        captions = await loop.run_in_executor(
            None, 
            get_youtube_transcript_ytdlp, 
            url, 
            temp_output_file
        )
        
        # Clean up the temporary output file if it was created
        try:
            if os.path.exists(temp_output_file):
                os.unlink(temp_output_file)
        except Exception as cleanup_error:
            print(f"⚠️ Warning: Could not clean up temp file {temp_output_file}: {cleanup_error}")
        
        if not captions:
            return {
                "verified": False,
                "verdict": "error",
                "message": "Could not extract captions from the YouTube video. The video may not have captions available.",
                "details": {
                    "video_url": url,
                    "error": "Caption extraction failed"
                },
                "source": "youtube_url"
            }
        
        print(f"βœ… Extracted {len(captions)} characters of captions")
        
        # Step 2: Extract claims using Gemini
        print(f"πŸ” Extracting controversial claims from captions...")
        genai.configure(api_key=config.GEMINI_API_KEY)
        gemini_model = genai.GenerativeModel(config.GEMINI_MODEL)
        
        claims = await _extract_claims_from_captions(captions, gemini_model)
        
        if not claims:
            return {
                "verified": False,
                "verdict": "uncertain",
                "message": "No verifiable claims were found in the video transcript. The video may contain only opinions, questions, or non-factual content.",
                "details": {
                    "video_url": url,
                    "captions_length": len(captions),
                    "claims_extracted": 0
                },
                "source": "youtube_url"
            }
        
        print(f"βœ… Extracted {len(claims)} claims, starting verification...")
        
        # Step 3: Verify each claim
        claim_results = []
        for i, claim in enumerate(claims, 1):
            print(f"πŸ” Verifying claim {i}/{len(claims)}: {claim[:100]}...")
            try:
                verification_result = await text_fact_checker.verify(
                    text_input=claim,
                    claim_context=f"Claim from YouTube video: {url}",
                    claim_date=claim_date
                )
                verification_result["claim_text"] = claim
                verification_result["claim_index"] = i
                claim_results.append(verification_result)
            except Exception as e:
                print(f"❌ Error verifying claim {i}: {e}")
                claim_results.append({
                    "claim_text": claim,
                    "claim_index": i,
                    "verified": False,
                    "verdict": "error",
                    "message": f"Error during verification: {str(e)}"
                })
        
        # Step 4: Combine results
        print(f"πŸ“Š Combining {len(claim_results)} claim verification results...")
        
        # Aggregate verdicts
        verdicts = [r.get("verdict", "uncertain") for r in claim_results]
        true_count = verdicts.count("true")
        false_count = verdicts.count("false")
        uncertain_count = verdicts.count("uncertain")
        mixed_count = verdicts.count("mixed")
        error_count = verdicts.count("error")
        
        # Determine overall verdict
        if false_count > 0:
            overall_verdict = "false"
            verified = False
        elif true_count > 0 and false_count == 0:
            overall_verdict = "true"
            verified = True
        elif mixed_count > 0:
            overall_verdict = "mixed"
            verified = False
        elif uncertain_count > 0:
            overall_verdict = "uncertain"
            verified = False
        else:
            overall_verdict = "error"
            verified = False
        
        # Step 5: Generate comprehensive summary using Gemini
        print(f"πŸ“ Generating comprehensive summary with Gemini...")
        combined_message = await _generate_claims_summary(claim_results, gemini_model)
        
        return {
            "verified": verified,
            "verdict": overall_verdict,
            "message": combined_message,
            "details": {
                "video_url": url,
                "captions_length": len(captions),
                "total_claims": len(claims),
                "claims_verified": true_count,
                "claims_false": false_count,
                "claims_mixed": mixed_count,
                "claims_uncertain": uncertain_count,
                "claims_error": error_count,
                "claim_results": claim_results
            },
            "source": "youtube_url"
        }
        
    except Exception as e:
        print(f"❌ Error verifying YouTube video: {e}")
        import traceback
        print(traceback.format_exc())
        return {
            "verified": False,
            "verdict": "error",
            "message": f"Error processing YouTube video: {str(e)}",
            "details": {
                "video_url": url,
                "error": str(e)
            },
            "source": "youtube_url"
        }


@app.post("/chatbot/verify")
async def chatbot_verify(
    request: Request,
    text_input: Optional[str] = Form(None),
    files: Optional[List[UploadFile]] = File(None),
    anonymous_id: Optional[str] = Form(None),
    user_id: Optional[str] = Form(None),
):
    """
    Chatbot-friendly endpoint that intelligently processes input and routes to appropriate verification
    """
    try:
        print(f"πŸ” DEBUG: Chatbot verify endpoint called")
        print(f"πŸ” DEBUG: text_input = {text_input}")
        print(f"πŸ” DEBUG: files = {files}")
        print(f"πŸ” DEBUG: files type = {type(files)}")
        print(f"πŸ” DEBUG: anonymous_id = {anonymous_id}")
        print(f"πŸ” DEBUG: user_id = {user_id}")

        # Determine logical user key and tier for rate limiting
        user_doc = None
        raw_tier = "Free"
        if user_id and mongodb_service:
            try:
                user_doc = mongodb_service.get_user_by_id(user_id)
            except Exception as e:
                logger.warning(
                    f"⚠️ Failed to load user {user_id} for tier resolution: {e}"
                )

        if user_doc:
            raw_tier = user_doc.get("subscription_tier") or "Free"
        else:
            raw_tier = "Free"

        limits = get_tier_limits(raw_tier)
        key_host = getattr(request.client, "host", "unknown")
        key = user_id or anonymous_id or f"ip:{key_host}"

        if mongodb_service:
            usage_info = mongodb_service.increment_usage_and_check_limits(
                key=key,
                feature="verification",
                daily_limit=limits.get("daily_verifications"),
                monthly_limit=limits.get("monthly_verifications"),
            )
        else:
            usage_info = {
                "allowed": True,
                "tier_limits": {
                    "daily": limits.get("daily_verifications"),
                    "monthly": limits.get("monthly_verifications"),
                },
            }

        if not usage_info.get("allowed", True):
            normalized_tier = get_normalized_tier(raw_tier)
            return JSONResponse(
                status_code=429,
                content={
                    "error": "verification_limit_reached",
                    "tier": normalized_tier,
                    "key": key,
                    "limits": usage_info.get("tier_limits"),
                    "usage": {
                        "daily": usage_info.get("daily"),
                        "monthly": usage_info.get("monthly"),
                    },
                },
            )
        received_files_meta: List[Dict[str, Any]] = []
        if files:
            for i, file in enumerate(files):
                print(f"πŸ” DEBUG: File {i}: filename={file.filename}, content_type={file.content_type}, size={file.size}")
                try:
                    received_files_meta.append({
                        "filename": getattr(file, "filename", None),
                        "content_type": getattr(file, "content_type", None),
                        "size": getattr(file, "size", None)
                    })
                except Exception:
                    received_files_meta.append({
                        "filename": getattr(file, "filename", None),
                        "content_type": getattr(file, "content_type", None),
                        "size": None
                    })
        
        # Process input with LLM
        print(f"πŸ” DEBUG: Calling input_processor.process_input()")
        processed_input = await input_processor.process_input(
            text_input=text_input,
            files=files
        )
        print(f"πŸ” DEBUG: processed_input = {processed_input}")
        
        if "error" in processed_input:
            print(f"❌ DEBUG: Error in processed_input: {processed_input['error']}")
            return {"error": processed_input["error"]}
        
        verification_type = processed_input["verification_type"]
        content = processed_input["content"]
        claim_context = processed_input["claim_context"]
        claim_date = processed_input["claim_date"]
        
        print(f"πŸ” DEBUG: verification_type = {verification_type}")
        print(f"πŸ” DEBUG: content = {content}")
        print(f"πŸ” DEBUG: claim_context = {claim_context}")
        print(f"πŸ” DEBUG: claim_date = {claim_date}")
        
        results = []
        temp_files_to_cleanup = []
        
        # Handle text-only verification
        if verification_type == "text" and content.get("text"):
            print(f"πŸ” DEBUG: Processing text verification with text: {content['text']}")
            result = await text_fact_checker.verify(
                text_input=content["text"],
                claim_context=claim_context,
                claim_date=claim_date
            )
            result["source"] = "text_input"
            results.append(result)
            print(f"πŸ” DEBUG: Text verification result: {result}")
        
        # Process files if any
        files_list = content.get("files", [])
        print(f"πŸ” DEBUG: Processing {len(files_list)} files")
        input_processor_for_audio = input_processor
        for i, file_path in enumerate(files_list):
            print(f"πŸ” DEBUG: Processing file {i}: {file_path}")
            temp_files_to_cleanup.append(file_path)
            
            if verification_type == "image":
                print(f"πŸ” DEBUG: Calling image_verifier.verify for file")
                result = await image_verifier.verify(
                    image_path=file_path,
                    claim_context=claim_context,
                    claim_date=claim_date
                )
            elif verification_type == "audio":
                print(f"πŸ” DEBUG: Calling detect_audio_deepfake for file (AUDIO)")
                deepfake = detect_audio_deepfake(file_path)
                # Use Gemini to frame a verdict
                try:
                    gemini_prompt = f"""
You are an assistant for audio authenticity analysis.
{('User question: ' + claim_context) if claim_context else ''}
The audio has been analyzed and the result is: {'deepfake' if deepfake else 'NOT deepfake'}.
Compose a clear, friendly, 1-2 line summary verdict for the user, tailored to the above context/result (do not answer with JSON or code, just a natural response).
Avoid repeating 'deepfake detection' technical language; be concise and direct.
Do NOT mention file names or file paths in your response.
"""
                    gemini_response = input_processor_for_audio.model.generate_content(gemini_prompt)
                    ai_message = None
                    if gemini_response and hasattr(gemini_response, 'text') and gemini_response.text:
                        response_text = gemini_response.text.strip()
                        # Case 1: JSON block
                        if response_text.startswith('{') or response_text.startswith('```json'):
                            rt = response_text.strip('` ')
                            # Remove leading/trailing markdown code block marks
                            rt = re.sub(r'^```json', '', rt, flags=re.I).strip()
                            rt = re.sub(r'^```', '', rt, flags=re.I).strip()
                            rt = re.sub(r'```$', '', rt, flags=re.I).strip()
                            try:
                                import json
                                json_obj = json.loads(rt)
                                ai_message = json_obj.get('message') or ''
                                if not ai_message and 'verdict' in json_obj:
                                    # fallback: concat verdict + any explanation
                                    ai_message = f"Verdict: {json_obj['verdict']}" + (f". {json_obj.get('reasoning','')}" if json_obj.get('reasoning') else '')
                            except Exception as excjson:
                                print(f"[audio Gemini JSON extract fail] {type(excjson).__name__}: {excjson}")
                                # Fallback to the text itself
                                ai_message = response_text
                        else:
                            ai_message = response_text
                except Exception as exc:
                    print(f"[gemini audio summary error] {type(exc).__name__}: {exc}")
                    ai_message = None
                if not ai_message:
                    ai_message = (
                        "This audio is likely AI-generated." if deepfake else "This audio appears authentic and human." )
                result = {
                    "verified": not deepfake,
                    "is_deepfake": deepfake,
                    "file": file_path,
                    "message": ai_message,
                    "source": "uploaded_file"
                }
            else:  # video
                print(f"πŸ” DEBUG: Calling video_verifier.verify for file")
                result = await video_verifier.verify(
                    video_path=file_path,
                    claim_context=claim_context,
                    claim_date=claim_date
                )
            
            result["source"] = "uploaded_file"
            results.append(result)
            print(f"πŸ” DEBUG: File verification result: {result}")
        
        # Process URLs if any
        urls_list = content.get("urls", [])
        print(f"πŸ” DEBUG: Processing {len(urls_list)} URLs")
        for i, url in enumerate(urls_list):
            print(f"πŸ” DEBUG: Processing URL {i}: {url}")
            
            # STEP 0: Check if this is a YouTube URL - handle specially
            if _is_youtube_url(url):
                print(f"πŸŽ₯ DEBUG: Detected YouTube URL, using caption-based verification: {url}")
                try:
                    result = await _verify_youtube_video(url, claim_context, claim_date)
                    results.append(result)
                    print(f"πŸ” DEBUG: YouTube verification result: {result}")
                    continue  # Skip the rest of the URL processing
                except Exception as e:
                    print(f"❌ DEBUG: YouTube verification failed: {e}")
                    import traceback
                    print(traceback.format_exc())
                    # Fall through to regular video processing as fallback
            
            # STEP 1: For social media URLs, use yt-dlp to fetch the actual media first
            # This determines the REAL media type, not just what the LLM guessed
            url_lower = url.lower()
            is_social_media = any(domain in url_lower for domain in [
                'twitter.com', 'x.com', 'instagram.com', 'tiktok.com', 
                'facebook.com', 'youtube.com', 'youtu.be'
            ])
            
            extracted_media = None
            if is_social_media:
                print(f"πŸ” DEBUG: Detected social media URL, extracting media with yt-dlp: {url}")
                try:
                    # Use yt-dlp to extract media and determine actual type
                    extracted_media = await _extract_media_from_url(url)
                    if extracted_media:
                        actual_type = extracted_media.get("type")  # "image" or "video"
                        media_path = extracted_media.get("path")
                        temp_dir = extracted_media.get("temp_dir")
                        
                        print(f"πŸ” DEBUG: yt-dlp extracted {actual_type} from URL: {media_path}")
                        
                        # Route based on ACTUAL media type, not LLM's guess
                        if actual_type == "image":
                            result = await image_verifier.verify(
                                image_path=media_path,
                                claim_context=claim_context,
                                claim_date=claim_date
                            )
                        else:  # video
                            result = await video_verifier.verify(
                                video_path=media_path,
                                claim_context=claim_context,
                                claim_date=claim_date
                            )
                        
                        result["source"] = "url"
                        results.append(result)
                        
                        # Add to cleanup list
                        if media_path:
                            temp_files_to_cleanup.append(media_path)
                        if temp_dir:
                            temp_files_to_cleanup.append(temp_dir)
                        
                        continue  # Skip the old routing logic below
                    else:
                        print(f"⚠️ DEBUG: yt-dlp extraction returned None, falling back to direct URL")
                except Exception as e:
                    print(f"⚠️ DEBUG: Failed to extract media from URL with yt-dlp: {e}, falling back to direct URL")
                    import traceback
                    print(traceback.format_exc())
                    # Fall through to old logic
            
            # STEP 2: Fallback to old routing (for direct image/video URLs or if yt-dlp fails)
            if verification_type == "image":
                print(f"πŸ” DEBUG: Calling image_verifier.verify for URL")
                result = await image_verifier.verify(
                    image_url=url,
                    claim_context=claim_context,
                    claim_date=claim_date
                )
            else:  # video
                print(f"πŸ” DEBUG: Calling video_verifier.verify for URL")
                result = await video_verifier.verify(
                    video_url=url,
                    claim_context=claim_context,
                    claim_date=claim_date
                )
            
            result["source"] = "url"
            results.append(result)
            print(f"πŸ” DEBUG: URL verification result: {result}")
        
        # Clean up temp files
        if temp_files_to_cleanup:
            input_processor.cleanup_temp_files(temp_files_to_cleanup)
        
        print(f"πŸ” DEBUG: Total results collected: {len(results)}")
        for i, result in enumerate(results):
            print(f"πŸ” DEBUG: Result {i}: {result}")
        
        # Aggregate verdict before using anywhere
        overall = _aggregate_verdicts(results)

        # Collect message/summary fields
        candidates = []
        for r in results:
            msg = (r.get("message") or r.get("summary") or "").strip()
            if msg:
                candidates.append(msg)
        best_msg = max(candidates, key=len, default="")

        # --- REFINE OUTPUT ---
        # For audio, force clear user-facing message
        verdict_is_audio = verification_type == "audio"
        if verdict_is_audio and results:
            # For batch, show the message(s) generated by Gemini/LLM for each result, joined with spacing.
            audio_msgs = [x["message"] for x in results if "message" in x and x["message"]]
            final_message = "\n\n".join(audio_msgs)
        else:
            # Final message extraction for ALL types: if best_msg is a raw JSON or code block, try extracting the `message` field.
            if not verdict_is_audio:
                raw_final = (best_msg or "").strip()
                nonjson = bool(raw_final) and not (raw_final.startswith('{') or raw_final.startswith('```'))
                extracted_message = raw_final
                if not nonjson:
                    rt = raw_final.strip('` \n')
                    rt = re.sub(r'^```json', '', rt, flags=re.I).strip()
                    rt = re.sub(r'^```', '', rt, flags=re.I).strip()
                    rt = re.sub(r'```$', '', rt, flags=re.I).strip()
                    try:
                        import json
                        json_obj = json.loads(rt)
                        extracted_message = json_obj.get('message') or ''
                        if not extracted_message and 'verdict' in json_obj:
                            extracted_message = f"Verdict: {json_obj['verdict']}" + (f". {json_obj.get('reasoning','')}" if json_obj.get('reasoning') else '')
                    except Exception as excjson:
                        print(f"[text gemini JSON extract fail] {type(excjson).__name__}: {excjson}")
                        extracted_message = raw_final
                final_message = extracted_message
                # Remove typical claim verdict phrases from start if present
                verdict_prefixes = [
                    "this claim is true:", "this claim is false:", "this claim is uncertain:", "this claim has mixed evidence:", "the claim is true:", "the claim is false:", "the claim is uncertain:", "result:",
                ]
                for prefix in verdict_prefixes:
                    if final_message.strip().lower().startswith(prefix):
                        final_message = final_message.strip()[len(prefix):].strip()
                        break
                # For stray audio check message from earlier code
                if final_message.strip().startswith("Audio deepfake detection completed"):
                    # Should not leak this to user; use generic fallback
                    final_message = "Audio deepfake detection was performed."
            else:
                final_message = (best_msg or "")
        print(f"πŸ” DEBUG: Final message: {final_message}")
        print(f"πŸ” DEBUG: Final verdict: {overall}")
        
        response = {
            "message": final_message,
            "verdict": overall,
            "details": {
                "results": results,
                "verification_type": verification_type,
                "claim_context": claim_context,
                "claim_date": claim_date,
                "received_files_count": len(received_files_meta),
                "received_files": received_files_meta
            }
        }
        
        print(f"πŸ” DEBUG: Final response: {response}")
        return response
            
    except Exception as e:
        print(f"❌ DEBUG: Exception in chatbot_verify: {e}")
        print(f"❌ DEBUG: Exception type: {type(e).__name__}")
        import traceback
        print(f"❌ DEBUG: Traceback: {traceback.format_exc()}")
        # Clean up any temp files on error
        if 'temp_files_to_cleanup' in locals():
            input_processor.cleanup_temp_files(temp_files_to_cleanup)
        raise HTTPException(status_code=500, detail=str(e))

def _aggregate_verdicts(results: List[Dict]) -> str:
    """Aggregate individual verification results into overall verdict.

    Supports image results (with 'verdict'), video results (with details.overall_verdict), 
    and text results (with 'verdict').
    """
    if not results:
        return "no_content"
    
    normalized: List[str] = []
    for r in results:
        # Prefer explicit boolean 'verified' if present
        if "verified" in r and isinstance(r.get("verified"), bool):
            v = "true" if r.get("verified") else "false"
        else:
            v = r.get("verdict")
        if not v:
            details = r.get("details") or {}
            v = details.get("overall_verdict")
        normalized.append((v or "unknown").lower())
    
    # If any false, overall is false
    if "false" in normalized:
        return "false"
    
    # If any uncertain, overall is uncertain
    if "uncertain" in normalized:
        return "uncertain"
    
    # If all true, overall is true
    if all(v == "true" for v in normalized):
        return "true"
    
    return "mixed"

@app.get("/mongodb/recent-posts")
async def get_recent_debunk_posts(limit: int = 5):
    """
    Get recent debunk posts from MongoDB
    
    Args:
        limit: Maximum number of posts to return (default: 5)
        
    Returns:
        List of recent debunk posts
    """
    try:
        print(f"πŸ” DEBUG: Endpoint called with limit={limit}")
        print(f"πŸ” DEBUG: MongoDB service available: {mongodb_service is not None}")
        
        if not mongodb_service:
            print("❌ DEBUG: MongoDB service is None!")
            raise HTTPException(
                status_code=503, 
                detail="MongoDB service is not available. Check MONGO_CONNECTION_STRING environment variable."
            )
        
        print("πŸ” DEBUG: Calling mongodb_service.get_recent_posts()")
        posts = mongodb_service.get_recent_posts(limit)
        print(f"πŸ” DEBUG: Service returned {len(posts)} posts")
        
        if posts:
            print(f"πŸ” DEBUG: First post keys: {list(posts[0].keys())}")
            print(f"πŸ” DEBUG: First post _id: {posts[0].get('_id')}")
        else:
            print("⚠️ DEBUG: No posts returned from service")
        
        result = {
            "success": True,
            "count": len(posts),
            "posts": posts
        }
        
        print(f"πŸ” DEBUG: Returning result with {len(posts)} posts")
        return result
        
    except Exception as e:
        print(f"❌ DEBUG: Exception in endpoint: {e}")
        print(f"πŸ” DEBUG: Exception type: {type(e).__name__}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/mongodb/search-similar")
async def search_similar_rumours(
    query: str,
    similarity_threshold: float = 0.6,
    limit: int = 5
):
    """
    Search for rumours similar to the query text
    
    Args:
        query: Search query text
        similarity_threshold: Minimum similarity score (0.0 to 1.0, default: 0.6)
        limit: Maximum number of results to return (default: 5)
        
    Returns:
        List of similar rumours with similarity scores
    """
    try:
        if not mongodb_service:
            raise HTTPException(
                status_code=503,
                detail="MongoDB service is not available. Check MONGO_CONNECTION_STRING environment variable."
            )
        
        if not query or not query.strip():
            return {
                "success": True,
                "count": 0,
                "results": []
            }
        
        # Validate threshold
        similarity_threshold = max(0.0, min(1.0, similarity_threshold))
        
        results = mongodb_service.search_similar_rumours(
            query=query,
            similarity_threshold=similarity_threshold,
            limit=limit
        )
        
        return {
            "success": True,
            "count": len(results),
            "query": query,
            "similarity_threshold": similarity_threshold,
            "results": results
        }
        
    except Exception as e:
        logger.error(f"❌ Error searching similar rumours: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/health")
async def health_check():
    return {"status": "healthy", "service": "visual-verification"}


@app.post("/speech-to-text")
async def speech_to_text(
    audio: UploadFile = File(...),
    language_code: str = Form("en-US")
):
    """Proxy uploaded audio to Google Speech-to-Text and return transcript.

    Accepts WEBM/OPUS, OGG/OPUS, or WAV/LINEAR16. For browser recordings via
    MediaRecorder the typical format is WEBM/OPUS which is supported by Google.
    """
    try:
        if not config.GOOGLE_API_KEY:
            raise HTTPException(status_code=500, detail="GOOGLE_API_KEY not configured")

        # Read audio bytes and base64-encode for Google API
        audio_bytes = await audio.read()
        if not audio_bytes:
            raise HTTPException(status_code=400, detail="Empty audio payload")

        audio_b64 = base64.b64encode(audio_bytes).decode("utf-8")

        # Infer encoding for common browser uploads; default to WEBM_OPUS if unknown
        content_type = (audio.content_type or "").lower()
        if "wav" in content_type or "x-wav" in content_type or "linear16" in content_type:
            encoding = "LINEAR16"
        elif "ogg" in content_type:
            encoding = "OGG_OPUS"
        else:
            encoding = "WEBM_OPUS"

        # Build request to Google Speech-to-Text v1 REST API
        endpoint = f"https://speech.googleapis.com/v1/speech:recognize?key={config.GOOGLE_API_KEY}"
        payload = {
            "config": {
                "encoding": encoding,
                "languageCode": language_code,
                # Enable auto punctuation; leave other options default to keep generalized
                "enableAutomaticPunctuation": True
            },
            "audio": {"content": audio_b64}
        }

        resp = requests.post(endpoint, json=payload, timeout=30)
        if resp.status_code != 200:
            detail = resp.text
            raise HTTPException(status_code=resp.status_code, detail=detail)

        data = resp.json()
        # Extract the best transcript
        transcript = ""
        if isinstance(data, dict):
            results = data.get("results") or []
            if results:
                alts = results[0].get("alternatives") or []
                if alts:
                    transcript = (alts[0].get("transcript") or "").strip()

        return {"transcript": transcript, "raw": data}

    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# Educational Content API Endpoints - Now fetching from MongoDB weekly_posts
@app.get("/educational/modules")
async def get_educational_modules():
    """Get list of available educational modules from MongoDB weekly_posts"""
    try:
        if not mongodb_service:
            raise HTTPException(status_code=503, detail="MongoDB service not available")
        
        modules_list = mongodb_service.get_educational_modules_list()
        response_data = {
            "modules": modules_list,
            "total": len(modules_list)
        }
        # Return with no-cache headers to prevent stale cache in production
        return JSONResponse(
            content=response_data,
            headers={
                "Cache-Control": "no-cache, no-store, must-revalidate, max-age=0",
                "Pragma": "no-cache",
                "Expires": "0"
            }
        )
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Failed to get educational modules: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/educational/modules/{module_id}")
async def get_module_content(
    module_id: str,
    difficulty_level: str = "beginner"  # Kept for backward compatibility but not used
):
    """Get educational content for a specific module from MongoDB weekly_posts"""
    try:
        if not mongodb_service:
            raise HTTPException(status_code=503, detail="MongoDB service not available")
        
        content = mongodb_service.get_educational_module_by_id(module_id)
        if not content:
            raise HTTPException(status_code=404, detail=f"Module '{module_id}' not found")
        
        # Return with no-cache headers to prevent stale cache in production
        return JSONResponse(
            content=content,
            headers={
                "Cache-Control": "no-cache, no-store, must-revalidate, max-age=0",
                "Pragma": "no-cache",
                "Expires": "0"
            }
        )
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Failed to get module content: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/educational/contextual-learning")
async def get_contextual_learning(verification_result: Dict[str, Any]):
    """Generate educational content based on verification result"""
    try:
        content = await educational_generator.generate_contextual_learning(
            verification_result
        )
        return content
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/educational/clear-cache")
async def clear_educational_cache():
    """
    Clear all educational content from Redis cache.
    
    Note: The /educational/modules endpoints now use no-cache headers
    to prevent browser/CDN caching. This endpoint is mainly for clearing
    any legacy Redis cache entries.
    """
    try:
        if educational_generator.redis_client:
            # Get all educational cache keys
            keys = educational_generator.redis_client.keys("educational:*")
            if keys:
                educational_generator.redis_client.delete(*keys)
                return {
                    "message": f"Cleared {len(keys)} cache entries",
                    "keys": keys,
                    "note": "Educational endpoints use no-cache headers to prevent stale data"
                }
            else:
                return {"message": "No cache entries found"}
        else:
            return {"message": "Redis not available"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/educational/cache-status")
async def get_cache_status():
    """Get status of educational content cache"""
    try:
        if educational_generator.redis_client:
            keys = educational_generator.redis_client.keys("educational:*")
            cache_info = {}
            for key in keys:
                ttl = educational_generator.redis_client.ttl(key)
                cache_info[key] = {
                    "ttl": ttl,
                    "exists": ttl > 0
                }
            return {
                "redis_connected": True,
                "total_keys": len(keys),
                "cache_info": cache_info
            }
        else:
            return {"redis_connected": False, "message": "Redis not available"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

from pydantic import BaseModel


# ---------- Auth endpoints (minimal implementation) ----------


class LoginRequest(BaseModel):
    email: str
    password: str

class SignupRequest(BaseModel):
    name: str
    email: str
    password: str
    phone_number: Optional[str] = None
    age: Optional[int] = None
    domain_preferences: Optional[List[str]] = []

class UserResponse(BaseModel):
    email: str
    id: Optional[str] = None

@app.post("/auth/signup")
async def signup(request: SignupRequest):
    """Sign up a new user"""
    if not mongodb_service:
        raise HTTPException(status_code=503, detail="MongoDB service not available")
    
    try:
        # Hash password (in production, use bcrypt or similar)
        import hashlib
        password_hash = hashlib.sha256(request.password.encode()).hexdigest()
        
        user_data = {
            "name": request.name,
            "email": request.email,
            "password": password_hash,
            "phone_number": request.phone_number,
            "age": request.age,
            "domain_preferences": request.domain_preferences or [],
            "created_at": None,  # Will be set by MongoDB service
            "updated_at": None,
        }
        
        user = mongodb_service.create_user(user_data)
        
        # Generate token (in production, use JWT)
        token = f"mock_token_{request.email}"
        
        return {
            "message": "User created successfully",
            "token": token,
            "user": {
                "name": user.get("name"),
                "email": user["email"],
                "id": user["id"],
                "phone_number": user.get("phone_number"),
                "age": user.get("age"),
                "domain_preferences": user.get("domain_preferences", [])
            }
        }
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except Exception as e:
        logger.error(f"Signup error: {e}")
        raise HTTPException(status_code=500, detail="Failed to create user")

@app.post("/auth/login")
async def login(request: LoginRequest):
    """Login user"""
    if not mongodb_service:
        raise HTTPException(status_code=503, detail="MongoDB service not available")
    
    try:
        user = mongodb_service.get_user_by_email(request.email)
        if not user:
            raise HTTPException(status_code=401, detail="Invalid email or password")
        
        # Verify password (in production, use bcrypt or similar)
        import hashlib
        password_hash = hashlib.sha256(request.password.encode()).hexdigest()
        
        if user["password"] != password_hash:
            raise HTTPException(status_code=401, detail="Invalid email or password")
        
        # Generate token (in production, use JWT)
        token = f"mock_token_{request.email}"
        
        return {
            "message": "Login successful",
            "token": token,
            "user": {
                "name": user.get("name"),
                "email": user["email"],
                "id": user["id"],
                "phone_number": user.get("phone_number"),
                "age": user.get("age"),
                "domain_preferences": user.get("domain_preferences", [])
            }
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Login error: {e}")
        raise HTTPException(status_code=500, detail="Failed to login")

@app.get("/auth/me")
async def get_current_user(request: Request):
    """Get current user (requires authentication in production)"""
    if not mongodb_service:
        raise HTTPException(status_code=503, detail="MongoDB service not available")
    
    # In production, verify JWT token from Authorization header
    auth_header = request.headers.get("Authorization")
    if not auth_header or not auth_header.startswith("Bearer "):
        raise HTTPException(status_code=401, detail="Not authenticated")
    
    token = auth_header.replace("Bearer ", "")
    
    # Extract email from token (in production, decode JWT)
    if not token.startswith("mock_token_"):
        raise HTTPException(status_code=401, detail="Invalid token")
    
    email = token.replace("mock_token_", "")
    
    try:
        user = mongodb_service.get_user_by_email(email)
        if not user:
            raise HTTPException(status_code=401, detail="User not found")
        
        # Get subscription tier from user document (preferred) or check subscription
        subscription_tier = user.get("subscription_tier", "Free")
        
        # If not in user doc, check active subscription
        if subscription_tier == "Free" and user.get("id"):
            subscription = mongodb_service.get_user_subscription(user_id=user["id"], status="active")
            if subscription:
                subscription_tier = subscription.get("plan_name", "Free")
                # Update user document with subscription tier
                mongodb_service.update_user_subscription_tier(user["id"], subscription_tier)
        
        return {
            "name": user.get("name"),
            "email": user["email"],
            "id": user["id"],
            "phone_number": user.get("phone_number"),
            "age": user.get("age"),
            "domain_preferences": user.get("domain_preferences", []),
            "subscription_tier": subscription_tier
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Get user error: {e}")
        raise HTTPException(status_code=500, detail="Failed to get user")


# ---------- Chat history endpoints ----------


class ChatSessionUpsert(BaseModel):
    session_id: Optional[str] = None
    title: Optional[str] = None
    user_id: Optional[str] = None
    anonymous_id: Optional[str] = None
    last_verdict: Optional[str] = None
    last_summary: Optional[str] = None


class ChatTurn(BaseModel):
    role: str
    content: str
    created_at: Optional[Any] = None  # Can be datetime, string, or None
    verdict: Optional[str] = None
    confidence: Optional[float] = None
    sources: Optional[Dict[str, Any]] = None
    attachments: Optional[List[Dict[str, Any]]] = None
    metadata: Optional[Dict[str, Any]] = None


class ChatMessagesAppend(BaseModel):
    session_id: str
    user_id: Optional[str] = None
    anonymous_id: Optional[str] = None
    messages: List[ChatTurn]


@app.get("/chat/sessions")
async def list_chat_sessions(
    user_id: Optional[str] = None,
    anonymous_id: Optional[str] = None,
):
    """Return chat sessions for logged-in users only.
    
    Anonymous users will receive an empty list since their sessions are not persisted.
    """
    try:
        if not mongodb_service:
            raise HTTPException(status_code=503, detail="MongoDB service not available")

        # Only return sessions for logged-in users
        if not user_id:
            logger.info(f"⏭️ No user_id provided, returning empty sessions list")
            return {"sessions": []}

        logger.info(f"πŸ” Loading chat sessions: user_id={user_id}")
        sessions = mongodb_service.get_chat_sessions(
            user_id=user_id,
            anonymous_id=None,  # Don't query by anonymous_id anymore
        )
        logger.info(f"βœ… Found {len(sessions)} chat sessions")
        return {"sessions": sessions}
    except Exception as e:
        logger.error(f"❌ Error loading chat sessions: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Failed to load chat sessions: {str(e)}")


@app.post("/chat/sessions")
async def upsert_chat_session(payload: ChatSessionUpsert):
    """Create or update a chat session.

    Only saves sessions for logged-in users (user_id required).
    Anonymous sessions are not persisted to MongoDB but a session_id is still returned for UI purposes.
    """
    try:
        if not mongodb_service:
            raise HTTPException(status_code=503, detail="MongoDB service not available")

        data = payload.dict(exclude_unset=True)
        user_id = data.get("user_id")
        anonymous_id = data.get("anonymous_id")
        
        # Only persist sessions for logged-in users
        if not user_id:
            # Still return a session_id for UI purposes, but don't persist
            import uuid
            session_id = data.get("session_id") or str(uuid.uuid4())
            logger.info(f"⏭️ Skipping session persistence for anonymous user (session_id={session_id})")
            return {
                "session_id": session_id,
                "title": data.get("title", "New Chat"),
                "user_id": None,
                "anonymous_id": anonymous_id,
                "created_at": None,
                "updated_at": None,
                "persisted": False,
            }

        logger.info(f"πŸ” Upserting chat session: {data}")

        # Optionally migrate anonymous history on first login
        if user_id and anonymous_id:
            try:
                migrated = mongodb_service.migrate_anonymous_sessions(
                    anonymous_id=anonymous_id, user_id=user_id
                )
                logger.info(f"βœ… Migrated {migrated} anonymous sessions to user {user_id}")
            except Exception as exc:
                logger.error(f"Failed to migrate anonymous sessions: {exc}")

        session_doc = mongodb_service.upsert_chat_session(data)
        logger.info(f"βœ… Created/updated session: {session_doc.get('session_id')}")
        return session_doc
    except Exception as e:
        logger.error(f"❌ Error upserting chat session: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=f"Failed to create/update chat session: {str(e)}")


@app.get("/chat/messages/{session_id}")
async def get_chat_messages(session_id: str):
    """Return all messages for a given chat session."""
    if not mongodb_service:
        raise HTTPException(status_code=503, detail="MongoDB service not available")

    messages = mongodb_service.get_chat_messages(session_id=session_id)
    return {"session_id": session_id, "messages": messages}


@app.post("/chat/messages")
async def append_chat_messages(payload: ChatMessagesAppend):
    """Append one or more messages to a chat session.
    
    Only saves messages for logged-in users (user_id required).
    Anonymous messages are not persisted to MongoDB.
    """
    if not mongodb_service:
        raise HTTPException(status_code=503, detail="MongoDB service not available")

    data = payload.dict()
    user_id = data.get("user_id")
    
    # Only persist messages for logged-in users
    if not user_id:
        logger.info(f"⏭️ Skipping message persistence for anonymous user (session_id={data['session_id']})")
        return {"inserted": 0, "message": "Messages not persisted for anonymous users"}

    inserted = mongodb_service.append_chat_messages(
        session_id=data["session_id"],
        messages=[m for m in data["messages"]],
        user_id=user_id,
        anonymous_id=data.get("anonymous_id"),
    )
    logger.info(f"βœ… Persisted {inserted} messages for user {user_id}")
    return {"inserted": inserted}


# ---------- Subscription endpoints ----------


class CreatePlanRequest(BaseModel):
    name: str
    amount: int  # Amount in paise (smallest currency unit)
    currency: str = "INR"
    interval: int = 1
    period: str = "monthly"  # daily, weekly, monthly, yearly
    description: Optional[str] = None


class CreateSubscriptionRequest(BaseModel):
    plan_id: str
    user_id: str
    customer_notify: int = 1
    total_count: Optional[int] = None
    notes: Optional[Dict[str, str]] = None


class CancelSubscriptionRequest(BaseModel):
    subscription_id: str
    cancel_at_cycle_end: bool = False


@app.post("/subscriptions/plans")
async def create_subscription_plan(request: CreatePlanRequest):
    """Create a subscription plan in Razorpay (admin/one-time setup)"""
    try:
        if not razorpay_service or not razorpay_service.client:
            raise HTTPException(
                status_code=503,
                detail="Razorpay service not available. Check RAZORPAY_ID and RAZORPAY_KEY."
            )
        
        plan = razorpay_service.create_plan(
            name=request.name,
            amount=request.amount,
            currency=request.currency,
            interval=request.interval,
            period=request.period,
            description=request.description
        )
        
        return {
            "success": True,
            "plan": plan
        }
    except Exception as e:
        logger.error(f"❌ Failed to create subscription plan: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/subscriptions/plans")
async def list_subscription_plans(count: int = 10, skip: int = 0):
    """List available subscription plans"""
    try:
        if not razorpay_service or not razorpay_service.client:
            raise HTTPException(
                status_code=503,
                detail="Razorpay service not available. Check RAZORPAY_ID and RAZORPAY_KEY."
            )
        
        plans = razorpay_service.list_plans(count=count, skip=skip)
        return {
            "success": True,
            "plans": plans
        }
    except Exception as e:
        logger.error(f"❌ Failed to list subscription plans: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/subscriptions/config")
async def get_subscription_config():
    """Get Razorpay public configuration (Key ID) for frontend"""
    try:
        if not config.RAZORPAY_ID:
            raise HTTPException(
                status_code=503,
                detail="Razorpay not configured"
            )
        
        return {
            "success": True,
            "razorpay_key_id": config.RAZORPAY_ID
        }
    except Exception as e:
        logger.error(f"❌ Failed to get subscription config: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/subscriptions/create")
async def create_subscription(request: CreateSubscriptionRequest):
    """Create a subscription for a user"""
    try:
        if not razorpay_service or not razorpay_service.client:
            raise HTTPException(
                status_code=503,
                detail="Razorpay service not available. Check RAZORPAY_ID and RAZORPAY_KEY."
            )
        
        if not mongodb_service:
            raise HTTPException(
                status_code=503,
                detail="MongoDB service not available"
            )
        
        # Create subscription in Razorpay
        subscription = razorpay_service.create_subscription(
            plan_id=request.plan_id,
            customer_notify=request.customer_notify,
            total_count=request.total_count,
            notes=request.notes
        )
        
        # Get plan details
        plan = razorpay_service.get_plan(request.plan_id)
        
        # Extract plan name - try multiple possible locations
        plan_name = "Pro"  # Default
        if plan:
            # Try different possible locations for plan name
            plan_name_raw = (
                plan.get("item", {}).get("name") or
                plan.get("name") or
                request.notes.get("plan_name") if request.notes else None or
                "Pro"
            )
            # Normalize plan name
            plan_name_raw_lower = plan_name_raw.lower()
            if "pro" in plan_name_raw_lower:
                plan_name = "Pro"
            elif "enterprise" in plan_name_raw_lower:
                plan_name = "Enterprise"
            else:
                plan_name = plan_name_raw
        
        # Store subscription in MongoDB
        from datetime import datetime
        subscription_data = {
            "user_id": request.user_id,
            "razorpay_subscription_id": subscription.get("id"),
            "razorpay_plan_id": request.plan_id,
            "plan_name": plan_name,
            "status": subscription.get("status", "created"),
            "amount": plan.get("item", {}).get("amount", 0) if plan else 0,
            "currency": plan.get("item", {}).get("currency", "INR") if plan else "INR",
            "current_start": subscription.get("current_start"),
            "current_end": subscription.get("current_end"),
            "next_billing_at": subscription.get("end_at"),
            "created_at": datetime.utcnow(),
            "razorpay_data": subscription  # Store full Razorpay response
        }
        
        mongodb_service.upsert_subscription(subscription_data)
        
        # Update user's subscription tier immediately if status is active
        # Otherwise, it will be updated via webhook when payment is completed
        if subscription.get("status") == "active":
            mongodb_service.update_user_subscription_tier(request.user_id, plan_name)
            logger.info(f"βœ… Updated user {request.user_id} subscription tier to {plan_name}")
        else:
            logger.info(f"⏳ Subscription created with status '{subscription.get('status')}'. User tier will be updated when subscription is activated via webhook.")
        
        return {
            "success": True,
            "subscription_id": subscription.get("id"),
            "short_url": subscription.get("short_url"),
            "subscription": subscription
        }
    except Exception as e:
        logger.error(f"❌ Failed to create subscription: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/subscriptions/status")
async def get_subscription_status(user_id: Optional[str] = None):
    """Get user's subscription status"""
    try:
        if not mongodb_service:
            raise HTTPException(
                status_code=503,
                detail="MongoDB service not available"
            )
        
        if not user_id:
            return {
                "success": True,
                "subscription": None,
                "message": "No user_id provided"
            }
        
        subscription = mongodb_service.get_user_subscription(user_id=user_id)
        
        if subscription:
            # Optionally fetch latest data from Razorpay
            if razorpay_service and razorpay_service.client:
                try:
                    razorpay_sub = razorpay_service.get_subscription(
                        subscription.get("razorpay_subscription_id")
                    )
                    # Update status if changed
                    if razorpay_sub.get("status") != subscription.get("status"):
                        mongodb_service.update_subscription_status(
                            subscription.get("razorpay_subscription_id"),
                            razorpay_sub.get("status"),
                            {
                                "current_start": razorpay_sub.get("current_start"),
                                "current_end": razorpay_sub.get("current_end"),
                                "next_billing_at": razorpay_sub.get("end_at")
                            }
                        )
                        subscription["status"] = razorpay_sub.get("status")
                except Exception as e:
                    logger.warning(f"Failed to sync with Razorpay: {e}")
        
        return {
            "success": True,
            "subscription": subscription
        }
    except Exception as e:
        logger.error(f"❌ Failed to get subscription status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/subscriptions/cancel")
async def cancel_subscription(request: CancelSubscriptionRequest):
    """Cancel user's subscription"""
    try:
        if not razorpay_service or not razorpay_service.client:
            raise HTTPException(
                status_code=503,
                detail="Razorpay service not available. Check RAZORPAY_ID and RAZORPAY_KEY."
            )
        
        if not mongodb_service:
            raise HTTPException(
                status_code=503,
                detail="MongoDB service not available"
            )
        
        # Cancel subscription in Razorpay
        subscription = razorpay_service.cancel_subscription(
            subscription_id=request.subscription_id,
            cancel_at_cycle_end=request.cancel_at_cycle_end
        )
        
        # Update status in MongoDB
        mongodb_service.update_subscription_status(
            request.subscription_id,
            subscription.get("status", "cancelled"),
            {
                "current_start": subscription.get("current_start"),
                "current_end": subscription.get("current_end"),
                "next_billing_at": subscription.get("end_at")
            }
        )
        
        return {
            "success": True,
            "subscription": subscription
        }
    except Exception as e:
        logger.error(f"❌ Failed to cancel subscription: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/webhooks/razorpay")
async def razorpay_webhook(request: Request):
    """Handle Razorpay webhook events"""
    try:
        if not razorpay_service:
            raise HTTPException(
                status_code=503,
                detail="Razorpay service not available"
            )
        
        if not mongodb_service:
            raise HTTPException(
                status_code=503,
                detail="MongoDB service not available"
            )
        
        # Get raw body for signature verification
        body = await request.body()
        body_str = body.decode('utf-8')
        
        # Get signature from header
        signature = request.headers.get("X-Razorpay-Signature", "")
        
        # Verify webhook signature
        if not razorpay_service.verify_webhook_signature(body_str, signature):
            logger.warning("⚠️ Invalid webhook signature")
            raise HTTPException(status_code=400, detail="Invalid webhook signature")
        
        # Parse webhook payload from body string
        webhook_data = json.loads(body_str)
        event = webhook_data.get("event")
        payload = webhook_data.get("payload", {})
        
        logger.info(f"πŸ“₯ Received Razorpay webhook: {event}")
        
        # Handle different webhook events
        if event == "subscription.activated":
            subscription = payload.get("subscription", {}).get("entity", {})
            subscription_id = subscription.get("id")
            
            if subscription_id:
                # Get subscription from DB to get user_id and plan_name
                sub_doc = mongodb_service.get_subscription_by_razorpay_id(subscription_id)
                if sub_doc:
                    user_id = sub_doc.get("user_id")
                    plan_name = sub_doc.get("plan_name", "Pro")
                    
                    logger.info(f"πŸ“₯ Processing subscription.activated for user {user_id}, plan {plan_name}")
                    
                    mongodb_service.update_subscription_status(
                        subscription_id,
                        "active",
                        {
                            "current_start": subscription.get("current_start"),
                            "current_end": subscription.get("current_end"),
                            "next_billing_at": subscription.get("end_at")
                        }
                    )
                    
                    # Update user's subscription tier
                    if user_id:
                        success = mongodb_service.update_user_subscription_tier(user_id, plan_name)
                        if success:
                            logger.info(f"βœ… Successfully updated user {user_id} tier to {plan_name} via webhook")
                        else:
                            logger.error(f"❌ Failed to update user {user_id} tier to {plan_name}")
                else:
                    logger.warning(f"⚠️ Subscription {subscription_id} not found in database")
        
        elif event == "subscription.charged":
            subscription = payload.get("subscription", {}).get("entity", {})
            payment = payload.get("payment", {}).get("entity", {})
            subscription_id = subscription.get("id")
            
            if subscription_id:
                # Get subscription from DB to get user_id and plan_name
                sub_doc = mongodb_service.get_subscription_by_razorpay_id(subscription_id)
                if sub_doc:
                    user_id = sub_doc.get("user_id")
                    plan_name = sub_doc.get("plan_name", "Pro")
                    
                    logger.info(f"πŸ“₯ Processing subscription.charged for user {user_id}, plan {plan_name}")
                    
                    # Update subscription with payment info
                    update_data = {
                        "current_start": subscription.get("current_start"),
                        "current_end": subscription.get("current_end"),
                        "next_billing_at": subscription.get("end_at"),
                        "last_payment_id": payment.get("id"),
                        "last_payment_amount": payment.get("amount"),
                        "last_payment_date": payment.get("created_at")
                    }
                    mongodb_service.update_subscription_status(
                        subscription_id,
                        subscription.get("status", "active"),
                        update_data
                    )
                    
                    # Update user's subscription tier when payment is charged
                    if user_id and subscription.get("status") == "active":
                        success = mongodb_service.update_user_subscription_tier(user_id, plan_name)
                        if success:
                            logger.info(f"βœ… Successfully updated user {user_id} tier to {plan_name} via subscription.charged webhook")
                        else:
                            logger.error(f"❌ Failed to update user {user_id} tier to {plan_name}")
                else:
                    logger.warning(f"⚠️ Subscription {subscription_id} not found in database for subscription.charged event")
        
        elif event == "subscription.cancelled":
            subscription = payload.get("subscription", {}).get("entity", {})
            subscription_id = subscription.get("id")
            
            if subscription_id:
                # Get subscription from DB to get user_id
                sub_doc = mongodb_service.get_subscription_by_razorpay_id(subscription_id)
                if sub_doc:
                    user_id = sub_doc.get("user_id")
                    
                    mongodb_service.update_subscription_status(
                        subscription_id,
                        "cancelled",
                        {
                            "current_start": subscription.get("current_start"),
                            "current_end": subscription.get("current_end"),
                            "next_billing_at": subscription.get("end_at")
                        }
                    )
                    
                    # Update user's subscription tier to Free
                    if user_id:
                        mongodb_service.update_user_subscription_tier(user_id, "Free")
        
        elif event == "payment.failed":
            payment = payload.get("payment", {}).get("entity", {})
            subscription_id = payment.get("subscription_id")
            
            if subscription_id:
                # Update subscription to reflect failed payment
                subscription = razorpay_service.get_subscription(subscription_id)
                mongodb_service.update_subscription_status(
                    subscription_id,
                    subscription.get("status", "pending"),
                    {
                        "last_payment_failed": True,
                        "last_payment_failure_reason": payment.get("error_description")
                    }
                )
        
        return {"success": True, "message": "Webhook processed"}
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"❌ Failed to process webhook: {e}")
        raise HTTPException(status_code=500, detail=str(e))


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=config.SERVICE_PORT)