"""Didomi CMP.

URLs:
  - sdk.privacy-center.org/<id>/notice/<lang>.json
  - api.privacy-center.org/v1/notices/...
Schema: app.vendors[], app.purposes[], notice texts
"""

import re

MATCHER = re.compile(
    r"(?:sdk|api)\.privacy-center\.org/.+/notice[s]?/.*\.json(?:\?|$)", re.I,
)


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

    app = d.get("app", d) or {}
    notice = d.get("notice", {}) or app.get("notice", {}) or {}
    for key in ("content", "title", "subtitle"):
        v = notice.get(key)
        if v:
            parts.append("")
            parts.append(str(v))

    purposes = app.get("purposes") or d.get("purposes") or []
    if purposes:
        parts.append("")
        parts.append("## Zwecke")
        for p in purposes:
            name = p.get("name") or p.get("id") or ""
            desc = p.get("description") or ""
            parts.append(f"- {name}: {desc[:200]}")

    vendors = app.get("vendors") or d.get("vendors") or []
    if vendors:
        parts.append("")
        parts.append(f"## Anbieter ({len(vendors)})")
        for v in vendors[:80]:
            name = v.get("name") or ""
            country = v.get("country") or ""
            policy = v.get("policyUrl") or ""
            line = f"- {name}"
            if country:
                line += f" — Sitz: {country}"
            if policy:
                line += f" — Datenschutz: {policy}"
            parts.append(line)

    return "\n".join(parts)
