from datetime import datetime
from typing import Optional, Union

from async_property import async_property

from core.database.abc.db_model_abc import DbModelABC
from core.typing import SerialId


class UserSetting(DbModelABC):
    def __init__(
        self,
        id: SerialId,
        user_id: SerialId,
        key: str,
        value: str,
        deleted: bool = False,
        editor_id: Optional[SerialId] = None,
        created: Optional[datetime] = None,
        updated: Optional[datetime] = None,
    ):
        DbModelABC.__init__(self, id, deleted, editor_id, created, updated)

        self._user_id = user_id
        self._key = key
        self._value = value

    @property
    def user_id(self) -> SerialId:
        return self._user_id

    @async_property
    async def user(self):
        from data.schemas.administration.user_dao import userDao

        return await userDao.get_by_id(self._user_id)

    @property
    def key(self) -> str:
        return self._key

    @property
    def value(self) -> str:
        return self._value

    @value.setter
    def value(self, value: Union[str, int, float, bool]):
        self._value = str(value)
