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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Channel Scanner Plugin β€” indexes existing channel content into the database.

Commands (owner-only, private chat):
    /scan          β€” Scan all AUTH_CHANNELs, skip already-indexed messages
    /scan <id>     β€” Scan a specific channel by numeric ID
    /rescan        β€” Wipe DB entries for AUTH_CHANNELs, then full re-index
    /scanstatus    β€” Show progress of any running scan
    /cancelscan    β€” Abort a running scan gracefully
"""

import asyncio
import time
from pyrogram import filters, Client, enums
from pyrogram.types import Message
from pyrogram.errors import FloodWait, ChannelPrivate, ChatAdminRequired

from Backend.helper.custom_filter import CustomFilters
from Backend.logger import LOGGER
from Backend import db
from Backend.helper.settings_manager import SettingsManager
from Backend.helper.pyro import clean_filename, get_readable_file_size, remove_urls
from Backend.helper.metadata import metadata
from Backend.helper.encrypt import encode_string


# ── Scan state (singleton β€” only one scan at a time) ────────────────────
class _ScanState:
    def __init__(self):
        self.reset()

    def reset(self):
        self.running = False
        self.cancelled = False
        self.channel_id = None
        self.channel_name = ""
        self.total_found = 0
        self.processed = 0
        self.indexed = 0
        self.skipped_dup = 0
        self.skipped_meta = 0
        self.skipped_nonvid = 0
        self.errors = 0
        self.started_at = 0.0
        self.status_msg: Message | None = None

    @property
    def elapsed(self) -> str:
        s = int(time.time() - self.started_at) if self.started_at else 0
        m, s = divmod(s, 60)
        h, m = divmod(m, 60)
        return f"{h}h {m}m {s}s" if h else f"{m}m {s}s"


scan_state = _ScanState()

PROGRESS_EVERY = 15  
RATE_LIMIT_DELAY = 0.3 


# ── Helpers ──────────────────────────────────────────────────────────────

async def _stream_id_exists(channel: int, msg_id: int) -> bool:
    """Check if this (channel, msg_id) combo is already in the DB."""
    try:
        stream_hash = await encode_string({"chat_id": channel, "msg_id": msg_id})
    except Exception:
        return False

    # Check across all storage DBs
    for i in range(1, db.current_db_index + 1):
        storage = db.dbs.get(f"storage_{i}")
        if storage is None:
            continue
        if await storage["movie"].find_one({"telegram.id": stream_hash}):
            return True
        if await storage["tv"].find_one({"seasons.episodes.telegram.id": stream_hash}):
            return True
    return False


async def _update_progress(force: bool = False):
    """Edit the status message with current scan progress."""
    s = scan_state
    if not s.status_msg:
        return
    if not force and s.processed % PROGRESS_EVERY != 0:
        return
    try:
        text = (
            f"<blockquote>πŸ“‘ <b>Scanning:</b> {s.channel_name}</blockquote>\n\n"
            f"⏱ Elapsed: <code>{s.elapsed}</code>\n"
            f"πŸ“¨ Processed: <code>{s.processed}</code>\n"
            f"βœ… Indexed: <code>{s.indexed}</code>\n"
            f"⏭ Skipped (duplicate): <code>{s.skipped_dup}</code>\n"
            f"⚠️ Skipped (metadata fail): <code>{s.skipped_meta}</code>\n"
            f"πŸ“Ž Skipped (non-video): <code>{s.skipped_nonvid}</code>\n"
            f"❌ Errors: <code>{s.errors}</code>"
        )
        await s.status_msg.edit_text(text, parse_mode=enums.ParseMode.HTML)
    except FloodWait as e:
        await asyncio.sleep(e.value)
    except Exception:
        pass  


async def _scan_channel(client: Client, chat_id: int):
    """Iterate all video messages in a channel and index them.
    
    Bots cannot use get_chat_history or search_messages (both user-only).
    The only reliable bot method is get_messages with explicit IDs.
    
    Strategy: walk forward from ID 1 in batches of 200. Keep going even
    through empty batches (deleted messages / gaps). Stop after 5
    consecutive all-empty batches, which tolerates gaps of up to ~1000 IDs.
    Hard cap at 500,000 to prevent infinite loops.
    """
    s = scan_state

    try:
        chat = await client.get_chat(chat_id)
        s.channel_name = getattr(chat, "title", str(chat_id))
    except (ChannelPrivate, ChatAdminRequired) as e:
        LOGGER.error(f"Cannot access channel {chat_id}: {e}")
        raise
    except Exception as e:
        s.channel_name = str(chat_id)
        LOGGER.warning(f"Could not resolve channel name for {chat_id}: {e}")

    s.channel_id = chat_id
    LOGGER.info(f"[Scanner] Starting scan of {s.channel_name} ({chat_id})")

    BATCH_SIZE = 200
    MAX_EMPTY_BATCHES = 5      
    MAX_ID_CAP = 500_000      
    empty_streak = 0
    current = 1

    while empty_streak < MAX_EMPTY_BATCHES and current < MAX_ID_CAP:
        if s.cancelled:
            LOGGER.info("[Scanner] Scan cancelled by user.")
            break

        batch_ids = list(range(current, min(current + BATCH_SIZE, MAX_ID_CAP)))

        try:
            messages = await client.get_messages(chat_id, batch_ids)
        except FloodWait as e:
            LOGGER.info(f"[Scanner] FloodWait {e.value}s, sleeping…")
            await asyncio.sleep(e.value)
            try:
                messages = await client.get_messages(chat_id, batch_ids)
            except Exception as ex:
                LOGGER.error(f"[Scanner] Retry failed at {current}: {ex}")
                s.errors += 1
                current += BATCH_SIZE
                empty_streak += 1
                continue
        except Exception as e:
            LOGGER.error(f"[Scanner] Batch fetch error at {current}: {e}")
            s.errors += 1
            current += BATCH_SIZE
            empty_streak += 1
            continue

        if not isinstance(messages, list):
            messages = [messages]

        batch_had_content = False

        for message in messages:
            if s.cancelled:
                break

            if message.empty:
                continue

            batch_had_content = True
            s.total_found += 1

            # Only process videos / video documents
            is_video = bool(message.video)
            is_video_doc = False
            if message.document and not is_video:
                mime = getattr(message.document, "mime_type", "") or ""
                is_video_doc = mime.startswith("video/")

            if not (is_video or is_video_doc):
                s.skipped_nonvid += 1
                s.processed += 1
                await _update_progress()
                continue

            file = message.video or message.document
            title = message.caption or file.file_name
            msg_id = message.id
            size = get_readable_file_size(file.file_size)
            channel = str(chat_id).replace("-100", "")
            channel_int = int(channel)

            # ── Duplicate check ──────────────────────────────────
            try:
                if await _stream_id_exists(channel_int, msg_id):
                    s.skipped_dup += 1
                    s.processed += 1
                    await _update_progress()
                    continue
            except Exception as e:
                LOGGER.warning(f"[Scanner] Dup-check error msg {msg_id}: {e}")

            # ── Metadata extraction (same pipeline as receiver) ──
            try:
                metadata_info = await metadata(clean_filename(title), channel_int, msg_id)
            except Exception as e:
                LOGGER.warning(f"[Scanner] Metadata exception for msg {msg_id}: {e}")
                metadata_info = None

            if metadata_info is None:
                s.skipped_meta += 1
                s.processed += 1
                await _update_progress()
                continue

            title_clean = remove_urls(title)
            if not title_clean.endswith(('.mkv', '.mp4')):
                title_clean += '.mkv'

            # ── Insert into DB ───────────────────────────────────
            try:
                updated_id = await db.insert_media(
                    metadata_info,
                    channel=channel_int,
                    msg_id=msg_id,
                    size=size,
                    name=title_clean,
                )
                if updated_id:
                    s.indexed += 1
                    LOGGER.info(f"[Scanner] Indexed msg {msg_id}: {title_clean}")
                else:
                    s.skipped_meta += 1
            except Exception as e:
                LOGGER.error(f"[Scanner] DB insert error msg {msg_id}: {e}")
                s.errors += 1

            s.processed += 1
            await _update_progress()

        # Track empty batches to know when to stop
        if batch_had_content:
            empty_streak = 0
        else:
            empty_streak += 1

        current += BATCH_SIZE
        # Small delay between batches
        await asyncio.sleep(RATE_LIMIT_DELAY)

    LOGGER.info(f"[Scanner] Finished {s.channel_name}: scanned up to ID {current}, "
                f"{s.total_found} messages found, {s.indexed} indexed")


# ── Bot Commands ─────────────────────────────────────────────────────────

@Client.on_message(filters.command('scan') & filters.private & CustomFilters.owner, group=10)
async def scan_command(client: Client, message: Message):
    """Scan AUTH_CHANNELs for existing content. Skips already-indexed messages."""
    if scan_state.running:
        await message.reply_text(
            "⚠️ A scan is already running. Use /scanstatus to check progress, "
            "or /cancelscan to abort it.",
            quote=True,
        )
        return

    # Optional: /scan -1001234567890  to scan a single channel
    args = message.text.split(maxsplit=1)
    if len(args) > 1:
        target_channels = [args[1].strip()]
    else:
        target_channels = list(SettingsManager.current().auth_channels)

    if not target_channels:
        await message.reply_text("❌ No AUTH_CHANNELs configured.", quote=True)
        return

    scan_state.reset()
    scan_state.running = True
    scan_state.started_at = time.time()
    scan_state.status_msg = await message.reply_text(
        "πŸ“‘ <b>Channel scan starting…</b>",
        quote=True,
        parse_mode=enums.ParseMode.HTML,
    )

    try:
        for ch_id_str in target_channels:
            if scan_state.cancelled:
                break
            try:
                ch_id = int(ch_id_str)
            except ValueError:
                LOGGER.warning(f"[Scanner] Invalid channel ID: {ch_id_str}")
                continue

            await _scan_channel(client, ch_id)

        # Final summary
        s = scan_state
        status = "πŸ›‘ Cancelled" if s.cancelled else "βœ… Complete"
        summary = (
            f"<blockquote>πŸ“‘ <b>Scan {status}</b></blockquote>\n\n"
            f"⏱ Duration: <code>{s.elapsed}</code>\n"
            f"πŸ“¨ Total messages seen: <code>{s.total_found}</code>\n"
            f"βœ… Newly indexed: <code>{s.indexed}</code>\n"
            f"⏭ Skipped (already in DB): <code>{s.skipped_dup}</code>\n"
            f"⚠️ Skipped (metadata fail): <code>{s.skipped_meta}</code>\n"
            f"πŸ“Ž Skipped (non-video): <code>{s.skipped_nonvid}</code>\n"
            f"❌ Errors: <code>{s.errors}</code>"
        )
        try:
            await s.status_msg.edit_text(summary, parse_mode=enums.ParseMode.HTML)
        except Exception:
            await message.reply_text(summary, parse_mode=enums.ParseMode.HTML)

        # Send a separate notification so the user gets a Telegram ping
        # (editing a message doesn't trigger a notification)
        if s.processed > 20:
            notify = (
                f"{'πŸ›‘ Scan cancelled' if s.cancelled else 'βœ… Scan complete'} β€” "
                f"{s.indexed} indexed, {s.skipped_dup} skipped, "
                f"{s.errors} errors ({s.elapsed})"
            )
            await message.reply_text(notify)

    except (ChannelPrivate, ChatAdminRequired) as e:
        await message.reply_text(
            f"❌ <b>Access denied</b> to channel.\n\n"
            f"Make sure the bot is an admin in the channel.\n"
            f"<code>{e}</code>",
            parse_mode=enums.ParseMode.HTML,
        )
    except Exception as e:
        LOGGER.error(f"[Scanner] Unexpected error: {e}")
        await message.reply_text(f"❌ Scan failed: <code>{e}</code>",
                                  parse_mode=enums.ParseMode.HTML)
    finally:
        scan_state.running = False


@Client.on_message(filters.command('rescan') & filters.private & CustomFilters.owner, group=10)
async def rescan_command(client: Client, message: Message):
    """Wipe all DB entries that belong to AUTH_CHANNELs, then do a full scan.

    This is the nuclear option β€” it re-indexes everything from scratch.
    """
    if scan_state.running:
        await message.reply_text(
            "⚠️ A scan is already running. Cancel it first with /cancelscan.",
            quote=True,
        )
        return

    channels = list(SettingsManager.current().auth_channels)
    if not channels:
        await message.reply_text("❌ No AUTH_CHANNELs configured.", quote=True)
        return

    confirm_msg = await message.reply_text(
        "⚠️ <b>RESCAN</b> will <u>delete all existing DB entries</u> "
        "for your AUTH_CHANNELs and re-index from scratch.\n\n"
        "Send <code>/rescan confirm</code> to proceed.",
        quote=True,
        parse_mode=enums.ParseMode.HTML,
    )

    args = message.text.split()
    if len(args) < 2 or args[1].lower() != "confirm":
        return

    # Purge existing entries for these channels
    purge_msg = await message.reply_text(
        "πŸ—‘ Purging existing entries…", parse_mode=enums.ParseMode.HTML
    )
    purged = 0
    for ch_id_str in channels:
        channel_int = int(ch_id_str.replace("-100", ""))
        purged += await _purge_channel_entries(channel_int)

    await purge_msg.edit_text(
        f"πŸ—‘ Purged <code>{purged}</code> stream entries. Starting full scan…",
        parse_mode=enums.ParseMode.HTML,
    )

    # Now run a normal scan
    message.text = "/scan"  # trick to reuse scan_command logic
    await scan_command(client, message)


async def _purge_channel_entries(channel_int: int) -> int:
    """Delete all movie/tv stream entries whose encoded chat_id matches this channel."""
    from Backend.helper.encrypt import decode_string

    purged = 0
    for i in range(1, db.current_db_index + 1):
        storage = db.dbs.get(f"storage_{i}")
        if storage is None:
            continue

        # Purge movies
        async for movie in storage["movie"].find({}):
            remaining = []
            changed = False
            for q in movie.get("telegram", []):
                try:
                    decoded = await decode_string(q["id"])
                    if int(decoded["chat_id"]) == channel_int:
                        purged += 1
                        changed = True
                        continue
                except Exception:
                    pass
                remaining.append(q)
            if changed:
                if remaining:
                    movie["telegram"] = remaining
                    await storage["movie"].replace_one({"_id": movie["_id"]}, movie)
                else:
                    await storage["movie"].delete_one({"_id": movie["_id"]})

        # Purge TV
        async for tv in storage["tv"].find({}):
            tv_changed = False
            for season in tv.get("seasons", []):
                for episode in season.get("episodes", []):
                    remaining = []
                    for q in episode.get("telegram", []):
                        try:
                            decoded = await decode_string(q["id"])
                            if int(decoded["chat_id"]) == channel_int:
                                purged += 1
                                tv_changed = True
                                continue
                        except Exception:
                            pass
                        remaining.append(q)
                    episode["telegram"] = remaining
                # Remove empty episodes
                season["episodes"] = [
                    ep for ep in season["episodes"] if ep.get("telegram")
                ]
            # Remove empty seasons
            tv["seasons"] = [s for s in tv["seasons"] if s.get("episodes")]
            if tv_changed:
                if tv["seasons"]:
                    await storage["tv"].replace_one({"_id": tv["_id"]}, tv)
                else:
                    await storage["tv"].delete_one({"_id": tv["_id"]})

    return purged


@Client.on_message(filters.command('scanstatus') & filters.private & CustomFilters.owner, group=10)
async def scan_status_command(client: Client, message: Message):
    """Show current scan progress."""
    s = scan_state
    if not s.running:
        await message.reply_text("ℹ️ No scan is currently running.", quote=True)
        return

    text = (
        f"<blockquote>πŸ“‘ <b>Scan in progress:</b> {s.channel_name}</blockquote>\n\n"
        f"⏱ Elapsed: <code>{s.elapsed}</code>\n"
        f"πŸ“¨ Processed: <code>{s.processed}</code>\n"
        f"βœ… Indexed: <code>{s.indexed}</code>\n"
        f"⏭ Skipped (duplicate): <code>{s.skipped_dup}</code>\n"
        f"⚠️ Skipped (metadata fail): <code>{s.skipped_meta}</code>\n"
        f"πŸ“Ž Skipped (non-video): <code>{s.skipped_nonvid}</code>\n"
        f"❌ Errors: <code>{s.errors}</code>"
    )
    await message.reply_text(text, quote=True, parse_mode=enums.ParseMode.HTML)


@Client.on_message(filters.command('cancelscan') & filters.private & CustomFilters.owner, group=10)
async def cancel_scan_command(client: Client, message: Message):
    """Abort a running scan gracefully."""
    if not scan_state.running:
        await message.reply_text("ℹ️ No scan is currently running.", quote=True)
        return

    scan_state.cancelled = True
    await message.reply_text(
        "πŸ›‘ Scan cancellation requested. Will stop after current message.",
        quote=True,
    )