"""Der Spiegel: Manuskripte auf spiegel.de.

SPIEGEL-Artikel haben typischerweise einen <article data-article-el>-Container.
SPIEGEL+-Artikel liefern ohne Login nur Teaser — der Length-Check in _common
sorgt dafuer, dass solche Teaser verworfen werden und die Kaskade weiterlaeuft.
"""
from __future__ import annotations

import logging
from typing import Optional

from . import TranscriptResult
from ._common import (
    episode_url,
    extract_longest_article_block,
    extract_text_by_container,
    fetch_html,
    matches_domain,
)

logger = logging.getLogger("osint.podcast.extractors.spiegel")

_DOMAINS = ("spiegel.de", "manager-magazin.de")

_CONTAINER_PATTERNS = [
    r'<main[^>]*data-area="article"[^>]*>',
    r'<article[^>]*data-article-el[^>]*>',
    r'<article\b[^>]*>',
    r'<main\b[^>]*>',
]


def can_handle(feed_entry: dict, feed_url: str) -> bool:
    url = episode_url(feed_entry) or feed_url
    return matches_domain(url, _DOMAINS) or matches_domain(feed_url, _DOMAINS)


async def fetch(feed_entry: dict, feed_url: str) -> Optional[TranscriptResult]:
    url = episode_url(feed_entry)
    if not url:
        return None
    html = await fetch_html(url)
    if not html:
        return None

    text = extract_text_by_container(html, _CONTAINER_PATTERNS)
    if not text:
        text = extract_longest_article_block(html)
    if not text:
        return None
    return TranscriptResult(text=text, source="website_scrape")
