"""에피소드별 state.json (캔버스·업로드 캐시·Element·컷 노드·제출 ID). 토큰·서명 URL은 저장하지 않는다."""
import json
from pathlib import Path


class State(dict):
    def __init__(self, ep_dir):
        self.path = Path(ep_dir) / "state.json"
        data = json.loads(self.path.read_text(encoding="utf-8")) if self.path.exists() else {}
        super().__init__(data)
        self.setdefault("episode", Path(ep_dir).name)

    def save(self):
        tmp = self.path.with_suffix(".json.tmp")
        tmp.write_text(json.dumps(self, ensure_ascii=False, indent=2), encoding="utf-8")
        tmp.replace(self.path)
