"""URL-based region leak check для Avito listings."""
from __future__ import annotations

import os
import re
from collections import Counter

import psycopg

dsn = os.environ["DATABASE_URL"].replace("postgresql+psycopg://", "postgresql://")
conn = psycopg.connect(dsn)
cur = conn.cursor()

cur.execute("SELECT source_url FROM listings WHERE source='avito'")
slug_re = re.compile(r"avito\.ru/([a-z_]+)/")

cities = Counter()
no_match = 0
for (url,) in cur.fetchall():
    if not url:
        no_match += 1
        continue
    m = slug_re.search(url)
    if m:
        cities[m.group(1)] += 1
    else:
        no_match += 1

print("city slug distribution in source_url:")
for city, n in cities.most_common():
    flag = "  ← EKB" if city == "ekaterinburg" else ("  ← OTHER CITY" if city not in ("ekaterinburg",) else "")
    print(f"  {city:<30} {n}{flag}")
print(f"  (no slug parse): {no_match}")

# Дальше — sample non-EKB
cur.execute(
    """
    SELECT source_id, source_url, address, price_rub
    FROM listings
    WHERE source='avito'
      AND source_url IS NOT NULL
      AND source_url !~ 'avito\\.ru/ekaterinburg/'
    ORDER BY scraped_at DESC NULLS LAST
    LIMIT 10
    """
)
print("\nsample non-EKB listings (by URL):")
for sid, url, addr, price in cur.fetchall():
    print(f"  {sid} {price:,}₽")
    print(f"    URL: {url}")
    print(f"    addr: {(addr or '')[:80]!r}")
