"""Persistent session-state helpers for GUI-only runtime preferences."""

from __future__ import annotations

from dataclasses import dataclass
import json
from pathlib import Path


@dataclass(slots=True)
class GuiSessionState:
    """Small persisted GUI session state."""

    last_profile_path: str = ""


class GuiSessionStateStore:
    """Atomic JSON store for GUI session-state file."""

    def __init__(self, path: Path) -> None:
        """Create store targeting `path`."""
        self._path = path
        self._path.parent.mkdir(parents=True, exist_ok=True)

    @property
    def path(self) -> Path:
        """Return backing session-state file path."""
        return self._path

    def load(self) -> GuiSessionState:
        """Load session-state from disk or return empty defaults when missing."""
        if not self._path.exists():
            return GuiSessionState()

        payload = json.loads(self._path.read_text(encoding="utf-8"))
        if not isinstance(payload, dict):
            raise ValueError(f"GUI session-state root must be JSON object: {self._path}")

        raw_path = payload.get("last_profile_path", "")
        if not isinstance(raw_path, str):
            raise ValueError("GUI session-state `last_profile_path` must be a string")
        return GuiSessionState(last_profile_path=raw_path)

    def write(self, state: GuiSessionState) -> Path:
        """Atomically write session-state JSON file."""
        temp_path = self._path.with_suffix(self._path.suffix + ".tmp")
        temp_path.write_text(
            json.dumps({"last_profile_path": state.last_profile_path}, indent=2),
            encoding="utf-8",
        )
        temp_path.replace(self._path)
        return self._path
