import os
import json
import hashlib
import time
from pathlib import Path

CACHE_ROOT = Path("/data/cache")


def _hash_key(source_lang: str, target_lang: str, text: str) -> str:
    raw = f"{source_lang}:{target_lang}:{text.strip()}"
    return hashlib.sha256(raw.encode("utf-8")).hexdigest()


def _get_path(source_lang: str, target_lang: str, key: str) -> Path:
    folder = CACHE_ROOT / f"{source_lang}_{target_lang}"
    folder.mkdir(parents=True, exist_ok=True)
    return folder / f"{key}.json"


def get_cached(source_lang: str, target_lang: str, text: str):
    key = _hash_key(source_lang, target_lang, text)
    path = _get_path(source_lang, target_lang, key)

    if not path.exists():
        return None

    try:
        with open(path, "r", encoding="utf-8") as f:
            data = json.load(f)
            return data.get("translated")
    except Exception:
        return None


def set_cache(source_lang: str, target_lang: str, text: str, translated: str, model: str):
    key = _hash_key(source_lang, target_lang, text)
    path = _get_path(source_lang, target_lang, key)

    data = {
        "source": text,
        "translated": translated,
        "model": model,
        "created": int(time.time()),
    }

    tmp_path = path.with_suffix(".tmp")

    with open(tmp_path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False)

    os.replace(tmp_path, path)
