"""HuggingFace-Helper — HF REST /api/models, sort=lastModified."""
from __future__ import annotations

import datetime as dt
import json
import logging
import urllib.parse
import urllib.request

log = logging.getLogger(__name__)

HF_MODELS_URL = "https://huggingface.co/api/models"
USER_AGENT = "aria-radar/0.1 (+aseckzai@gmail.com)"
TIMEOUT_SECONDS = 15

INTERESTING_PIPELINES = {"text-generation", "conversational"}


def fetch(*, since: dt.datetime, max_items: int = 20) -> list[dict]:
    """Fetch recently-modified text-generation models from HuggingFace."""
    params = {
        "sort": "lastModified",
        "direction": "-1",
        "limit": str(max_items * 3),
        "pipeline_tag": "text-generation",
    }
    url = f"{HF_MODELS_URL}?{urllib.parse.urlencode(params)}"
    req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT, "Accept": "application/json"})

    with urllib.request.urlopen(req, timeout=TIMEOUT_SECONDS) as resp:
        payload = json.loads(resp.read())

    items: list[dict] = []
    for model in payload:
        last_modified_raw = model.get("lastModified") or ""
        try:
            last_modified = dt.datetime.fromisoformat(last_modified_raw.replace("Z", "+00:00"))
        except ValueError:
            continue
        if last_modified < since:
            continue
        model_id = model.get("modelId") or model.get("id") or ""
        if not model_id:
            continue
        items.append({
            "title": model_id,
            "url": f"https://huggingface.co/{model_id}",
            "date": last_modified.isoformat(),
            "source": "huggingface",
            "score": model.get("downloads"),
            "raw": {
                "pipeline_tag": model.get("pipeline_tag"),
                "tags": model.get("tags", [])[:10],
                "likes": model.get("likes"),
                "library_name": model.get("library_name"),
            },
        })
        if len(items) >= max_items:
            break
    log.info("huggingface: %d items", len(items))
    return items


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    items = fetch(since=dt.datetime.now(dt.timezone.utc) - dt.timedelta(hours=24))
    print(json.dumps(items, indent=2))
