"""Reddit "karštos" temos.

Naudojam viešus .json endpoints (be auth, bet su tinkamu User-Agent).
Kiekvienai šaliai – po keletą subredditų. Galima papildyti rankiniu būdu.

Pastaba: Reddit API politika kinta, todėl jei .json grąžins 403/429,
HttpClient retry sustos; logas parodys.
"""

from __future__ import annotations

from typing import Dict, List

from .base import BaseCollector, TrendItem


# Kiekvienai šaliai – jos kalbos / vietos subredditai.
SUBREDDITS_BY_COUNTRY: Dict[str, List[str]] = {
    "LT": ["lithuania", "Lietuva"],
    "LV": ["latvia"],
    "EE": ["Eesti"],
    "US": ["news", "politics", "sports", "movies", "all"],
    "GB": ["unitedkingdom", "ukpolitics"],
    "JP": ["japan", "newsokur"],
    "DE": ["de", "germany"],
    "FR": ["france"],
    "IT": ["italy"],
    "ES": ["spain", "es"],
    "PL": ["polska"],
    "FI": ["Suomi"],
    "SE": ["sweden"],
    "NO": ["norge"],
    "DK": ["Denmark"],
    "NL": ["thenetherlands"],
    "BR": ["brasil"],
    "MX": ["mexico"],
    "AR": ["argentina"],
    "CA": ["canada"],
    "AU": ["australia"],
    "IN": ["india"],
    "RU": ["russia"],
    "UA": ["ukraina"],
    "TR": ["turkey"],
    "KR": ["korea"],
}


class RedditCollector(BaseCollector):
    name = "reddit"
    source_type = "reddit"

    def __init__(self, *, db, http, cfg):
        super().__init__(db=db, http=http, cfg=cfg)
        self.limit = cfg.get("collectors", {}).get("reddit", {}).get("limit_per_subreddit", 25)

    def _fetch_hot(self, subreddit: str) -> List[dict]:
        url = f"https://www.reddit.com/r/{subreddit}/hot.json?limit={self.limit}"
        data = self.http.get_json(url, respect_robots=False,
                                  headers={"User-Agent": self.http.user_agent})
        if not data:
            return []
        return [c.get("data", {}) for c in data.get("data", {}).get("children", [])]

    def collect_for_country(self, country) -> List[TrendItem]:
        subs = SUBREDDITS_BY_COUNTRY.get(country.iso_code, [])
        if not subs:
            return []

        items: List[TrendItem] = []
        for sub in subs:
            posts = self._fetch_hot(sub)
            for rank, p in enumerate(posts, start=1):
                title = (p.get("title") or "").strip()
                if not title:
                    continue
                score = float(p.get("score") or 0)
                items.append(TrendItem(
                    keyword=title,
                    trend_type="hourly",
                    rank=rank,
                    score=min(1.0, score / 10000.0),  # gruba normalizacija
                    volume=int(p.get("num_comments") or 0),
                    description=f"r/{sub}",
                    language=country.primary_language,
                    url="https://reddit.com" + (p.get("permalink") or ""),
                ))
            self.log.info("[%s] r/%s: %d įrašų", country.iso_code, sub, len(posts))
        return items
