from __future__ import annotations

import json
from dataclasses import dataclass

from openhands.app_server.file_store.files import FileStore
from openhands.app_server.settings.settings_models import Settings
from openhands.app_server.settings.settings_store import SettingsStore
from openhands.app_server.utils.async_utils import call_sync_from_async


@dataclass
class FileSettingsStore(SettingsStore):
    file_store: FileStore
    path: str = 'settings.json'

    async def load(self) -> Settings | None:
        try:
            json_str = await call_sync_from_async(self.file_store.read, self.path)
            kwargs = json.loads(json_str)
            # Seed a Default profile from legacy agent_settings.llm when
            # llm_profiles is absent — pre-llm_profiles settings.json files
            # would otherwise present an empty profiles UI on upgrade.
            if 'llm_profiles' not in kwargs:
                legacy_llm = (kwargs.get('agent_settings') or {}).get('llm')
                if isinstance(legacy_llm, dict) and legacy_llm.get('model'):
                    kwargs['llm_profiles'] = {
                        'profiles': {'Default': legacy_llm},
                        'active': 'Default',
                    }
            settings = Settings(**kwargs)

            # Turn on V1 in OpenHands
            # We can simplify / remove this as part of V0 removal
            settings.v1_enabled = True

            return settings
        except FileNotFoundError:
            return None

    async def store(self, settings: Settings) -> None:
        json_str = settings.model_dump_json(
            context={'expose_secrets': True, 'persist_settings': True}
        )
        await call_sync_from_async(self.file_store.write, self.path, json_str)

    @classmethod
    async def get_instance(cls, user_id: str | None) -> FileSettingsStore:
        """Get a FileSettingsStore instance using the global config's file_store.

        TODO: This method should be replaced with dependency injection.
        """
        from openhands.app_server.config import get_global_config

        file_store = get_global_config().file_store
        return FileSettingsStore(file_store)
