File size: 130,752 Bytes
36dde81
d3a7f5b
 
36dde81
 
 
 
 
1404692
36dde81
1404692
 
 
 
36dde81
238a311
 
36dde81
1404692
36dde81
1404692
 
07f63a8
1404692
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
1404692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
1404692
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
 
 
 
 
 
 
 
 
 
 
 
07f63a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f63a8
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
07f63a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
e581a8c
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e581a8c
 
 
 
 
 
 
36dde81
e581a8c
 
 
 
36dde81
e581a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
e581a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
e581a8c
 
 
 
 
36dde81
e581a8c
 
 
36dde81
e581a8c
 
 
 
36dde81
e581a8c
 
 
36dde81
e581a8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e18b74
 
 
 
 
e581a8c
3e18b74
 
 
e581a8c
 
 
3e18b74
e581a8c
3e18b74
 
e581a8c
3e18b74
 
e581a8c
3e18b74
 
 
 
 
 
 
 
 
e581a8c
36dde81
 
 
3e18b74
 
e581a8c
3e18b74
e581a8c
3e18b74
e581a8c
36dde81
 
 
 
 
 
3e18b74
 
e581a8c
 
36dde81
 
 
 
 
 
 
e581a8c
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
e581a8c
 
36dde81
e581a8c
 
36dde81
 
 
e581a8c
 
 
 
 
36dde81
 
 
 
e581a8c
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
 
36dde81
 
b5f02cc
 
 
36dde81
 
 
 
 
b5f02cc
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
36dde81
 
 
 
 
 
 
b5f02cc
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
 
 
 
 
 
 
 
36dde81
 
b5f02cc
 
 
 
 
36dde81
 
b5f02cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
b5f02cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2c4766
36dde81
 
e2c4766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
e2c4766
 
 
 
 
 
 
36dde81
e2c4766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f02cc
 
 
36dde81
b5f02cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
b5f02cc
 
 
 
36dde81
b5f02cc
 
 
 
 
36dde81
b5f02cc
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
1404692
 
 
 
 
 
 
4232240
 
 
 
 
36dde81
 
4232240
 
1404692
 
4232240
 
36dde81
 
 
 
4232240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
4232240
 
 
 
 
 
 
 
 
 
522e2cd
1404692
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
1404692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
36dde81
238a311
 
36dde81
 
238a311
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238a311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
238a311
 
 
 
 
 
 
 
e2c4766
238a311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2c4766
238a311
 
 
 
 
 
 
1404692
 
36dde81
 
1404692
238a311
 
 
 
 
 
 
 
 
 
 
36dde81
 
238a311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
1404692
 
 
 
36dde81
1404692
 
36dde81
 
1404692
 
 
36dde81
 
1404692
 
 
fcc3ca3
522e2cd
1404692
e2c4766
 
 
238a311
1404692
36dde81
1404692
 
 
 
36dde81
1404692
 
36dde81
 
1404692
 
 
36dde81
 
1404692
 
 
fcc3ca3
522e2cd
1404692
e2c4766
 
 
1404692
 
 
 
 
 
fcc3ca3
 
36dde81
238a311
1404692
 
 
36dde81
1404692
076b7d8
 
 
 
 
 
 
 
238a311
 
 
 
 
 
076b7d8
238a311
 
 
 
 
 
076b7d8
 
 
 
 
 
 
 
 
 
 
 
238a311
 
 
 
36dde81
 
238a311
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
4ab0074
36dde81
 
4ab0074
36dde81
 
 
4ab0074
 
36dde81
 
4ab0074
 
 
 
 
 
36dde81
4ab0074
 
 
 
 
 
 
36dde81
4ab0074
 
 
 
 
 
 
 
 
36dde81
 
4ab0074
 
 
36dde81
4ab0074
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
4ab0074
 
 
36dde81
1404692
 
 
 
 
 
7232f1e
1404692
36dde81
1404692
 
14ad4e9
1404692
14ad4e9
1404692
14ad4e9
1404692
36dde81
 
 
1404692
36dde81
1404692
 
 
 
 
 
 
 
 
 
 
36dde81
1404692
dfbfa7a
 
 
 
 
36dde81
 
e2c4766
 
dfbfa7a
1404692
 
36dde81
 
 
14ad4e9
1404692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
1404692
 
 
 
 
14ad4e9
7232f1e
 
36dde81
1404692
 
14ad4e9
1404692
 
 
36dde81
 
 
1404692
36dde81
1404692
 
 
 
 
 
 
 
 
 
 
36dde81
1404692
14ad4e9
dfbfa7a
 
 
 
36dde81
 
e2c4766
 
dfbfa7a
1404692
 
14ad4e9
 
 
1404692
 
14ad4e9
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
14ad4e9
 
 
 
36dde81
 
 
14ad4e9
 
 
4ab0074
36dde81
4ab0074
14ad4e9
1404692
 
36dde81
1404692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4232240
36dde81
1404692
4232240
36dde81
 
4232240
36dde81
4232240
1404692
 
 
 
 
 
4232240
1404692
 
36dde81
1404692
4232240
36dde81
 
4232240
36dde81
4232240
1404692
 
 
 
 
 
4232240
1404692
 
 
 
 
 
36dde81
 
 
1404692
 
 
 
 
 
36dde81
1404692
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
36dde81
1404692
 
36dde81
 
 
 
 
 
1404692
 
 
36dde81
1404692
 
36dde81
 
 
 
 
1404692
 
36dde81
 
 
 
 
 
 
1404692
 
 
 
 
 
 
36dde81
1404692
 
 
 
 
 
36dde81
1404692
 
 
36dde81
 
 
 
1404692
 
 
 
 
 
 
 
 
 
adf3244
36dde81
adf3244
36dde81
 
 
 
 
 
1404692
adf3244
36dde81
 
 
 
 
 
 
 
 
adf3244
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf3244
 
36dde81
adf3244
 
 
 
 
 
 
36dde81
 
adf3244
36dde81
 
adf3244
 
36dde81
adf3244
 
 
 
 
36dde81
 
adf3244
 
 
 
1404692
36dde81
 
1404692
 
adf3244
1404692
 
adf3244
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adf3244
 
 
 
 
 
36dde81
adf3244
 
 
 
 
 
 
1404692
36dde81
 
 
1404692
 
 
36dde81
1404692
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
 
 
 
 
36dde81
1404692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
7013119
36dde81
 
 
 
 
7013119
36dde81
 
 
 
 
 
 
 
7013119
36dde81
 
7013119
 
 
 
1404692
 
36dde81
ed5fc32
36dde81
 
 
 
7013119
36dde81
 
ed5fc32
 
7013119
36dde81
 
1404692
36dde81
1404692
 
 
 
 
f697c3b
 
 
 
36dde81
f697c3b
 
 
 
 
 
36dde81
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
da0d638
 
 
36dde81
da0d638
 
36dde81
 
 
 
 
da0d638
 
 
e2c4766
da0d638
 
 
 
 
36dde81
da0d638
 
 
 
 
 
36dde81
 
da0d638
 
 
 
 
 
 
e2c4766
da0d638
 
 
 
 
 
14ad4e9
36dde81
 
 
 
 
 
 
 
 
 
1404692
14ad4e9
36dde81
 
 
 
 
1404692
ed5fc32
14ad4e9
7013119
ed5fc32
1404692
14ad4e9
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
1404692
36dde81
 
 
1404692
36dde81
1404692
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
ed5fc32
36dde81
 
1404692
 
 
 
 
36dde81
 
1404692
 
 
 
 
 
 
 
522e2cd
d3a7f5b
 
 
36dde81
 
 
d3a7f5b
36dde81
 
e581a8c
 
 
 
 
d3a7f5b
 
36dde81
d3a7f5b
 
 
e581a8c
36dde81
 
 
d3a7f5b
 
 
 
 
 
 
 
 
 
 
36dde81
d3a7f5b
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3a7f5b
 
 
 
36dde81
 
 
 
 
d3a7f5b
 
 
 
 
 
 
36dde81
 
 
 
 
d3a7f5b
 
36dde81
e581a8c
36dde81
 
 
 
 
 
 
 
 
 
 
 
e581a8c
 
 
36dde81
 
 
 
 
 
 
d3a7f5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f697c3b
36dde81
 
 
f697c3b
36dde81
f697c3b
 
 
36dde81
f697c3b
 
36dde81
f697c3b
 
 
 
36dde81
 
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
f697c3b
 
 
 
 
 
36dde81
 
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
 
 
f697c3b
 
36dde81
f697c3b
 
 
 
 
 
36dde81
 
 
f697c3b
 
 
 
 
 
 
 
36dde81
f697c3b
 
36dde81
 
 
 
 
 
 
 
 
 
f697c3b
 
 
 
36dde81
f697c3b
 
 
36dde81
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
f697c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36dde81
f697c3b
 
 
 
 
 
 
 
36dde81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f697c3b
 
 
 
36dde81
 
 
f697c3b
 
 
 
36dde81
c778c21
 
 
dfbfa7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c778c21
 
 
 
 
 
 
 
36dde81
c778c21
 
 
 
 
 
dfbfa7a
c778c21
0b3bde4
 
 
 
 
 
dfbfa7a
 
 
c778c21
 
dfbfa7a
 
0b3bde4
 
 
 
 
 
 
 
 
 
c778c21
dfbfa7a
c778c21
 
 
 
dfbfa7a
 
0b3bde4
 
 
 
 
 
 
 
 
 
c778c21
dfbfa7a
c778c21
 
 
dfbfa7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c778c21
dfbfa7a
c778c21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
import re
import secrets
import string
import time
from asyncio import create_task, gather
from datetime import datetime, timedelta, timezone
from typing import Any, Dict, List, Optional, Tuple

import motor.motor_asyncio
from bson import ObjectId
from pydantic import ValidationError
from pymongo import ASCENDING, DESCENDING

from Backend.config import Telegram
from Backend.helper.bounded import BoundedDict
from Backend.helper.encrypt import decode_string, encode_string
from Backend.helper.modal import Episode, MovieSchema, QualityDetail, QualityPart, Season, TVShowSchema
from Backend.helper.settings_manager import SettingsManager
from Backend.helper.task_manager import delete_message
from Backend.logger import LOGGER



def convert_objectid_to_str(document: Dict[str, Any]) -> Dict[str, Any]:
    for key, value in document.items():
        if isinstance(value, ObjectId):
            document[key] = str(value)
        elif isinstance(value, list):
            document[key] = [convert_objectid_to_str(item) if isinstance(item, dict) else item for item in value]
        elif isinstance(value, dict):
            document[key] = convert_objectid_to_str(value)
    return document


META_PROJECTION = {"telegram": 0, "seasons": 0}
_COUNT_TTL = 60
_count_cache = BoundedDict(500)


class Database:
    def __init__(self, db_name: str = "dbFyvio"):
        self.db_uris = Telegram.DATABASE
        self.db_name = db_name

        if len(self.db_uris) < 2:
            raise ValueError("At least 2 database URIs are required (1 for tracking + 1 for storage).")

        self.clients: Dict[str, motor.motor_asyncio.AsyncIOMotorClient] = {}
        self.dbs: Dict[str, motor.motor_asyncio.AsyncIOMotorDatabase] = {}

        self.current_db_index = 1

    async def connect(self):
        try:
            for index, uri in enumerate(self.db_uris):
                client = motor.motor_asyncio.AsyncIOMotorClient(uri)
                db_key = "tracking" if index == 0 else f"storage_{index}"
                self.clients[db_key] = client
                self.dbs[db_key] = client[self.db_name]
                db_type = "Tracking" if index == 0 else f"Storage {index}"

                masked_uri = re.sub(r"://(.*?):.*?@", r"://\1:*****@", uri)
                masked_uri = masked_uri.split('?')[0]
                
                LOGGER.info(f"{db_type} Database connected successfully: {masked_uri}")

            state = await self.dbs["tracking"]["state"].find_one({"_id": "db_index"})
            if not state:
                await self.dbs["tracking"]["state"].insert_one({"_id": "db_index", "current_index": 1})
                self.current_db_index = 1
            else:
                self.current_db_index = state["current_index"]

            LOGGER.info(f"Active storage DB: storage_{self.current_db_index}")

            await self.ensure_indexes()

        except Exception as e:
            LOGGER.error(f"Database connection error: {e}")

    #----- Create the indexes catalog/stream lookups rely on.
    #----- create_index is idempotent, so this is safe to call repeatedly.
    async def ensure_indexes(self) -> None:
        tracking = self.dbs.get("tracking")
        if tracking is not None:
            try:
                await tracking["custom_catalogs"].create_index([("updated_at", DESCENDING)])
                await tracking["api_tokens"].create_index([("token", ASCENDING)])
                await tracking["api_tokens"].create_index([("user_id", ASCENDING)])
                await tracking["custom_catalogs"].create_index(
                    [("items.tmdb_id", ASCENDING), ("items.media_type", ASCENDING)]
                )
                await self._ensure_subtitle_indexes(tracking)
            except Exception as e:
                LOGGER.error(f"Failed creating tracking indexes: {e}")
            try:
                await tracking["stream_analytics"].create_index([("logged_at", ASCENDING)], expireAfterSeconds=30 * 86400)
            except Exception as e:
                LOGGER.error(f"Failed creating stream_analytics TTL index: {e}")

        for db_key in list(self.dbs.keys()):
            if db_key.startswith("storage_"):
                await self._ensure_storage_indexes(db_key)

    async def _ensure_subtitle_indexes(self, tracking) -> None:
        subs = tracking["subtitles"]
        try:
            info = await subs.index_information()
        except Exception:
            info = {}
        for name, spec in info.items():
            if name == "_id_":
                continue
            keys = [k for k, _ in spec.get("key", [])]
            if keys == ["stream_id"] or (spec.get("unique") and "stream_id" in keys):
                try:
                    await subs.drop_index(name)
                    LOGGER.info(f"Dropped stale subtitle index {name}")
                except Exception as e:
                    LOGGER.error(f"Failed dropping subtitle index {name}: {e}")
        await subs.create_index([("chat_id", ASCENDING), ("msg_id", ASCENDING)], unique=True)
        await subs.create_index([("imdb_id", ASCENDING), ("season", ASCENDING), ("episode", ASCENDING)])

    #----- Ensure per-storage-DB indexes on the movie/tv collections.
    #----- tmdb_id + imdb_id drive catalog hydration and stream lookups.
    async def _ensure_storage_indexes(self, db_key: str) -> None:
        db = self.dbs.get(db_key)
        if db is None:
            return
        for collection_name in ("movie", "tv"):
            try:
                await db[collection_name].create_index([("tmdb_id", ASCENDING)])
                await db[collection_name].create_index([("imdb_id", ASCENDING)])
                await db[collection_name].create_index([("kitsu_id", ASCENDING)])
                await db[collection_name].create_index([("updated_on", DESCENDING)])
                await db[collection_name].create_index([("rating", DESCENDING)])
                prefix = "telegram" if collection_name == "movie" else "seasons.episodes.telegram"
                await db[collection_name].create_index([(f"{prefix}.id", ASCENDING)])
                await db[collection_name].create_index([(f"{prefix}.parts.msg_id", ASCENDING)])
                for field in ("title", "title_english", "original_title", f"{prefix}.name"):
                    await db[collection_name].create_index([(field, ASCENDING)])
            except Exception as e:
                LOGGER.error(f"Failed creating index on {db_key}/{collection_name}: {e}")

    async def disconnect(self):
        for client in self.clients.values():
            client.close()
        LOGGER.info("All database connections closed.")

    async def update_current_db_index(self):
        await self.dbs["tracking"]["state"].update_one(
            {"_id": "db_index"},
            {"$set": {"current_index": self.current_db_index}},
            upsert=True
        )


    async def get_settings(self) -> dict:
        try:
            doc = await self.dbs["tracking"]["settings"].find_one({"_id": "app_settings"})
            return doc or {}
        except Exception as e:
            LOGGER.error(f"Database.get_settings error: {e}")
            return {}

    async def save_settings(self, settings: dict) -> bool:
        try:
            clean = {k: v for k, v in settings.items() if k != "_id"}
            await self.dbs["tracking"]["settings"].update_one(
                {"_id": "app_settings"},
                {"$set": clean},
                upsert=True,
            )
            return True
        except Exception as e:
            LOGGER.error(f"Database.save_settings error: {e}")
            return False

    async def get_catalog_order(self) -> List[str]:
        doc = await self.dbs["tracking"]["state"].find_one({"_id": "catalog_order"})
        return list((doc or {}).get("order", []))

    async def get_hidden_catalogs(self) -> List[str]:
        doc = await self.dbs["tracking"]["state"].find_one({"_id": "catalog_hidden"})
        return list((doc or {}).get("keys", []))

    async def save_hidden_catalogs(self, keys: List[str]) -> None:
        await self.dbs["tracking"]["state"].update_one(
            {"_id": "catalog_hidden"}, {"$set": {"keys": [str(x) for x in keys]}}, upsert=True)

    async def save_catalog_order(self, order: List[str]) -> bool:
        await self.dbs["tracking"]["state"].update_one(
            {"_id": "catalog_order"},
            {"$set": {"order": [str(x) for x in (order or [])]}},
            upsert=True,
        )
        return True



    async def connect_storage_db(self, uri: str, index: int) -> bool:
        try:
            client = motor.motor_asyncio.AsyncIOMotorClient(uri)
            await client.admin.command("ping")

            db_key = "tracking" if index == 0 else f"storage_{index}"
            self.clients[db_key] = client
            self.dbs[db_key] = client[self.db_name]

            db_type = "Tracking" if index == 0 else f"Storage {index}"
            masked_uri = re.sub(r"://(.*?):.*?@", r"://\1:*****@", uri).split('?')[0]
            LOGGER.info(f"{db_type} Database connected successfully: {masked_uri}")
            if index > 0:
                await self._ensure_storage_indexes(db_key)
            return True
        except Exception as e:
            LOGGER.error(f"Failed to connect database at index {index}: {e}")
            return False

    async def disconnect_storage_db(self, index: int) -> None:
        db_key = f"storage_{index}"
        client = self.clients.pop(db_key, None)
        self.dbs.pop(db_key, None)
        if client:
            client.close()
            LOGGER.info(f"Disconnected {db_key}.")

    def get_database_list(self) -> List[Dict[str, Any]]:
        result = []
        for index, uri in enumerate(self.db_uris):
            masked = re.sub(r"://(.*?):.*?@", r"://\1:*****@", uri).split('?')[0]
            db_key = "tracking" if index == 0 else f"storage_{index}"
            entry = {
                "index": index,
                "uri_masked": masked,
                "locked": index <= 1,
                "type": "tracking" if index == 0 else "storage",
                "connected": db_key in self.clients,
            }
            if index > 1:
                entry["full_uri"] = uri
            result.append(entry)
        return result

    async def reload_extra_databases(self, new_extra_uris: List[str]) -> Dict[str, Any]:
        old_extra = self.db_uris[2:]
        new_extra = [u.strip() for u in (new_extra_uris or []) if u and u.strip()]

        common_len = min(len(old_extra), len(new_extra))
        for i in range(common_len):
            if old_extra[i] != new_extra[i]:
                raise ValueError(
                    f"Cannot modify storage_{i + 2} in place — existing media "
                    f"documents reference it by position. Only appending new "
                    f"databases or removing the last one(s) is supported."
                )

        added = 0
        removed = 0

        if len(new_extra) > len(old_extra):
            for offset, uri in enumerate(new_extra[len(old_extra):]):
                index = len(old_extra) + 2 + offset
                ok = await self.connect_storage_db(uri, index)
                if not ok:
                    raise ValueError(
                        f"Failed to connect new database at storage_{index}. "
                        f"Check the URI and try again — no changes were saved."
                    )
                added += 1

        elif len(new_extra) < len(old_extra):
            for index in range(len(old_extra) + 1, len(new_extra) + 1, -1):
                await self.disconnect_storage_db(index)
                removed += 1

        self.db_uris = self.db_uris[:2] + new_extra

        message = f"{added} database(s) added, {removed} database(s) removed."
        LOGGER.info(f"reload_extra_databases: {message}")
        return {"added": added, "removed": removed, "message": message}

    #-----
    #----- User Subscription Management
    #-----
    async def get_user(self, user_id: int) -> Optional[dict]:
        return await self.dbs["tracking"]["users"].find_one({"_id": user_id})

    #----- Whether a user doc represents a currently-active subscription
    @staticmethod
    def is_subscription_active(user: Optional[dict], now: datetime = None) -> bool:
        if not user or user.get("subscription_status") != "active":
            return False
        expiry = user.get("subscription_expiry")
        if not expiry:
            return False
        reference = now or datetime.utcnow()
        try:
            if expiry.tzinfo is not None:
                reference = datetime.now(timezone.utc)
        except AttributeError:
            pass
        return expiry > reference

    #----- (movie_count, tv_count) totals summed across per-DB stats
    @staticmethod
    def content_totals(db_stats: List[dict]) -> Tuple[int, int]:
        total_movies = sum(stat.get("movie_count", 0) for stat in db_stats)
        total_tv = sum(stat.get("tv_count", 0) for stat in db_stats)
        return total_movies, total_tv

    async def update_user_interaction(self, user_id: int, first_name: str, username: str):
        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": {"first_name": first_name, "username": username, "last_interaction": datetime.utcnow()}},
            upsert=True
        )

    async def set_pending_payment(self, user_id: int, plan_duration: int, msg_id: int, price=0, admin_messages: list = None, currency: str = "INR"):
        update_data = {
            "pending_payment": {
                "duration": plan_duration,
                "price": price,
                "currency": (currency or "INR").upper(),
                "msg_id": msg_id,
                "date": datetime.utcnow(),
            }
        }
        if admin_messages is not None:
            update_data["pending_payment"]["admin_messages"] = admin_messages
        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": update_data},
            upsert=True
        )

    async def approve_payment(self, user_id: int) -> Optional[dict]:
        user = await self.get_user(user_id)
        if not user or "pending_payment" not in user:
            return None

        duration = user["pending_payment"]["duration"]
        
        #----- Calculate new expiry
        current_expiry = user.get("subscription_expiry")
        now = datetime.utcnow()
        if current_expiry and current_expiry > now:
            new_expiry = current_expiry + timedelta(days=duration)
        else:
            new_expiry = now + timedelta(days=duration)

        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {
                "$set": {"subscription_expiry": new_expiry, "subscription_status": "active"},
                "$unset": {"pending_payment": ""}
            }
        )
        return await self.get_user(user_id)

    async def reject_payment(self, user_id: int) -> bool:
        result = await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$unset": {"pending_payment": ""}}
        )
        return result.modified_count > 0

    async def get_expired_users(self) -> List[dict]:
        cursor = self.dbs["tracking"]["users"].find({
            "subscription_expiry": {"$lt": datetime.utcnow()},
            "subscription_status": "active"
        })
        return await cursor.to_list(None)

    async def mark_user_expired(self, user_id: int):
        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": {"subscription_status": "expired"}}
        )

    async def get_expiring_users(self, hours: int = 24) -> List[dict]:
        now = datetime.utcnow()
        target_time = now + timedelta(hours=hours)
        cursor = self.dbs["tracking"]["users"].find({
            "subscription_expiry": {"$gt": now, "$lte": target_time},
            "reminder_sent": {"$ne": True},
            "subscription_status": "active"
        })
        return await cursor.to_list(None)
        
    async def mark_reminder_sent(self, user_id: int):
         await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": {"reminder_sent": True}}
        )

    #-----
    #----- Admin Subscription Management
    #-----
    async def get_subscription_plans(self) -> List[dict]:
        cursor = self.dbs["tracking"]["sub_plans"].find().sort("days", ASCENDING)
        plans = await cursor.to_list(None)
        return [convert_objectid_to_str(plan) for plan in plans]

    async def add_subscription_plan(self, days: int, price: float, currency: str = "INR") -> Optional[str]:
        result = await self.dbs["tracking"]["sub_plans"].insert_one({
            "days": days,
            "price": price,
            "currency": (currency or "INR").upper(),
            "created_at": datetime.utcnow()
        })
        return str(result.inserted_id)

    async def update_subscription_plan(self, plan_id: str, days: int, price: float, currency: str = "INR") -> bool:
        try:
            result = await self.dbs["tracking"]["sub_plans"].update_one(
                {"_id": ObjectId(plan_id)},
                {"$set": {"days": days, "price": price, "currency": (currency or "INR").upper(), "updated_at": datetime.utcnow()}}
            )
            return result.modified_count > 0
        except Exception:
            return False

    async def delete_subscription_plan(self, plan_id: str) -> bool:
        try:
            result = await self.dbs["tracking"]["sub_plans"].delete_one({"_id": ObjectId(plan_id)})
            return result.deleted_count > 0
        except Exception:
            return False

    async def get_all_subscribers(self) -> List[dict]:
        cursor = self.dbs["tracking"]["users"].find({
            "subscription_status": {"$in": ["active", "expired"]}
        }).sort("subscription_expiry", DESCENDING)
        users = await cursor.to_list(None)
        return [convert_objectid_to_str(u) for u in users]

    async def manage_subscriber(self, user_id: int, action: str, days: int = 0) -> bool:
        now = datetime.utcnow()
        user = await self.get_user(user_id)

        if action in ("extend", "reduce"):
            current_expiry = user.get("subscription_expiry") if user else None

            if action == "extend":
                base = current_expiry if (current_expiry and current_expiry > now) else now
                new_expiry = base + timedelta(days=days)
            else:
                if current_expiry:
                    new_expiry = current_expiry - timedelta(days=days)
                    if new_expiry < now:
                        new_expiry = now
                else:
                    new_expiry = now

            status = "active" if new_expiry > now else "expired"

            await self.dbs["tracking"]["users"].update_one(
                {"_id": user_id},
                {
                    "$set": {"subscription_expiry": new_expiry, "subscription_status": status},
                    "$setOnInsert": {
                        "first_name": f"User {user_id}",
                        "username": None,
                        "created_at": now,
                    },
                },
                upsert=True
            )
            if status == "active":
                await self.ensure_api_token_for_user(user_id, (user or {}).get("first_name"))
            await self.align_token_with_subscription(user_id)
            return True

        elif action == "delete":
            await self.dbs["tracking"]["users"].update_one(
                {"_id": user_id},
                {"$set": {"subscription_status": "expired", "subscription_expiry": now}}
            )
            await self.align_token_with_subscription(user_id)
            return True

        elif action == "remove":
            await self.align_token_with_subscription(user_id)
            await self.dbs["tracking"]["users"].delete_one({"_id": user_id})
            return True

        return False

    #----- Update a subscriber's display name (and the linked token's name)
    async def update_subscriber_name(self, user_id: int, name: str) -> None:
        await self.dbs["tracking"]["users"].update_one({"_id": user_id}, {"$set": {"first_name": name}})
        await self.dbs["tracking"]["api_tokens"].update_one({"user_id": user_id}, {"$set": {"name": name}})

    async def assign_subscription(self, user_id: int, days: int, name: str = None) -> dict:
        #----- Upsert a subscription for any user_id, creating a record if it doesn't exist
        now = datetime.utcnow()

        user = await self.get_user(user_id)
        if user:
            current_expiry = user.get("subscription_expiry")
            if current_expiry and current_expiry > now:
                new_expiry = current_expiry + timedelta(days=days)
            else:
                new_expiry = now + timedelta(days=days)
        else:
            new_expiry = now + timedelta(days=days)

        set_fields = {"subscription_expiry": new_expiry, "subscription_status": "active"}
        insert_fields = {"_id": user_id, "username": None, "created_at": now}
        if name:
            set_fields["first_name"] = name
        else:
            insert_fields["first_name"] = f"User {user_id}"

        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": set_fields, "$setOnInsert": insert_fields},
            upsert=True
        )
        token_doc = await self.ensure_api_token_for_user(user_id, (user or {}).get("first_name"))
        token = token_doc.get("token") if token_doc else None
        await self.align_token_with_subscription(user_id)
        return {
            "user_id": user_id,
            "subscription_expiry": new_expiry.isoformat(),
            "subscription_status": "active",
            "days_assigned": days,
            "token": token,
            "addon_url": (
                f"{SettingsManager.current().base_url}/stremio/{token}/manifest.json" if token else None
            ),
        }

    #----- Give a user's token never-expiring access (clears any expiry date)
    async def set_user_never_expires(self, user_id: int, name: str = None) -> dict:
        now = datetime.utcnow()
        user = await self.get_user(user_id)
        set_fields = {"subscription_status": "active"}
        insert_fields = {"username": None, "created_at": now}
        if name:
            set_fields["first_name"] = name
        else:
            insert_fields["first_name"] = f"User {user_id}"
        await self.dbs["tracking"]["users"].update_one(
            {"_id": user_id},
            {"$set": set_fields, "$unset": {"subscription_expiry": ""}, "$setOnInsert": insert_fields},
            upsert=True,
        )
        token_doc = await self.ensure_api_token_for_user(user_id, (user or {}).get("first_name"))
        token = token_doc.get("token") if token_doc else None
        if token:
            await self.set_token_lifetime(token, True)
        return {
            "user_id": user_id,
            "never_expires": True,
            "token": token,
            "addon_url": (
                f"{SettingsManager.current().base_url}/stremio/{token}/manifest.json" if token else None
            ),
        }

    #-----
    #----- Custom Catalog Management
    #-----
    #----- Backfill the visibility model on catalogs that predate it
    @staticmethod
    def _normalize_catalog(catalog: Optional[dict]) -> Optional[dict]:
        if not catalog:
            return catalog
        if catalog.get("visibility") not in ("public", "tokens", "owner"):
            catalog["visibility"] = "public" if catalog.get("visible", True) else "owner"
        catalog.setdefault("allowed_tokens", [])
        catalog.setdefault("exclusive", False)
        catalog.setdefault("searchable", False)
        return catalog

    async def create_custom_catalog(self, name: str, visibility: str = "public", allowed_tokens: Optional[List[str]] = None) -> Optional[str]:
        name = (name or "").strip()
        if not name:
            return None

        if visibility not in ("public", "tokens", "owner"):
            visibility = "public"
        now = datetime.utcnow()
        result = await self.dbs["tracking"]["custom_catalogs"].insert_one({
            "name": name,
            "visibility": visibility,
            "allowed_tokens": list(allowed_tokens or []),
            "visible": visibility != "owner",
            "exclusive": False,
            "searchable": False,
            "items": [],
            "created_at": now,
            "updated_at": now,
        })
        return str(result.inserted_id)

    #----- slim=True keeps only distinct (media_type, visibility, allowed_tokens) per item
    async def get_custom_catalogs(self, visible_only: bool = False, slim: bool = False, item_fields: Optional[tuple] = None) -> List[dict]:
        pipeline = [{"$match": {"visible": True} if visible_only else {}}, {"$sort": {"updated_at": DESCENDING}}]
        if slim:
            pipeline.append({"$addFields": {"items": {"$setUnion": [{"$map": {
                "input": {"$ifNull": ["$items", []]},
                "in": {"media_type": "$$this.media_type", "visibility": "$$this.visibility", "allowed_tokens": "$$this.allowed_tokens"},
            }}]}}})
        if item_fields is not None:
            pipeline.append({"$addFields": {"item_count": {"$size": {"$ifNull": ["$items", []]}}}})
            if item_fields:
                pipeline.append({"$addFields": {"items": {"$map": {
                    "input": {"$ifNull": ["$items", []]},
                    "in": {f: f"$$this.{f}" for f in item_fields},
                }}}})
            else:
                pipeline.append({"$project": {"items": 0}})
        catalogs = await self.dbs["tracking"]["custom_catalogs"].aggregate(pipeline).to_list(None)
        return [self._normalize_catalog(convert_objectid_to_str(catalog)) for catalog in catalogs]

    async def get_custom_catalog(self, catalog_id: str) -> Optional[dict]:
        try:
            if catalog_id.startswith("auto_"):
                catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({"auto_key": catalog_id})
            else:
                catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({"_id": ObjectId(catalog_id)})
            if not catalog:
                catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({"auto_key": catalog_id})
            return self._normalize_catalog(convert_objectid_to_str(catalog)) if catalog else None
        except Exception:
            try:
                catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({"auto_key": catalog_id})
                return self._normalize_catalog(convert_objectid_to_str(catalog)) if catalog else None
            except Exception:
                return None

    async def update_custom_catalog(
        self,
        catalog_id: str,
        name: Optional[str] = None,
        visibility: Optional[str] = None,
        allowed_tokens: Optional[List[str]] = None,
        exclusive: Optional[bool] = None,
        searchable: Optional[bool] = None,
    ) -> bool:
        try:
            existing = await self.dbs["tracking"]["custom_catalogs"].find_one({"_id": ObjectId(catalog_id)})
        except Exception:
            return False
        if not existing:
            return False

        update_data = {"updated_at": datetime.utcnow()}
        if name is not None:
            clean_name = name.strip()
            if clean_name:
                update_data["name"] = clean_name

        #----- Setting catalog visibility cascades to every title in the catalog
        cascade = visibility in ("public", "tokens", "owner")
        tokens = list(allowed_tokens or [])
        final_visibility = visibility if cascade else existing.get("visibility", "public")
        if cascade:
            update_data["visibility"] = visibility
            update_data["visible"] = visibility != "owner"
            update_data["allowed_tokens"] = tokens
            update_data["items.$[].visibility"] = visibility
            update_data["items.$[].allowed_tokens"] = tokens

        #----- Exclusive locks every title to this catalog only (never on auto catalogs,
        #----- and only meaningful for restricted visibility)
        is_auto = bool(existing.get("auto"))
        want_exclusive = None
        if exclusive is not None and not is_auto:
            want_exclusive = bool(exclusive) and final_visibility in ("tokens", "owner")
            update_data["exclusive"] = want_exclusive
            update_data["searchable"] = bool(searchable) if want_exclusive else False

        try:
            result = await self.dbs["tracking"]["custom_catalogs"].update_one(
                {"_id": ObjectId(catalog_id)},
                {"$set": update_data}
            )
        except Exception:
            return False

        catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({"_id": ObjectId(catalog_id)})
        items = catalog.get("items", []) if catalog else []

        #----- Stamp visibility onto the underlying media documents so the default
        #----- Latest/Popular catalogs and search honour it too
        if cascade:
            await self._apply_visibility_to_docs(items, final_visibility, tokens)

        #----- Apply/clear exclusivity on the documents and purge from every other catalog
        if want_exclusive is True:
            await self._apply_exclusivity_to_docs(items, catalog_id, bool(searchable))
            await self.purge_items_from_other_catalogs(catalog_id, items)
        elif want_exclusive is False:
            await self._clear_exclusivity_from_docs(items)

        return result.modified_count > 0

    #----- Stamp visibility onto media documents referenced by the given catalog items
    async def _apply_visibility_to_docs(self, items: List[dict], visibility: str, allowed_tokens: List[str]) -> None:
        groups: Dict[Tuple[int, str], List[int]] = {}
        for it in items or []:
            try:
                db_index = int(it.get("db_index", 1))
                collection = self._collection_for(it.get("media_type", "movie"))
                groups.setdefault((db_index, collection), []).append(int(it.get("tmdb_id")))
            except (TypeError, ValueError):
                continue
        for (db_index, collection), ids in groups.items():
            db_key = f"storage_{db_index}"
            if db_key not in self.dbs:
                continue
            try:
                #----- Metadata-only change: never touch updated_on (keeps Latest order)
                await self.dbs[db_key][collection].update_many(
                    {"tmdb_id": {"$in": ids}},
                    {"$set": {"visibility": visibility, "allowed_tokens": allowed_tokens}},
                )
            except Exception as e:
                LOGGER.error(f"_apply_visibility_to_docs failed for {db_key}.{collection}: {e}")

    #----- Group catalog items into {(db_index, collection): [tmdb_id, ...]}
    def _group_items_by_storage(self, items: List[dict]) -> Dict[Tuple[int, str], List[int]]:
        groups: Dict[Tuple[int, str], List[int]] = {}
        for it in items or []:
            try:
                db_index = int(it.get("db_index", 1))
                collection = self._collection_for(it.get("media_type", "movie"))
                groups.setdefault((db_index, collection), []).append(int(it.get("tmdb_id")))
            except (TypeError, ValueError):
                continue
        return groups

    #----- Lock the given titles to a single catalog (source of truth on the docs)
    async def _apply_exclusivity_to_docs(self, items: List[dict], catalog_id: str, searchable: bool) -> None:
        for (db_index, collection), ids in self._group_items_by_storage(items).items():
            db_key = f"storage_{db_index}"
            if db_key not in self.dbs:
                continue
            try:
                #----- Metadata-only change: never touch updated_on (keeps Latest order)
                await self.dbs[db_key][collection].update_many(
                    {"tmdb_id": {"$in": ids}},
                    {"$set": {"exclusive_catalog_id": str(catalog_id), "exclusive_searchable": bool(searchable)}},
                )
            except Exception as e:
                LOGGER.error(f"_apply_exclusivity_to_docs failed for {db_key}.{collection}: {e}")

    #----- Unlock the given titles so they return to default/auto/other catalogs
    async def _clear_exclusivity_from_docs(self, items: List[dict]) -> None:
        for (db_index, collection), ids in self._group_items_by_storage(items).items():
            db_key = f"storage_{db_index}"
            if db_key not in self.dbs:
                continue
            try:
                #----- Unlocking is metadata-only: keep updated_on so titles slot back into
                #----- their original place in Latest (auto.synced reset lets sync re-add)
                await self.dbs[db_key][collection].update_many(
                    {"tmdb_id": {"$in": ids}},
                    {"$unset": {"exclusive_catalog_id": "", "exclusive_searchable": ""},
                     "$set": {"auto_catalog.synced": False}},
                )
            except Exception as e:
                LOGGER.error(f"_clear_exclusivity_from_docs failed for {db_key}.{collection}: {e}")

    #----- Remove the given titles from every catalog except the one that owns them
    async def purge_items_from_other_catalogs(self, catalog_id: str, items: List[dict]) -> None:
        ids_by_type: Dict[str, set] = {}
        for it in items or []:
            try:
                ids_by_type.setdefault(self._collection_for(it.get("media_type", "movie")), set()).add(int(it.get("tmdb_id")))
            except (TypeError, ValueError):
                continue
        if not ids_by_type:
            return
        coll = self.dbs["tracking"]["custom_catalogs"]
        now = datetime.utcnow()
        for media_type, ids in ids_by_type.items():
            id_list = list(ids)
            try:
                await coll.update_many(
                    {"_id": {"$ne": ObjectId(catalog_id)},
                     "items": {"$elemMatch": {"tmdb_id": {"$in": id_list}, "media_type": media_type}}},
                    {"$pull": {"items": {"tmdb_id": {"$in": id_list}, "media_type": media_type}},
                     "$set": {"updated_at": now}},
                )
            except Exception as e:
                LOGGER.error(f"purge_items_from_other_catalogs failed: {e}")

    #----- Mark a single freshly-added title exclusive to its catalog
    async def mark_item_exclusive(self, catalog_id: str, tmdb_id: int, db_index: int, media_type: str, searchable: bool) -> None:
        item = {"tmdb_id": int(tmdb_id), "db_index": int(db_index), "media_type": self._collection_for(media_type)}
        await self._apply_exclusivity_to_docs([item], catalog_id, searchable)
        await self.purge_items_from_other_catalogs(catalog_id, [item])

    #----- Clear exclusivity for a single title (e.g. removed from its catalog)
    async def clear_item_exclusive(self, tmdb_id: int, db_index: int, media_type: str) -> None:
        item = {"tmdb_id": int(tmdb_id), "db_index": int(db_index), "media_type": self._collection_for(media_type)}
        await self._clear_exclusivity_from_docs([item])

    #----- Override one title's visibility inside a single catalog
    async def set_catalog_item_visibility(
        self, catalog_id: str, tmdb_id: int, db_index: int, media_type: str,
        visibility: str, allowed_tokens: Optional[List[str]] = None,
    ) -> bool:
        if visibility not in ("public", "tokens", "owner"):
            return False
        media_type = self._collection_for(media_type)
        try:
            result = await self.dbs["tracking"]["custom_catalogs"].update_one(
                {"_id": ObjectId(catalog_id),
                 "items": {"$elemMatch": {"tmdb_id": int(tmdb_id), "db_index": int(db_index), "media_type": media_type}}},
                {"$set": {
                    "items.$.visibility": visibility,
                    "items.$.allowed_tokens": list(allowed_tokens or []),
                    "updated_at": datetime.utcnow(),
                }},
            )
            return result.modified_count > 0
        except Exception:
            return False

    #----- Set a title's own visibility (source of truth for default + custom catalogs)
    async def set_media_visibility(
        self, tmdb_id: int, db_index: int, media_type: str,
        visibility: str, allowed_tokens: Optional[List[str]] = None,
    ) -> int:
        if visibility not in ("public", "tokens", "owner"):
            return 0
        tokens = list(allowed_tokens or [])
        collection = self._collection_for(media_type)
        now = datetime.utcnow()

        db_key = f"storage_{int(db_index)}"
        if db_key in self.dbs:
            try:
                #----- Metadata-only change: never touch updated_on (keeps Latest order)
                await self.dbs[db_key][collection].update_one(
                    {"tmdb_id": int(tmdb_id)},
                    {"$set": {"visibility": visibility, "allowed_tokens": tokens}},
                )
            except Exception as e:
                LOGGER.error(f"set_media_visibility doc update failed: {e}")

        #----- Keep any catalog items in sync so custom-catalog filtering matches
        try:
            result = await self.dbs["tracking"]["custom_catalogs"].update_many(
                {"items": {"$elemMatch": {"tmdb_id": int(tmdb_id), "db_index": int(db_index), "media_type": collection}}},
                {"$set": {
                    "items.$.visibility": visibility,
                    "items.$.allowed_tokens": tokens,
                    "updated_at": now,
                }},
            )
            return result.modified_count
        except Exception:
            return 0

    #----- A title's own visibility (from its media document)
    async def get_media_visibility(self, tmdb_id: int, db_index: int, media_type: str) -> Optional[dict]:
        doc = await self.get_document(media_type, int(tmdb_id), int(db_index))
        if not doc:
            return None
        return {
            "visibility": doc.get("visibility") or "public",
            "allowed_tokens": doc.get("allowed_tokens") or [],
        }

    async def delete_custom_catalog(self, catalog_id: str) -> bool:
        try:
            result = await self.dbs["tracking"]["custom_catalogs"].delete_one({"_id": ObjectId(catalog_id)})
            return result.deleted_count > 0
        except Exception:
            return False

    async def add_item_to_custom_catalog(
        self, catalog_id: str, tmdb_id: int, db_index: int, media_type: str
    ) -> bool:
        media_type = self._collection_for(media_type)
        doc = await self.get_document(media_type, int(tmdb_id), int(db_index))
        item = {
            "tmdb_id": int(tmdb_id),
            "db_index": int(db_index),
            "media_type": media_type,
            "added_at": datetime.utcnow(),
            "visibility": (doc.get("visibility") if doc else None) or "public",
            "allowed_tokens": (doc.get("allowed_tokens") if doc else None) or [],
        }
        try:
            result = await self.dbs["tracking"]["custom_catalogs"].update_one(
                {
                    "_id": ObjectId(catalog_id),
                    "items": {
                        "$not": {
                            "$elemMatch": {
                                "tmdb_id": int(tmdb_id),
                                "db_index": int(db_index),
                                "media_type": media_type,
                            }
                        }
                    },
                },
                {
                    "$push": {"items": {"$each": [item], "$position": 0}},
                    "$set": {"updated_at": datetime.utcnow()},
                }
            )
            return result.modified_count > 0
        except Exception:
            return False

    async def remove_item_from_custom_catalog(
        self, catalog_id: str, tmdb_id: int, db_index: int, media_type: str
    ) -> bool:
        media_type = self._collection_for(media_type)
        try:
            result = await self.dbs["tracking"]["custom_catalogs"].update_one(
                {"_id": ObjectId(catalog_id)},
                {
                    "$pull": {
                        "items": {
                            "tmdb_id": int(tmdb_id),
                            "db_index": int(db_index),
                            "media_type": media_type,
                        }
                    },
                    "$set": {"updated_at": datetime.utcnow()},
                }
            )
            return result.modified_count > 0
        except Exception:
            return False

    async def find_media_doc(self, media_type: str, tmdb_id: int) -> Optional[Tuple[dict, int]]:
        #----- Locate a media doc by tmdb_id across storage DBs -> (doc, db_index) or None
        collection_name = self._collection_for(media_type)
        try:
            tmdb_id = int(tmdb_id)
        except (TypeError, ValueError):
            return None

        for i in range(1, self.current_db_index + 1):
            db_key = f"storage_{i}"
            if db_key not in self.dbs:
                continue
            doc = await self.dbs[db_key][collection_name].find_one({"tmdb_id": tmdb_id})
            if doc:
                doc["db_index"] = i
                return doc, i
        return None

    async def purge_media_from_catalogs(self, tmdb_id: int, media_type: str) -> int:
        #----- Remove a media item from every catalog (auto + manual) by tmdb_id + media_type
        if tmdb_id in (None, "", 0):
            return 0
        try:
            tmdb_id = int(tmdb_id)
        except (TypeError, ValueError):
            return 0

        media_type = self._collection_for(media_type)
        collection = self.dbs["tracking"]["custom_catalogs"]
        now = datetime.utcnow()
        try:
            result = await collection.update_many(
                {"items": {"$elemMatch": {"tmdb_id": tmdb_id, "media_type": media_type}}},
                [
                    {
                        "$set": {
                            "items": {
                                "$filter": {
                                    "input": "$items",
                                    "as": "it",
                                    "cond": {
                                        "$not": [
                                            {
                                                "$and": [
                                                    {"$eq": ["$$it.tmdb_id", tmdb_id]},
                                                    {"$eq": ["$$it.media_type", media_type]},
                                                ]
                                            }
                                        ]
                                    },
                                }
                            }
                        }
                    },
                    {"$set": {"item_count": {"$size": "$items"}, "updated_at": now}},
                ],
            )
            if result.modified_count:
                LOGGER.info(
                    f"Purged {media_type} tmdb_id {tmdb_id} from "
                    f"{result.modified_count} catalog(s)."
                )
            return result.modified_count
        except Exception as e:
            LOGGER.error(f"Failed to purge tmdb_id {tmdb_id} from catalogs: {e}")
            return 0

    async def custom_catalog_contains_item(
        self, catalog_id: str, tmdb_id: int, db_index: int, media_type: str
    ) -> bool:
        media_type = self._collection_for(media_type)
        try:
            catalog = await self.dbs["tracking"]["custom_catalogs"].find_one({
                "_id": ObjectId(catalog_id),
                "items": {
                    "$elemMatch": {
                        "tmdb_id": int(tmdb_id),
                        "db_index": int(db_index),
                        "media_type": media_type,
                    }
                }
            })
            return bool(catalog)
        except Exception:
            return False

    async def get_custom_catalog_items(
        self, catalog_id: str, media_type: Optional[str] = None, page: int = 1, page_size: int = 24
    ) -> dict:
        catalog = await self.get_custom_catalog(catalog_id)
        if not catalog:
            return {"catalog": None, "items": [], "total_count": 0, "current_page": page, "total_pages": 0}

        db_media_type = None
        if media_type:
            db_media_type = self._collection_for(media_type)

        raw_items = catalog.get("items", []) or []
        if db_media_type:
            raw_items = [item for item in raw_items if item.get("media_type") == db_media_type]
        raw_items = sorted(raw_items, key=lambda i: str(i.get("release_date") or ""), reverse=True)

        total_count = len(raw_items)
        skip = (page - 1) * page_size
        selected_items = raw_items[skip:skip + page_size]

        hydrated_items = await self.get_documents(selected_items)

        total_pages = (total_count + page_size - 1) // page_size if total_count else 0
        return {
            "catalog": catalog,
            "items": hydrated_items,
            "total_count": total_count,
            "current_page": page,
            "total_pages": total_pages,
        }


    #-----
    #----- Helper Methods for Repeated Logic
    #-----
    def _get_sort_dict(self, sort_params: List[Tuple[str, str]]) -> Dict[str, int]:
        if sort_params:
            sort_field, sort_direction = sort_params[0]
            return {sort_field: DESCENDING if sort_direction.lower() == "desc" else ASCENDING}
        return {"updated_on": DESCENDING}

    async def _paginate_collection(
        self,
        collection_name: str,
        sort_dict: Dict[str, int],
        page: int,
        page_size: int,
        filter_dict: Optional[dict] = None,
        projection: Optional[dict] = None,
    ):
        filter_dict = filter_dict or {}
        skip = (page - 1) * page_size
        results = []
        dbs_checked = []

        indexes = range(1, self.current_db_index + 1)
        counts = await gather(*(self._count(f"storage_{i}", collection_name, filter_dict) for i in indexes))
        db_counts = list(zip(indexes, counts))
        total_count = sum(counts)

        start_db_index = None
        for db_index, count in reversed(db_counts):
            if skip < count:
                start_db_index = db_index
                break
            skip -= count

        if not start_db_index:
            return [], [], total_count

        for db_index, count in reversed(db_counts):
            if db_index < start_db_index:
                continue

            db_key = f"storage_{db_index}"
            db = self.dbs[db_key]
            dbs_checked.append(db_index)

            cursor = (
                db[collection_name]
                .find(filter_dict, projection)
                .sort(sort_dict)
                .skip(skip if db_index == start_db_index else 0)
                .limit(page_size - len(results))
            )

            docs = await cursor.to_list(None)
            results.extend(docs)

            if len(results) >= page_size:
                break

        return results, dbs_checked, total_count

    async def _count(self, db_key: str, collection_name: str, filter_dict: dict) -> int:
        coll = self.dbs[db_key][collection_name]
        if not filter_dict:
            return await coll.estimated_document_count()
        key = (db_key, collection_name, repr(filter_dict))
        cached = _count_cache.get(key)
        if cached and time.time() - cached[0] < _COUNT_TTL:
            return cached[1]
        count = await coll.count_documents(filter_dict)
        _count_cache[key] = (time.time(), count)
        return count

    async def _move_document(
        self, collection_name: str, document: dict, old_db_index: int
    ) -> bool:
        current_db_key = f"storage_{self.current_db_index}"
        old_db_key = f"storage_{old_db_index}"
        document["db_index"] = self.current_db_index
        try:
            await self.dbs[current_db_key][collection_name].insert_one(document)
            await self.dbs[old_db_key][collection_name].delete_one({"_id": document["_id"]})
            LOGGER.info(f"✅ Moved document {document.get('tmdb_id')} from {old_db_key} to {current_db_key}")
            return True
        except Exception as e:
            LOGGER.error(f"Error moving document to {current_db_key}: {e}")
            return False

    async def _handle_storage_error(self, func, *args, total_storage_dbs: int) -> Optional[Any]:
        next_db_index = (self.current_db_index % total_storage_dbs) + 1
        if next_db_index == 1:
            LOGGER.warning("⚠️ All storage databases are full! Add more.")
            return None
        self.current_db_index = next_db_index
        await self.update_current_db_index()
        LOGGER.info(f"Switched to storage_{self.current_db_index}")
        return await func(*args)

    #----- Map any media_type spelling to its collection name
    @staticmethod
    def _collection_for(media_type: str) -> str:
        return "tv" if str(media_type).lower() in ("tv", "series") else "movie"

    #----- Load a doc by tmdb_id, apply an async mutator, and save only if it changed
    async def _edit_media_doc(self, collection_name: str, tmdb_id: int, db_index: int, mutate) -> bool:
        db_key = f"storage_{db_index}"
        doc = await self.dbs[db_key][collection_name].find_one({"tmdb_id": tmdb_id})
        if not doc:
            return False
        if not await mutate(doc):
            return False
        doc["updated_on"] = datetime.utcnow()
        result = await self.dbs[db_key][collection_name].replace_one({"tmdb_id": tmdb_id}, doc)
        return result.modified_count > 0

    #----- Locate an existing doc across storage DBs by imdb_id, then tmdb_id, then title+year
    async def _find_existing_media(
        self, collection_name: str, imdb_id, tmdb_id, title, release_year, total_storage_dbs: int, kitsu_id=None
    ) -> Tuple[Optional[dict], Optional[str], Optional[int]]:
        for db_index in range(1, total_storage_dbs + 1):
            col = self.dbs[f"storage_{db_index}"][collection_name]
            doc = None
            if imdb_id:
                doc = await col.find_one({"imdb_id": imdb_id})
            if not doc and tmdb_id:
                doc = await col.find_one({"tmdb_id": tmdb_id})
            if not doc and kitsu_id:
                try:
                    kid = int(kitsu_id)
                    doc = await col.find_one({"kitsu_id": kid})
                except (TypeError, ValueError):
                    pass
            if not doc and title and release_year:
                doc = await col.find_one({"title": title, "release_year": release_year})
            if doc:
                return doc, f"storage_{db_index}", db_index
        return None, None, None


    #-----
    #----- Multi Database Method for insert/update/delete/list
    #-----

    async def _build_part_id_and_size(self, parts: List[dict], archive: Optional[str] = None) -> Tuple[str, str]:
        sorted_parts = sorted(parts, key=lambda p: p.get("part_number", 0))
        payload = {"parts": [{"chat_id": p["chat_id"], "msg_id": p["msg_id"]} for p in sorted_parts]}
        if archive == "zip":
            payload["zip"] = True
        encoded = await encode_string(payload)
        total_bytes = sum(p.get("size_bytes", 0) for p in sorted_parts)
        from Backend.helper.pyro import get_readable_file_size 
        size_str = get_readable_file_size(total_bytes)
        return encoded, size_str

    async def get_media_ids_by_part(
        self, channel: int, msg_id: int
    ) -> Optional[Tuple[Optional[str], Optional[int], str]]:
        try:
            legacy_hash = await encode_string({"chat_id": channel, "msg_id": msg_id})
        except Exception:
            legacy_hash = None

        part_match = {"$elemMatch": {"chat_id": channel, "msg_id": msg_id}}
        projection = {"imdb_id": 1, "tmdb_id": 1}

        for i in range(1, self.current_db_index + 1):
            db = self.dbs[f"storage_{i}"]

            movie_or = [{"telegram.parts": part_match}]
            tv_or = [{"seasons.episodes.telegram.parts": part_match}]
            if legacy_hash:
                movie_or.append({"telegram.id": legacy_hash})
                tv_or.append({"seasons.episodes.telegram.id": legacy_hash})

            doc = await db["movie"].find_one({"$or": movie_or}, projection)
            if doc:
                return doc.get("imdb_id"), doc.get("tmdb_id"), "movie"
            doc = await db["tv"].find_one({"$or": tv_or}, projection)
            if doc:
                return doc.get("imdb_id"), doc.get("tmdb_id"), "tv"

        return None

    #----- Edited caption: the stored file name follows it (a split file takes part 1's caption)
    async def rename_media_part(self, channel: int, msg_id: int, name: str) -> bool:
        legacy_hash = await encode_string({"chat_id": channel, "msg_id": msg_id})
        part_match = {"$elemMatch": {"chat_id": channel, "msg_id": msg_id}}

        def is_target(q: dict) -> bool:
            first = (q.get("parts") or [{}])[0]
            return q.get("id") == legacy_hash or (first.get("chat_id") == channel and first.get("msg_id") == msg_id)

        for i in range(1, self.current_db_index + 1):
            db = self.dbs[f"storage_{i}"]
            for coll, path in (("movie", "telegram"), ("tv", "seasons.episodes.telegram")):
                doc = await db[coll].find_one({"$or": [{f"{path}.id": legacy_hash}, {f"{path}.parts": part_match}]})
                if not doc:
                    continue
                qualities = (doc.get("telegram") or []) if coll == "movie" else [
                    q for s in doc.get("seasons") or [] for e in s.get("episodes") or [] for q in e.get("telegram") or []
                ]
                changed = False
                for q in qualities:
                    if is_target(q) and q.get("name") != name:
                        q["name"] = name
                        q.pop("ptn", None)
                        changed = True
                if changed:
                    doc["updated_on"] = datetime.utcnow()
                    await db[coll].replace_one({"_id": doc["_id"]}, doc)
                return changed
        return False

    async def remove_media_part(self, channel: int, msg_id: int) -> bool:
        try:
            legacy_hash = await encode_string({"chat_id": channel, "msg_id": msg_id})
            if await self.delete_media_by_stream_id(legacy_hash):
                return True
        except Exception as e:
            LOGGER.error(f"remove_media_part: legacy lookup failed: {e}")

        for i in range(1, self.current_db_index + 1):
            db = self.dbs[f"storage_{i}"]

            movie = await db["movie"].find_one(
                {"telegram.parts": {"$elemMatch": {"chat_id": channel, "msg_id": msg_id}}}
            )
            if movie:
                new_telegram = []
                for q in movie.get("telegram", []):
                    parts = q.get("parts")
                    if parts and any(p.get("chat_id") == channel and p.get("msg_id") == msg_id for p in parts):
                        remaining = [p for p in parts if not (p.get("chat_id") == channel and p.get("msg_id") == msg_id)]
                        if not remaining:
                            continue  #----- last part removed: drop the whole quality entry
                        new_id, new_size = await self._build_part_id_and_size(remaining)
                        q["parts"] = remaining
                        q["id"] = new_id
                        q["size"] = new_size
                    new_telegram.append(q)

                if len(new_telegram) == 0:
                    await db["movie"].delete_one({"_id": movie["_id"]})
                    await self.purge_media_from_catalogs(movie.get("tmdb_id"), "movie")
                else:
                    movie["telegram"] = new_telegram
                    movie["updated_on"] = datetime.utcnow()
                    await db["movie"].replace_one({"_id": movie["_id"]}, movie)
                return True

            tv = await db["tv"].find_one(
                {"seasons.episodes.telegram.parts": {"$elemMatch": {"chat_id": channel, "msg_id": msg_id}}}
            )
            if tv:
                for season in tv.get("seasons", []):
                    for episode in season.get("episodes", []):
                        new_telegram = []
                        for q in episode.get("telegram", []):
                            parts = q.get("parts")
                            if parts and any(p.get("chat_id") == channel and p.get("msg_id") == msg_id for p in parts):
                                remaining = [p for p in parts if not (p.get("chat_id") == channel and p.get("msg_id") == msg_id)]
                                if not remaining:
                                    continue
                                new_id, new_size = await self._build_part_id_and_size(remaining)
                                q["parts"] = remaining
                                q["id"] = new_id
                                q["size"] = new_size
                            new_telegram.append(q)
                        episode["telegram"] = new_telegram
                    season["episodes"] = [e for e in season.get("episodes", []) if e.get("telegram")]
                tv["seasons"] = [s for s in tv.get("seasons", []) if s.get("episodes")]

                if len(tv["seasons"]) == 0:
                    await db["tv"].delete_one({"_id": tv["_id"]})
                    await self.purge_media_from_catalogs(tv.get("tmdb_id"), "tv")
                else:
                    tv["updated_on"] = datetime.utcnow()
                    await db["tv"].replace_one({"_id": tv["_id"]}, tv)
                return True

        return False

    async def insert_media(
        self, metadata_info: dict,
        channel: int, msg_id: int, size: str, name: str, raw_size: int = 0,
        status: Optional[dict] = None
    ) -> Optional[ObjectId]:

        group_key = metadata_info.get("group_key")
        part_number = metadata_info.get("part_number")

        if group_key:
            part = {
                "part_number": part_number or 1,
                "chat_id": channel,
                "msg_id": msg_id,
                "size_bytes": raw_size,
            }
            archive = "zip" if str(group_key).endswith(".zip") else None
            part_id, part_size = await self._build_part_id_and_size([part], archive)
            quality_detail = QualityDetail(
                quality=metadata_info['quality'],
                id=part_id,
                name=name,
                size=part_size,
                group_key=group_key,
                parts=[QualityPart(**part)],
            )
        else:
            quality_detail = QualityDetail(
                quality=metadata_info['quality'],
                id=metadata_info['encoded_string'],
                name=name,
                size=size,
            )

        def _as_int(val):
            if val is None or val == "":
                return None
            try:
                return int(val)
            except (TypeError, ValueError):
                return None

        kitsu_id = _as_int(metadata_info.get("kitsu_id"))
        absolute_episode = _as_int(metadata_info.get("absolute_episode"))

        if metadata_info['media_type'] == "movie":
            media = MovieSchema(
                tmdb_id=metadata_info['tmdb_id'],
                imdb_id=metadata_info['imdb_id'],
                kitsu_id=kitsu_id,
                db_index=self.current_db_index,
                title=metadata_info['title'],
                title_english=metadata_info.get('title_english') or None,
                original_title=metadata_info.get('original_title') or None,
                genres=metadata_info['genres'],
                description=metadata_info['description'],
                rating=metadata_info['rate'],
                release_year=metadata_info['year'] if isinstance(metadata_info.get('year'), int) else (int(str(metadata_info.get('year'))[:4]) if metadata_info.get('year') else None),
                release_year_end=metadata_info.get('year_end') or None,
                poster=metadata_info['poster'],
                backdrop=metadata_info['backdrop'],
                logo=metadata_info['logo'],
                cast=metadata_info['cast'],
                runtime=metadata_info['runtime'],
                media_type=metadata_info['media_type'],
                is_anime=metadata_info.get('is_anime', False),
                original_language=metadata_info.get('original_language'),
                origin_country=metadata_info.get('origin_country', []) or [],
                telegram=[quality_detail]
            )
            return await self.update_movie(media, status)
        else:
            tv_show = TVShowSchema(
                tmdb_id=metadata_info['tmdb_id'],
                imdb_id=metadata_info['imdb_id'],
                kitsu_id=kitsu_id,
                db_index=self.current_db_index,
                title=metadata_info['title'],
                title_english=metadata_info.get('title_english') or None,
                original_title=metadata_info.get('original_title') or None,
                genres=metadata_info['genres'],
                description=metadata_info['description'],
                rating=metadata_info['rate'],
                release_year=metadata_info['year'] if isinstance(metadata_info.get('year'), int) else (int(str(metadata_info.get('year'))[:4]) if metadata_info.get('year') else None),
                release_year_end=metadata_info.get('year_end') or None,
                poster=metadata_info['poster'],
                backdrop=metadata_info['backdrop'],
                logo=metadata_info['logo'],
                cast=metadata_info['cast'],
                runtime=metadata_info['runtime'],
                media_type=metadata_info['media_type'],
                is_anime=metadata_info.get('is_anime', False),
                original_language=metadata_info.get('original_language'),
                origin_country=metadata_info.get('origin_country', []) or [],
                seasons=[Season(
                    season_number=metadata_info['season_number'],
                    episodes=[Episode(
                        episode_number=metadata_info['episode_number'],
                        title=metadata_info['episode_title'],
                        episode_backdrop=metadata_info['episode_backdrop'],
                        overview=metadata_info['episode_overview'],
                        released=metadata_info['episode_released'],
                        absolute_episode=absolute_episode,
                        telegram=[quality_detail]
                    )]
                )]
            )
            return await self.update_tv_show(tv_show, status)

    async def _delete_split_part(self, part: dict) -> None:
        try:
            chat_id = int(f"-100{part['chat_id']}")
            msg_id = int(part["msg_id"])
            create_task(delete_message(chat_id, msg_id))
        except Exception as e:
            LOGGER.error(f"Failed to delete split part message: {e}")

    async def _merge_split_part(self, qualities: List[dict], quality_to_update: dict) -> List[dict]:
        group_key = quality_to_update.get("group_key")
        incoming_parts = quality_to_update.get("parts") or []
        if not incoming_parts:
            return qualities + [quality_to_update]
        new_part = incoming_parts[0]
        replace_mode = SettingsManager.current().replace_mode

        merged = False
        result = []
        for q in qualities:
            if group_key is not None and q.get("group_key") == group_key:
                existing_parts = q.get("parts") or []

                if replace_mode:
                    for old_part in existing_parts:
                        if old_part.get("part_number") != new_part.get("part_number"):
                            continue
                        if (
                            old_part.get("chat_id") == new_part.get("chat_id")
                            and old_part.get("msg_id") == new_part.get("msg_id")
                        ):
                            continue
                        await self._delete_split_part(old_part)

                existing_parts = [
                    p for p in existing_parts if p.get("part_number") != new_part.get("part_number")
                ]
                existing_parts.append(new_part)
                archive = "zip" if str(group_key).endswith(".zip") else None
                new_id, new_size = await self._build_part_id_and_size(existing_parts, archive)
                q["parts"] = existing_parts
                q["id"] = new_id
                q["size"] = new_size
                q["name"] = quality_to_update.get("name", q.get("name"))
                merged = True
            result.append(q)

        if not merged:
            result.append(quality_to_update)
        return result

    #----- Identity of a non-split stream for duplicate protection (quality + name + size)
    @staticmethod
    def _dup_key(quality: dict) -> tuple:
        name = re.sub(r"\s+", " ", str(quality.get("name") or "").strip().lower())
        size = str(quality.get("size") or "").strip().lower()
        return (quality.get("quality"), name, size)

    @staticmethod
    def _is_personal_tmdb(tmdb_id) -> bool:
        try:
            return int(tmdb_id) < 0
        except (TypeError, ValueError):
            return False

    async def _apply_quality_update(
        self, existing_qualities: List[dict], quality_to_update: dict,
        is_personal: bool = False, status: Optional[dict] = None
    ) -> List[dict]:
        target_quality = quality_to_update.get("quality")
        incoming_group_key = quality_to_update.get("group_key")
        replace_mode = SettingsManager.current().replace_mode

        if incoming_group_key:
            #----- Incoming is a split part.
            if replace_mode:
                stale = [
                    q for q in existing_qualities
                    if q.get("quality") == target_quality
                    and q.get("group_key") != incoming_group_key
                ]
                for q in stale:
                    await self._queue_quality_deletion(q)
                existing_qualities = [
                    q for q in existing_qualities
                    if not (
                        q.get("quality") == target_quality
                        and q.get("group_key") != incoming_group_key
                    )
                ]
            return await self._merge_split_part(existing_qualities, quality_to_update)

        #----- Incoming is a normal (non-split) file; a re-save of the same message is never its own duplicate.
        existing_qualities = [q for q in existing_qualities if q.get("id") != quality_to_update.get("id")]
        if replace_mode:
            stale = [q for q in existing_qualities if q.get("quality") == target_quality]
            for q in stale:
                await self._queue_quality_deletion(q)
            existing_qualities = [
                q for q in existing_qualities if q.get("quality") != target_quality
            ]
            existing_qualities.append(quality_to_update)
            return existing_qualities

        #----- REPLACE_MODE off: skip exact duplicates when protection is on, else stack.
        if SettingsManager.current().duplicate_protection and not is_personal:
            key = self._dup_key(quality_to_update)
            for q in existing_qualities:
                if not q.get("group_key") and self._dup_key(q) == key:
                    LOGGER.info(f"Duplicate protection: skipped existing stream '{quality_to_update.get('name')}'.")
                    if status is not None:
                        status["duplicate_skipped"] = True
                    return existing_qualities
        existing_qualities.append(quality_to_update)
        return existing_qualities

    async def update_movie(self, movie_data: MovieSchema, status: Optional[dict] = None) -> Optional[ObjectId]:
        try:
            movie_dict = movie_data.dict()
        except ValidationError as e:
            LOGGER.error(f"Validation error: {e}")
            return None

        imdb_id = movie_dict["imdb_id"]
        tmdb_id = movie_dict["tmdb_id"]
        kitsu_id = movie_dict.get("kitsu_id")
        title = movie_dict["title"]
        release_year = movie_dict["release_year"]

        quality_to_update = movie_dict["telegram"][0]

        current_db_key = f"storage_{self.current_db_index}"
        total_storage_dbs = len(self.dbs) - 1

        existing_movie, existing_db_key, existing_db_index = await self._find_existing_media(
            "movie", imdb_id, tmdb_id, title, release_year, total_storage_dbs, kitsu_id=kitsu_id
        )

        #----- INSERT NEW MOVIE ----------------
        if not existing_movie:
            try:
                movie_dict["db_index"] = self.current_db_index
                result = await self.dbs[current_db_key]["movie"].insert_one(movie_dict)
                return result.inserted_id
            except Exception as e:
                LOGGER.error(f"Insertion failed in {current_db_key}: {e}")
                if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                    return await self._handle_storage_error(self.update_movie, movie_data, total_storage_dbs=total_storage_dbs)
                return None

        #----- UPDATE MOVIE ----------------
        movie_id = existing_movie["_id"]

        if imdb_id and not existing_movie.get("imdb_id"):
            existing_movie["imdb_id"] = imdb_id
        if tmdb_id and not existing_movie.get("tmdb_id"):
            existing_movie["tmdb_id"] = tmdb_id
        if kitsu_id and not existing_movie.get("kitsu_id"):
            existing_movie["kitsu_id"] = kitsu_id
        if movie_dict.get("is_anime"):
            existing_movie["is_anime"] = True

        existing_qualities = existing_movie.get("telegram", [])

        existing_qualities = await self._apply_quality_update(
            existing_qualities, quality_to_update, self._is_personal_tmdb(tmdb_id), status
        )

        existing_movie["telegram"] = existing_qualities
        existing_movie["updated_on"] = datetime.utcnow()

        if existing_db_index != self.current_db_index:
            try:
                if await self._move_document("movie", existing_movie, existing_db_index):
                    return movie_id
            except Exception as e:
                LOGGER.error(f"Error moving movie to {current_db_key}: {e}")
                if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                    return await self._handle_storage_error(self.update_movie, movie_data, total_storage_dbs=total_storage_dbs)

        try:
            await self.dbs[existing_db_key]["movie"].replace_one({"_id": movie_id}, existing_movie)
            return movie_id
        except Exception as e:
            LOGGER.error(f"Failed to update movie {tmdb_id} in {existing_db_key}: {e}")
            if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                return await self._handle_storage_error(self.update_movie, movie_data, total_storage_dbs=total_storage_dbs)

    async def update_tv_show(self, tv_show_data: TVShowSchema, status: Optional[dict] = None) -> Optional[ObjectId]:
        try:
            tv_show_dict = tv_show_data.dict()
        except ValidationError as e:
            LOGGER.error(f"Validation error: {e}")
            return None

        imdb_id = tv_show_dict.get("imdb_id")
        tmdb_id = tv_show_dict.get("tmdb_id")
        kitsu_id = tv_show_dict.get("kitsu_id")
        title = tv_show_dict["title"]
        release_year = tv_show_dict["release_year"]

        current_db_key = f"storage_{self.current_db_index}"
        total_storage_dbs = len(self.dbs) - 1

        existing_tv, existing_db_key, existing_db_index = await self._find_existing_media(
            "tv", imdb_id, tmdb_id, title, release_year, total_storage_dbs, kitsu_id=kitsu_id
        )

        #----- INSERT NEW TV ----------------
        if not existing_tv:
            try:
                tv_show_dict["db_index"] = self.current_db_index
                result = await self.dbs[current_db_key]["tv"].insert_one(tv_show_dict)
                return result.inserted_id
            except Exception as e:
                LOGGER.error(f"Insertion failed in {current_db_key}: {e}")
                if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                    return await self._handle_storage_error(self.update_tv_show, tv_show_data, total_storage_dbs=total_storage_dbs)
                return None

        #----- UPDATE TV ----------------
        tv_id = existing_tv["_id"]

        if imdb_id and not existing_tv.get("imdb_id"):
            existing_tv["imdb_id"] = imdb_id
        if tmdb_id and not existing_tv.get("tmdb_id"):
            existing_tv["tmdb_id"] = tmdb_id
        if kitsu_id and not existing_tv.get("kitsu_id"):
            existing_tv["kitsu_id"] = kitsu_id
        if tv_show_dict.get("is_anime"):
            existing_tv["is_anime"] = True

        for season in tv_show_dict["seasons"]:
            existing_season = next(
                (s for s in existing_tv["seasons"]
                if s["season_number"] == season["season_number"]),
                None
            )

            if not existing_season:
                existing_tv["seasons"].append(season)
                continue

            for episode in season["episodes"]:
                existing_episode = next(
                    (e for e in existing_season["episodes"]
                    if e["episode_number"] == episode["episode_number"]),
                    None
                )

                if not existing_episode and episode.get("absolute_episode") is not None:
                    existing_episode = next(
                        (e for e in existing_season["episodes"]
                         if e.get("absolute_episode") is not None
                         and e.get("absolute_episode") == episode.get("absolute_episode")),
                        None
                    )

                if not existing_episode:
                    existing_season["episodes"].append(episode)
                    continue

                if episode.get("absolute_episode") is not None and not existing_episode.get("absolute_episode"):
                    existing_episode["absolute_episode"] = episode["absolute_episode"]

                existing_episode.setdefault("telegram", [])

                for quality in episode["telegram"]:
                    existing_episode["telegram"] = await self._apply_quality_update(
                        existing_episode["telegram"], quality, self._is_personal_tmdb(tmdb_id), status
                    )

        existing_tv["updated_on"] = datetime.utcnow()

        #----- MOVE DB IF NEEDED ----------------
        if existing_db_index != self.current_db_index:
            try:
                if await self._move_document("tv", existing_tv, existing_db_index):
                    return tv_id
            except Exception as e:
                LOGGER.error(f"Error moving TV show to {current_db_key}: {e}")
                if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                    return await self._handle_storage_error(self.update_tv_show, tv_show_data, total_storage_dbs=total_storage_dbs)
            return tv_id

        try:
            await self.dbs[existing_db_key]["tv"].replace_one({"_id": tv_id}, existing_tv)
            return tv_id
        except Exception as e:
            LOGGER.error(f"Failed to update TV show {tmdb_id} in {existing_db_key}: {e}")
            if any(keyword in str(e).lower() for keyword in ["storage", "quota"]):
                return await self._handle_storage_error(self.update_tv_show, tv_show_data, total_storage_dbs=total_storage_dbs)
    
    async def sort_movies(self, sort_params, page, page_size, genre_filter=None, extra_filter=None, projection=None):
        sort_dict = self._get_sort_dict(sort_params)
        filter_dict = {"genres": {"$in": [genre_filter]}} if genre_filter else {}
        if extra_filter:
            filter_dict.update(extra_filter)
        results, dbs_checked, total_count = await self._paginate_collection(
            "movie", sort_dict, page, page_size, filter_dict=filter_dict, projection=projection
        )
        total_pages = (total_count + page_size - 1) // page_size
        return {
            "total_count": total_count,
            "total_pages": total_pages,
            "databases_checked": dbs_checked,
            "current_page": page,
            "movies": [convert_objectid_to_str(result) for result in results],
        }

    async def sort_tv_shows(self, sort_params, page, page_size, genre_filter=None, extra_filter=None, projection=None):
        sort_dict = self._get_sort_dict(sort_params)
        filter_dict = {"genres": {"$in": [genre_filter]}} if genre_filter else {}
        if extra_filter:
            filter_dict.update(extra_filter)
        results, dbs_checked, total_count = await self._paginate_collection(
            "tv", sort_dict, page, page_size, filter_dict=filter_dict, projection=projection
        )
        total_pages = (total_count + page_size - 1) // page_size
        return {
            "total_count": total_count,
            "total_pages": total_pages,
            "databases_checked": dbs_checked,
            "current_page": page,
            "tv_shows": [convert_objectid_to_str(result) for result in results],
        }

    async def search_documents(
            self, 
            query: str, 
            page: int, 
            page_size: int,
            extra_filter: Optional[dict] = None,
            media_type: Optional[str] = None,
        ) -> dict:

            skip = (page - 1) * page_size
            
            words = query.split()
            regex_query = {
                '$regex': '.*'.join(map(re.escape, words)), 
                '$options': 'i'
            }

            tv_match = {"$or": [
                {"title": regex_query},
                {"title_english": regex_query},
                {"original_title": regex_query},
                {"seasons.episodes.telegram.name": regex_query}
            ]}
            movie_match = {"$or": [
                {"title": regex_query},
                {"title_english": regex_query},
                {"original_title": regex_query},
                {"telegram.name": regex_query}
            ]}
            if extra_filter:
                tv_match = {"$and": [tv_match, extra_filter]}
                movie_match = {"$and": [movie_match, extra_filter]}

            tv_pipeline = [
                {"$match": tv_match},
                {"$project": {
                    "_id": 1, "tmdb_id": 1, "title": 1, "genres": 1, "rating": 1, "imdb_id": 1,
                    "release_year": 1, "release_year_end": 1, "poster": 1, "backdrop": 1, "description": 1, "logo": 1,
                    "media_type": 1, "db_index": 1, "title_english": 1, "original_title": 1,
                    "seasons.episodes.telegram.quality": 1, "seasons.episodes.telegram.size": 1,
                    "seasons.episodes.telegram.group_key": 1,
                }},
                {"$limit": skip + page_size},
            ]
            
            movie_pipeline = [
                {"$match": movie_match},
                {"$project": {
                    "_id": 1, "tmdb_id": 1, "title": 1, "genres": 1, "rating": 1,
                    "release_year": 1, "release_year_end": 1, "poster": 1, "backdrop": 1, "description": 1,
                    "media_type": 1, "db_index": 1, "imdb_id": 1, "logo": 1, "title_english": 1, "original_title": 1,
                    "telegram.quality": 1, "telegram.size": 1, "telegram.group_key": 1,
                }},
                {"$limit": skip + page_size},
            ]
            
            searches = [(c, m, p) for c, m, p in (("tv", tv_match, tv_pipeline), ("movie", movie_match, movie_pipeline))
                        if media_type in (None, c)]

            async def _search(db):
                found = await gather(*(db[c].aggregate(p).to_list(None) for c, _, p in searches))
                return [doc for docs in found for doc in docs]

            results = []
            dbs_checked = []
            
            active_db_key = f"storage_{self.current_db_index}"
            active_db = self.dbs[active_db_key]
            dbs_checked.append(self.current_db_index)
            
            results.extend(await _search(active_db))
            
            if len(results) < page_size:
                previous_db_index = self.current_db_index - 1
                while previous_db_index > 0 and len(results) < page_size:
                    prev_db_key = f"storage_{previous_db_index}"
                    prev_db = self.dbs[prev_db_key]
                    results.extend(await _search(prev_db))
                    dbs_checked.append(previous_db_index)
                    previous_db_index -= 1

            total_count = sum(await gather(*(
                self._count(f"storage_{i}", coll, match)
                for i in dbs_checked for coll, match, _ in searches
            )))
            
            paged_results = results[skip:skip + page_size]

            return {
                "total_count": total_count,
                "results": [convert_objectid_to_str(doc) for doc in paged_results]
            }


    async def get_media_details(
        self, 
        imdb_id: str = None,
        season_number: Optional[int] = None, 
        episode_number: Optional[int] = None,
        kitsu_id: Optional[int] = None,
        absolute_episode: Optional[int] = None,
        tmdb_id: Optional[int] = None,
        media_type: Optional[str] = None,
        projection: Optional[dict] = None,
    ) -> Optional[dict]:

        def _find_doc(col):
            if imdb_id:
                return col.find_one({"imdb_id": imdb_id}, projection)
            if kitsu_id is not None:
                return col.find_one({"kitsu_id": int(kitsu_id)}, projection)
            if tmdb_id is not None:
                return col.find_one({"tmdb_id": int(tmdb_id)}, projection)
            return None

        for db_idx in range(self.current_db_index, 0, -1):
            db_key = f"storage_{db_idx}"

            if absolute_episode is not None and (kitsu_id is not None or imdb_id or tmdb_id is not None):
                tv_show = await _find_doc(self.dbs[db_key]["tv"])
                if tv_show:
                    for season in tv_show.get("seasons", []):
                        for episode in season.get("episodes", []):
                            if episode.get("absolute_episode") == absolute_episode:
                                details = convert_objectid_to_str(episode)
                                details.update({
                                    "imdb_id": tv_show.get("imdb_id") or imdb_id,
                                    "kitsu_id": tv_show.get("kitsu_id") or kitsu_id,
                                    "type": "tv",
                                    "title": tv_show.get("title"),
                                    "is_anime": tv_show.get("is_anime"),
                                    "season_number": season.get("season_number"),
                                    "episode_number": episode.get("episode_number"),
                                    "absolute_episode": absolute_episode,
                                    "backdrop": episode.get("episode_backdrop"),
                                    "db_index": db_idx
                                })
                                return details
                    if season_number is None and episode_number is None:
                        for season in tv_show.get("seasons", []):
                            for episode in season.get("episodes", []):
                                if episode.get("episode_number") == absolute_episode and season.get("season_number") == 1:
                                    details = convert_objectid_to_str(episode)
                                    details.update({
                                        "imdb_id": tv_show.get("imdb_id") or imdb_id,
                                        "kitsu_id": tv_show.get("kitsu_id") or kitsu_id,
                                        "type": "tv",
                                        "title": tv_show.get("title"),
                                        "is_anime": tv_show.get("is_anime"),
                                        "season_number": 1,
                                        "episode_number": absolute_episode,
                                        "absolute_episode": absolute_episode,
                                        "backdrop": episode.get("episode_backdrop"),
                                        "db_index": db_idx
                                    })
                                    return details
            
            if episode_number is not None and season_number is not None:
                tv_show = await _find_doc(self.dbs[db_key]["tv"])
                if tv_show:
                    for season in tv_show.get("seasons", []):
                        if season.get("season_number") == season_number:
                            for episode in season.get("episodes", []):
                                if episode.get("episode_number") == episode_number:
                                    details = convert_objectid_to_str(episode)
                                    details.update({
                                        "imdb_id": tv_show.get("imdb_id") or imdb_id,
                                        "kitsu_id": tv_show.get("kitsu_id") or kitsu_id,
                                        "type": "tv",
                                        "title": tv_show.get("title"),
                                        "is_anime": tv_show.get("is_anime"),
                                        "season_number": season_number,
                                        "episode_number": episode_number,
                                        "absolute_episode": episode.get("absolute_episode"),
                                        "backdrop": episode.get("episode_backdrop"),
                                        "db_index": db_idx
                                    })
                                    return details
            
            elif season_number is not None and absolute_episode is None:
                tv_show = await _find_doc(self.dbs[db_key]["tv"])
                if tv_show:
                    for season in tv_show.get("seasons", []):
                        if season.get("season_number") == season_number:
                            details = convert_objectid_to_str(season)
                            details.update({
                                "imdb_id": tv_show.get("imdb_id") or imdb_id,
                                "kitsu_id": tv_show.get("kitsu_id") or kitsu_id,
                                "type": "tv",
                                "season_number": season_number,
                                "db_index": db_idx
                            })
                            return details
            
            else:
                if tmdb_id is not None and media_type == "movie":
                    movie_doc = await _find_doc(self.dbs[db_key]["movie"])
                    if movie_doc:
                        movie_doc = convert_objectid_to_str(movie_doc)
                        movie_doc["type"] = "movie"
                        movie_doc["db_index"] = db_idx
                        return movie_doc
                    continue

                if tmdb_id is not None and media_type in ("series", "tv"):
                    tv_doc = await _find_doc(self.dbs[db_key]["tv"])
                    if tv_doc:
                        tv_doc = convert_objectid_to_str(tv_doc)
                        tv_doc["type"] = "tv"
                        tv_doc["db_index"] = db_idx
                        return tv_doc
                    continue

                tv_doc = await _find_doc(self.dbs[db_key]["tv"])
                if tv_doc:
                    tv_doc = convert_objectid_to_str(tv_doc)
                    tv_doc["type"] = "tv"
                    tv_doc["db_index"] = db_idx
                    return tv_doc
                
                movie_doc = await _find_doc(self.dbs[db_key]["movie"])
                if movie_doc:
                    movie_doc = convert_objectid_to_str(movie_doc)
                    movie_doc["type"] = "movie"
                    movie_doc["db_index"] = db_idx
                    return movie_doc
        
        return None

    #-----
    #----- DB Method for Edit Post
    #-----

    async def get_document(self, media_type: str, tmdb_id: int, db_index: int) -> Optional[Dict[str, Any]]:
        db_key = f"storage_{db_index}"
        collection_name = self._collection_for(media_type)
        document = await self.dbs[db_key][collection_name].find_one({"tmdb_id": int(tmdb_id)})
        return convert_objectid_to_str(document) if document else None

    async def get_documents(self, refs: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        if not refs:
            return []

        groups: Dict[Tuple[int, str], List[int]] = {}
        normalized: List[Tuple[int, str, int]] = []
        for ref in refs:
            try:
                tmdb_id = int(ref.get("tmdb_id"))
                db_index = int(ref.get("db_index", 1))
            except (TypeError, ValueError):
                continue
            collection_name = self._collection_for(ref.get("media_type", "movie"))
            groups.setdefault((db_index, collection_name), []).append(tmdb_id)
            normalized.append((db_index, collection_name, tmdb_id))

        lookup: Dict[Tuple[int, str, int], Dict[str, Any]] = {}
        for (db_index, collection_name), ids in groups.items():
            db_key = f"storage_{db_index}"
            if db_key not in self.dbs:
                continue
            try:
                cursor = self.dbs[db_key][collection_name].find({"tmdb_id": {"$in": ids}}, META_PROJECTION)
                async for document in cursor:
                    document = convert_objectid_to_str(document)
                    try:
                        key = (db_index, collection_name, int(document.get("tmdb_id")))
                    except (TypeError, ValueError):
                        continue
                    lookup[key] = document
            except Exception as e:
                LOGGER.error(f"get_documents batch fetch failed for {db_key}/{collection_name}: {e}")

        ordered: List[Dict[str, Any]] = []
        for key in normalized:
            document = lookup.get(key)
            if document is not None:
                ordered.append(document)
        return ordered

    async def update_document(
        self, media_type: str, tmdb_id: int, db_index: int, update_data: Dict[str, Any]
    ):
        update_data.pop('_id', None)
        db_key = f"storage_{db_index}"
        collection_name = self._collection_for(media_type)
        collection = self.dbs[db_key][collection_name]

        try:
            result = await collection.update_one({"tmdb_id": int(tmdb_id)}, {"$set": update_data})

            return result.modified_count > 0

        except Exception as e:
            err_str = str(e).lower()
            LOGGER.error(f"Error updating document in {db_key}: {e}")
            if "storage" in err_str or "quota" in err_str:
                total_storage_dbs = len(self.dbs) - 1
                db_index_int = int(db_index)
                next_db_index = (db_index_int % total_storage_dbs) + 1
                if next_db_index == 1:
                    LOGGER.warning("⚠️ All storage databases are full! Add more.")
                    return False

                new_db_key = f"storage_{next_db_index}"
                LOGGER.info(f"Switching from {db_key} to {new_db_key} due to storage error.")

                try:
                    old_doc = await self.dbs[db_key][collection_name].find_one({"tmdb_id": int(tmdb_id)})
                    if not old_doc:
                        LOGGER.error(f"Document with tmdb_id {tmdb_id} not found in {db_key} during migration.")
                        return False

                    old_doc.update(update_data)
                    old_doc["db_index"] = next_db_index
                    old_doc.pop("_id", None)
                    insert_result = await self.dbs[new_db_key][collection_name].insert_one(old_doc)
                    LOGGER.info(f"Inserted document {insert_result.inserted_id} into {new_db_key}")
                    await self.dbs[db_key][collection_name].delete_one({"tmdb_id": int(tmdb_id)})
                    LOGGER.info(f"Deleted document tmdb_id {tmdb_id} from {db_key}")
                    self.current_db_index = next_db_index
                    await self.update_current_db_index()
                    LOGGER.info(f"Switched to {new_db_key} and document migrated successfully.")
                    return True

                except Exception as migrate_error:
                    LOGGER.error(f"Error migrating document tmdb_id {tmdb_id} to {new_db_key}: {migrate_error}")
                    return False
            raise

    #----- Queue deletion of the Telegram message(s) backing a quality (split or single)
    async def _queue_quality_deletion(self, quality: dict) -> None:
        parts = quality.get("parts")
        if parts:
            for part in parts:
                await self._delete_split_part(part)
            return

        old_id = quality.get("id")
        if not old_id:
            return
        try:
            decoded = await decode_string(old_id)
            if isinstance(decoded, dict) and decoded.get("parts"):
                for part in decoded["parts"]:
                    await self._delete_split_part(part)
                return
            chat_id = int(f"-100{decoded['chat_id']}")
            msg_id = int(decoded["msg_id"])
            create_task(delete_message(chat_id, msg_id))
        except Exception as e:
            LOGGER.error(f"Failed to queue file for deletion: {e}")

    async def delete_document(self, media_type: str, tmdb_id: int, db_index: int) -> bool:
        db_key = f"storage_{db_index}"
        collection_name = self._collection_for(media_type)

        doc = await self.dbs[db_key][collection_name].find_one({"tmdb_id": tmdb_id})
        if doc:
            if collection_name == "movie":
                for quality in doc.get("telegram", []):
                    await self._queue_quality_deletion(quality)
            else:
                for season in doc.get("seasons", []):
                    for episode in season.get("episodes", []):
                        for quality in episode.get("telegram", []):
                            await self._queue_quality_deletion(quality)

        result = await self.dbs[db_key][collection_name].delete_one({"tmdb_id": tmdb_id})
        if result.deleted_count > 0:
            await self.purge_media_from_catalogs(tmdb_id, collection_name)
            LOGGER.info(f"{media_type} with tmdb_id {tmdb_id} deleted successfully.")
            return True
        LOGGER.info(f"No document found with tmdb_id {tmdb_id}.")
        return False

    async def get_title_by_stream_id(self, stream_id_hash: str) -> Optional[str]:
        for i in range(1, self.current_db_index + 1):
            db = self.dbs[f"storage_{i}"]
            
            #----- Check Movies
            movie = await db["movie"].find_one({"telegram.id": stream_id_hash})
            if movie and "telegram" in movie:
                for t in movie["telegram"]:
                    if t.get("id") == stream_id_hash:
                        return movie.get("title")

            #----- Check TV Shows
            tv = await db["tv"].find_one({"seasons.episodes.telegram.id": stream_id_hash})
            if tv and "seasons" in tv:
                title = tv.get("title", "Unknown Series")
                for season in tv.get("seasons", []):
                    for episode in season.get("episodes", []):
                        for t in episode.get("telegram", []):
                            if t.get("id") == stream_id_hash:
                                s_num = season.get("season_number", 0)
                                e_num = episode.get("episode_number", 0)
                                return f"{title} S{s_num:02d}E{e_num:02d}"

        return None

    async def delete_media_by_stream_id(self, stream_id_hash: str, delete_file: bool = False) -> bool:
        for i in range(1, self.current_db_index + 1):
            db = self.dbs[f"storage_{i}"]
            
            #----- Check Movies
            movie = await db["movie"].find_one({"telegram.id": stream_id_hash})
            if movie:
                if delete_file:
                    for q in movie.get("telegram", []):
                        if q.get("id") == stream_id_hash:
                            await self._queue_quality_deletion(q)
                            break
                movie["telegram"] = [q for q in movie.get("telegram", []) if q.get("id") != stream_id_hash]
                if len(movie["telegram"]) == 0:
                    await db["movie"].delete_one({"_id": movie["_id"]})
                    await self.purge_media_from_catalogs(movie.get("tmdb_id"), "movie")
                else:
                    movie['updated_on'] = datetime.utcnow()
                    await db["movie"].replace_one({"_id": movie["_id"]}, movie)
                return True

            #----- Check TV Shows
            tv = await db["tv"].find_one({"seasons.episodes.telegram.id": stream_id_hash})
            if tv:
                for season in tv.get("seasons", []):
                    for episode in season.get("episodes", []):
                        for q in episode.get("telegram", []):
                            if q.get("id") == stream_id_hash:
                                if delete_file:
                                    await self._queue_quality_deletion(q)
                                episode["telegram"] = [t for t in episode.get("telegram", []) if t.get("id") != stream_id_hash]
                                if len(episode["telegram"]) == 0:
                                    season["episodes"] = [e for e in season.get("episodes", []) if e.get("episode_number") != episode.get("episode_number")]
                                    if len(season["episodes"]) == 0:
                                        tv["seasons"] = [s for s in tv.get("seasons", []) if s.get("season_number") != season.get("season_number")]
                                        if len(tv["seasons"]) == 0:
                                            await db["tv"].delete_one({"_id": tv["_id"]})
                                            await self.purge_media_from_catalogs(tv.get("tmdb_id"), "tv")
                                            return True
                                tv['updated_on'] = datetime.utcnow()
                                await db["tv"].replace_one({"_id": tv["_id"]}, tv)
                                return True
        return False

    async def delete_movie_quality(self, tmdb_id: int, db_index: int, id: str) -> bool:
        async def mutate(movie):
            qualities = movie.get("telegram") or []
            for q in qualities:
                if q.get("id") == id:
                    await self._queue_quality_deletion(q)
                    break
            original_len = len(qualities)
            movie["telegram"] = [q for q in qualities if q.get("id") != id]
            return len(movie["telegram"]) != original_len
        return await self._edit_media_doc("movie", tmdb_id, db_index, mutate)

    async def delete_tv_quality(self, tmdb_id: int, db_index: int, season_number: int, episode_number: int, id: str) -> bool:
        async def mutate(tv):
            for season in tv.get("seasons", []):
                if season.get("season_number") != season_number:
                    continue
                for episode in season.get("episodes", []):
                    if episode.get("episode_number") == episode_number and "telegram" in episode:
                        for q in episode["telegram"]:
                            if q.get("id") == id:
                                await self._queue_quality_deletion(q)
                                break
                        original_len = len(episode["telegram"])
                        episode["telegram"] = [q for q in episode["telegram"] if q.get("id") != id]
                        return original_len > len(episode["telegram"])
            return False
        return await self._edit_media_doc("tv", tmdb_id, db_index, mutate)

    async def delete_tv_episode(self, tmdb_id: int, db_index: int, season_number: int, episode_number: int) -> bool:
        async def mutate(tv):
            for season in tv.get("seasons", []):
                if season.get("season_number") != season_number:
                    continue
                episodes = season.get("episodes", [])
                for ep in episodes:
                    if ep.get("episode_number") == episode_number:
                        for quality in ep.get("telegram", []):
                            await self._queue_quality_deletion(quality)
                        break
                original_len = len(episodes)
                season["episodes"] = [ep for ep in episodes if ep.get("episode_number") != episode_number]
                return original_len > len(season["episodes"])
            return False
        return await self._edit_media_doc("tv", tmdb_id, db_index, mutate)

    async def delete_tv_season(self, tmdb_id: int, db_index: int, season_number: int) -> bool:
        async def mutate(tv):
            seasons = tv.get("seasons", [])
            for season in seasons:
                if season.get("season_number") == season_number:
                    for episode in season.get("episodes", []):
                        for quality in episode.get("telegram", []):
                            await self._queue_quality_deletion(quality)
                    break
            original_len = len(seasons)
            tv["seasons"] = [s for s in seasons if s.get("season_number") != season_number]
            return len(tv["seasons"]) != original_len
        return await self._edit_media_doc("tv", tmdb_id, db_index, mutate)


    #----- Get per-DB statistics (movies, tv shows, used size, etc.)
    async def get_database_stats(self):
        stats = []
        for key in self.dbs.keys():
            if key.startswith("storage_"):
                db = self.dbs[key]
                movie_count = await db["movie"].estimated_document_count()
                tv_count = await db["tv"].estimated_document_count()
                db_stats = await db.command("dbstats")
                stats.append({
                    "db_name": key,
                    "movie_count": movie_count,
                    "tv_count": tv_count,
                    "storageSize": db_stats.get("storageSize", 0),
                    "dataSize": db_stats.get("dataSize", 0)
                })
        return stats



    #-----
    #----- API Token Methods
    #-----

    async def add_api_token(self, name: str, daily_limit_gb: float = None, monthly_limit_gb: float = None, user_id: int = None, subscription_exempt: bool = False) -> dict:
        #----- If a user_id is provided, return existing token if already created
        if user_id:
            existing = await self.dbs["tracking"]["api_tokens"].find_one({"user_id": user_id})
            if existing:
                return convert_objectid_to_str(existing)

        alphabet = string.ascii_letters + string.digits
        token = ''.join(secrets.choice(alphabet) for _ in range(32))

        token_doc = {
            "name": name,
            "token": token,
            "user_id": user_id,
            "is_admin": self._is_owner(user_id),
            "subscription_exempt": bool(subscription_exempt),
            "expires_at": None,
            "created_at": datetime.utcnow(),
            "limits": {
                "daily_limit_gb": daily_limit_gb if daily_limit_gb else 0,
                "monthly_limit_gb": monthly_limit_gb if monthly_limit_gb else 0
            },
            "usage": {
                "total_bytes": 0,
                "daily": {"date": datetime.now(timezone.utc).strftime("%Y-%m-%d"), "bytes": 0},
                "monthly": {"month": datetime.now(timezone.utc).strftime("%Y-%m"), "bytes": 0}
            }
        }

        await self.dbs["tracking"]["api_tokens"].insert_one(token_doc)
        return convert_objectid_to_str(token_doc)

    #----- Return the user's token, creating one if none exists
    async def ensure_api_token_for_user(self, user_id: int, name: str = None) -> Optional[dict]:
        if not user_id:
            return None
        existing = await self.dbs["tracking"]["api_tokens"].find_one({"user_id": user_id})
        if existing:
            return convert_objectid_to_str(existing)
        return await self.add_api_token(name or f"User {user_id}", user_id=user_id)

    async def align_token_with_subscription(self, user_id: int) -> None:
        if not SettingsManager.current().subscription:
            return
        doc = await self.dbs["tracking"]["api_tokens"].find_one({"user_id": user_id})
        if doc:
            await self.dbs["tracking"]["api_tokens"].update_one(
                {"_id": doc["_id"]},
                {"$set": {"subscription_exempt": False, "expires_at": None}},
            )

    #----- Toggle a token's lifetime (subscription-exempt) flag
    async def set_token_lifetime(self, token: str, exempt: bool) -> bool:
        result = await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token}, {"$set": {"subscription_exempt": bool(exempt)}}
        )
        return result.modified_count > 0

    #----- Set/extend/reduce a token's own expiry (used when subscription mode is off).
    #----- 'set' with 0/None days clears the expiry (never expires).
    async def update_token_expiry(self, token: str, action: str = "set", days: int = 0) -> Optional[dict]:
        doc = await self.dbs["tracking"]["api_tokens"].find_one({"token": token})
        if not doc:
            return None
        now = datetime.utcnow()
        current = doc.get("expires_at")
        if action == "set":
            new_expiry = now + timedelta(days=days) if days and days > 0 else None
        elif action == "extend":
            base = current if (current and current > now) else now
            new_expiry = base + timedelta(days=days)
        elif action == "reduce":
            base = current if current else now
            new_expiry = base - timedelta(days=days)
            if new_expiry < now:
                new_expiry = now
        else:
            return None
        await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token},
            {"$set": {"expires_at": new_expiry, "subscription_exempt": new_expiry is None}},
        )
        return await self.get_api_token(token)

    #----- Mark every token that isn't linked to a user as lifetime
    async def grant_lifetime_to_unlinked(self) -> int:
        result = await self.dbs["tracking"]["api_tokens"].update_many(
            {"$or": [{"user_id": None}, {"user_id": {"$exists": False}}]},
            {"$set": {"subscription_exempt": True}},
        )
        return result.modified_count

    #----- Count tokens that would stop working if subscription mode is enabled
    async def count_uncovered_tokens(self) -> int:
        tokens = await self.get_all_api_tokens()
        now = datetime.utcnow()
        count = 0
        for t in tokens:
            if t.get("is_admin") or t.get("subscription_exempt"):
                continue
            exp = t.get("expires_at")
            if exp and exp > now:
                continue  #----- token has its own live expiry (honoured in sub mode)
            uid = t.get("user_id")
            if not uid:
                count += 1
                continue
            if not self.is_subscription_active(await self.get_user(int(uid))):
                count += 1
        return count

    async def get_api_token(self, token: str) -> Optional[dict]:
        doc = await self.dbs["tracking"]["api_tokens"].find_one({"token": token})
        return convert_objectid_to_str(doc) if doc else None

    #----- The (single) token linked to a given user_id, if any
    async def get_api_token_by_user(self, user_id: int) -> Optional[dict]:
        doc = await self.dbs["tracking"]["api_tokens"].find_one({"user_id": user_id})
        return convert_objectid_to_str(doc) if doc else None

    async def get_all_api_tokens(self) -> List[dict]:
        cursor = self.dbs["tracking"]["api_tokens"].find().sort("created_at", DESCENDING)
        tokens = await cursor.to_list(None)
        return [convert_objectid_to_str(token) for token in tokens]

    async def revoke_api_token(self, token: str) -> bool:
        result = await self.dbs["tracking"]["api_tokens"].delete_one({"token": token})
        if result.deleted_count > 0:
            try:
                await self.dbs["tracking"]["user_activity"].delete_one({"_id": token})
            except Exception:
                pass
        return result.deleted_count > 0

    async def set_token_config(self, token: str, config: dict) -> bool:
        result = await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token}, {"$set": {"config": config}}
        )
        return result.modified_count > 0 or result.matched_count > 0

    async def link_token_user(self, token: str, user_id: int, name: str = None) -> bool:
        #----- Link an existing token to a Telegram user_id; elevate to admin when
        #----- the linked user is the configured owner. Optionally overwrite the name.
        update = {"user_id": user_id, "is_admin": self._is_owner(user_id)}
        if name:
            update["name"] = name
        result = await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token}, {"$set": update}
        )
        return result.modified_count > 0

    @staticmethod
    def _is_owner(user_id) -> bool:
        try:
            return user_id is not None and int(user_id) == int(Telegram.OWNER_ID)
        except (TypeError, ValueError):
            return False

    async def update_token_usage(self, token: str, bytes_delta: int):
        today_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
        month_str = datetime.now(timezone.utc).strftime("%Y-%m")
        
        token_doc = await self.dbs["tracking"]["api_tokens"].find_one({"token": token})
        if not token_doc:
             return

        current_daily = token_doc.get("usage", {}).get("daily", {})
        if current_daily.get("date") != today_str:
            await self.dbs["tracking"]["api_tokens"].update_one(
                {"token": token},
                {"$set": {"usage.daily": {"date": today_str, "bytes": 0}}}
            )

        current_monthly = token_doc.get("usage", {}).get("monthly", {})
        if current_monthly.get("month") != month_str:
            await self.dbs["tracking"]["api_tokens"].update_one(
                {"token": token},
                {"$set": {"usage.monthly": {"month": month_str, "bytes": 0}}}
            )

        await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token},
            {
                "$inc": {
                    "usage.total_bytes": bytes_delta,
                    "usage.daily.bytes": bytes_delta,
                    "usage.monthly.bytes": bytes_delta
                }
            }
        )

    async def update_api_token_limits(self, token: str, daily_limit_gb: float, monthly_limit_gb: float) -> bool:
        result = await self.dbs["tracking"]["api_tokens"].update_one(
            {"token": token},
            {"$set": {
                "limits": {
                    "daily_limit_gb": daily_limit_gb if daily_limit_gb else 0,
                    "monthly_limit_gb": monthly_limit_gb if monthly_limit_gb else 0
                }
            }}
        )
        return result.modified_count > 0

    #-----
    #----- Admin / Link Checker Methods
    #-----
    async def flag_dead_link(self, media_type: str, tmdb_id: int, db_index: int, quality_id: str) -> bool:
        #----- Flag a specific telegram quality entry as is_dead=True
        db_key = f"storage_{db_index}"
        
        if media_type == "movie":
            #----- Direct update in the telegram array for movies
            result = await self.dbs[db_key]["movie"].update_one(
                {"tmdb_id": tmdb_id, "telegram.id": quality_id},
                {"$set": {"telegram.$.is_dead": True}}
            )
            return result.modified_count > 0
            
        elif media_type == "tv":
            #----- Nested update for TV (arrayFilters needed since we don't know the exact indices)
            #----- Find the TV show docs
            tv = await self.dbs[db_key]["tv"].find_one({"tmdb_id": tmdb_id})
            if not tv or "seasons" not in tv:
                return False
                
            found = False
            for s_idx, season in enumerate(tv["seasons"]):
                for e_idx, episode in enumerate(season.get("episodes", [])):
                    for q_idx, quality in enumerate(episode.get("telegram", [])):
                        if quality.get("id") == quality_id:
                            tv["seasons"][s_idx]["episodes"][e_idx]["telegram"][q_idx]["is_dead"] = True
                            found = True
                            break
                    if found: break
                if found: break
                
            if found:
                result = await self.dbs[db_key]["tv"].replace_one({"tmdb_id": tmdb_id}, tv)
                return result.modified_count > 0
                
        return False

    async def get_all_dead_links(self) -> List[dict]:
        #----- Flattened list of all dead links across storage DBs for the Admin UI
        dead_links = []
        
        for i in range(1, self.current_db_index + 1):
            db_key = f"storage_{i}"
            db = self.dbs[db_key]
            
            #----- Scan Movies ---
            #----- Match any movie where at least one telegram entry has is_dead=True
            movie_cursor = db["movie"].find({"telegram.is_dead": True})
            async for movie in movie_cursor:
                for quality in movie.get("telegram", []):
                    if quality.get("is_dead"):
                        dead_links.append({
                            "type": "movie",
                            "tmdb_id": movie.get("tmdb_id"),
                            "db_index": movie.get("db_index", i),
                            "title": movie.get("title"),
                            "year": movie.get("year"),
                            "poster": movie.get("poster"),
                            "quality_id": quality.get("id"),
                            "quality": quality.get("quality"),
                            "size": quality.get("size"),
                            "date_added": quality.get("date_added")
                        })
                        
            #----- Scan TV Shows ---
            #----- Match any TV where seasons.episodes.telegram.is_dead=True
            tv_cursor = db["tv"].find({"seasons.episodes.telegram.is_dead": True})
            async for tv in tv_cursor:
                title = tv.get("title")
                year = tv.get("year")
                poster = tv.get("poster")
                for season in tv.get("seasons", []):
                    s_num = season.get("season_number")
                    for ep in season.get("episodes", []):
                        e_num = ep.get("episode_number")
                        for quality in ep.get("telegram", []):
                            if quality.get("is_dead"):
                                dead_links.append({
                                    "type": "tv",
                                    "tmdb_id": tv.get("tmdb_id"),
                                    "db_index": tv.get("db_index", i),
                                    "title": f"{title} (S{s_num:02d}E{e_num:02d})",
                                    "year": year,
                                    "poster": poster,
                                    "season": s_num,
                                    "episode": e_num,
                                    "quality_id": quality.get("id"),
                                    "quality": quality.get("quality"),
                                    "size": quality.get("size"),
                                    "date_added": quality.get("date_added")
                                })
                                
        return dead_links

    #-----
    #----- Stream Analytics
    #-----

    async def log_stream_stats(self, stats: dict) -> None:
        #----- Persist a finished-stream record to the tracking DB for analytics
        try:
            record = {
                "stream_id":   stats.get("stream_id"),
                "msg_id":      stats.get("msg_id"),
                "chat_id":     stats.get("chat_id"),
                "dc_id":       stats.get("dc_id"),
                "title":       stats.get("meta", {}).get("title"),  #----- Added title
                "user_name":   stats.get("meta", {}).get("user_name"),
                "token":       stats.get("meta", {}).get("token"),
                "client_index": stats.get("client_index"),
                "total_bytes": stats.get("total_bytes", 0),
                "duration_sec": round(stats.get("duration", 0.0), 2),
                "avg_mbps":    round(stats.get("avg_mbps", 0.0), 3),
                "peak_mbps":   round(stats.get("peak_mbps", 0.0), 3),
                "status":      stats.get("status", "finished"),
                "parallelism": stats.get("parallelism"),
                "chunk_size":  stats.get("chunk_size"),
                "logged_at":   datetime.now(timezone.utc),
            }
            await self.dbs["tracking"]["stream_analytics"].insert_one(record)
            token = stats.get("meta", {}).get("token")
            if token:
                upd = {"last_active": datetime.now(timezone.utc)}
                if record.get("title"):
                    upd["last_title"] = record["title"]
                if record.get("user_name"):
                    upd["name"] = record["user_name"]
                await self.dbs["tracking"]["user_activity"].update_one(
                    {"_id": token}, {"$set": upd, "$inc": {"streams": 1}}, upsert=True
                )
        except Exception as e:
            LOGGER.warning(f"Stream analytics log failed: {e}")

    async def get_stream_analytics(self, limit: int = 200) -> dict:
        #----- Return summary stats + recent stream records from the tracking DB
        try:
            col = self.dbs["tracking"]["stream_analytics"]

            #----- Aggregate totals
            pipeline = [
                {"$group": {
                    "_id": None,
                    "total_streams":     {"$sum": 1},
                    "total_bytes":       {"$sum": "$total_bytes"},
                    "avg_speed":         {"$avg": "$avg_mbps"},
                    "peak_speed":        {"$max": "$peak_mbps"},
                    "avg_duration":      {"$avg": "$duration_sec"},
                }},
            ]
            agg = await col.aggregate(pipeline).to_list(1)
            summary = agg[0] if agg else {}
            summary.pop("_id", None)

            #----- Per-client breakdown
            per_client_pipeline = [
                {"$group": {
                    "_id":          "$client_index",
                    "streams":      {"$sum": 1},
                    "avg_mbps":     {"$avg": "$avg_mbps"},
                    "peak_mbps":    {"$max": "$peak_mbps"},
                    "total_bytes":  {"$sum": "$total_bytes"},
                }},
                {"$sort": {"_id": 1}},
            ]
            per_client = await col.aggregate(per_client_pipeline).to_list(None)
            for row in per_client:
                row["client_index"] = row.pop("_id")
                row["avg_mbps"]     = round(row.get("avg_mbps", 0), 3)
                row["peak_mbps"]    = round(row.get("peak_mbps", 0), 3)

            #----- Recent records (newest first)
            recent_cursor = col.find(
                {},
                {"_id": 0, "stream_id": 1, "client_index": 1, "dc_id": 1,
                 "total_bytes": 1, "duration_sec": 1, "avg_mbps": 1,
                 "peak_mbps": 1, "status": 1, "logged_at": 1, "title": 1}
            ).sort("logged_at", DESCENDING).limit(limit)
            recent = await recent_cursor.to_list(None)
            for r in recent:
                if "logged_at" in r and r["logged_at"] is not None:
                    ts = r["logged_at"]
                    if getattr(ts, "tzinfo", None) is None:
                        ts = ts.replace(tzinfo=timezone.utc)
                    r["logged_at"] = ts.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"

            #----- Most-streamed titles
            top_titles = await col.aggregate([
                {"$match": {"title": {"$nin": [None, ""]}}},
                {"$group": {"_id": "$title", "streams": {"$sum": 1}, "total_bytes": {"$sum": "$total_bytes"}}},
                {"$sort": {"streams": -1}},
                {"$limit": 8},
            ]).to_list(None)
            for r in top_titles:
                r["title"] = r.pop("_id")

            #----- Heaviest viewers (by data transferred)
            top_users = await col.aggregate([
                {"$match": {"user_name": {"$nin": [None, ""]}}},
                {"$group": {"_id": "$user_name", "streams": {"$sum": 1}, "total_bytes": {"$sum": "$total_bytes"}}},
                {"$sort": {"total_bytes": -1}},
                {"$limit": 8},
            ]).to_list(None)
            for r in top_users:
                r["user"] = r.pop("_id")

            #----- Streams & data per day (last 14 days, chronological)
            per_day = await col.aggregate([
                {"$group": {
                    "_id": {"$dateToString": {"format": "%Y-%m-%d", "date": "$logged_at"}},
                    "streams": {"$sum": 1},
                    "total_bytes": {"$sum": "$total_bytes"},
                }},
                {"$sort": {"_id": -1}},
                {"$limit": 14},
            ]).to_list(None)
            for r in per_day:
                r["date"] = r.pop("_id")
            per_day.reverse()

            distinct_users = await col.distinct("user_name")
            summary["active_users"] = len([u for u in distinct_users if u and u != "Unknown"])

            return {
                "summary":    summary,
                "per_client": per_client,
                "top_titles": top_titles,
                "top_users":  top_users,
                "per_day":    per_day,
                "recent":     recent,
            }
        except Exception as e:
            LOGGER.error(f"get_stream_analytics error: {e}")
            return {"summary": {}, "per_client": [], "top_titles": [], "top_users": [], "per_day": [], "recent": []}



    @staticmethod
    def _merge_telegram_lists(primary: List[dict], secondary: List[dict]) -> List[dict]:
        merged = list(primary or [])
        existing_ids = {q.get("id") for q in merged if q.get("id")}
        existing_groups = {q.get("group_key") for q in merged if q.get("group_key")}
        for q in (secondary or []):
            group_key = q.get("group_key")
            if group_key and group_key in existing_groups:
                continue
            if q.get("id") and q.get("id") in existing_ids:
                continue
            merged.append(q)
            if q.get("id"):
                existing_ids.add(q.get("id"))
            if group_key:
                existing_groups.add(group_key)
        return merged

    def _merge_season_lists(self, primary: List[dict], secondary: List[dict]) -> List[dict]:
        merged = list(primary or [])
        season_map = {s.get("season_number"): s for s in merged}
        for season in (secondary or []):
            season_number = season.get("season_number")
            target_season = season_map.get(season_number)
            if not target_season:
                merged.append(season)
                season_map[season_number] = season
                continue

            target_season.setdefault("episodes", [])
            episode_map = {e.get("episode_number"): e for e in target_season["episodes"]}
            for episode in season.get("episodes", []):
                episode_number = episode.get("episode_number")
                target_episode = episode_map.get(episode_number)
                if not target_episode:
                    target_season["episodes"].append(episode)
                    episode_map[episode_number] = episode
                    continue
                target_episode["telegram"] = self._merge_telegram_lists(
                    target_episode.get("telegram", []), episode.get("telegram", [])
                )
        return merged

    async def replace_media_metadata(
        self,
        media_type: str,
        tmdb_id: int,
        db_index: int,
        metadata: Dict[str, Any]
    ) -> Optional[dict]:
        db_key = f"storage_{db_index}"
        collection_name = self._collection_for(media_type)
        collection = self.dbs[db_key][collection_name]

        current_doc = await collection.find_one({"tmdb_id": int(tmdb_id)})
        if not current_doc:
            return None

        source_id = current_doc["_id"]

        def _pick(key: str):
            new_value = metadata.get(key)
            if new_value in (None, "", [], {}, 0, 0.0, "0"):
                return current_doc.get(key)
            return new_value

        new_tmdb_id = int(metadata.get("tmdb_id") or tmdb_id)
        new_imdb_id = _pick("imdb_id")

        if collection_name == "movie":
            current_doc.update({
                "tmdb_id": new_tmdb_id,
                "imdb_id": new_imdb_id,
                "title": _pick("title"),
                "release_year": _pick("release_year"),
                "rating": _pick("rating"),
                "description": _pick("description"),
                "poster": _pick("poster"),
                "backdrop": _pick("backdrop"),
                "logo": _pick("logo"),
                "genres": _pick("genres"),
                "cast": _pick("cast"),
                "runtime": _pick("runtime"),
                "media_type": "movie",
                "telegram": current_doc.get("telegram", []),
                "updated_on": datetime.utcnow(),
            })
        else:
            current_doc.update({
                "tmdb_id": new_tmdb_id,
                "imdb_id": new_imdb_id,
                "title": _pick("title"),
                "release_year": _pick("release_year"),
                "rating": _pick("rating"),
                "description": _pick("description"),
                "poster": _pick("poster"),
                "backdrop": _pick("backdrop"),
                "logo": _pick("logo"),
                "genres": _pick("genres"),
                "cast": _pick("cast"),
                "runtime": _pick("runtime"),
                "media_type": "tv",
                "seasons": current_doc.get("seasons", []),
                "updated_on": datetime.utcnow(),
            })

        identity_filters = []
        if new_imdb_id:
            identity_filters.append({"imdb_id": new_imdb_id})
        identity_filters.append({"tmdb_id": new_tmdb_id})

        existing_other = await collection.find_one({
            "$and": [{"$or": identity_filters}, {"_id": {"$ne": source_id}}]
        })

        if existing_other:
            if collection_name == "movie":
                existing_other["telegram"] = self._merge_telegram_lists(
                    existing_other.get("telegram", []), current_doc.get("telegram", [])
                )
            else:
                existing_other["seasons"] = self._merge_season_lists(
                    existing_other.get("seasons", []), current_doc.get("seasons", [])
                )

            for field in (
                "tmdb_id", "imdb_id", "title", "release_year", "rating",
                "description", "poster", "backdrop", "logo", "genres",
                "cast", "runtime", "media_type",
            ):
                if field in current_doc:
                    existing_other[field] = current_doc[field]
            existing_other["updated_on"] = datetime.utcnow()

            await collection.delete_one({"_id": source_id})
            await collection.replace_one({"_id": existing_other["_id"]}, existing_other)

            updated_doc = await collection.find_one({"_id": existing_other["_id"]})
            return convert_objectid_to_str(updated_doc) if updated_doc else None
        await collection.replace_one({"_id": source_id}, current_doc)

        updated_doc = await collection.find_one({"_id": source_id})
        return convert_objectid_to_str(updated_doc) if updated_doc else None