"""SxyLandScraper — direct HTML scrape sxyland.com search.

Search: `https://sxyland.com/?s=<query>` zwraca wyniki w formacie
`https://sxyland.com/<numeric_id>/<slug>/`. Filtrujemy linki bez numeric ID
(legal pages typu /18-u-s-c-2257/).
"""
from __future__ import annotations

import logging
import re
import urllib.parse
from collections.abc import Iterator

from app.connectors.base import RawPerformer, RawPlaybackSource, RawScene
from app.connectors.direct_scrapers.base import BaseDirectTubeScraper
from app.extractors import browser_get

log = logging.getLogger(__name__)


_SCENE_URL_RE = re.compile(r'href="(https://sxyland\.com/(\d+)/([^"/]+))/?"')


class SxyLandScraper(BaseDirectTubeScraper):
    sitetag = "sxylandcom"

    def search(
        self,
        query: str,
        *,
        page: int = 1,
        limit: int | None = None,
    ) -> Iterator[RawScene]:
        q = urllib.parse.quote_plus(query.strip())
        url = f"https://sxyland.com/page/{page}/?s={q}"
        try:
            r = browser_get(url, timeout=30)
        except Exception as e:
            log.warning("sxyland search fetch failed: %s", e)
            return
        if r.status_code != 200:
            return

        query_tokens = {tok for tok in query.lower().split() if len(tok) >= 3}

        seen: set[str] = set()
        yielded = 0
        for m in _SCENE_URL_RE.finditer(r.text):
            scene_url = m.group(1) + "/"
            slug = m.group(3)
            if scene_url in seen:
                continue
            seen.add(scene_url)

            slug_lower = slug.lower()
            if query_tokens and not any(tok in slug_lower for tok in query_tokens):
                continue

            title = slug.replace("-", " ").strip()

            yield RawScene(
                external_id=f"sxylandcom:{scene_url}",
                title=title,
                url=scene_url,
                playback_sources=[
                    RawPlaybackSource(origin="tube:sxylandcom", page_url=scene_url)
                ],
                performers=[RawPerformer(name=query.strip())],
                raw={
                    "source": "direct_scraper:sxyland",
                    "query": query,
                    "page": page,
                    "url": scene_url,
                },
            )
            yielded += 1
            if limit is not None and yielded >= limit:
                return
