import hashlib
import json
from importlib.resources import files
from pathlib import Path

PACKAGE_NAME = "exteragram_support_assets"


def get_asset_path(filename: str) -> Path:
    return Path(files(PACKAGE_NAME).joinpath("data", filename))


def read_asset_text(filename: str, encoding: str = "utf-8") -> str:
    return get_asset_path(filename).read_text(encoding=encoding)


def read_asset_bytes(filename: str) -> bytes:
    return get_asset_path(filename).read_bytes()


def read_hashes() -> dict[str, str]:
    hashes_path = Path(files(PACKAGE_NAME).joinpath("hashes.json"))
    return json.loads(hashes_path.read_text(encoding="utf-8"))


def verify_asset(filename: str) -> bool:
    expected = read_hashes().get(filename)
    if not expected:
        return False
    digest = hashlib.sha256(read_asset_bytes(filename)).hexdigest()
    return digest.lower() == expected.lower()
