import os
import re
import json
from pathlib import Path

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

# Regex pattern for MCP terms
MCP_REGEX = re.compile(
    r'(\bmodel\s+context\s+protocol\b|'
    r'\bfastmcp\b|'
    r'\bmcp[-\s]сервер[а-я]*\b|'
    r'\b(?:создать|создание|создаем|написать|написание|разработка|разработать|свой|своих|кастомн[а-я]+)\s+(?:свой\s+)?mcp\b|'
    r'\bmcp\s+(?:сервер[а-я]*|server[s]?|protocol|gateway|client|sdk|tools?)\b|'
    r'\b(?:курсор|cursor|claude|anthropic|windsurf)\b[^\n.!?]*\bmcp\b|'
    r'\bmcp\b)',
    re.IGNORECASE
)

# Broad check for MCP boundary
BASIC_MCP_CHECK = re.compile(r'\bmcp\b|model\s+context\s+protocol|fastmcp', re.IGNORECASE)

# Keywords indicating creation / building / guide / development
DEV_KEYWORDS = [
    'сервер', 'server', 'protocol', 'созда', 'написа', 'разработ', 'библиотек',
    'sdk', 'api', 'fastmcp', 'github', 'репозитор', 'код', 'гайд', 'туториал',
    'настроить', 'интеграци', 'инструмент', 'подключ', 'guide', 'tutorial',
    'build', 'create', 'develop', 'client', 'custom'
]


def is_mcp_dev_post(text: str) -> tuple[bool, list[str]]:
    matches = set(BASIC_MCP_CHECK.findall(text))
    if not matches:
        return False, []

    # Check if text is related to Model Context Protocol (Anthropic's MCP)
    # Filter out false positives like Minecraft PE (MCPE), Master Control Program, MCP in medicine/finance
    lower = text.lower()

    # Discard obvious false positives
    if 'minecraft' in lower or 'майнкрафт' in lower:
        return False, []

    # Check for dev relevance
    found_dev_kw = [kw for kw in DEV_KEYWORDS if kw in lower]
    if not found_dev_kw:
        return False, []

    # Check for strong signals: Anthropic, Claude, Cursor, LLM, ИИ, AI, server, protocol, FastMCP
    ai_context = any(term in lower for term in [
        'ai', 'ии', 'claude', 'anthropic', 'llm', 'cursor', 'gpt', 'нейросет',
        'модел', 'model context protocol', 'fastmcp', 'agent', 'агент', 'prompt'
    ])

    if not ai_context and 'fastmcp' not in lower and 'model context protocol' not in lower:
        return False, []

    return True, found_dev_kw


def main():
    all_hits = []

    for file_path in sorted(CLEANED_DIR.glob("*.jsonl")):
        with open(file_path, "r", encoding="utf-8") as f:
            for line in f:
                if not line.strip():
                    continue
                post = json.loads(line)
                text = post.get("text", "")
                is_hit, dev_kws = is_mcp_dev_post(text)
                if is_hit:
                    post["dev_kws"] = dev_kws
                    all_hits.append(post)

    print(f"Total matching posts found: {len(all_hits)}")
    for i, hit in enumerate(all_hits, 1):
        print(f"[{i}] Channel: {hit['channel']} | Date: {hit['date']} | ID: {hit['message_id']}")
        first_line = hit['text'].splitlines()[0] if hit['text'] else ""
        print(f"    Title/Snippet: {first_line[:120]}")
        print(f"    Links: {hit['links'][:3]}")
        print("-" * 50)


if __name__ == "__main__":
    main()
