"""Fetch Avito search page и анализ URL slugs для room-filter."""
import re
import sys
import types
from pathlib import Path

_BACKEND = Path(__file__).resolve().parent.parent / "backend"
if str(_BACKEND) not in sys.path:
    sys.path.insert(0, str(_BACKEND))

for _m in ("weasyprint", "weasyprint.text", "weasyprint.text.ffi", "weasyprint.css", "weasyprint.css.targets"):
    sys.modules[_m] = types.ModuleType(_m)
sys.modules["weasyprint"].HTML = type("HTML", (), {"__init__": lambda self, *a, **kw: None})
sys.modules["weasyprint"].CSS = type("CSS", (), {"__init__": lambda self, *a, **kw: None})

import asyncio  # noqa: E402

from curl_cffi.requests import AsyncSession  # noqa: E402


async def main() -> None:
    url = "https://www.avito.ru/ekaterinburg/kvartiry/prodam-ASgBAgICAUSSA8YQ?s=104&p=1"
    async with AsyncSession(
        impersonate="chrome120",
        headers={
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language": "ru-RU,ru;q=0.9",
            "Sec-Fetch-Dest": "document",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
        },
    ) as s:
        r = await s.get(url)
        print(f"status={r.status_code} size={len(r.content)}")
        text = r.text
        with Path("/tmp/avito-search-sample.html").open("wb") as f:
            f.write(r.content)
        print("saved → /tmp/avito-search-sample.html")
        # Find filter links — pattern /ekaterinburg/kvartiry/prodam/<slug>-ASg...
        links = re.findall(r'href="(/ekaterinburg/kvartiry/prodam/[^"]+)"', text)
        unique = sorted(set(links))
        print(f"\nUnique filter links ({len(unique)} found):")
        for link in unique[:30]:
            # Decode shortened
            if "komnatn" in link.lower() or "studii" in link.lower() or "svobodnaya" in link.lower():
                print(f"  ROOMS: {link}")
            else:
                print(f"  other: {link[:120]}")
        # Also check direct headings
        room_headings = re.findall(r'>(Студии?|[0-9]-[Кк]омнатн\w+|Свободная планировка)<', text)
        print(f"\nRoom headings in HTML: {sorted(set(room_headings))}")


if __name__ == "__main__":
    asyncio.run(main())
