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
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
import os
import time
# Enforce IST Timezone globally for the python process
os.environ['TZ'] = 'Asia/Kolkata'
if hasattr(time, 'tzset'):
    time.tzset()

from flask import Flask, request, jsonify, send_file, g
from flask_cors import CORS
from werkzeug.utils import secure_filename
from pathlib import Path
import os
import hashlib
import numpy as np
import json
import threading
import uuid
import copy
import re
from datetime import datetime
from sqlalchemy import func
from flask import send_from_directory
from flask.json.provider import DefaultJSONProvider
import requests

# Local imports
from cv_processor import CVProcessor
from database import Database, Event, Person, PersonActivity, User, text
from energy_analyzer import EnergyAnalyzer
from blockchain import BlockchainManager
from config import config
from jwt_auth import (
    create_access_token, create_refresh_token, decode_token,
    hash_password, verify_password,
    jwt_required, jwt_optional, role_required, admin_required,
    faculty_or_admin_required,
    get_current_user, get_current_user_role
)


class NumpyJSONProvider(DefaultJSONProvider):
    """ Custom JSON provider for numpy data types compatible with Flask 3.x """
    def default(self, obj):
        if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
                            np.int16, np.int32, np.int64, np.uint8,
                            np.uint16, np.uint32, np.uint64)):
            return int(obj)
        elif isinstance(obj, (np.float_, np.float16, np.float32, np.float64)):
            return float(obj)
        elif isinstance(obj, (np.ndarray,)):
            return obj.tolist()
        elif isinstance(obj, (np.bool_)):
            return bool(obj)
        return super().default(obj)

app = Flask(__name__)
app.json = NumpyJSONProvider(app)

# Enable CORS with environment-based origins
if config.is_local():
    # In Local mode, allow all origins for easier local/web demo testing
    CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)
else:
    CORS(app, origins=config.CORS_ORIGINS, supports_credentials=True)

# Initialize Rate Limiter
try:
    from flask_limiter import Limiter
    from flask_limiter.util import get_remote_address
    
    limiter = Limiter(
        get_remote_address,
        app=app,
        default_limits=[f"{config.RATE_LIMIT_PER_MINUTE} per minute"] if config.RATE_LIMIT_ENABLED else [],
        storage_uri="memory://"
    )
    print("✓ Rate limiting enabled")
except ImportError:
    print("⚠️ Flask-Limiter not installed. Rate limiting disabled.")
    # Dummy limiter to prevent crashes
    class DummyLimiter:
        def limit(self, *args, **kwargs):
            def decorator(f):
                return f
            return decorator
    limiter = DummyLimiter()


# Configuration from environment - Using absolute paths for robustness
IS_VERCEL = os.environ.get('VERCEL') == '1'
BASE_DIR = config.BASE_DIR

if IS_VERCEL:
    # Vercel only allows writing to /tmp
    UPLOAD_FOLDER = Path('/tmp') / 'uploads'
    OUTPUT_FOLDER = Path('/tmp') / 'outputs'
else:
    UPLOAD_FOLDER = BASE_DIR / 'uploads'
    OUTPUT_FOLDER = BASE_DIR / 'outputs'

MODELS_FOLDER = BASE_DIR / 'models'
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'webm'}

# Create folders if they don't exist
try:
    UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)
    OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
    if not IS_VERCEL:
        MODELS_FOLDER.mkdir(parents=True, exist_ok=True)
except Exception as e:
    print(f"⚠️ Warning: Could not create directories: {e}")

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Increased for long video uploads - supports up to 1GB
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024  # 1GB max file size

# Task status tracking for background processing


# Initialize CV processor and database lazily
cv_processor = None
db = None
energy_analyzer = None
blockchain_manager = None


def initialize_resources():
    """Ensure folders exist and initialize database and analytics."""
    global db, energy_analyzer, cv_processor, blockchain_manager

    print("🚀 Neural Node initializing resources...")
    # Ensure essential directories exist
    try:
        UPLOAD_FOLDER.mkdir(parents=True, exist_ok=True)
        OUTPUT_FOLDER.mkdir(parents=True, exist_ok=True)
        (OUTPUT_FOLDER / 'face_database').mkdir(parents=True, exist_ok=True)
        if not IS_VERCEL:
            MODELS_FOLDER.mkdir(parents=True, exist_ok=True)
        print("📁 Directory structure verified.")
    except Exception as e:
        print(f"⚠️ Directory creation warning: {e}")

    # Initialize database and analytics
    if db is None:
        print("💾 Connecting to Persistence Engine...")
        db = Database()
        print("✅ Persistence Engine Online.")

    if energy_analyzer is None:
        print("🔋 Calibrating Energy Analytics...")
        energy_analyzer = EnergyAnalyzer()
        
    if blockchain_manager is None:
        print("🔗 Synchronizing Blockchain Interface...")
        blockchain_manager = BlockchainManager()

    print("🧠 Neural Node fully initialized and ready.")

# --- Heartbeat Turnaround for HF Free Tier ---
def run_heartbeat():
    """Background thread to ping the node's own public URL to prevent idling."""
    # This URL should be set in HF Space Secrets as SPACE_PUBLIC_URL 
    # e.g. https://user-name-space-name.hf.space
    public_url = os.environ.get('SPACE_PUBLIC_URL')
    if not public_url:
        print("⚠️ [Heartbeat] SPACE_PUBLIC_URL not set. Self-pinging disabled.")
        return

    # Wait for the server to fully start before the first ping
    time.sleep(10)
    
    print(f"💓 [Heartbeat] Initialized for: {public_url}")
    while True:
        try:
            # We ping the /status endpoint which is lightweight
            ping_url = f"{public_url.rstrip('/')}/status"
            print(f"💓 [Heartbeat] Sending pulse to {ping_url}...")
            response = requests.get(ping_url, timeout=15)
            if response.status_code == 200:
                print(f"✅ [Heartbeat] Pulse recorded at {datetime.now().strftime('%H:%M:%S')}")
            else:
                print(f"⚠️ [Heartbeat] Unexpected response: {response.status_code}")
        except Exception as e:
            print(f"💔 [Heartbeat] Pulse failed: {e}")
            
        # Pinging every 1 hour is enough to keep most proxies active.
        # HF free tier sleeps after 48h of inactivity.
        time.sleep(3600)

def start_heartbeat():
    """Start the heartbeat thread if configured."""
    if os.environ.get('SPACE_PUBLIC_URL'):
        thread = threading.Thread(target=run_heartbeat, daemon=True)
        thread.start()
        print("🚀 [Heartbeat] Monitor started in background.")

# ----------------------------------------------

# Deferred initialization to prevent Gunicorn timeout
_initialized = False

@app.before_request
def ensure_initialized():
    global _initialized
    if not _initialized:
        initialize_resources()
        start_heartbeat()
        _initialized = True

def allowed_file(filename):
    """Check if file extension is allowed"""
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def get_cv_processor(optimization_mode=None):
    """Get or create CV processor instance with database enabled"""
    global cv_processor
    # If mode is requested, we might need to re-initialize or update the existing one
    if cv_processor is None:
        cv_processor = CVProcessor(use_database=True, db_instance=db, optimization_mode=optimization_mode)
    elif optimization_mode and cv_processor.optimization_mode != optimization_mode:
        # Update thresholds for existing processor
        cv_processor.optimization_mode = optimization_mode
        cv_processor.set_thresholds_by_mode(optimization_mode)
        cv_processor.energy_analyzer.optimization_mode = optimization_mode
    return cv_processor


@app.teardown_appcontext
def shutdown_session(exception=None):
    """Ensure database sessions are closed after request"""
    if db:
        db.close()


@app.route('/')
def index():
    """API info endpoint"""
    db_type = 'PostgreSQL' if db and db.engine.name == 'postgresql' else 'SQLite'
    return jsonify({
        'service': 'SCA CV Module API',
        'version': '2.0',
        'database': f'{db_type} with SQLAlchemy',
        'endpoints': {
            'POST /upload': 'Upload a video file',
            'POST /process': 'Process an uploaded video',
            'GET /results/<filename>': 'Get processing results',
            'GET /status': 'Get API status',
            'GET /db/events': 'Get all events from database',
            'GET /db/events/<event_id>': 'Get specific event',
            'GET /db/persons': 'Get all persons',
            'GET /db/persons/<person_id>': 'Get specific person details',
            'GET /db/persons/<person_id>/events': 'Get events for a person',
            'GET /db/persons/<person_id>/activities': 'Get activities for a person',
            'GET /db/leaderboard': 'Get person leaderboard with scores',
            'GET /db/stats': 'Get database statistics',
            'GET /energy/report': 'Get energy usage report',
            'GET /energy/blockchain-credits': 'Get blockchain credits summary',
            'GET /energy/sustainable-actions': 'Get sustainable actions log',
            'GET /energy/live-metrics': 'Get real-time energy metrics'
        }
    })


@app.route('/status', methods=['GET'])
def status():
    """Get API status with node health metrics"""
    try:
        # Check database connectivity
        session = db.get_session()
        try:
            session.execute(text('SELECT 1'))
            db_status = 'operational'
        except:
            db_status = 'degraded'
        finally:
            session.close()
        
        # Check blockchain connectivity
        blockchain_status = 'operational' if blockchain_manager and blockchain_manager.w3.is_connected() else 'offline'
        
        # Overall node status - degraded if either DB or Blockchain is not operational
        overall_status = 'operational' if db_status == 'operational' and blockchain_status == 'operational' else 'degraded'
        
        return jsonify({
            'status': overall_status,
            'timestamp': datetime.now().isoformat(),
            'uploads_count': len(list(UPLOAD_FOLDER.glob('*'))),
            'outputs_count': len(list(OUTPUT_FOLDER.glob('*.json'))),
            'database_status': db_status,
            'blockchain_status': blockchain_status,
            'node_id': 'SCA_NODE_1',
            'version': '2.0'
        })
    except Exception as e:
        return jsonify({
            'status': 'error',
            'error': str(e),
            'timestamp': datetime.now().isoformat()
        }), 500


@app.route('/contact', methods=['POST'])
def contact():
    """Submit a contact form inquiry"""
    data = request.json
    if not data:
        return jsonify({'error': 'Missing form data'}), 400
    
    name = data.get('name')
    email = data.get('email')
    message = data.get('message')
    
    if not name or not email or not message:
        return jsonify({'error': 'Name, email, and message are required'}), 400
    
    # Validate email format (strict regex)
    email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    if not re.match(email_regex, email):
        return jsonify({'error': 'Invalid email format'}), 400
        
    # Input sanitization (basic HTML tag stripping)
    clean_message = re.sub(r'<[^>]*>', '', message)
    clean_name = re.sub(r'<[^>]*>', '', name)
    
    # Save to database
    session = db.get_session()
    try:
        from database import ContactInquiry
        
        # Get client IP and user agent for tracking
        ip_address = request.headers.get('X-Forwarded-For', request.remote_addr)
        user_agent = request.headers.get('User-Agent', '')[:500]  # Limit length
        
        inquiry = ContactInquiry(
            name=clean_name,
            email=email,
            message=clean_message,
            ip_address=ip_address,
            user_agent=user_agent
        )
        
        session.add(inquiry)
        session.commit()
        session.refresh(inquiry)
        
        print(f"✓ Contact Inquiry #{inquiry.inquiry_id} saved: From={name}, Email={email}")
        preview = message[:100] if len(message) > 100 else message
        print(f"  Message preview: {preview}{'...' if len(message) > 100 else ''}")
        
        return jsonify({
            'success': True,
            'message': 'Signal received and cached by node admins.',
            'received_at': datetime.now().isoformat(),
            'inquiry_id': inquiry.inquiry_id
        })
    except Exception as e:
        session.rollback()
        print(f"✗ Failed to save contact inquiry: {e}")
        return jsonify({'error': 'Failed to save inquiry', 'details': str(e)}), 500
    finally:
        session.close()



@app.route('/auth/login', methods=['POST'])
def login():
    """Authenticate user and return JWT tokens with role-based access"""
    data = request.json
    if not data:
        return jsonify({'error': 'Missing credentials'}), 400
    
    email = data.get('email')
    password = data.get('password')
    
    # Sandbox mode flag (frontend-only feature for demo purposes)
    # Note: This doesn't affect backend authentication - users still authenticate against real database
    # The flag is used by frontend to show demo UI elements and mock data after authentication
    use_mock = data.get('use_mock', False)
    
    if not email or not password:
        return jsonify({'error': 'Email and password are required'}), 400
    
    # Look up user in database
    session = db.get_session()
    try:
        user = session.query(User).filter_by(email=email).first()
        
        if not user:
            return jsonify({'error': 'User not found. Please register first.'}), 404
        
        # Verify password (hashed only - legacy plain-text disabled)
        if not verify_password(password, user.password_hash):
            return jsonify({'error': 'Invalid credentials'}), 401
        
        # Check if user is active/suspended
        if not user.is_active:
            return jsonify({
                'error': 'Account Suspended',
                'message': 'Your account has been deactivated by an administrator.'
            }), 403
            
        # Update last login
        user.last_login = datetime.now()
        session.commit()
        
        # Generate JWT tokens
        access_token = create_access_token(
            user_id=user.user_id,
            email=user.email,
            role=user.role,
            name=user.name,
            department=user.department
        )
        refresh_token = create_refresh_token(
            user_id=user.user_id,
            email=user.email
        )
        
        return jsonify({
            'success': True,
            'message': f"Session initialized for {email}",
            'access_token': access_token,
            'refresh_token': refresh_token,
            'token_type': 'Bearer',
            'expires_in': 86400,  # 24 hours in seconds
            'user': {
                'user_id': user.user_id,
                'email': user.email,
                'role': user.role,
                'name': user.name,
                'department': user.department,
                'node_id': f"SCA_NODE_{user.user_id}",
                'last_login': datetime.now().isoformat()
            },
            'data_mode': 'Sandbox' if use_mock else 'Mainnet'
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/auth/register', methods=['POST'])
def register():
    """Register a new user (student or faculty only - admin is auto-created)"""
    data = request.json
    if not data:
        return jsonify({'error': 'Missing registration data'}), 400
    
    email = data.get('email')
    password = data.get('password')
    name = data.get('name', email.split('@')[0] if email else 'User')
    role = data.get('role', 'student')
    department = data.get('department', 'General')
    
    if not email or not password:
        return jsonify({'error': 'Email and password are required'}), 400
    
    if len(password) < 6:
        return jsonify({'error': 'Password must be at least 6 characters'}), 400
    
    # Validate email format (strict regex)
    email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    if not re.match(email_regex, email):
        return jsonify({'error': 'Invalid email format'}), 400
    
    # Admin role cannot be self-registered
    if role == 'admin':
        return jsonify({'error': 'Admin accounts cannot be self-registered'}), 403
    
    if role not in ['student', 'faculty']:
        return jsonify({'error': 'Invalid role. Must be student or faculty'}), 400
    
    session = db.get_session()
    try:
        # Check if user already exists
        existing = session.query(User).filter_by(email=email).first()
        if existing:
            return jsonify({'error': 'User with this email already exists'}), 409
        
        # Hash the password before storing
        hashed_password = hash_password(password)
        
        # Create new user
        new_user = User(
            email=email,
            password_hash=hashed_password,
            name=name,
            role=role,
            department=department
        )
        session.add(new_user)
        
        # Also create or update corresponding Person record for leaderboard/tracking
        existing_person = session.query(Person).filter_by(person_id=email).first()
        if not existing_person:
            new_person = Person(
                person_id=email,
                student_id=name,
                department=department,
                user_type=role,
                first_seen=datetime.now(),
                last_seen=datetime.now()
            )
            session.add(new_person)
        
        session.commit()
        session.refresh(new_user)
        
        # Generate tokens for immediate login after registration
        access_token = create_access_token(
            user_id=new_user.user_id,
            email=new_user.email,
            role=new_user.role,
            name=new_user.name,
            department=new_user.department
        )
        refresh_token = create_refresh_token(
            user_id=new_user.user_id,
            email=new_user.email
        )
        
        return jsonify({
            'success': True,
            'message': f'Registration successful for {name}',
            'access_token': access_token,
            'refresh_token': refresh_token,
            'token_type': 'Bearer',
            'expires_in': 86400,  # 24 hours in seconds
            'user': {
                'user_id': new_user.user_id,
                'email': new_user.email,
                'role': new_user.role,
                'name': new_user.name,
                'department': new_user.department
            }
        }), 201
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/auth/refresh', methods=['POST'])
def refresh_token():
    """Refresh access token using refresh token"""
    data = request.json
    if not data or 'refresh_token' not in data:
        return jsonify({'error': 'Refresh token required'}), 400
    
    try:
        payload = decode_token(data['refresh_token'])
        
        if payload.get('type') != 'refresh':
            return jsonify({'error': 'Invalid token type'}), 401
        
        # Get user to ensure they still exist and are active
        session = db.get_session()
        try:
            user_id = payload.get('user_id') or payload.get('sub')
            user = session.query(User).filter_by(user_id=int(user_id)).first()
            if not user or not user.is_active:
                return jsonify({'error': 'User not found or inactive'}), 401
            
            # Generate new access token
            access_token = create_access_token(
                user_id=user.user_id,
                email=user.email,
                role=user.role,
                name=user.name,
                department=user.department
            )
            
            return jsonify({
                'success': True,
                'access_token': access_token,
                'token_type': 'Bearer',
                'expires_in': 86400
            })
        finally:
            session.close()
            
    except Exception as e:
        return jsonify({'error': 'Invalid refresh token', 'message': str(e)}), 401


@app.route('/auth/me', methods=['GET'])
@jwt_required
def get_current_user_info():
    """Get current authenticated user info"""
    user = get_current_user()
    return jsonify({
        'success': True,
        'user': user
    })


@app.route('/auth/wallet', methods=['POST'])
@jwt_required
def update_user_wallet():
    """Update wallet address for the current authenticated user"""
    user_info = get_current_user()
    email = user_info['email']
    data = request.json
    
    if not data or 'wallet_address' not in data:
        return jsonify({'error': 'Wallet address is required'}), 400
        
    wallet_address = data.get('wallet_address')
    
    # Validation logic: allow empty string for unlinking, otherwise check EIP-55 format
    if wallet_address:
        if not wallet_address.startswith('0x') or len(wallet_address) != 42:
            return jsonify({'error': 'Invalid Ethereum wallet address format'}), 400
    else:
        # User is unlinking
        wallet_address = None
        
    session = db.get_session()
    try:
        person = session.query(Person).filter_by(person_id=email).first()
        if not person:
            # Create a person record if it doesn't exist
            person = Person(
                person_id=email,
                wallet_address=wallet_address,
                department=user_info.get('department', 'General'),
                total_credits_earned=0.0
            )
            session.add(person)
        else:
            person.wallet_address = wallet_address
            
        session.commit()
        return jsonify({
            'success': True,
            'message': 'Wallet address registered with node authority.',
            'wallet_address': wallet_address
        })
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()

@app.route('/energy/blockchain-credits', methods=['GET'])
@jwt_required
def get_energy_blockchain_credits():
    """Get internal and on-chain credit summary for a user"""
    person_id = request.args.get('person_id')
    user_info = get_current_user()
    if not person_id:
        person_id = user_info['email']
        
    session = db.get_session()
    try:
        person = session.query(Person).filter_by(person_id=person_id).first()
        internal_credits = person.total_credits_earned if person else 0.0
        wallet_address = person.wallet_address if person else None
        
        blockchain_credits = 0.0
        blockchain_status = False
        on_chain_sync = False
        sync_warning = None
        
        if wallet_address and blockchain_manager:
            blockchain_credits = blockchain_manager.get_wallet_balance(wallet_address)
            blockchain_status = blockchain_manager.is_connected
            
            # Check if on-chain balance matches internal credits
            # Allow 5% tolerance for rounding and pending transactions
            if blockchain_status and internal_credits > 0:
                sync_diff = abs(blockchain_credits - internal_credits)
                tolerance = internal_credits * 0.05
                on_chain_sync = sync_diff <= tolerance
                
                if not on_chain_sync:
                    if blockchain_credits < internal_credits:
                        sync_warning = f"On-chain balance is {sync_diff:.2f} credits lower. Bridge recommended."
                    else:
                        sync_warning = f"On-chain balance is {sync_diff:.2f} credits higher. Verify transactions."
            
        activities = session.query(PersonActivity).filter_by(person_id=person_id).order_by(PersonActivity.timestamp.desc()).limit(30).all()
        history = [a.to_dict() for a in activities]
            
        return jsonify({
            'success': True,
            'total_credits': internal_credits,
            'total_blockchain_credits': blockchain_credits,
            'blockchain_status': blockchain_status,
            'wallet_address': wallet_address,
            'on_chain_sync': on_chain_sync,
            'sync_warning': sync_warning,
            'recent_history': history
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()




@app.route('/auth/users', methods=['GET'])
@jwt_required
@admin_required
def list_users():
    """List all users (admin only endpoint)"""
    session = db.get_session()
    try:
        users = session.query(User).all()
        return jsonify({
            'total': len(users),
            'users': [{
                'user_id': u.user_id,
                'email': u.email,
                'name': u.name,
                'role': u.role,
                'department': u.department,
                'is_active': u.is_active,
                'created_at': u.created_at.isoformat() if u.created_at else None,
                'last_login': u.last_login.isoformat() if u.last_login else None
            } for u in users]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/auth/users/<int:user_id>', methods=['PATCH'])
@jwt_required
@admin_required
def update_user(user_id):
    """Update user role or status (admin only)"""
    data = request.json
    if not data:
        return jsonify({'error': 'Missing data'}), 400
        
    session = db.get_session()
    try:
        user = session.query(User).filter_by(user_id=user_id).first()
        if not user:
            return jsonify({'error': 'User not found'}), 404
            
        if 'role' in data:
            if data['role'] not in ['student', 'faculty', 'admin']:
                return jsonify({'error': 'Invalid role'}), 400
            user.role = data['role']
            
        if 'is_active' in data:
            user.is_active = bool(data['is_active'])
            
        session.commit()
        return jsonify({
            'success': True,
            'message': f'User {user_id} updated successfully',
            'user': user.to_dict()
        })
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/upload', methods=['POST'])
@jwt_required
def upload_video():
    """
    Upload a video file
    
    Form data:
        file: Video file
        
    Returns:
        JSON with upload status and filename
    """
    if 'file' not in request.files:
        return jsonify({'error': 'No file part in request'}), 400
    
    file = request.files['file']
    
    if file.filename == '':
        return jsonify({'error': 'No file selected'}), 400
    
    if not allowed_file(file.filename):
        return jsonify({
            'error': 'Invalid file type',
            'allowed_types': list(ALLOWED_EXTENSIONS)
        }), 400
    
    filename = secure_filename(file.filename)
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    filename_with_timestamp = f"{timestamp}_{filename}"
    filepath = UPLOAD_FOLDER / filename_with_timestamp
    
    try:
        file.save(str(filepath))
        return jsonify({
            'message': 'File uploaded successfully',
            'filename': filename_with_timestamp,
            'path': str(filepath),
            'size_bytes': filepath.stat().st_size
        }), 201
    except Exception as e:
        return jsonify({'error': f'Upload failed: {str(e)}'}), 500


@app.route('/uploads/<path:filename>')
def uploaded_file(filename):
    """Serve uploaded files with appropriate mimetype detection"""
    import mimetypes
    mimetype, _ = mimetypes.guess_type(filename)
    response = send_from_directory(app.config['UPLOAD_FOLDER'], filename, mimetype=mimetype)
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    response.headers['Access-Control-Allow-Origin'] = '*'
    return response




@app.route('/process', methods=['POST'])
@jwt_required
def process_video_endpoint():
    """
    Process an uploaded video with background task support (Persistent)
    """
    data = request.get_json()
    
    if not data or 'filename' not in data:
        return jsonify({'error': 'filename required in request body'}), 400
    
    filename = data['filename']
    confidence = data.get('confidence', 0.5)
    skip_frames = data.get('skip_frames', None) 
    persist = data.get('persist', True) 
    video_path = UPLOAD_FOLDER / filename
    
    if not video_path.exists():
        return jsonify({'error': f'Video file not found: {filename}'}), 404
        
    task_id = str(uuid.uuid4())
    
    # Create Task in Database
    u_email = None
    u_dept = None
    try:
        current_user = get_current_user()
        if current_user:
            u_email = current_user.get('email')
            u_dept = current_user.get('department')
    except: pass
    
    if db:
        db.create_task(task_id, filename, u_email, u_dept)
    else:
        # Fallback if DB init failed (should not happen)
        return jsonify({'error': 'Database not initialized'}), 500
    
    def run_analysis(tid, vpath, conf, skip, persist_flag, user_email, user_dept):
        try:
            if db: db.update_task_status(tid, status='processing', progress=0)
            
            # Generate output
            v_p = Path(vpath)
            output_filename = f"{v_p.stem}_detections.json"
            output_path = OUTPUT_FOLDER / output_filename
            
            # Progress callback
            def update_progress(current, total, pct):
                if db: db.update_task_status(tid, progress=pct, current_frame=current, total_frames=total)

            # Force high-fidelity 'precision' mode
            processor = get_cv_processor(optimization_mode='precision')
            results = processor.process_video(
                video_path=str(vpath),
                output_json_path=str(output_path),
                confidence_threshold=conf,
                skip_frames=skip,
                progress_callback=update_progress
            )
            
            # Persist events logic (Keep existing logic)
            events_created = 0
            if persist_flag and results.get('events'):
                print(f"📊 Persisting {len(results['events'])} events to database...")
                for event_item in results['events']:
                    try:
                        event_data = {
                            'room_id': event_item.get('room_id', 'UPLOAD_STATION'),
                            'occupancy': event_item.get('occupancy', False),
                            'person_count': event_item.get('person_count', 0),
                            'person_id': user_email,
                            'department': user_dept,
                            'confidence': event_item.get('overall_confidence', 0.0),
                            'video_file': event_item.get('video_file', filename),
                            'frame_number': event_item.get('frame_number', 0),
                            'action_detected': event_item.get('action_detected', 'unknown'),
                            'action_type': event_item.get('action_type', 'neutral'),
                            'energy_saved_estimate': event_item.get('energy_saved_estimate', 0.0),
                            'blockchain_credits': event_item.get('blockchain_credits', 0.0),
                            'overall_confidence': event_item.get('overall_confidence', 0.0),
                            'action_confidence': event_item.get('action_confidence', 0.0),
                            'devices_on': event_item.get('devices_on', []),
                            'devices_off': event_item.get('devices_off', []),
                            'lights_on': event_item.get('lights_on', False),
                            'impact_analytics': event_item.get('impact_analytics', []),
                            'status': 'pending'
                        }
                        # Foreign Key Check
                        if user_email:
                            db.add_person(user_email, detection_method='appearance')
                            
                        db.add_event(event_data)
                        events_created += 1
                    except Exception as e:
                        print(f"⚠ DB Save error for event: {e}")

            # Mark Completed
            if db:
                db.update_task_status(
                    tid, 
                    status='completed', 
                    progress=100, 
                    result={'events_created': events_created, 'output_file': output_filename}
                )
            
        except Exception as e:
            print(f"❌ Background processing failed for {tid}: {e}")
            if db:
                db.update_task_status(tid, status='failed', error=str(e))

    # Start thread
    thread = threading.Thread(target=run_analysis, args=(task_id, video_path, confidence, skip_frames, persist, u_email, u_dept))
    thread.daemon = True
    thread.start()
    
    return jsonify({
        'message': 'Analysis started in background',
        'task_id': task_id,
        'status_url': f'/process/status/{task_id}'
    }), 202

@app.route('/process/status/<task_id>', methods=['GET'])
@jwt_required
def get_task_status(task_id):
    """Check status of a background processing task (Persistent)"""
    if not db:
        return jsonify({'error': 'Database not initialized'}), 500
        
    task = db.get_task(task_id)
    if not task:
        return jsonify({'error': 'Task ID not found'}), 404
        
    return jsonify(task)


@app.route('/results/<filename>', methods=['GET'])
@jwt_required
def get_results(filename):
    """
    Get processing results JSON file
    
    Args:
        filename: Name of the results JSON file
        
    Returns:
        JSON file or JSON data
    """
    output_path = OUTPUT_FOLDER / filename
    
    if not output_path.exists():
        return jsonify({'error': f'Results file not found: {filename}'}), 404
    
    # Check if user wants to download or view
    download = request.args.get('download', 'false').lower() == 'true'
    
    if download:
        return send_file(str(output_path), as_attachment=True)
    else:
        with open(output_path, 'r') as f:
            data = json.load(f)
        return jsonify(data)



@app.route('/uploads/<path:filename>', methods=['GET'])
def get_upload_content(filename):
    """Serve uploaded/generated video files (Public for playback)"""
    return send_from_directory(UPLOAD_FOLDER, filename)


@app.route('/list/uploads', methods=['GET'])
@jwt_required
def list_uploads():
    """List all uploaded video files"""
    files = []
    for filepath in UPLOAD_FOLDER.glob('*'):
        if filepath.is_file():
            files.append({
                'filename': filepath.name,
                'size_bytes': filepath.stat().st_size,
                'uploaded_at': datetime.fromtimestamp(filepath.stat().st_mtime).isoformat()
            })
    
    return jsonify({
        'count': len(files),
        'files': sorted(files, key=lambda x: x['uploaded_at'], reverse=True)
    })


@app.route('/list/results', methods=['GET'])
@jwt_required
def list_results():
    """List all processing results"""
    files = []
    for filepath in OUTPUT_FOLDER.glob('*.json'):
        if filepath.is_file():
            files.append({
                'filename': filepath.name,
                'size_bytes': filepath.stat().st_size,
                'created_at': datetime.fromtimestamp(filepath.stat().st_mtime).isoformat()
            })
    
    return jsonify({
        'count': len(files),
        'files': sorted(files, key=lambda x: x['created_at'], reverse=True)
    })


@app.route('/summary/<filename>', methods=['GET'])
def get_summary(filename):
    """
    Get detection summary for a results file
    
    Args:
        filename: Name of the results JSON file
        
    Returns:
        Summary statistics
    """
    output_path = OUTPUT_FOLDER / filename
    
    if not output_path.exists():
        return jsonify({'error': f'Results file not found: {filename}'}), 404
    
    with open(output_path, 'r') as f:
        data = json.load(f)
    
    # Calculate class distribution
    class_counts = {}
    for event in data.get('events', []):
        class_name = event['class']
        class_counts[class_name] = class_counts.get(class_name, 0) + 1
    
    summary = {
        'video_file': data.get('video_file'),
        'total_detections': data.get('total_detections', 0),
        'duration_seconds': data.get('duration_seconds', 0),
        'class_distribution': class_counts,
        'processed_at': data.get('processed_at')
    }
    
    return jsonify(summary)


# ============================================================
# DATABASE ENDPOINTS
# ============================================================

@app.route('/db/events', methods=['GET'])
@jwt_required
def get_db_events():
    """Get all events from database with pagination and aggregated stats"""
    limit = request.args.get('limit', 100, type=int)
    offset = request.args.get('offset', 0, type=int)
    person_id = request.args.get('person_id', None)
    status = request.args.get('status', None)
    action = request.args.get('action', None)
    room_id = request.args.get('room_id', None)
    search = request.args.get('search', None)
    department = request.args.get('department', None)
    
    current_user = get_current_user()
    role = get_current_user_role()
    
    # Enforce departmental visibility for faculty
    # Administrators can see everything, or filter by any department
    if role == 'faculty' and current_user:
        department = current_user.get('department')
        print(f"🔐 Enforcing departmental filter for faculty: {department}")
    
    session = db.get_session()
    try:
        from sqlalchemy import func
        query = session.query(Event)
        
        if person_id:
            query = query.filter(Event.person_id == person_id)
        if status:
            query = query.filter(Event.status == status)
        if action:
            query = query.filter(Event.action_detected == action)
        if room_id:
            query = query.filter(Event.room_id == room_id)
        if department:
            query = query.filter(Event.department == department)
        if search:
            search_query = f"%{search}%"
            query = query.filter(
                (Event.action_detected.ilike(search_query)) | 
                (Event.room_id.ilike(search_query)) |
                (Event.department.ilike(search_query))
            )
        
        # Calculate totals before pagination
        total = query.count()
        total_credits = session.query(func.sum(Event.blockchain_credits)).filter(Event.event_id.in_(query.with_entities(Event.event_id))).scalar() or 0
        total_impact = session.query(func.sum(Event.energy_saved_estimate)).filter(Event.event_id.in_(query.with_entities(Event.event_id))).scalar() or 0
        
        query = query.order_by(Event.timestamp.desc())
        events = query.offset(offset).limit(limit).all()
        
        # Expunge objects to prevent DetachedInstanceError
        for event in events:
            session.expunge(event)
        
        return jsonify({
            'total': total,
            'total_credits': float(total_credits),
            'total_impact': float(total_impact),
            'limit': limit,
            'offset': offset,
            'events': [event.to_dict() for event in events]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/events/<int:event_id>', methods=['GET'])
def get_db_event(event_id):
    """Get specific event by ID"""
    session = db.get_session()
    try:
        event = session.query(Event).filter_by(event_id=event_id).first()
        
        if not event:
            return jsonify({'error': 'Event not found'}), 404
        
        # Expunge to prevent DetachedInstanceError
        session.expunge(event)
        return jsonify(event.to_dict())
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/events/<int:event_id>/status', methods=['POST'])
@jwt_required
@role_required('admin', 'faculty')
def update_event_status(event_id):
    """Update event verification status and trigger blockchain minting if verified"""
    data = request.get_json()
    if not data or 'status' not in data:
        return jsonify({'error': 'status required'}), 400
    
    new_status = data['status']
    if new_status not in ['pending', 'verified', 'rejected']:
        return jsonify({'error': 'invalid status'}), 400
        
    session = db.get_session()
    try:
        event = session.query(Event).filter_by(event_id=event_id).first()
        if not event:
            return jsonify({'error': 'Event not found'}), 404
            
        # Security Check: Faculty can only update events from their own department
        role = get_current_user_role()
        current_user = get_current_user()
        if role == 'faculty' and current_user:
            user_dept = current_user.get('department')
            if event.department != user_dept:
                return jsonify({
                    'error': 'Access Denied',
                    'message': f'You are only authorized to manage events for the {user_dept} department.'
                }), 403
        
        # If transitioning to verified, trigger blockchain minting
        if new_status == 'verified' and event.status != 'verified':
            if event.action_type == 'sustainable' and event.blockchain_credits > 0:
                if blockchain_manager:
                    # Get person's wallet or fallback to pseudo-wallet
                    person = session.query(Person).filter_by(person_id=event.person_id).first()
                    wallet = person.wallet_address if person else None
                    
                    if not wallet:
                        import hashlib
                        wallet = f"0x{hashlib.sha256((event.person_id or 'unknown').encode()).hexdigest()[:40]}"
                    
                    try:
                        tx_hash = blockchain_manager.mint_credits(
                            target_address=wallet,
                            amount=float(event.blockchain_credits),
                            action_type=event.action_detected or 'Verified Action',
                            room_id=event.room_id
                        )
                        print(f"✓ Blockchain Minting Successful: {tx_hash}")
                        
                        # Update person's cumulative internal credits in DB
                        if person:
                            person.total_credits_earned += float(event.blockchain_credits or 0)
                            print(f"💰 Internal credits updated for {event.person_id}: +{event.blockchain_credits}")
                            
                    except Exception as be:
                        print(f"⚠ Blockchain Minting Failed: {be}")
        
        event.status = new_status
        session.commit()
        session.refresh(event)
        session.expunge(event)
        return jsonify(event.to_dict())
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/events/bulk-verify', methods=['POST'])
@jwt_required
@admin_required
def bulk_verify_events():
    """Bulk verify pending events with high confidence and trigger minting"""
    data = request.json or {}
    threshold = data.get('confidence_threshold', 0.8)
    
    session = db.get_session()
    try:
        # Fetch events that meet the criteria
        events_to_verify = session.query(Event).filter(
            Event.status == 'pending',
            Event.confidence >= threshold
        ).all()
        
        updated_count = 0
        for event in events_to_verify:
            # Trigger blockchain minting for sustainable actions
            if event.action_type == 'sustainable' and event.blockchain_credits > 0:
                if blockchain_manager:
                    person = session.query(Person).filter_by(person_id=event.person_id).first()
                    wallet = person.wallet_address if person else None
                    if not wallet:
                        import hashlib
                        wallet = f"0x{hashlib.sha256((event.person_id or 'unknown').encode()).hexdigest()[:40]}"
                    
                    try:
                        blockchain_manager.mint_credits(
                            target_address=wallet,
                            amount=int(event.blockchain_credits),
                            action_type=event.action_detected or 'Bulk Verified Action',
                            room_id=event.room_id
                        )
                    except Exception as be:
                        print(f"⚠ Bulk Blockchain Minting Failed for event {event.event_id}: {be}")
            
            event.status = 'verified'
            updated_count += 1
        
        session.commit()
        return jsonify({
            'success': True,
            'message': f'Successfully verified {updated_count} high-confidence events',
            'updated_count': updated_count
        })
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/outputs/<path:filename>')
def serve_output(filename):
    """Serve output files (processed images, etc)"""
    return send_from_directory(OUTPUT_FOLDER, filename)


@app.route('/uploads/<path:filename>')
def serve_upload(filename):
    """Serve uploaded video files for auditing"""
    return send_from_directory(UPLOAD_FOLDER, filename)


@app.route('/db/events/export', methods=['GET'])
@jwt_required
@admin_required
def export_events():
    """Export verified events as CSV"""
    session = db.get_session()
    try:
        events = session.query(Event).filter_by(status='verified').all()
        
        import csv
        import io
        
        output = io.StringIO()
        writer = csv.writer(output)
        
        # Headers - Terminological consistency with UX (Fidelity vs Confidence)
        writer.writerow([
            'Event ID', 'Timestamp', 'Room', 'Department', 
            'Action', 'Action Type', 'Fidelity index', 
            'Energy Saved', 'Credits'
        ])
        
        # Data
        for e in events:
            writer.writerow([
                e.event_id, 
                e.timestamp.isoformat() if hasattr(e.timestamp, 'isoformat') else e.timestamp,
                e.room_id,
                e.department,
                e.action_detected or 'unknown',
                e.action_type or 'unknown',
                f"{round((e.overall_confidence or 0) * 100)}%" if hasattr(e, 'overall_confidence') else "0%",
                f"{e.energy_saved_estimate}W",
                e.blockchain_credits
            ])
            
        output.seek(0)
        return send_file(
            io.BytesIO(output.getvalue().encode('utf-8')),
            mimetype='text/csv',
            as_attachment=True,
            download_name=f'sca_audit_export_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
        )
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/auth/users/export', methods=['GET'])
@jwt_required
@admin_required
def export_users():
    """Export all system users as CSV"""
    session = db.get_session()
    try:
        users = session.query(User).all()
        
        import csv
        import io
        
        output = io.StringIO()
        writer = csv.writer(output)
        
        # Headers
        writer.writerow([
            'User ID', 'Name', 'Email', 'Role', 
            'Department', 'Status', 'Registration Date'
        ])
        
        for user in users:
            writer.writerow([
                user.user_id,
                user.name,
                user.email,
                user.role,
                user.department,
                'Active' if user.is_active else 'Disabled',
                user.created_at.isoformat() if user.created_at else 'N/A'
            ])
            
        output.seek(0)
        return send_file(
            io.BytesIO(output.getvalue().encode('utf-8')),
            mimetype='text/csv',
            as_attachment=True,
            download_name=f'sca_users_export_{datetime.now().strftime("%Y%m%d")}.csv'
        )
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/persons', methods=['GET'])
@jwt_required
def get_db_persons():
    """Get all persons from database"""
    try:
        persons = db.get_all_persons()
        
        persons_data = []
        for person in persons:
            persons_data.append({
                'person_id': person.person_id,
                'student_id': person.student_id,
                'department': person.department,
                'user_type': person.user_type,
                'total_credits_earned': person.total_credits_earned,
                'first_seen': person.first_seen.isoformat() if person.first_seen else None,
                'last_seen': person.last_seen.isoformat() if person.last_seen else None,
                'total_detections': person.total_detections,
                'detection_method': person.detection_method,
                'face_image_path': person.face_image_path
            })
        
        return jsonify({
            'total': len(persons_data),
            'persons': persons_data
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500


@app.route('/db/persons/<person_id>', methods=['GET'])
def get_db_person(person_id):
    """Get specific person details"""
    session = db.get_session()
    try:
        person = session.query(Person).filter_by(person_id=person_id).first()
        
        if not person:
            return jsonify({'error': 'Person not found'}), 404
        
        # Get score
        score = db.get_person_score(person_id)
        
        # Expunge to prevent DetachedInstanceError
        session.expunge(person)
        
        return jsonify({
            'person_id': person.person_id,
            'first_seen': person.first_seen.isoformat() if person.first_seen else None,
            'last_seen': person.last_seen.isoformat() if person.last_seen else None,
            'total_detections': person.total_detections,
            'detection_method': person.detection_method,
            'face_image_path': person.face_image_path,
            'wallet_address': person.wallet_address,
            'total_score': score
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/persons/<person_id>/events', methods=['GET'])
def get_person_db_events(person_id):
    """Get all events for a specific person"""
    try:
        events = db.get_person_events(person_id)
        
        return jsonify({
            'person_id': person_id,
            'total_events': len(events),
            'events': [event.to_dict() for event in events]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500


@app.route('/db/persons/<person_id>/activities', methods=['GET'])
def get_person_activities(person_id):
    """Get all activities for a specific person"""
    session = db.get_session()
    try:
        activities = session.query(PersonActivity).filter_by(person_id=person_id).order_by(PersonActivity.timestamp.desc()).all()
        
        # Expunge objects to prevent DetachedInstanceError
        for activity in activities:
            session.expunge(activity)
        
        return jsonify({
            'person_id': person_id,
            'total_activities': len(activities),
            'activities': [activity.to_dict() for activity in activities]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/leaderboard', methods=['GET'])
@jwt_required
def get_db_leaderboard():
    """Get person leaderboard with scores"""
    try:
        leaderboard = db.get_leaderboard()
        
        return jsonify({
            'total_persons': len(leaderboard),
            'leaderboard': leaderboard
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500


@app.route('/db/stats', methods=['GET'])
@jwt_optional
def get_db_stats():
    """Get database statistics - optimized with selective column loading"""
    session = db.get_session()
    try:
        from sqlalchemy import func
        # Calculate verified automated impact from events
        event_credits = session.query(func.sum(Event.blockchain_credits)).filter(
            Event.blockchain_credits > 0,
            Event.status == 'verified'
        ).scalar() or 0
        total_energy_saved = session.query(func.sum(Event.energy_saved_estimate)).filter(
            Event.energy_saved_estimate > 0,
            Event.status == 'verified'
        ).scalar() or 0
        
        # Calculate manual disbursements and incentives from activities
        # Filter for positive points only (receipts/disbursements to users)
        manual_credits = session.query(func.sum(PersonActivity.incentive_points)).filter(
            PersonActivity.incentive_points > 0
        ).scalar() or 0
        
        total_credits = float(event_credits) + float(manual_credits)
        
        # Count verified automated signals + manual activity events
        total_events = session.query(Event).filter(Event.status == 'verified').count()
        total_activities = session.query(PersonActivity).count()
        
        # For the UI 'Verified Signals' count, we can combine them or keep separate
        # User requested 'disbursed by admins', so summing them makes sense
        display_signals = total_events + total_activities
        
        # Recent activity - load only necessary columns
        recent_events = session.query(Event).order_by(Event.timestamp.desc()).limit(5).all()
        
        # Expunge objects to prevent DetachedInstanceError
        for event in recent_events:
            session.expunge(event)
        
        return jsonify({
            'total_persons': session.query(Person).count(),
            'total_events': display_signals,
            'total_activities': total_activities,
            'total_credits': float(total_credits),
            'total_energy_saved': float(total_energy_saved),
            'recent_events': [event.to_dict() for event in recent_events]
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/db/admin-stats', methods=['GET'])
@jwt_required
@faculty_or_admin_required
def get_admin_stats_endpoint():
    """Get statistics for the Admin dashboard (Enforces departmental isolation for faculty)"""
    try:
        role = get_current_user_role()
        current_user = get_current_user()
        
        department = None
        if role == 'faculty' and current_user:
            department = current_user.get('department')
            
        stats = db.get_admin_stats(department=department)
        return jsonify(stats)
    except Exception as e:
        return jsonify({'error': str(e)}), 500


# ============================================================
# ENERGY ANALYTICS ENDPOINTS
# ============================================================



        


@app.route('/energy/transfer', methods=['POST'])
@app.route('/db/transfer', methods=['POST'])
@jwt_required
def transfer_credits():
    """Transfer credits between persons"""
    data = request.json
    if not data:
        return jsonify({'error': 'Missing data'}), 400
        
    sender_id = data.get('sender_id')
    recipient_id = data.get('recipient_id')
    amount = data.get('amount')
    
    if not all([sender_id, recipient_id, amount]):
        return jsonify({'error': 'Missing required fields'}), 400
        
    if sender_id == recipient_id:
        return jsonify({'error': 'Self-transfer prohibited. Assets must be routed to external nodes.'}), 400
        
    try:
        amount = float(amount)
        if amount <= 0:
            return jsonify({'error': 'Amount must be positive'}), 400
    except ValueError:
        return jsonify({'error': 'Invalid amount'}), 400
        
    session = db.get_session()
    try:
        # 1. Validate Recipient First
        recipient = session.query(Person).filter_by(person_id=recipient_id).first()
        if not recipient:
            user_exists = session.query(User).filter_by(email=recipient_id).first()
            is_eth_address = recipient_id.startswith('0x') and len(recipient_id) == 42
            if not user_exists and not is_eth_address:
                return jsonify({'error': 'Recipient not recognized. Please provide a valid decentralised ID or 0x wallet address.'}), 404
                
            recipient = Person(person_id=recipient_id, total_credits_earned=0)
            session.add(recipient)
            session.flush()

        # 2. Get or create sender and check balance
        sender = session.query(Person).filter_by(person_id=sender_id).first()
        if not sender:
            # Check if sender has earned any credits from events if no Person record exists
            total_earned = session.query(func.sum(Event.blockchain_credits)).filter_by(person_id=sender_id).scalar() or 0
            sender = Person(person_id=sender_id, total_credits_earned=total_earned)
            session.add(sender)
            session.flush()
            
        if sender.total_credits_earned < amount:
            return jsonify({'error': 'Insufficient balance. Process more energy events to earn XP.'}), 400
            
        # Perform transfer
        sender.total_credits_earned -= amount
        recipient.total_credits_earned += amount
        
        # 🔗 Attempt Actual Blockchain Transfer if wallets are linked
        blockchain_tx = None
        if blockchain_manager and sender.wallet_address and recipient.wallet_address:
            blockchain_tx = blockchain_manager.transfer_credits(
                to_address=recipient.wallet_address,
                amount=amount
            )
        
        # Generate a pseudo-hash for the UI if blockchain failed or not present
        tx_hash_val = blockchain_tx if blockchain_tx else hashlib.sha256(f"{sender_id}{recipient_id}{amount}{datetime.now()}".encode()).hexdigest()
        full_tx_hash = f"0x{tx_hash_val if blockchain_tx else tx_hash_val[:40]}"

        # Record activities
        
        # Debit log
        debit = PersonActivity(
            person_id=sender_id,
            activity_type='disbursement',
            incentive_points=-float(amount),
            incentive_reason=f'Transfer to {recipient_id}',
            details={'recipient': recipient_id, 'tx_type': 'out', 'tx_hash': full_tx_hash}
        )
        
        # Credit log
        credit = PersonActivity(
            person_id=recipient_id,
            activity_type='transfer_receipt',
            incentive_points=float(amount),
            incentive_reason=f'Transfer from {sender_id}',
            details={'sender': sender_id, 'tx_type': 'in', 'tx_hash': full_tx_hash}
        )
        
        session.add(debit)
        session.add(credit)
        session.commit()
        
        return jsonify({
            'success': True,
            'message': 'Transfer successful',
            'transaction_hash': full_tx_hash,
            'blockchain_verified': bool(blockchain_tx),
            'amount': amount
        })
        
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()





@app.route('/energy/bridge', methods=['POST'])
@jwt_required
def bridge_credits():
    """Convert internal credits to on-chain tokens (Mint/Award)"""
    user_info = get_current_user()
    email = user_info['email']
    data = request.json
    
    amount = data.get('amount')
    if not amount:
        return jsonify({'error': 'Amount is required'}), 400
        
    try:
        amount = float(amount)
        if amount <= 0:
            return jsonify({'error': 'Amount must be positive'}), 400
    except ValueError:
        return jsonify({'error': 'Invalid amount'}), 400
        
    session = db.get_session()
    try:
        person = session.query(Person).filter_by(person_id=email).first()
        if not person or person.total_credits_earned < amount:
            return jsonify({'error': 'Insufficient internal credits to bridge.'}), 400
            
        if not person.wallet_address:
            return jsonify({'error': 'No wallet address linked. Please connect MetaMask first.'}), 400
            
        # 1. Debit internal balance
        person.total_credits_earned -= amount
        
        # 2. Trigger On-Chain Award
        tx_hash = "0x_simulated"
        if blockchain_manager and blockchain_manager.is_connected:
            # Award credits on-chain
            tx_hash = blockchain_manager.mint_credits(
                target_address=person.wallet_address,
                amount=amount,
                action_type="bridge_withdrawal",
                room_id="CAMPUS_NODE"
            ) or "0x_failed"
            
        # 3. Log activity
        withdrawal = PersonActivity(
            person_id=email,
            activity_type='bridge_withdrawal',
            incentive_points=-float(amount),
            incentive_reason='Internal Credits -> Blockchain SCC',
            details={'tx_hash': tx_hash, 'wallet': person.wallet_address}
        )
        session.add(withdrawal)
        session.commit()
        
        return jsonify({
            'success': True,
            'message': 'Bridge operation initiated.',
            'transaction_hash': tx_hash,
            'amount_bridged': amount
        })
    except Exception as e:
        session.rollback()
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/energy/report', methods=['GET'])
@jwt_required
def get_energy_report():
    """Get comprehensive energy and sustainability report"""
    session = db.get_session()
    try:
        hours = request.args.get('hours', 24, type=int)
        start_time = datetime.now() - timedelta(hours=hours)
        
        # Get events in the time period
        events = session.query(Event).filter(Event.timestamp >= start_time).all()
        
        # Generate summary using EnergyAnalyzer
        event_dicts = [e.to_dict() for e in events]
        report = energy_analyzer.generate_energy_report(event_dicts, time_period_hours=hours)
        
        # Add department-wise breakdown
        from sqlalchemy import func
        dept_stats = session.query(
            Event.department,
            func.count(Event.event_id).label('total'),
            func.sum(Event.blockchain_credits).label('credits'),
            func.sum(Event.energy_saved_estimate).label('energy')
        ).filter(Event.timestamp >= start_time).group_by(Event.department).all()
        
        report['department_breakdown'] = {
            row.department or 'Unknown': {
                'events': row.total,
                'credits': round(float(row.credits or 0), 2),
                'energy_saved': round(float(row.energy or 0), 2)
            } for row in dept_stats
        }
        
        return jsonify({
            'success': True,
            'report': report
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/process/realtime', methods=['POST'])
@jwt_required
def process_realtime():
    """Start real-time inference from default video source (Webcam)"""
    data = request.json or {}
    room_id = data.get('room_id', 'CS_LAB_101')
    duration = data.get('duration_seconds', 30) # Capture for 30s by default
    
    try:
        # Initialize processor for this specific room
        processor = CVProcessor(use_database=True, db_instance=db, room_id=room_id)
        
        # In a real production app, this would be a background task
        # For this module, we run it and return the results
        results = processor.process_video(
            mode='realtime',
            confidence_threshold=data.get('confidence', 0.5),
            duration_seconds=duration
        )
        
        return jsonify({
            'success': True,
            'message': f'Inference complete for room {room_id}',
            'results': results
        })
    except Exception as e:
        return jsonify({'error': f'Real-time inference failed: {str(e)}'}), 500


@app.route('/energy/sustainable-actions', methods=['GET'])
@jwt_required
def get_sustainable_actions():
    """
    Get log of sustainable and unsustainable actions
    
    Query params:
        action_type: Filter by 'sustainable' or 'unsustainable' (optional)
        limit: Number of results (default: 50)
    """
    session = db.get_session()
    try:
        action_type = request.args.get('action_type', None)
        limit = request.args.get('limit', 50, type=int)
        
        from database import Event
        
        query = session.query(Event).filter(Event.action_type.isnot(None))
        
        if action_type:
            query = query.filter_by(action_type=action_type)
        
        actions = query.order_by(Event.timestamp.desc()).limit(limit).all()
        
        # Expunge objects to prevent DetachedInstanceError
        for action in actions:
            session.expunge(action)
        
        actions_data = []
        for action in actions:
            actions_data.append({
                'event_id': action.event_id,
                'timestamp': action.timestamp.isoformat(),
                'person_id': action.person_id,
                'room_id': action.room_id,
                'action_type': action.action_type,
                'action_detected': action.action_detected,
                'energy_saved_estimate': action.energy_saved_estimate,
                'blockchain_credits': action.blockchain_credits,
                'devices_on_count': len(json.loads(action.devices_on) if isinstance(action.devices_on, str) else action.devices_on) if action.devices_on else 0,
                'devices_off_count': len(json.loads(action.devices_off) if isinstance(action.devices_off, str) else action.devices_off) if action.devices_off else 0
            })
        
        # Calculate summary
        sustainable_count = len([a for a in actions_data if a['action_type'] == 'sustainable'])
        unsustainable_count = len([a for a in actions_data if a['action_type'] == 'unsustainable'])
        
        return jsonify({
            'total_actions': len(actions_data),
            'sustainable_actions': sustainable_count,
            'unsustainable_actions': unsustainable_count,
            'filter_applied': action_type,
            'actions': actions_data
        })
        
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/energy/live-metrics', methods=['GET'])
@jwt_required
def get_live_metrics():
    """Get real-time energy metrics from most recent events"""
    session = db.get_session()
    try:
        from database import Event
        
        # Get most recent event
        latest_event = session.query(Event).order_by(Event.timestamp.desc()).first()
        
        if not latest_event:
            return jsonify({'error': 'No events found'}), 404
        
        # Get events from last 5 minutes for trending
        five_min_ago = datetime.now() - timedelta(minutes=5)
        recent_events = session.query(Event).filter(
            Event.timestamp >= five_min_ago
        ).all()
        
        # Expunge objects to prevent DetachedInstanceError
        session.expunge(latest_event)
        for event in recent_events:
            session.expunge(event)
        
        # Calculate metrics
        total_credits_5min = sum([e.blockchain_credits or 0 for e in recent_events])
        total_energy_5min = sum([e.energy_saved_estimate or 0 for e in recent_events])
        
        devices_on = []
        if latest_event.devices_on:
             devices_on = json.loads(latest_event.devices_on) if isinstance(latest_event.devices_on, str) else latest_event.devices_on
        
        devices_off = []
        if latest_event.devices_off:
             devices_off = json.loads(latest_event.devices_off) if isinstance(latest_event.devices_off, str) else latest_event.devices_off
        
        return jsonify({
            'current_time': datetime.now().isoformat(),
            'latest_event': {
                'timestamp': latest_event.timestamp.isoformat(),
                'room_id': latest_event.room_id,
                'occupancy': latest_event.occupancy,
                'person_count': latest_event.person_count,
                'devices_on_count': len(devices_on),
                'devices_off_count': len(devices_off),
                'lights_on': latest_event.lights_on,
                'action_type': latest_event.action_type,
                'blockchain_credits': latest_event.blockchain_credits
            },
            'last_5_minutes': {
                'total_events': len(recent_events),
                'total_credits_earned': round(total_credits_5min, 2),
                'total_energy_saved_watts': round(total_energy_5min, 2)
            },
            'device_power_reference': energy_analyzer.DEVICE_POWER,
            'credit_rate_per_kwh': energy_analyzer.CREDIT_RATE_PER_KWH
        })
        
    except Exception as e:
        return jsonify({'error': str(e)}), 500
    finally:
        session.close()


@app.route('/uploads/<path:filename>')
def serve_uploads(filename):
    """Serve uploaded files (videos/images)"""
    return send_from_directory(UPLOAD_FOLDER, filename)


if __name__ == '__main__':
    print("=" * 50)
    print("SCA CV Module API Server")
    print("=" * 50)
    
    # Validate configuration
    if not config.validate():
        print("\n⚠️  WARNING: Configuration validation failed!")
        print("   Review the errors above before proceeding.\n")
    
    # Display configuration info
    import json
    print("\nEnvironment Configuration:")
    print(json.dumps(config.get_info(), indent=2))
    
    print(f"\nUpload folder: {UPLOAD_FOLDER.absolute()}")
    print(f"Output folder: {OUTPUT_FOLDER.absolute()}")
    print(f"Models folder: {MODELS_FOLDER.absolute()}")
    print("=" * 50)
    
    # Use config for Flask debug mode
    app.run(debug=config.FLASK_DEBUG, host='0.0.0.0', port=5000)