"""Reject fake source lines; keep only verifiable source markers."""

from __future__ import annotations

import re

_SOURCE_LINE_RE = re.compile(
    r"(?im)^\s*(?:источник|source|sources|источники)\s*:\s*(.+?)\s*$"
)
_URL_RE = re.compile(r"https?://[^\s)\"']+", re.IGNORECASE)
_FILE_RE = re.compile(
    r"(?i)\b(?:file|document|doc|memory|record|system\s+knowledge|сканирование\s+устройства)\b"
)
_VALID_KNOWN_MARKERS = (
    "http://",
    "https://",
    "file:",
    "document:",
    "memory:",
    "system knowledge",
    "сканирование устройства",
    "device scan",
)
_FAKE_SOURCE_HINTS = (
    "нейронн",
    "neural network",
    "deep learning",
    "модель",
    "model weights",
    "training data",
    "общие знания",
    "general knowledge",
    "внутренн",
    "internal model",
)


def _line_has_real_source_marker(line: str) -> bool:
    text = str(line or "").strip()
    if not text:
        return False
    lower = text.lower()
    if _URL_RE.search(text):
        return True
    if _FILE_RE.search(text):
        return True
    if any(marker in lower for marker in _VALID_KNOWN_MARKERS):
        return True
    if re.search(r"\[[^\]]+\]\(https?://", text, flags=re.IGNORECASE):
        return True
    return False


def is_valid_source_line(line: str) -> bool:
    text = str(line or "").strip()
    if not text:
        return False
    if not _line_has_real_source_marker(text):
        return False
    lower = text.lower()
    if any(hint in lower for hint in _FAKE_SOURCE_HINTS) and not _URL_RE.search(text):
        return False
    return True


def strip_fake_sources(text: str) -> str:
    value = str(text or "")
    if not value.strip():
        return ""

    lines: list[str] = []
    for line in value.splitlines():
        match = _SOURCE_LINE_RE.match(line)
        if match and not is_valid_source_line(line):
            continue
        lines.append(line)

    cleaned = "\n".join(lines)
    cleaned = re.sub(r"\n{3,}", "\n\n", cleaned).strip()
    return cleaned
