from __future__ import annotations

import hashlib
import json
from typing import Any

_EXCLUDED_FROM_HASH = frozenset(
    {
        "version",
        "content_hash",
        "sync_timestamp",
        "sync_status",
        "created_at",
        "updated_at",
        "created_by",
        "updated_by",
        "client_id",
        "server_synced",
        "is_deleted",
        "deleted_at",
        "deleted_by",
        "enterprise_id",
    }
)


def stable_payload_for_hash(data: dict[str, Any]) -> dict[str, Any]:
    return {k: v for k, v in data.items() if k not in _EXCLUDED_FROM_HASH}


def compute_content_hash_hex(payload: dict[str, Any]) -> str:
    raw = json.dumps(payload, sort_keys=True, ensure_ascii=False, default=str).encode("utf-8")
    return hashlib.sha256(raw).hexdigest()


def compute_content_hash(data: dict[str, Any]) -> str:
    return compute_content_hash_hex(stable_payload_for_hash(data))
