from __future__ import annotations

import json
from pathlib import Path

from keiro.gateway.infra.sur import SUR

from ...ports.sur import SurStore


class FileSurStore(SurStore):
    """Persist SUR documents to disk in JSON format."""

    def __init__(self, root: str) -> None:
        self._root = Path(root)

    def persist(self, record: SUR) -> None:
        self._root.mkdir(parents=True, exist_ok=True)
        path = self._root / f"{record.request_id}.json"
        with path.open("w", encoding="utf-8") as file:
            json.dump(record.as_dict(), file, ensure_ascii=False, sort_keys=True)
