File size: 63,667 Bytes
dfd46e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import asyncio
import json
import logging
import os
import sqlite3
import socket
import time
import base64
from collections import Counter
from io import BytesIO
from datetime import datetime, timezone, timedelta
from pathlib import Path
from typing import Any, Optional

import discord
from discord import app_commands
from discord.ext import tasks
from fastapi import FastAPI
import aiohttp
import uvicorn
import requests
import yarl
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

from tracker_client import TrackerClient, TrackerError

APP_DIR = Path(__file__).resolve().parent
DB_PATH = Path(os.getenv("DB_PATH", str(APP_DIR / "rocket_watch.db")))
POLL_SECONDS = max(60, int(os.getenv("POLL_SECONDS", "120")))
ALERT_MAX_AGE_MINUTES = max(5, int(os.getenv("ALERT_MAX_AGE_MINUTES", "30")))
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN", "").strip()
GUILD_ID = os.getenv("DISCORD_GUILD_ID", "").strip()
DISCORD_LOGIN_TIMEOUT = max(5, int(os.getenv("DISCORD_LOGIN_TIMEOUT", "20")))
DISCORD_READY_TIMEOUT = max(10, int(os.getenv("DISCORD_READY_TIMEOUT", "30")))
DISCORD_RELAY_ORIGIN = (os.getenv("DISCORD_RELAY_ORIGIN") or os.getenv("DISCORD_PROXY_ORIGIN", "")).strip().rstrip("/")
DISCORD_RELAY_SECRET = (os.getenv("DISCORD_RELAY_SECRET") or os.getenv("DISCORD_PROXY_SECRET", "")).strip().strip("/")
GHT = os.getenv("GHT", "").strip()
GITHUB_DATA_REPO = os.getenv("GITHUB_DATA_REPO", "Wuhpondiscord/rldevs").strip()
GITHUB_DATA_BRANCH = os.getenv("GITHUB_DATA_BRANCH", "main").strip() or "main"
GITHUB_SYNC_ACTIVITY_LIMIT = max(100, int(os.getenv("GITHUB_SYNC_ACTIVITY_LIMIT", "5000")))
GITHUB_AUTO_SYNC_SECONDS = max(60, int(os.getenv("GITHUB_AUTO_SYNC_SECONDS", "300")))

logging.basicConfig(
    level=os.getenv("LOG_LEVEL", "INFO").upper(),
    format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
log = logging.getLogger("rocket-watch")


def configure_discord_relay() -> Optional[str]:
    """Route discord.py REST + Gateway traffic through the external Deno relay.

    The relay uses a secret path segment so it cannot be used as an open Discord
    proxy by anyone who does not know the shared secret. The bot token is not
    stored in the relay; discord.py sends it as the normal Authorization header.
    """
    if not DISCORD_RELAY_ORIGIN and not DISCORD_RELAY_SECRET:
        return None
    if not DISCORD_RELAY_ORIGIN or not DISCORD_RELAY_SECRET:
        raise RuntimeError(
            "Set both DISCORD_RELAY_ORIGIN and DISCORD_RELAY_SECRET, or neither."
        )
    if not DISCORD_RELAY_ORIGIN.startswith("https://"):
        raise RuntimeError("DISCORD_RELAY_ORIGIN must start with https://")

    from discord.http import Route
    from discord.gateway import DiscordWebSocket

    rest_base = f"{DISCORD_RELAY_ORIGIN}/p/{DISCORD_RELAY_SECRET}/api/v10"
    gateway = DISCORD_RELAY_ORIGIN.replace("https://", "wss://", 1)
    gateway = f"{gateway}/p/{DISCORD_RELAY_SECRET}/gateway"

    Route.BASE = rest_base
    DiscordWebSocket.DEFAULT_GATEWAY = yarl.URL(gateway)

    # Discord's READY event contains a direct resume_gateway_url. discord.py stores
    # that URL for later RESUME attempts. Restore our proxy URL immediately after
    # READY/RESUMED processing so reconnects never escape the relay.
    original_received_message = DiscordWebSocket.received_message

    async def proxied_received_message(self, msg):
        result = await original_received_message(self, msg)
        if getattr(self, "session_id", None) and str(getattr(self, "gateway", "")) != gateway:
            self.gateway = yarl.URL(gateway)
        return result

    if not getattr(DiscordWebSocket.received_message, "_rocket_watch_proxy_patch", False):
        proxied_received_message._rocket_watch_proxy_patch = True
        DiscordWebSocket.received_message = proxied_received_message

    log.info("Discord relay enabled through %s", DISCORD_RELAY_ORIGIN)
    return gateway


DISCORD_RELAY_GATEWAY = configure_discord_relay()


def utc_now_iso() -> str:
    return datetime.now(timezone.utc).isoformat()


def parse_dt(value: Optional[str]) -> Optional[datetime]:
    if not value:
        return None
    try:
        dt = datetime.fromisoformat(value.replace("Z", "+00:00"))
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=timezone.utc)
        return dt.astimezone(timezone.utc)
    except ValueError:
        return None


def age_text(value: Optional[str]) -> str:
    dt = parse_dt(value)
    if not dt:
        return "unknown"
    sec = max(0, int((datetime.now(timezone.utc) - dt).total_seconds()))
    if sec < 60:
        return f"{sec}s ago"
    if sec < 3600:
        return f"{sec // 60}m ago"
    if sec < 86400:
        return f"{sec // 3600}h {(sec % 3600) // 60}m ago"
    return f"{sec // 86400}d ago"


def discord_time(value: Optional[str], style: str = "F") -> str:
    """Render a timestamp in each Discord viewer's own locale/timezone."""
    dt = parse_dt(value)
    if not dt:
        return "unknown"
    return f"<t:{int(dt.timestamp())}:{style}>"


def queue_window_text(value: Optional[str]) -> str:
    """Conservative inference from Tracker collection time; never claims live presence."""
    dt = parse_dt(value)
    if not dt:
        return "Unknown"
    age_min = max(0, (datetime.now(timezone.utc) - dt).total_seconds() / 60)
    if age_min <= 3:
        return "**Very fresh result** β€” strongest window to try the same playlist/MMR"
    if age_min <= 8:
        return "**Fresh** β€” they may still be playing or re-queueing"
    if age_min <= 20:
        return "**Recent** β€” useful signal, but queue status is uncertain"
    return "**Stale** β€” weak signal for current matchmaking"


def activity_label(value: Optional[str]) -> str:
    dt = parse_dt(value)
    if not dt:
        return "UNKNOWN"
    age = (datetime.now(timezone.utc) - dt).total_seconds()
    if age <= 8 * 60:
        return "HOT"
    if age <= 20 * 60:
        return "RECENT"
    if age <= 60 * 60:
        return "COOLING"
    return "INACTIVE"


def trunc(s: str, n: int) -> str:
    return s if len(s) <= n else s[: n - 1] + "…"


class Store:
    def __init__(self, path: Path):
        self.path = path
        self.path.parent.mkdir(parents=True, exist_ok=True)
        self._init()

    def conn(self):
        conn = sqlite3.connect(self.path, timeout=30)
        conn.row_factory = sqlite3.Row
        return conn

    def _init(self):
        with self.conn() as c:
            c.executescript(
                """
                PRAGMA journal_mode=WAL;
                CREATE TABLE IF NOT EXISTS players (
                    id INTEGER PRIMARY KEY,
                    guild_id INTEGER NOT NULL,
                    label TEXT NOT NULL,
                    platform TEXT NOT NULL,
                    identifier TEXT NOT NULL,
                    handle TEXT,
                    tracker_player_id INTEGER,
                    alert_channel_id INTEGER,
                    enabled INTEGER NOT NULL DEFAULT 1,
                    created_at TEXT NOT NULL,
                    alert_watermark_at TEXT,
                    UNIQUE(guild_id, platform, identifier)
                );

                CREATE TABLE IF NOT EXISTS snapshots (
                    id INTEGER PRIMARY KEY,
                    player_id INTEGER NOT NULL,
                    observed_at TEXT NOT NULL,
                    tracker_updated_at TEXT,
                    playlist_json TEXT NOT NULL,
                    FOREIGN KEY(player_id) REFERENCES players(id)
                );
                CREATE INDEX IF NOT EXISTS idx_snapshots_player_time
                    ON snapshots(player_id, observed_at DESC);

                CREATE TABLE IF NOT EXISTS matches (
                    id INTEGER PRIMARY KEY,
                    player_id INTEGER NOT NULL,
                    external_id TEXT NOT NULL,
                    collected_at TEXT,
                    playlist TEXT,
                    result TEXT,
                    rating INTEGER,
                    rating_delta INTEGER,
                    tier TEXT,
                    division TEXT,
                    is_grouped INTEGER,
                    matches_played INTEGER,
                    wins INTEGER,
                    goals INTEGER,
                    assists INTEGER,
                    saves INTEGER,
                    shots INTEGER,
                    mvps INTEGER,
                    first_seen_at TEXT NOT NULL,
                    raw_json TEXT,
                    FOREIGN KEY(player_id) REFERENCES players(id),
                    UNIQUE(player_id, external_id)
                );
                CREATE INDEX IF NOT EXISTS idx_matches_player_collected
                    ON matches(player_id, collected_at DESC);
                """
            )
            cols = {row["name"] for row in c.execute("PRAGMA table_info(players)")}
            if "alert_watermark_at" not in cols:
                c.execute("ALTER TABLE players ADD COLUMN alert_watermark_at TEXT")

    def add_player(self, guild_id: int, label: str, platform: str, identifier: str, channel_id: int) -> int:
        with self.conn() as c:
            c.execute(
                """
                INSERT INTO players(guild_id,label,platform,identifier,alert_channel_id,created_at)
                VALUES(?,?,?,?,?,?)
                ON CONFLICT(guild_id, platform, identifier) DO UPDATE SET
                    label=excluded.label,
                    alert_channel_id=excluded.alert_channel_id,
                    enabled=1
                """,
                (guild_id, label, platform, identifier, channel_id, utc_now_iso()),
            )
            row = c.execute(
                "SELECT id FROM players WHERE guild_id=? AND platform=? AND identifier=?",
                (guild_id, platform, identifier),
            ).fetchone()
            return int(row["id"])

    def remove_player(self, guild_id: int, query: str) -> bool:
        with self.conn() as c:
            cur = c.execute(
                "DELETE FROM players WHERE guild_id=? AND (label=? COLLATE NOCASE OR identifier=? COLLATE NOCASE)",
                (guild_id, query, query),
            )
            return cur.rowcount > 0

    def players(self, guild_id: Optional[int] = None):
        with self.conn() as c:
            if guild_id is None:
                return c.execute("SELECT * FROM players WHERE enabled=1 ORDER BY guild_id,label").fetchall()
            return c.execute(
                "SELECT * FROM players WHERE enabled=1 AND guild_id=? ORDER BY label COLLATE NOCASE",
                (guild_id,),
            ).fetchall()

    def get_player(self, guild_id: int, query: str):
        with self.conn() as c:
            return c.execute(
                """
                SELECT * FROM players
                WHERE guild_id=? AND enabled=1
                  AND (label=? COLLATE NOCASE OR handle=? COLLATE NOCASE OR identifier=? COLLATE NOCASE)
                LIMIT 1
                """,
                (guild_id, query, query, query),
            ).fetchone()

    def update_identity(self, player_id: int, handle: str, tracker_player_id: Optional[int]):
        with self.conn() as c:
            c.execute(
                "UPDATE players SET handle=?, tracker_player_id=? WHERE id=?",
                (handle, tracker_player_id, player_id),
            )

    def set_alert_watermark(self, player_id: int, value: Optional[str]) -> None:
        if not value:
            return
        with self.conn() as c:
            c.execute("UPDATE players SET alert_watermark_at=? WHERE id=?", (value, player_id))

    def ensure_alert_watermark(self, player_id: int) -> Optional[str]:
        with self.conn() as c:
            row = c.execute("SELECT alert_watermark_at, created_at FROM players WHERE id=?", (player_id,)).fetchone()
            if not row:
                return None
            if row["alert_watermark_at"]:
                return row["alert_watermark_at"]
            latest = c.execute(
                "SELECT collected_at FROM matches WHERE player_id=? AND collected_at IS NOT NULL ORDER BY collected_at DESC LIMIT 1",
                (player_id,),
            ).fetchone()
            value = latest["collected_at"] if latest else row["created_at"]
            c.execute("UPDATE players SET alert_watermark_at=? WHERE id=?", (value, player_id))
            return value

    def add_snapshot(self, player_id: int, profile) -> None:
        with self.conn() as c:
            c.execute(
                "INSERT INTO snapshots(player_id,observed_at,tracker_updated_at,playlist_json) VALUES(?,?,?,?)",
                (
                    player_id,
                    utc_now_iso(),
                    profile.last_updated.isoformat() if profile.last_updated else None,
                    json.dumps(profile.playlists, separators=(",", ":")),
                ),
            )

    def latest_snapshot(self, player_id: int):
        with self.conn() as c:
            return c.execute(
                "SELECT * FROM snapshots WHERE player_id=? ORDER BY observed_at DESC LIMIT 1",
                (player_id,),
            ).fetchone()

    def insert_matches(self, player_id: int, matches: list[dict[str, Any]]) -> list[dict[str, Any]]:
        newly_added: list[dict[str, Any]] = []
        with self.conn() as c:
            for m in matches:
                ext = m.get("match_id")
                if not ext:
                    continue
                cur = c.execute(
                    """
                    INSERT OR IGNORE INTO matches(
                        player_id,external_id,collected_at,playlist,result,rating,rating_delta,tier,division,
                        is_grouped,matches_played,wins,goals,assists,saves,shots,mvps,first_seen_at,raw_json
                    ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                    """,
                    (
                        player_id, ext, m.get("collected_at"), m.get("playlist"), m.get("result"),
                        m.get("rating"), m.get("rating_delta"), m.get("tier"), m.get("division"),
                        int(bool(m.get("is_grouped"))), m.get("matches_played"), m.get("wins"),
                        m.get("goals"), m.get("assists"), m.get("saves"), m.get("shots"), m.get("mvps"),
                        utc_now_iso(), json.dumps(m, separators=(",", ":")),
                    ),
                )
                if cur.rowcount:
                    newly_added.append(m)
        return newly_added

    def recent_matches(self, player_id: int, limit: int = 10):
        with self.conn() as c:
            return c.execute(
                "SELECT * FROM matches WHERE player_id=? ORDER BY collected_at DESC LIMIT ?",
                (player_id, limit),
            ).fetchall()

    def latest_match(self, player_id: int):
        rows = self.recent_matches(player_id, 1)
        return rows[0] if rows else None

    def matches_since(self, player_id: int, since_iso: str):
        with self.conn() as c:
            return c.execute(
                """
                SELECT * FROM matches
                WHERE player_id=? AND collected_at IS NOT NULL AND collected_at>=?
                ORDER BY collected_at ASC, id ASC
                """,
                (player_id, since_iso),
            ).fetchall()


    def export_players(self) -> list[dict[str, Any]]:
        with self.conn() as c:
            rows = c.execute("SELECT * FROM players ORDER BY guild_id,label COLLATE NOCASE").fetchall()
        return [dict(r) for r in rows]

    def export_matches(self, limit: int = 5000) -> list[dict[str, Any]]:
        with self.conn() as c:
            rows = c.execute(
                """
                SELECT p.guild_id,p.platform,p.identifier,p.label,m.*
                FROM matches m JOIN players p ON p.id=m.player_id
                ORDER BY m.collected_at DESC, m.id DESC LIMIT ?
                """,
                (limit,),
            ).fetchall()
        out = []
        for r in rows:
            d = dict(r)
            d.pop("id", None)
            d.pop("player_id", None)
            out.append(d)
        return out

    def restore_player(self, item: dict[str, Any]) -> Optional[int]:
        try:
            guild_id = int(item["guild_id"])
            platform = str(item["platform"])
            identifier = str(item["identifier"])
            label = str(item.get("label") or identifier)
            channel_id = int(item.get("alert_channel_id") or 0)
        except (KeyError, TypeError, ValueError):
            return None
        with self.conn() as c:
            c.execute(
                """
                INSERT INTO players(
                    guild_id,label,platform,identifier,handle,tracker_player_id,
                    alert_channel_id,enabled,created_at,alert_watermark_at
                ) VALUES(?,?,?,?,?,?,?,?,?,?)
                ON CONFLICT(guild_id,platform,identifier) DO UPDATE SET
                    label=excluded.label,
                    handle=COALESCE(excluded.handle,players.handle),
                    tracker_player_id=COALESCE(excluded.tracker_player_id,players.tracker_player_id),
                    alert_channel_id=CASE WHEN excluded.alert_channel_id>0 THEN excluded.alert_channel_id ELSE players.alert_channel_id END,
                    enabled=excluded.enabled,
                    alert_watermark_at=COALESCE(excluded.alert_watermark_at,players.alert_watermark_at)
                """,
                (
                    guild_id,label,platform,identifier,item.get("handle"),item.get("tracker_player_id"),
                    channel_id,int(item.get("enabled",1)),item.get("created_at") or utc_now_iso(),item.get("alert_watermark_at"),
                ),
            )
            row=c.execute(
                "SELECT id FROM players WHERE guild_id=? AND platform=? AND identifier=?",
                (guild_id,platform,identifier),
            ).fetchone()
            return int(row["id"]) if row else None

    def restore_match(self, player_id: int, item: dict[str, Any]) -> None:
        ext = item.get("external_id") or item.get("match_id")
        if not ext:
            return
        with self.conn() as c:
            c.execute(
                """
                INSERT OR IGNORE INTO matches(
                    player_id,external_id,collected_at,playlist,result,rating,rating_delta,tier,division,
                    is_grouped,matches_played,wins,goals,assists,saves,shots,mvps,first_seen_at,raw_json
                ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                """,
                (
                    player_id,ext,item.get("collected_at"),item.get("playlist"),item.get("result"),
                    item.get("rating"),item.get("rating_delta"),item.get("tier"),item.get("division"),
                    int(bool(item.get("is_grouped"))),item.get("matches_played"),item.get("wins"),
                    item.get("goals"),item.get("assists"),item.get("saves"),item.get("shots"),item.get("mvps"),
                    item.get("first_seen_at") or utc_now_iso(),item.get("raw_json") or json.dumps(item,separators=(",",":")),
                ),
            )


class GitHubDataStore:
    API = "https://api.github.com"

    def __init__(self, token: str, repo: str, branch: str = "main"):
        self.token = token
        self.repo = repo
        self.branch = branch
        self.enabled = bool(token and repo)
        self.session = requests.Session()
        if self.enabled:
            self.session.headers.update({
                "Authorization": f"Bearer {token}",
                "Accept": "application/vnd.github+json",
                "X-GitHub-Api-Version": "2022-11-28",
                "User-Agent": "rocket-watch-hf/1.0",
            })
        self.last_sync_at: Optional[str] = None
        self.last_error: Optional[str] = None

    def _url(self, path: str) -> str:
        return f"{self.API}/repos/{self.repo}/contents/{path}"

    def read_json(self, path: str, default: dict[str, Any]) -> tuple[dict[str, Any], Optional[str]]:
        if not self.enabled:
            return default, None
        r = self.session.get(self._url(path), params={"ref": self.branch}, timeout=20)
        if r.status_code == 404:
            return default, None
        r.raise_for_status()
        payload = r.json()
        raw = base64.b64decode(payload.get("content", "")).decode("utf-8")
        return json.loads(raw), payload.get("sha")

    def write_json(self, path: str, data: dict[str, Any], message: str) -> None:
        if not self.enabled:
            return
        _, sha = self.read_json(path, {})
        body = {
            "message": message,
            "content": base64.b64encode((json.dumps(data, indent=2, sort_keys=False) + "\n").encode()).decode(),
            "branch": self.branch,
        }
        if sha:
            body["sha"] = sha
        r = self.session.put(self._url(path), json=body, timeout=30)
        r.raise_for_status()

    def ensure_files(self) -> None:
        if not self.enabled:
            return
        defaults = {
            "datasets/users.json": {"schema_version": 1, "updated_at": None, "players": []},
            "datasets/activity.json": {"schema_version": 1, "updated_at": None, "matches": []},
        }
        for path, default in defaults.items():
            data, sha = self.read_json(path, default)
            if sha is None:
                self.write_json(path, data, f"Create {path}")

    def restore_into(self, store: Store) -> dict[str, int]:
        if not self.enabled:
            return {"players": 0, "matches": 0}
        users, _ = self.read_json("datasets/users.json", {"players": []})
        activity, _ = self.read_json("datasets/activity.json", {"matches": []})
        key_to_id: dict[tuple[int,str,str], int] = {}
        restored_players = 0
        for item in users.get("players") or []:
            pid = store.restore_player(item)
            if pid is not None:
                key_to_id[(int(item["guild_id"]), str(item["platform"]), str(item["identifier"]))] = pid
                restored_players += 1
        restored_matches = 0
        for item in activity.get("matches") or []:
            try:
                key = (int(item["guild_id"]), str(item["platform"]), str(item["identifier"]))
            except (KeyError, TypeError, ValueError):
                continue
            pid = key_to_id.get(key)
            if pid is None:
                row = store.get_player(key[0], key[2])
                pid = int(row["id"]) if row else None
            if pid is not None:
                store.restore_match(pid, item)
                restored_matches += 1
        self.last_sync_at = utc_now_iso()
        self.last_error = None
        return {"players": restored_players, "matches": restored_matches}

    def push_all(self, store: Store, reason: str = "Sync RL dev tracker datasets") -> None:
        if not self.enabled:
            return
        now = utc_now_iso()
        users = {"schema_version": 1, "updated_at": now, "players": store.export_players()}
        activity = {"schema_version": 1, "updated_at": now, "matches": store.export_matches(GITHUB_SYNC_ACTIVITY_LIMIT)}
        self.write_json("datasets/users.json", users, f"{reason}: users")
        self.write_json("datasets/activity.json", activity, f"{reason}: activity")
        self.last_sync_at = now
        self.last_error = None


github_data = GitHubDataStore(GHT, GITHUB_DATA_REPO, GITHUB_DATA_BRANCH)


store = Store(DB_PATH)
tracker = TrackerClient()


async def github_push(reason: str = "Sync RL dev tracker datasets") -> bool:
    if not github_data.enabled:
        return False
    try:
        await asyncio.to_thread(github_data.push_all, store, reason)
        return True
    except Exception as exc:
        github_data.last_error = f"{type(exc).__name__}: {exc}"
        log.warning("GitHub dataset sync failed: %s", exc)
        return False


intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

# Runtime state used by the health endpoint.
runtime = {
    "discord_connected": False,
    "discord_user": None,
    "discord_last_error": None,
    "discord_last_attempt": None,
    "discord_last_connected": None,
    "discord_retry_seconds": None,
    "discord_stage": "idle",
    "discord_stage_since": None,
}


async def fetch_and_store(player_row, *, seed: bool = False) -> tuple[Any, list[dict[str, Any]]]:
    profile = await asyncio.to_thread(tracker.profile, player_row["platform"], player_row["identifier"])
    sessions_payload = await asyncio.to_thread(tracker.sessions, player_row["platform"], player_row["identifier"])
    matches = tracker.flatten_session_matches(sessions_payload)

    store.update_identity(player_row["id"], profile.handle, profile.player_id)
    store.add_snapshot(player_row["id"], profile)
    new_matches = store.insert_matches(player_row["id"], matches)

    # On initial add, establish a watermark at the newest historical match. Tracker can
    # reveal older/grouped rows later, and those must never be announced as live activity.
    if seed:
        dated = [m for m in matches if parse_dt(m.get("collected_at"))]
        if dated:
            newest = max(dated, key=lambda m: parse_dt(m.get("collected_at")))
            store.set_alert_watermark(player_row["id"], newest.get("collected_at"))
        else:
            store.set_alert_watermark(player_row["id"], utc_now_iso())
        return profile, []

    return profile, new_matches


def profile_embed(player_row, profile, latest_match=None) -> discord.Embed:
    e = discord.Embed(
        title=f"{profile.handle} β€” Rocket League",
        description=f"{profile.platform.upper()} β€’ Season {profile.season or '?'}",
        color=discord.Color.blurple(),
        timestamp=datetime.now(timezone.utc),
    )
    if profile.avatar_url:
        e.set_thumbnail(url=profile.avatar_url)

    if latest_match:
        e.add_field(
            name="Activity",
            value=f"**{activity_label(latest_match['collected_at'])}** β€’ {discord_time(latest_match['collected_at'], 'R')}\n{discord_time(latest_match['collected_at'], 'f')}",
            inline=True,
        )
        e.add_field(name="Last mode", value=latest_match["playlist"] or "Unknown", inline=True)
        if latest_match["rating"] is not None:
            delta = latest_match["rating_delta"]
            ds = f" ({delta:+d})" if delta is not None else ""
            e.add_field(name="Last MMR", value=f"{latest_match['rating']}{ds}", inline=True)

    ranked = [p for p in profile.playlists if p.get("rating") is not None]
    ranked.sort(key=lambda p: ("Ranked" not in (p.get("name") or ""), p.get("name") or ""))
    for p in ranked[:10]:
        rank = " ".join(x for x in [p.get("tier"), p.get("division")] if x) or "β€”"
        peak = f" β€’ peak {p['peak_rating']}" if p.get("peak_rating") is not None else ""
        e.add_field(
            name=trunc(p.get("name") or "Playlist", 256),
            value=f"**{p['rating']} MMR** β€’ {rank}{peak}",
            inline=False,
        )
    e.set_footer(text="Tracker collection times can lag actual in-game activity.")
    return e


def match_embed(player_row, match: dict[str, Any]) -> discord.Embed:
    rating = match.get("rating")
    delta = match.get("rating_delta")
    mmr = "Unknown"
    if rating is not None:
        mmr = str(rating)
        if delta is not None:
            mmr += f" ({delta:+d})"
    result = match.get("result") or "activity"
    e = discord.Embed(
        title=f"New RL activity β€” {player_row['label']}",
        description=f"**{match.get('playlist') or 'Unknown playlist'}** β€’ {result}",
        color=discord.Color.green() if str(result).lower() == "victory" else discord.Color.orange(),
        timestamp=parse_dt(match.get("collected_at")) or datetime.now(timezone.utc),
    )
    e.add_field(name="MMR", value=mmr, inline=True)
    rank = " ".join(x for x in [match.get("tier"), match.get("division")] if x)
    if rank:
        e.add_field(name="Rank", value=rank, inline=True)
    e.add_field(
        name="Observed",
        value=f"{discord_time(match.get('collected_at'), 'R')}\n{discord_time(match.get('collected_at'), 'f')}",
        inline=True,
    )
    e.add_field(name="Matchmaking signal", value=queue_window_text(match.get("collected_at")), inline=False)
    stats = []
    for k, label in [("goals", "G"), ("assists", "A"), ("saves", "S"), ("shots", "Shots")]:
        if match.get(k) is not None:
            stats.append(f"{label}: {match[k]}")
    if stats:
        e.add_field(name="Stats", value=" β€’ ".join(stats), inline=False)
    e.set_footer(text="Activity is inferred from Tracker updates; it is not a direct online-status signal.")
    return e




def build_activity_graph(matches, label: str, range_days: int, style: str = "bar") -> tuple[BytesIO, dict[str, Any], str]:
    valid = []
    for m in matches:
        if m["is_grouped"]:
            continue
        dt = parse_dt(m["collected_at"])
        if not dt:
            continue
        valid.append((m, dt))
    if not valid:
        raise ValueError("No non-grouped matches in the selected time range.")

    hourly = [0] * 24
    dow_hour = [[0 for _ in range(24)] for _ in range(7)]
    playlist_counter: Counter[str] = Counter()
    active_days = set()
    for m, dt in valid:
        hourly[dt.hour] += 1
        dow_hour[dt.weekday()][dt.hour] += 1
        playlist_counter[m["playlist"] or "Unknown"] += 1
        active_days.add(dt.date().isoformat())

    top_hour = max(range(24), key=lambda h: hourly[h])
    top_hour_count = hourly[top_hour]
    top_day_index = max(range(7), key=lambda d: sum(dow_hour[d]))
    day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
    top_day_count = sum(dow_hour[top_day_index])
    avg_per_active_day = len(valid) / max(1, len(active_days))
    top_playlist, top_playlist_count = playlist_counter.most_common(1)[0]

    fig = plt.figure(figsize=(10.5, 4.8 if style == "bar" else 5.8))
    ax = fig.add_subplot(111)
    title = f"{label} activity by time of day β€” last {range_days} day(s)"

    if style == "heatmap":
        im = ax.imshow(dow_hour, aspect="auto", interpolation="nearest")
        ax.set_title(title + " (UTC)")
        ax.set_xlabel("Hour of day (UTC)")
        ax.set_ylabel("Day of week")
        ax.set_xticks(range(0, 24, 1))
        ax.set_xticklabels([str(h) for h in range(24)], fontsize=8)
        ax.set_yticks(range(7))
        ax.set_yticklabels(day_names)
        fig.colorbar(im, ax=ax, label="Observed matches")
        caption = "Heatmap uses UTC. Static images cannot auto-convert to each viewer's local timezone."
    else:
        ax.bar(list(range(24)), hourly)
        ax.set_title(title + " (UTC)")
        ax.set_xlabel("Hour of day (UTC)")
        ax.set_ylabel("Observed matches")
        ax.set_xticks(range(24))
        ax.set_xlim(-0.6, 23.6)
        ax.grid(True, axis="y", alpha=0.3)
        caption = "Bar chart uses UTC. Static images cannot auto-convert to each viewer's local timezone."

    fig.tight_layout()
    buf = BytesIO()
    fig.savefig(buf, format="png", dpi=170, bbox_inches="tight")
    plt.close(fig)
    buf.seek(0)

    stats = {
        "match_count": len(valid),
        "active_days": len(active_days),
        "avg_per_active_day": avg_per_active_day,
        "top_hour": top_hour,
        "top_hour_count": top_hour_count,
        "top_day": day_names[top_day_index],
        "top_day_count": top_day_count,
        "top_playlist": top_playlist,
        "top_playlist_count": top_playlist_count,
    }
    return buf, stats, caption

@client.event
async def on_ready():
    runtime["discord_connected"] = True
    runtime["discord_user"] = str(client.user) if client.user else None
    runtime["discord_last_connected"] = utc_now_iso()
    runtime["discord_last_error"] = None
    runtime["discord_retry_seconds"] = None
    log.info("Logged in as %s (%s)", client.user, client.user.id if client.user else "?")
    if not watcher.is_running():
        watcher.change_interval(seconds=POLL_SECONDS)
        watcher.start()
    if github_data.enabled and not github_auto_sync.is_running():
        github_auto_sync.change_interval(seconds=GITHUB_AUTO_SYNC_SECONDS)
        github_auto_sync.start()


@client.event
async def on_disconnect():
    runtime["discord_connected"] = False




@tree.command(name="help", description="Explain the tracker commands, alerts, and GitHub persistence")
async def help_cmd(interaction: discord.Interaction):
    e = discord.Embed(
        title="Dev Stalker β€” Help",
        description=(
            "Tracks public Rocket League Tracker activity for selected accounts. "
            "Tracker data can lag, so freshness is an inference rather than direct online/queue status."
        ),
        color=discord.Color.blurple(),
    )
    e.add_field(
        name="Tracking",
        value=(
            "`/track_add` β€” add an Epic/Steam/Xbox/PSN account and alert this channel.\n"
            "`/track_remove` β€” stop tracking an account.\n"
            "`/track_list` β€” list accounts tracked in this server.\n"
            "`/rl_refresh` β€” force an immediate Tracker refresh for all tracked accounts."
        ),
        inline=False,
    )
    e.add_field(
        name="Player info",
        value=(
            "`/rl_player` β€” current profile, ranks/MMR, and latest observed activity.\n"
            "`/rl_recent` β€” recent recorded matches/activity with local Discord timestamps.\n"
            "`/activity_graph` β€” graph the hours/day patterns for one tracked target or all tracked targets over a time range.\n"
            "`/watch_status` β€” compact playlist/MMR/freshness view for one target.\n"
            "`/watch_hot` β€” rank tracked targets by freshest observed activity."
        ),
        inline=False,
    )
    e.add_field(
        name="GitHub persistence",
        value=(
            "GitHub backup is automatic. Adds/removes/new activity trigger immediate syncs, and a "
            f"background safety sync runs about every **{GITHUB_AUTO_SYNC_SECONDS // 60 or 1} minute(s)**.\n"
            "`/github_status` β€” show persistence health and last successful sync.\n"
            "`/github_sync` β€” manually force a backup if you suspect something went wrong."
        ),
        inline=False,
    )
    e.add_field(
        name="Alert behavior",
        value=(
            "Historical matches are seeded silently when a player is first added. Grouped `Multiple` rows are "
            "kept in history but suppressed from live alerts. Discord timestamps render in each viewer's local timezone."
        ),
        inline=False,
    )
    e.set_footer(text="Use /github_status if persistence ever looks unhealthy.")
    await interaction.response.send_message(embed=e, ephemeral=True)


@tree.command(name="track_add", description="Track a Rocket League account and alert this channel on new activity")
@app_commands.describe(platform="steam, epic, xbox, or psn", identifier="Tracker-compatible account identifier", label="Short name shown by the bot")
async def track_add(interaction: discord.Interaction, platform: str, identifier: str, label: Optional[str] = None):
    if not interaction.guild_id or not interaction.channel_id:
        await interaction.response.send_message("Use this command inside a server channel.", ephemeral=True)
        return
    platform = platform.strip().lower()
    aliases = {"xbox": "xbl", "playstation": "psn", "epicgames": "epic"}
    platform = aliases.get(platform, platform)
    if platform not in {"steam", "epic", "xbl", "psn"}:
        await interaction.response.send_message("Platform must be `steam`, `epic`, `xbl`, or `psn`.", ephemeral=True)
        return

    await interaction.response.defer(ephemeral=True)
    row_id = store.add_player(interaction.guild_id, label or identifier, platform, identifier.strip(), interaction.channel_id)
    row = store.get_player(interaction.guild_id, label or identifier)
    if not row:
        # If label changed on an existing record, fetch by identifier.
        row = store.get_player(interaction.guild_id, identifier.strip())
    try:
        profile, _ = await fetch_and_store(row, seed=True)
    except TrackerError as exc:
        store.remove_player(interaction.guild_id, identifier.strip())
        await interaction.followup.send(f"Could not load that Tracker profile: `{exc}`", ephemeral=True)
        return

    await github_push("Track add")
    await interaction.followup.send(
        f"Tracking **{profile.handle}** as **{label or profile.handle}**. New activity will be posted in <#{interaction.channel_id}>. Historical sessions were seeded without alerts.",
        ephemeral=True,
    )


@tree.command(name="track_remove", description="Stop tracking a Rocket League account")
async def track_remove(interaction: discord.Interaction, player: str):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    ok = store.remove_player(interaction.guild_id, player.strip())
    if ok:
        await github_push("Track remove")
    await interaction.response.send_message("Removed." if ok else "No matching tracked player.", ephemeral=True)


@tree.command(name="track_list", description="List Rocket League accounts tracked in this server")
async def track_list(interaction: discord.Interaction):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    rows = store.players(interaction.guild_id)
    if not rows:
        await interaction.response.send_message("No players are being tracked yet.", ephemeral=True)
        return
    lines = []
    for r in rows:
        last = store.latest_match(r["id"])
        status = activity_label(last["collected_at"]) if last else "UNKNOWN"
        mode = last["playlist"] if last else "no session data"
        lines.append(f"β€’ **{r['label']}** β€” `{r['platform']}` / `{r['identifier']}` β€” **{status}** β€” {mode}")
    await interaction.response.send_message("\n".join(lines), ephemeral=True)


@tree.command(name="rl_player", description="Show current MMRs and recent activity for a tracked player")
async def rl_player(interaction: discord.Interaction, player: str):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    row = store.get_player(interaction.guild_id, player.strip())
    if not row:
        await interaction.response.send_message("Player not found. Use `/track_list`.", ephemeral=True)
        return
    await interaction.response.defer()
    try:
        profile, _ = await fetch_and_store(row, seed=False)
    except TrackerError as exc:
        await interaction.followup.send(f"Tracker request failed: `{exc}`")
        return
    latest = store.latest_match(row["id"])
    await interaction.followup.send(embed=profile_embed(row, profile, latest))


@tree.command(name="rl_recent", description="Show a tracked player's recent recorded matches/activity")
@app_commands.describe(count="Number of entries to show (1-10)")
async def rl_recent(interaction: discord.Interaction, player: str, count: app_commands.Range[int, 1, 10] = 5):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    row = store.get_player(interaction.guild_id, player.strip())
    if not row:
        await interaction.response.send_message("Player not found. Use `/track_list`.", ephemeral=True)
        return
    matches = store.recent_matches(row["id"], count)
    if not matches:
        await interaction.response.send_message("No session records stored yet.")
        return
    lines = []
    for m in matches:
        mmr = "?" if m["rating"] is None else str(m["rating"])
        if m["rating_delta"] is not None:
            mmr += f" ({m['rating_delta']:+d})"
        lines.append(
            f"**{m['playlist'] or 'Unknown'}** β€” {m['result'] or 'activity'} β€” `{mmr} MMR` β€” "
            f"{discord_time(m['collected_at'], 'R')} ({discord_time(m['collected_at'], 't')})"
        )
    e = discord.Embed(title=f"Recent activity β€” {row['label']}", description="\n".join(lines), color=discord.Color.blurple())
    await interaction.response.send_message(embed=e)


@tree.command(name="rl_refresh", description="Refresh all tracked players in this server now")
async def rl_refresh(interaction: discord.Interaction):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    await interaction.response.defer(ephemeral=True)
    rows = store.players(interaction.guild_id)
    ok = 0
    errors = []
    for row in rows:
        try:
            await fetch_and_store(row, seed=False)
            ok += 1
        except TrackerError as exc:
            errors.append(f"{row['label']}: {exc}")
    if ok:
        await github_push("Manual refresh")
    msg = f"Refreshed {ok}/{len(rows)} tracked players."
    if errors:
        msg += "\n" + "\n".join(f"β€’ {trunc(e, 160)}" for e in errors[:5])
    await interaction.followup.send(msg, ephemeral=True)


@tree.command(name="watch_status", description="Show the most useful current matchmaking signal for a tracked player")
async def watch_status(interaction: discord.Interaction, player: str):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    row = store.get_player(interaction.guild_id, player.strip())
    if not row:
        await interaction.response.send_message("Player not found. Use `/track_list`.", ephemeral=True)
        return
    latest = store.latest_match(row["id"])
    if not latest:
        await interaction.response.send_message("No activity has been recorded for that player yet.")
        return
    rating = "?" if latest["rating"] is None else str(latest["rating"])
    if latest["rating_delta"] is not None:
        rating += f" ({latest['rating_delta']:+d})"
    e = discord.Embed(title=f"Watch status β€” {row['label']}", color=discord.Color.blurple())
    e.add_field(name="Playlist", value=latest["playlist"] or "Unknown", inline=True)
    e.add_field(name="MMR", value=rating, inline=True)
    e.add_field(name="Freshness", value=activity_label(latest["collected_at"]), inline=True)
    e.add_field(name="Observed", value=f"{discord_time(latest['collected_at'],'R')}\n{discord_time(latest['collected_at'],'f')}", inline=False)
    e.add_field(name="Matchmaking signal", value=queue_window_text(latest["collected_at"]), inline=False)
    e.set_footer(text="Tracker collection times can lag and do not prove the player is currently queued.")
    await interaction.response.send_message(embed=e)


@tree.command(name="watch_hot", description="List tracked players with the freshest recent activity")
async def watch_hot(interaction: discord.Interaction):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    entries=[]
    for row in store.players(interaction.guild_id):
        last=store.latest_match(row["id"])
        if not last:
            continue
        dt=parse_dt(last["collected_at"])
        if not dt:
            continue
        entries.append((dt,row,last))
    if not entries:
        await interaction.response.send_message("No tracked activity is stored yet.", ephemeral=True)
        return
    entries.sort(key=lambda x:x[0], reverse=True)
    lines=[]
    for _,row,last in entries[:15]:
        mmr="?" if last["rating"] is None else str(last["rating"])
        lines.append(f"**{row['label']}** β€” {last['playlist'] or 'Unknown'} β€” `{mmr} MMR` β€” **{activity_label(last['collected_at'])}** β€” {discord_time(last['collected_at'],'R')}")
    await interaction.response.send_message(embed=discord.Embed(title="Freshest tracked activity",description="\n".join(lines),color=discord.Color.blurple()))


@tree.command(name="activity_graph", description="Graph when a tracked player or all tracked players are most active")
@app_commands.describe(player="Tracked player label/handle/identifier, or leave empty to graph all tracked players", range_days="Trailing days to analyze (1-365)", style="Recommended graph style")
@app_commands.choices(style=[
    app_commands.Choice(name="Hourly bar chart (recommended)", value="bar"),
    app_commands.Choice(name="Day Γ— hour heatmap", value="heatmap"),
])
async def activity_graph(
    interaction: discord.Interaction,
    player: Optional[str] = None,
    range_days: app_commands.Range[int, 1, 365] = 30,
    style: app_commands.Choice[str] | None = None,
):
    if not interaction.guild_id:
        await interaction.response.send_message("Use this in a server.", ephemeral=True)
        return
    await interaction.response.defer()
    since_dt = datetime.now(timezone.utc) - timedelta(days=int(range_days))

    selected_label = None
    matches = []
    tracked_count = 0

    if player and player.strip():
        row = store.get_player(interaction.guild_id, player.strip())
        if not row:
            await interaction.followup.send("Player not found. Use `/track_list`.")
            return
        selected_label = row["label"]
        matches = store.matches_since(row["id"], since_dt.isoformat())
        tracked_count = 1
    else:
        rows = store.players(interaction.guild_id)
        tracked_count = len(rows)
        if not rows:
            await interaction.followup.send("No players are being tracked yet.")
            return
        selected_label = f"All tracked players ({tracked_count})"
        for row in rows:
            matches.extend(store.matches_since(row["id"], since_dt.isoformat()))

    if not matches:
        await interaction.followup.send("No stored activity falls inside that time range yet.")
        return

    chosen_style = style.value if style else "bar"
    try:
        image_buf, stats, caption = build_activity_graph(matches, selected_label, int(range_days), chosen_style)
    except ValueError as exc:
        await interaction.followup.send(str(exc))
        return

    safe_name = selected_label.replace(' ', '_').replace('(', '').replace(')', '').replace('/', '_')
    filename = f"activity_{safe_name}_{chosen_style}.png"
    file = discord.File(image_buf, filename=filename)
    embed = discord.Embed(
        title=f"Activity graph β€” {selected_label}",
        description=(
            f"Observed public Tracker activity over the last **{int(range_days)} day(s)**. "
            "Use this as a tendency signal, not exact online status."
        ),
        color=discord.Color.blurple(),
    )
    embed.add_field(name="Scope", value=selected_label, inline=False)
    embed.add_field(name="Top hour (UTC)", value=f"`{stats['top_hour']:02d}:00–{(stats['top_hour'] + 1) % 24:02d}:00` β€’ {stats['top_hour_count']} matches", inline=True)
    embed.add_field(name="Most active day", value=f"**{stats['top_day']}** β€’ {stats['top_day_count']} matches", inline=True)
    embed.add_field(name="Top playlist", value=f"**{stats['top_playlist']}** β€’ {stats['top_playlist_count']} matches", inline=True)
    embed.add_field(name="Observed matches", value=str(stats['match_count']), inline=True)
    embed.add_field(name="Active days", value=str(stats['active_days']), inline=True)
    embed.add_field(name="Avg / active day", value=f"{stats['avg_per_active_day']:.2f}", inline=True)
    embed.set_image(url=f"attachment://{filename}")
    embed.set_footer(text=caption)
    await interaction.followup.send(embed=embed, file=file)


@tree.command(name="github_sync", description="Manually sync the bot datasets to GitHub")
async def github_sync_cmd(interaction: discord.Interaction):
    await interaction.response.defer(ephemeral=True)
    if not github_data.enabled:
        await interaction.followup.send("GitHub persistence is disabled. Add `GHT` as a Hugging Face secret.", ephemeral=True)
        return
    ok=await github_push("Manual Discord sync")
    if ok:
        await interaction.followup.send(f"Synced datasets to `{GITHUB_DATA_REPO}`.", ephemeral=True)
    else:
        await interaction.followup.send(f"GitHub sync failed: `{trunc(github_data.last_error or 'unknown error',300)}`", ephemeral=True)


@tree.command(name="github_status", description="Show GitHub persistence status")
async def github_status_cmd(interaction: discord.Interaction):
    state="enabled" if github_data.enabled else "disabled"
    msg=(f"GitHub persistence: **{state}**\nRepo: `{GITHUB_DATA_REPO}`\nBranch: `{GITHUB_DATA_BRANCH}`\n"
         f"Last sync: {discord_time(github_data.last_sync_at,'R') if github_data.last_sync_at else 'never'}")
    if github_data.last_error:
        msg += f"\nLast error: `{trunc(github_data.last_error,300)}`"
    await interaction.response.send_message(msg, ephemeral=True)


@tasks.loop(seconds=300)
async def github_auto_sync():
    if not github_data.enabled:
        return
    ok = await github_push("Automatic safety sync")
    if ok:
        log.info("Automatic GitHub dataset sync completed")


@github_auto_sync.before_loop
async def before_github_auto_sync():
    await client.wait_until_ready()


@tasks.loop(seconds=120)
async def watcher():
    rows = store.players()
    for row in rows:
        try:
            profile, new_matches = await fetch_and_store(row, seed=False)
            if not new_matches:
                continue

            watermark_raw = store.ensure_alert_watermark(row["id"])
            watermark = parse_dt(watermark_raw)
            now = datetime.now(timezone.utc)
            filtered: list[dict[str, Any]] = []
            for match in new_matches:
                collected = parse_dt(match.get("collected_at"))
                if not collected:
                    continue
                # Grouped/aggregate Tracker rows such as "Multiple" can be backfilled later and
                # are not reliable enough for live matchmaking alerts. Keep them in history only.
                if match.get("is_grouped"):
                    continue
                if watermark and collected <= watermark:
                    continue
                if (now - collected).total_seconds() > ALERT_MAX_AGE_MINUTES * 60:
                    continue
                filtered.append(match)

            if not filtered:
                continue

            filtered.sort(key=lambda m: m.get("collected_at") or "", reverse=True)
            channel = client.get_channel(row["alert_channel_id"])
            if channel is None:
                try:
                    channel = await client.fetch_channel(row["alert_channel_id"])
                except discord.DiscordException:
                    log.warning("Could not resolve channel %s", row["alert_channel_id"])
                    continue
            sent_any = False
            for match in filtered[:3]:
                try:
                    await channel.send(embed=match_embed(row, match))
                    store.set_alert_watermark(row["id"], match.get("collected_at"))
                    sent_any = True
                except discord.DiscordException as exc:
                    log.warning("Failed sending alert for %s: %s", row["label"], exc)
            if sent_any:
                await github_push("New Tracker activity")
        except TrackerError as exc:
            log.warning("Tracker error for %s: %s", row["label"], exc)
        except Exception:
            log.exception("Unexpected watcher error for %s", row["label"])
        await asyncio.sleep(1)


@watcher.before_loop
async def before_watcher():
    await client.wait_until_ready()


# Hugging Face Docker Spaces expect an HTTP service. This tiny endpoint also gives
# you a convenient uptime/health check while the Discord bot runs in the same process.
health = FastAPI(title="Rocket Watch Health")


@health.get("/")
def root():
    return {
        "ok": True,
        "service": "rocket-watch-discord-bot",
        "discord_connected": runtime["discord_connected"],
        "discord_user": runtime["discord_user"],
        "discord_last_error": runtime["discord_last_error"],
        "discord_last_attempt": runtime["discord_last_attempt"],
        "discord_last_connected": runtime["discord_last_connected"],
        "discord_retry_seconds": runtime["discord_retry_seconds"],
        "discord_retry_max_seconds": max(10, int(os.getenv("DISCORD_RETRY_MAX_SECONDS", "30"))),
        "discord_stage": runtime["discord_stage"],
        "discord_stage_since": runtime["discord_stage_since"],
        "discord_relay_enabled": bool(DISCORD_RELAY_GATEWAY),
        "discord_relay_origin": DISCORD_RELAY_ORIGIN or None,
        "tracked_players": len(store.players()),
        "poll_seconds": POLL_SECONDS,
        "alert_max_age_minutes": ALERT_MAX_AGE_MINUTES,
        "github_persistence_enabled": github_data.enabled,
        "github_data_repo": GITHUB_DATA_REPO,
        "github_last_sync_at": github_data.last_sync_at,
        "github_auto_sync_seconds": GITHUB_AUTO_SYNC_SECONDS,
        "github_last_error": github_data.last_error,
    }


@health.get("/health")
def healthcheck():
    return {
        "ok": True,
        "http": "online",
        "discord": "connected" if runtime["discord_connected"] else "disconnected",
        "discord_last_error": runtime["discord_last_error"],
        "discord_stage": runtime["discord_stage"],
        "discord_stage_since": runtime["discord_stage_since"],
    }


def set_discord_stage(stage: str) -> None:
    runtime["discord_stage"] = stage
    runtime["discord_stage_since"] = utc_now_iso()


@health.get("/debug/discord")
async def debug_discord():
    """Test both direct Discord and the configured Deno relay without a token."""

    async def probe(label: str, url: str):
        started = time.monotonic()
        try:
            timeout = aiohttp.ClientTimeout(total=10, connect=5, sock_connect=5, sock_read=5)
            async with aiohttp.ClientSession(timeout=timeout) as session:
                async with session.get(url) as resp:
                    body = await resp.text()
                    return {
                        "label": label,
                        "url": url.replace(DISCORD_RELAY_SECRET, "***") if DISCORD_RELAY_SECRET else url,
                        "status": resp.status,
                        "body": body[:300],
                        "elapsed_ms": round((time.monotonic() - started) * 1000),
                    }
        except Exception as exc:
            return {
                "label": label,
                "url": url.replace(DISCORD_RELAY_SECRET, "***") if DISCORD_RELAY_SECRET else url,
                "error": f"{type(exc).__name__}: {exc}",
                "elapsed_ms": round((time.monotonic() - started) * 1000),
            }

    dns = None
    try:
        infos = await asyncio.wait_for(
            asyncio.get_running_loop().run_in_executor(
                None, lambda: socket.getaddrinfo("discord.com", 443, type=socket.SOCK_STREAM)
            ),
            timeout=5,
        )
        dns = sorted({item[4][0] for item in infos})[:6]
    except Exception as exc:
        dns = f"{type(exc).__name__}: {exc}"

    result = {
        "discord_dns": dns,
        "direct": await probe("direct", "https://discord.com/api/v10/gateway"),
        "relay_enabled": bool(DISCORD_RELAY_GATEWAY),
    }
    if DISCORD_RELAY_GATEWAY:
        relay_url = f"{DISCORD_RELAY_ORIGIN}/p/{DISCORD_RELAY_SECRET}/api/v10/gateway"
        result["relay"] = await probe("deno-relay", relay_url)
    return result


async def sync_commands():
    """Sync slash commands after Discord is actually connected."""
    try:
        if GUILD_ID:
            guild = discord.Object(id=int(GUILD_ID))
            tree.copy_global_to(guild=guild)
            synced = await tree.sync(guild=guild)
            log.info("Synced %d slash commands to guild %s", len(synced), GUILD_ID)
        else:
            synced = await tree.sync()
            log.info("Synced %d global slash commands", len(synced))
    except Exception:
        log.exception("Slash-command sync failed")


def rebuild_discord_client() -> None:
    """Replace the Discord client with a brand-new HTTP/WebSocket stack.

    discord.py can close its internal aiohttp session after a failed login.
    Client.clear() resets cache/state but does not make that closed session usable
    again, so retries must use a fresh Client instance.
    """
    global client, tree

    # Preserve the application-command objects already registered on the current
    # tree, then attach them to a fresh tree/client.
    commands = list(tree.get_commands())

    new_client = discord.Client(intents=intents)
    new_tree = app_commands.CommandTree(new_client)
    for command in commands:
        new_tree.add_command(command)

    # Re-register event callbacks on the new Client. The callbacks intentionally
    # reference the module-level `client`, which is rebound below.
    new_client.event(on_ready)
    new_client.event(on_disconnect)

    client = new_client
    tree = new_tree


async def discord_supervisor():
    """Keep Discord failures isolated from the Hugging Face HTTP service.

    The Space listens only on $PORT (7860 by default). Discord uses outbound
    HTTPS/WSS and therefore does not need another local listening port. Every
    retry uses a completely fresh discord.Client so a failed aiohttp session
    cannot poison future attempts.
    """
    delay = 3
    # Keep recovery quick. discord.py's own reconnect loop can grow to very long
    # waits after repeated relay 503s, so this supervisor owns reconnects instead.
    max_delay = max(10, int(os.getenv("DISCORD_RETRY_MAX_SECONDS", "30")))
    first_attempt = True

    while True:
        runtime["discord_last_attempt"] = utc_now_iso()
        runtime["discord_retry_seconds"] = None

        if not first_attempt:
            rebuild_discord_client()
        first_attempt = False

        connect_task = None
        try:
            log.info("Connecting to Discord with a fresh client...")

            set_discord_stage("rest_login")
            await asyncio.wait_for(
                client.login(DISCORD_BOT_TOKEN),
                timeout=DISCORD_LOGIN_TIMEOUT,
            )
            log.info("Discord REST login succeeded as %s", client.user)

            set_discord_stage("command_sync")
            await asyncio.wait_for(sync_commands(), timeout=20)

            # client.connect() is intentionally long-running. Start it separately,
            # then require READY within a bounded amount of time. This distinguishes
            # a successful REST login from a Gateway/WebSocket failure.
            set_discord_stage("gateway_connect")
            # Important: do NOT let discord.py own the reconnect loop here. Its
            # exponential backoff can grow to many minutes if the relay returns
            # repeated 503s after a Deno Deploy eviction. A single gateway failure
            # should return control to this supervisor so it can rebuild the entire
            # Client/HTTP session and retry with a small capped delay.
            connect_task = asyncio.create_task(client.connect(reconnect=False), name="discord-gateway")
            await asyncio.wait_for(client.wait_until_ready(), timeout=DISCORD_READY_TIMEOUT)
            set_discord_stage("connected")
            log.info("Discord gateway is READY")
            delay = 3

            # Stay attached until the gateway closes. With reconnect=False this
            # returns/raises immediately on a broken relay connection instead of
            # entering discord.py's long internal backoff loop.
            await connect_task

            runtime["discord_connected"] = False
            runtime["discord_last_error"] = "Discord connection closed"
            set_discord_stage("disconnected")
            delay = 3
        except asyncio.CancelledError:
            try:
                if not client.is_closed():
                    await client.close()
            finally:
                raise
        except asyncio.TimeoutError:
            stage = runtime.get("discord_stage") or "unknown"
            timeout_used = DISCORD_LOGIN_TIMEOUT if stage == "rest_login" else DISCORD_READY_TIMEOUT
            runtime["discord_connected"] = False
            runtime["discord_last_error"] = f"Timeout while in {stage} after {timeout_used}s"
            log.error("Discord timed out during %s after %ss", stage, timeout_used)
        except discord.LoginFailure as exc:
            runtime["discord_connected"] = False
            runtime["discord_last_error"] = f"Login failure: {exc}"
            delay = 300
            log.error("Discord rejected the bot token: %s", exc)
        except Exception as exc:
            runtime["discord_connected"] = False
            runtime["discord_last_error"] = f"{type(exc).__name__}: {exc}"
            log.exception("Discord connection attempt failed")

        # Best-effort cleanup. We never reuse this instance after a failure.
        if connect_task is not None and not connect_task.done():
            connect_task.cancel()
            try:
                await connect_task
            except (asyncio.CancelledError, Exception):
                pass
        try:
            if not client.is_closed():
                await client.close()
        except Exception:
            log.debug("Discord client cleanup failed", exc_info=True)

        runtime["discord_retry_seconds"] = delay
        set_discord_stage("retry_wait")
        log.warning("Discord unavailable; retrying with a fresh client in %ss", delay)
        await asyncio.sleep(delay)
        delay = min(delay * 2, max_delay)


async def main():
    if not DISCORD_BOT_TOKEN:
        raise RuntimeError(
            "DISCORD_BOT_TOKEN is not set. Add it as a Hugging Face Space secret or environment variable."
        )

    if github_data.enabled:
        try:
            await asyncio.to_thread(github_data.ensure_files)
            restored = await asyncio.to_thread(github_data.restore_into, store)
            log.info("Restored %s players and %s activity records from GitHub", restored["players"], restored["matches"])
            await github_push("Startup reconciliation")
        except Exception as exc:
            github_data.last_error = f"{type(exc).__name__}: {exc}"
            log.warning("GitHub startup restore failed: %s", exc)

    # Hugging Face exposes one HTTP port. Uvicorn owns that port; Discord runs as
    # an outbound background connection and never binds a local port.
    port = int(os.getenv("PORT", "7860"))
    config = uvicorn.Config(
        health,
        host="0.0.0.0",
        port=port,
        log_level=os.getenv("UVICORN_LOG_LEVEL", "info").lower(),
        lifespan="on",
    )
    server = uvicorn.Server(config)

    discord_task = asyncio.create_task(discord_supervisor(), name="discord-supervisor")
    try:
        await server.serve()
    finally:
        discord_task.cancel()
        try:
            await discord_task
        except asyncio.CancelledError:
            pass
        if not client.is_closed():
            await client.close()


if __name__ == "__main__":
    asyncio.run(main())