DEFAULT_LIMIT = 30
import typing

from snek.system.model import BaseModel


class BaseMapper:

    model_class: BaseModel = None
    default_limit: int = DEFAULT_LIMIT
    table_name: str = None

    def __init__(self, app):
        self.app = app

        self.default_limit = self.__class__.default_limit

    @property
    def db(self):
        return self.app.db

    async def new(self):
        return self.model_class(mapper=self)

    @property
    def table(self):
        return self.db[self.table_name]

    async def get(self, uid: str = None, **kwargs) -> BaseModel:
        if uid:
            kwargs["uid"] = uid
        record = self.table.find_one(**kwargs)
        if not record:
            return None
        record = dict(record)
        model = await self.new()
        for key, value in record.items():
            model[key] = value
        return model
        return await self.model_class.from_record(mapper=self, record=record)

    async def exists(self, **kwargs):
        return self.table.exists(**kwargs)

    async def count(self, **kwargs) -> int:
        return self.table.count(**kwargs)

    async def save(self, model: BaseModel) -> bool:
        if not model.record.get("uid"):
            raise Exception(f"Attempt to save without uid: {model.record}.")
        model.updated_at.update()
        return self.table.upsert(model.record, ["uid"])

    async def find(self, **kwargs) -> typing.AsyncGenerator:
        if not kwargs.get("_limit"):
            kwargs["_limit"] = self.default_limit
        for record in self.table.find(**kwargs):
            model = await self.new()
            for key, value in record.items():
                model[key] = value
            yield model

    async def query(self, sql, *args):
        for record in self.db.query(sql, *args):
            yield dict(record)

    async def delete(self, kwargs=None) -> int:
        if not kwargs or not isinstance(kwargs, dict):
            raise Exception("Can't execute delete with no filter.")
        return self.table.delete(**kwargs)
