from __future__ import annotations

import json
from types import SimpleNamespace
from typing import Any, Optional


def _clean_str(value: Any) -> Optional[str]:
    v = (value or "").strip() if isinstance(value, str) else (str(value).strip() if value is not None else "")
    return v or None


def dumps_snapshot(data: dict[str, Any] | None) -> Optional[str]:
    if not data:
        return None
    return json.dumps(data, ensure_ascii=False, separators=(",", ":"))


def loads_snapshot(text: str | None) -> dict[str, Any] | None:
    if not text:
        return None
    try:
        obj = json.loads(text)
        return obj if isinstance(obj, dict) else None
    except Exception:
        return None


def client_display_name(snapshot: dict[str, Any] | None) -> str:
    if not snapshot:
        return ""
    name = _clean_str(snapshot.get("name"))
    if name:
        return name
    fn = _clean_str(snapshot.get("first_name")) or ""
    ln = _clean_str(snapshot.get("last_name")) or ""
    return (fn + (" " + ln if ln else "")).strip()


def company_display_name(snapshot: dict[str, Any] | None) -> str:
    if not snapshot:
        return ""
    return _clean_str(snapshot.get("name")) or ""


def build_client_snapshot(client) -> dict[str, Any] | None:
    if not client:
        return None
    return {
        "id": getattr(client, "id", None),
        "name": getattr(client, "name", None),
        "first_name": getattr(client, "first_name", None),
        "last_name": getattr(client, "last_name", None),
        "sigla_nazione": getattr(client, "sigla_nazione", None),
        "country": getattr(client, "country", None),
        "piva_cf": getattr(client, "piva_cf", None),
        "address": getattr(client, "address", None),
        "street_number": getattr(client, "street_number", None),
        "cap": getattr(client, "cap", None),
        "city": getattr(client, "city", None),
        "province": getattr(client, "province", None),
        "email_pec": getattr(client, "email_pec", None),
        "codice_univoco": getattr(client, "codice_univoco", None),
        "codice_destinatario": getattr(client, "codice_destinatario", None),
        "phone": getattr(client, "phone", None),
        "website": getattr(client, "website", None),
    }


def build_company_snapshot(company) -> dict[str, Any] | None:
    if not company:
        return None
    return {
        "id": getattr(company, "id", None),
        "name": getattr(company, "name", None),
        "sigla_nazione": getattr(company, "sigla_nazione", None),
        "country": getattr(company, "country", None),
        "vat_country": getattr(company, "vat_country", None),
        "vat_number": getattr(company, "vat_number", None),
        "tax_code": getattr(company, "tax_code", None),
        "address": getattr(company, "address", None),
        "street_number": getattr(company, "street_number", None),
        "cap": getattr(company, "cap", None),
        "city": getattr(company, "city", None),
        "province": getattr(company, "province", None),
        "email_pec": getattr(company, "email_pec", None),
        "codice_univoco": getattr(company, "codice_univoco", None),
        "codice_destinatario": getattr(company, "codice_destinatario", None),
        "phone": getattr(company, "phone", None),
        "website": getattr(company, "website", None),
        "transmitter_id_country": getattr(company, "transmitter_id_country", None),
        "transmitter_id_code": getattr(company, "transmitter_id_code", None),
    }


def snapshot_to_namespace(snapshot: dict[str, Any] | None) -> SimpleNamespace:
    return SimpleNamespace(**(snapshot or {}))
