from sqlalchemy import Column, Integer, String, Boolean, Numeric, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declared_attr

from schnitzelserver.models.modelbase import SchnitzelBase, inherits


class GroupMembership(SchnitzelBase):
    __tablename__ = 'schnitzel_group_membership'
    __audit__ = False
    user = Column(Integer, ForeignKey('schnitzel_user.id'), primary_key=True)
    group = Column(Integer, ForeignKey('schnitzel_group.id'), primary_key=True)


class User(SchnitzelBase):
    """
    A basic schnitzel-user which can belong to groups
    """
    __tablename__ = 'schnitzel_user'
    __audit__ = True
    id = Column(Integer, primary_key=True)
    username = Column(String(50), unique=True)
    groups = relationship("Group",
                          secondary="schnitzel_group_membership",
                          backref="members")
    locked_permission = Column(Boolean)  # if set to true, can't be granted additional
    # permissions

    def validate(self):
        print("i am the user")
        super().validate()

@inherits('schnitzel_user')
class Module(SchnitzelBase):
    """
    A module has its dedicated user being in all groups
    """
    __tablename__ = 'schnitzel_module'
    id = Column(Integer, ForeignKey('schnitzel_user.id'), primary_key=True)
    version = Column(Numeric)



class Group(SchnitzelBase):
    """
    A basic group to have access on models/fields
    """

    __tablename__ = 'schnitzel_group'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))


