import os
import re
import json
import glob
from pathlib import Path

# Paths to process
TARGET_DIRS = [
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\ChatExport_2026-11-22 (1)",
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\ChatExport_2026-11-22",
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\Костыль",
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\Эксплойт",
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\NN",
    r"C:\Users\Daniil\Downloads\AyuGram Desktop\Робот Сочинит Симфонию",
]

OUTPUT_DIR = Path(r"c:\Users\Daniil\VS_Code\Trash\cleaned_backups")

# Known channel usernames for direct link building if available
KNOWN_USERNAMES = {
    "AI для своих": "ai4svoi",
    "Абьюзер ChatGPT | 2.0": "",
    "Костыль": "kostil",
    "Эксплойт": "exploitex",
    "NN": "naebnet",
    "Робот сочинит симфонию?": "",
}


def sanitize_filename(name: str) -> str:
    return re.sub(r'[\\/*?:"<>|]', "_", name).strip()


def clean_text_field(text_val) -> tuple[str, list[str]]:
    """
    Extracts clean text and embedded links from Telegram text entity structure.
    telegram Desktop JSON can provide text as:
    - string
    - list of strings and entity dicts:
        {"type": "text_link", "text": "...", "href": "..."}
        {"type": "bold", "text": "..."}
        {"type": "link", "text": "https://..."}
    """
    links = []
    if not text_val:
        return "", links

    if isinstance(text_val, str):
        raw_text = text_val
    elif isinstance(text_val, list):
        parts = []
        for item in text_val:
            if isinstance(item, str):
                parts.append(item)
            elif isinstance(item, dict):
                entity_type = item.get("type", "")
                entity_text = item.get("text", "")
                href = item.get("href", "")

                if entity_type == "text_link" and href:
                    links.append(href)
                    if entity_text and entity_text != href:
                        parts.append(f"[{entity_text}]({href})")
                    else:
                        parts.append(href)
                elif entity_type == "link":
                    url = entity_text or href
                    if url:
                        links.append(url)
                        parts.append(url)
                else:
                    parts.append(entity_text)
        raw_text = "".join(parts)
    else:
        raw_text = str(text_val)

    # Also capture any plain URLs in text that weren't formatted as entities
    found_urls = re.findall(r'https?://[^\s()<>\[\]"\']+', raw_text)
    for u in found_urls:
        if u not in links:
            links.append(u)

    # Clean technical garbage links (like tg://emoji?id=...)
    raw_text = re.sub(r'\[(.*?)\]\(tg://emoji\?[^\)]+\)', r'\1', raw_text)
    raw_text = re.sub(r'tg://emoji\?[^\s)\]]+', '', raw_text)

    # Normalize whitespace: strip trailing spaces on lines, collapse 3+ newlines to 2
    lines = [line.rstrip() for line in raw_text.splitlines()]
    cleaned = "\n".join(lines)
    cleaned = re.sub(r'\n{3,}', '\n\n', cleaned).strip()

    return cleaned, links


def process_json_backup(file_path: Path):
    print(f"\nProcessing JSON backup: {file_path}")
    with open(file_path, "r", encoding="utf-8") as f:
        data = json.load(f)

    channel_name = data.get("name", "Unknown Channel")
    channel_id = data.get("id")
    channel_type = data.get("type", "")
    username = KNOWN_USERNAMES.get(channel_name, "")

    messages = data.get("messages", [])
    cleaned_messages = []

    total_msgs = len(messages)
    skipped_service = 0
    skipped_empty = 0

    for msg in messages:
        # 1. Skip service messages
        msg_type = msg.get("type")
        if msg_type != "message":
            skipped_service += 1
            continue

        # 2. Extract and clean text & links
        raw_text = msg.get("text", "")
        cleaned_text, links = clean_text_field(raw_text)

        # 3. Skip empty posts (no text/caption)
        if not cleaned_text:
            skipped_empty += 1
            continue

        msg_id = msg.get("id")
        date_str = msg.get("date", "")

        # Form direct post link if possible
        post_link = ""
        if username:
            post_link = f"https://t.me/{username}/{msg_id}"
        elif channel_id:
            # For public or private channel without known handle
            post_link = f"https://t.me/c/{channel_id}/{msg_id}"

        entry = {
            "channel": channel_name,
            "channel_id": channel_id,
            "message_id": msg_id,
            "date": date_str,
            "post_link": post_link,
            "text": cleaned_text,
            "links": links
        }
        cleaned_messages.append(entry)

    print(f"  Channel: '{channel_name}'")
    print(f"  Total raw items: {total_msgs}")
    print(f"  Service messages removed: {skipped_service}")
    print(f"  Empty media/posts removed: {skipped_empty}")
    print(f"  Clean posts retained: {len(cleaned_messages)}")

    return channel_name, cleaned_messages


def save_cleaned_channel(channel_name: str, messages: list[dict]):
    safe_name = sanitize_filename(channel_name)
    jsonl_path = OUTPUT_DIR / f"{safe_name}.jsonl"

    with open(jsonl_path, "w", encoding="utf-8") as f:
        for m in messages:
            f.write(json.dumps(m, ensure_ascii=False) + "\n")

    print(f"  Saved JSONL: {jsonl_path} ({len(messages)} posts)")


def main():
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    summary = []

    for target_dir in TARGET_DIRS:
        tpath = Path(target_dir)
        if not tpath.exists():
            print(f"Directory not found: {target_dir}")
            continue

        json_file = tpath / "result.json"
        if json_file.exists():
            channel_name, cleaned_msgs = process_json_backup(json_file)
            save_cleaned_channel(channel_name, cleaned_msgs)
            summary.append((channel_name, len(cleaned_msgs)))
        else:
            print(f"No result.json found in {target_dir}")

    print("\n" + "=" * 50)
    print("CLEANING COMPLETED SUMMARY:")
    for ch_name, count in summary:
        print(f" - {ch_name}: {count} cleaned posts")


if __name__ == "__main__":
    main()
