from datetime import datetime
from sqlalchemy import Boolean, Column, DateTime, Integer, String, Text
from db import Base, CRUD
import utils


class AccessLevel(Base):
    __tablename__ = "access_level"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, unique=True, index=True)
    description = Column(Text)
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)

    # permissions
    receiving_edit = Column(Boolean, default=False)
    refund_request_edit = Column(Boolean, default=False)
    refund_request_confirm = Column(Boolean, default=False)
    debit_request_edit = Column(Boolean, default=False)
    debit_request_confirm = Column(Boolean, default=False)
    tools_creation = Column(Boolean, default=False)
    tools_registration = Column(Boolean, default=False)
    tools_registration_edit = Column(Boolean, default=False)
    tools_edit = Column(Boolean, default=False)
    tools_delete = Column(Boolean, default=False)
    users_creation = Column(Boolean, default=False)
    users_edit = Column(Boolean, default=False)
    users_disabling = Column(Boolean, default=False)
    users_view = Column(Boolean, default=False)
    available_own_toolbox = Column(Boolean, default=False)
    view_all_toolboxes = Column(Boolean, default=False)
    view_requests = Column(Boolean, default=False)
    view_services = Column(Boolean, default=False)
    access_level_view = Column(Boolean, default=False)
    access_level_edit = Column(Boolean, default=False)
    manage_toolboxes = Column(Boolean, default=False)

    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    def toDict(self):
        return utils.toDict(self)

    async def save(self):
        return await CRUD.create(self, refresh=True)

    async def edit(self, **kwargs):
        return await CRUD.update(AccessLevel, self.id, **kwargs)
