import json
from pathlib import Path

from supabase_auth._async.storage import AsyncSupportedStorage

from harbor.auth.constants import CREDENTIALS_PATH


class AsyncFileStorage(AsyncSupportedStorage):
    """Persists Supabase auth sessions to ~/.harbor/credentials.json.

    Implements the AsyncSupportedStorage interface so the Supabase SDK
    automatically persists session changes (tokens, refresh tokens) to disk.
    """

    def __init__(self, path: Path | None = None):
        self._path = path or CREDENTIALS_PATH
        self._data: dict[str, str] = {}
        self._load()

    def _load(self) -> None:
        if self._path.exists():
            self._data = json.loads(self._path.read_text())

    def _save(self) -> None:
        self._path.parent.mkdir(parents=True, exist_ok=True)
        self._path.write_text(json.dumps(self._data, indent=2))
        self._path.chmod(0o600)

    async def get_item(self, key: str) -> str | None:
        return self._data.get(key)

    async def set_item(self, key: str, value: str) -> None:
        self._data[key] = value
        self._save()

    async def remove_item(self, key: str) -> None:
        self._data.pop(key, None)
        self._save()
