"""Structure-preserving extraction via trafilatura markdown output."""
from __future__ import annotations

import re
from typing import Optional

import trafilatura


def extract_structured(html: str, url: str = "",
                       max_chars: int = 200_000) -> str:
    """Render page body as Markdown, preserving code/headings/lists/tables."""
    if not html:
        return ""
    try:
        md = trafilatura.extract(
            html, output_format="markdown", include_tables=True,
            include_formatting=True, include_links=False,
            include_comments=False, favor_precision=False,
            url=url or None,
        ) or ""
    except Exception:
        return ""
    md = re.sub(r"\n{3,}", "\n\n", md).strip()
    return md[:max_chars]


def structure_score(text: str) -> int:
    """How much structure survived - used to pick the better extraction."""
    if not text:
        return 0
    lines = text.split("\n")
    return (text.count("```") * 3
            + sum(1 for l in lines if l.startswith("#")) * 2
            + sum(1 for l in lines if l.lstrip()[:2] in ("- ", "1."))
            + text.count("\n|") // 2)
