"""Deduplicate repeated titles and headings inside prompt context blocks."""

from __future__ import annotations

import re
from typing import Any

from iuve_connect_agents.shared.answer_quality.semantic_dedup import similarity

_TITLE_LINE_RE = re.compile(
    r"^(?:#{1,6}\s+|\*\*)(.+?)(?:\*\*)?\s*$",
    re.MULTILINE,
)


def _normalize_title(value: str) -> str:
    text = str(value or "").strip().lower()
    text = re.sub(r"[^\w\sа-яё0-9]", " ", text, flags=re.IGNORECASE)
    return re.sub(r"\s+", " ", text).strip()


def extract_titles(text: str) -> list[str]:
    titles: list[str] = []
    for line in str(text or "").splitlines():
        match = _TITLE_LINE_RE.match(line.strip())
        if match:
            title = str(match.group(1) or "").strip()
            if title:
                titles.append(title)
    return titles


def deduplicate_titles(titles: list[str], *, threshold: float = 0.9) -> tuple[list[str], int]:
    kept: list[str] = []
    removed = 0
    for title in titles:
        text = str(title or "").strip()
        if not text:
            continue
        duplicate = False
        for prior in kept:
            if _normalize_title(text) == _normalize_title(prior) or similarity(text, prior) >= threshold:
                duplicate = True
                break
        if duplicate:
            removed += 1
            continue
        kept.append(text)
    return kept, removed


def audit_prompt_context(
    chunks: list[str],
    *,
    titles: list[str] | None = None,
    threshold: float = 0.9,
) -> dict[str, Any]:
    """Return deduplicated prompt chunks/titles and duplicate count."""
    deduped_chunks = []
    removed_chunks = 0
    prior_chunks: list[str] = []
    for chunk in chunks:
        text = str(chunk or "").strip()
        if not text:
            continue
        duplicate = any(similarity(text, prior) >= threshold for prior in prior_chunks)
        if duplicate:
            removed_chunks += 1
            continue
        prior_chunks.append(text)
        deduped_chunks.append(text)

    title_list = list(titles or [])
    for chunk in deduped_chunks:
        title_list.extend(extract_titles(chunk))
    deduped_titles, removed_titles = deduplicate_titles(title_list, threshold=threshold)

    return {
        "chunks": deduped_chunks,
        "titles": deduped_titles,
        "removed_chunks": removed_chunks,
        "removed_titles": removed_titles,
        "prompt_duplicates_found": removed_chunks + removed_titles,
    }
