"""Cookiebot (by Usercentrics A/S — separate product from Usercentrics CMP).

URLs (multiple shapes observed):
  - consent.cookiebot.com/<id>/cc.js     (JSONP-wrapped)
  - consent.cookiebot.com/uc.js?...      (JSONP)
  - consent.cookiebot.com/<id>/cd.js     (cookie declaration)
We accept any URL on consent.cookiebot.com that returns JSON-like data.
The capture pipeline JSON-decodes; if it's JSONP we'd need to strip the
callback wrapper. For now we match only direct JSON responses.

Schema (cookiedeclaration JSON):
  Categories: list with name + cookies (each with name, vendor, expires)
"""

import re

MATCHER = re.compile(r"consent\.cookiebot\.com/.*\.(?:json|js)(?:\?|$)", re.I)


def reconstruct(d: dict) -> str:
    parts: list[str] = ["# Cookie-Richtlinie (Cookiebot)"]

    cats = d.get("Categories") or d.get("categories") or []
    for cat in cats:
        name = cat.get("Name") or cat.get("name") or ""
        desc = cat.get("Description") or cat.get("description") or ""
        parts.append("")
        parts.append(f"## {name}")
        if desc:
            parts.append(desc)
        cookies = cat.get("Cookies") or cat.get("cookies") or []
        for c in cookies[:50]:
            cn = c.get("Name") or c.get("name") or ""
            vendor = c.get("Vendor") or c.get("vendor") or ""
            expires = c.get("Expires") or c.get("expires") or ""
            purpose = c.get("Purpose") or c.get("purpose") or ""
            line = f"- {cn}"
            if vendor:
                line += f" ({vendor})"
            if purpose:
                line += f" — {purpose[:120]}"
            if expires:
                line += f" — Speicherdauer: {expires}"
            parts.append(line)

    return "\n".join(parts)
