from abc import ABC
from datetime import datetime, timezone
from typing import Optional

from async_property import async_property

from core.typing import Id, SerialId


class DbModelABC(ABC):
    def __init__(
        self,
        id: Id,
        deleted: bool = False,
        editor_id: Optional[SerialId] = None,
        created: Optional[datetime] = None,
        updated: Optional[datetime] = None,
    ):
        self._id = id
        self._deleted = deleted
        self._editor_id = editor_id

        self._created = (
            created if created is not None else datetime.now(timezone.utc).isoformat()
        )
        self._updated = (
            updated if updated is not None else datetime.now(timezone.utc).isoformat()
        )

    @property
    def id(self) -> Id:
        return self._id

    @property
    def deleted(self) -> bool:
        return self._deleted

    @deleted.setter
    def deleted(self, value: bool):
        self._deleted = value

    @property
    def editor_id(self) -> SerialId:
        return self._editor_id

    @editor_id.setter
    def editor_id(self, value: SerialId):
        self._editor_id = value

    @async_property
    async def editor(self):
        if self._editor_id is None:
            return None

        from data.schemas.administration.user_dao import userDao

        return await userDao.get_by_id(self._editor_id)

    @property
    def created(self) -> datetime:
        return self._created

    @property
    def updated(self) -> datetime:
        return self._updated

    @updated.setter
    def updated(self, value: datetime):
        self._updated = value
