"""PornTrex.com — direct HTML scrape search results.

Search: `https://www.porntrex.com/search/<q>/` (single page, brak ?page=).
Scene URL: `https://www.porntrex.com/video/<id>/<slug>/`

Porntrex pagination niespójne między widokami — używamy `?from=<offset>` gdy page>1.
"""
from __future__ import annotations

import re

from app.connectors.direct_scrapers._search_base import BaseSearchScraper


class PornTrexScraper(BaseSearchScraper):
    sitetag = "porntrexcom"
    _search_url_template = "https://www.porntrex.com/search/{query}/"
    _scene_url_re = re.compile(
        r'href="(?P<url>https://www\.porntrex\.com/video/\d+/(?P<slug>[a-z0-9_\-]+))/?"',
        re.IGNORECASE,
    )

    def search(self, query, *, page=1, limit=None):
        # Porntrex używa offset w URL gdy page > 1: `/search/<q>/?from_videos=<page>`
        if page > 1:
            original = self._search_url_template
            self._search_url_template = f"{original.rstrip('/')}/?from_videos={page}"
            try:
                yield from super().search(query, page=page, limit=limit)
            finally:
                self._search_url_template = original
        else:
            yield from super().search(query, page=page, limit=limit)
