"""
Trafilatura-based content extractor.

Uses statistical + rule-based heuristics for boilerplate removal with
built-in language detection and optional markdown output. Trafilatura
internally falls back to readability and justext when its primary
heuristic fails, making it a strong standalone or first-pass extractor.
"""

from typing import Optional

from loguru import logger

from .base import BaseExtractor


class TrafilaturaExtractor(BaseExtractor):
    """Extract content using trafilatura."""

    def __init__(
        self,
        output_format: str = "markdown",
        include_tables: bool = True,
        include_links: bool = False,
        include_comments: bool = False,
        include_formatting: bool = True,
    ):
        self.output_format = output_format
        self.include_tables = include_tables
        self.include_links = include_links
        self.include_comments = include_comments
        self.include_formatting = include_formatting

    def extract(self, html: str) -> Optional[str]:
        if not html or not html.strip():
            return None

        try:
            import trafilatura
        except ImportError:
            logger.warning("trafilatura not installed — skipping extraction")
            return None

        try:
            result = trafilatura.extract(
                html,
                output_format=self.output_format,
                include_tables=self.include_tables,
                include_links=self.include_links,
                include_comments=self.include_comments,
                include_formatting=self.include_formatting,
            )
            return result if result and result.strip() else None
        except Exception:
            logger.exception("trafilatura extraction failed")
            return None
