"""Zentrale Betreiberdaten – Impressum & Datenschutz (David Prechtel)."""

from __future__ import annotations

import json
from pathlib import Path

from jam.paypal_donate import PAYPAL_EMAIL

ROOT = Path(__file__).resolve().parents[1]

OPERATOR_BASE: dict[str, str] = {
    "name": "David Prechtel",
    "brand": "KI Web Studio",
    "legal_form": "Einzelunternehmen",
    "street": "Mühlfeldsiedlung 24",
    "zip_city": "86660 Tapfheim",
    "country": "Deutschland",
    "phone": "09070 / 3069987",
    "phone_e164": "+4990703069987",
    "email": "kontakt@ki-web-studio.de",
    "email_private": "david.prechtel1986@gmail.com",
    "web": "https://ki-web-studio.de",
    "jami_url": "https://www.jami-dating.de",
    "vidpuls_url": "https://vidpuls.de",
    "ust_id": "DE351530475",
    "paypal": PAYPAL_EMAIL,
}


def _read_config() -> dict:
    for name in ("vidpuls-config.json", "clipwelle-config.json", "jampulse-config.json"):
        p = ROOT / "data" / name
        if p.is_file():
            try:
                data = json.loads(p.read_text(encoding="utf-8"))
                if isinstance(data, dict):
                    return data
            except (json.JSONDecodeError, OSError):
                pass
    return {}


def operator(*, product: str = "base") -> dict[str, str]:
    """Vollständige Betreiberdaten für Impressum (product: base | jami | vidpuls)."""
    op = dict(OPERATOR_BASE)
    cfg = _read_config()
    if cfg.get("owner_email"):
        op["email_private"] = str(cfg["owner_email"]).strip()
    if cfg.get("paypal_email"):
        op["paypal"] = str(cfg["paypal_email"]).strip()
    bank = cfg.get("bank_transfer") or {}
    iban = str(bank.get("iban") or "").replace(" ", "").strip()
    if iban:
        op["bank_holder"] = str(bank.get("holder") or op["name"])
        op["bank_iban"] = iban
        op["bank_bic"] = str(bank.get("bic") or "").strip()
        op["bank_name"] = str(bank.get("bank") or "").strip()
    if product == "jami":
        op["brand"] = "KI Web Studio / Jami-Dating"
        op["service_url"] = str(cfg.get("site_url") or op["jami_url"]).rstrip("/")
    elif product == "vidpuls":
        op["brand"] = f"KI Web Studio / VidPuls"
        op["service_url"] = str(cfg.get("site_url") or op["vidpuls_url"]).rstrip("/")
    else:
        op["service_url"] = op["web"]
    return op


# Legacy-Alias (Jami-Dating)
OPERATOR = operator(product="jami")
