__author__ = "Florian Kaiser"
__copyright__ = "Copyright 2022, Project Aktienbot"
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
__license__ = "GPL 3.0"
__version__ = "1.0.1"

from app.db import database as db


class User(db.Model):
    __tablename__ = 'users'
    email = db.Column('email', db.String(255), primary_key=True, nullable=False, unique=True)
    password = db.Column('password', db.BINARY(60), nullable=False)
    username = db.Column('username', db.String(255), nullable=False, server_default='')
    telegram_user_id = db.Column('telegram_user_id', db.String(255), nullable=True, server_default='')
    admin = db.Column('admin', db.Boolean(), server_default='0')  # 0 = False, 1 = True
    cron = db.Column('cron', db.String(20), server_default='0 8 * * *', nullable=False)

    def as_dict(self):
        return {
            "email": self.email,
            "username": self.username,
            "telegram_user_id": self.telegram_user_id,
            "admin": self.admin,
            "cron": self.cron
        }


class Transaction(db.Model):
    __tablename__ = 'transactions'
    t_id = db.Column('t_id', db.Integer(), nullable=False, unique=True, primary_key=True)
    email = db.Column('email', db.String(255), db.ForeignKey('users.email', ondelete='CASCADE'))
    isin = db.Column('isin', db.String(255))
    comment = db.Column('comment', db.String(255))
    time = db.Column('time', db.DateTime())
    count = db.Column('count', db.Integer())
    price = db.Column('price', db.Float())

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class Keyword(db.Model):
    __tablename__ = 'keywords'
    s_id = db.Column('s_id', db.Integer(), nullable=False, unique=True, primary_key=True)
    email = db.Column('email', db.String(255), db.ForeignKey('users.email', ondelete='CASCADE'))
    keyword = db.Column('keyword', db.String(255))

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class Share(db.Model):
    __tablename__ = 'shares'
    a_id = db.Column('a_id', db.Integer(), nullable=False, unique=True, primary_key=True)
    email = db.Column('email', db.String(255), db.ForeignKey('users.email', ondelete='CASCADE'))
    isin = db.Column('isin', db.String(255))
    comment = db.Column('comment', db.String(255))

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class SharePrice(db.Model):
    __tablename__ = 'share_price'
    id = db.Column('id', db.Integer(), nullable=False, unique=True, primary_key=True)
    isin = db.Column('isin', db.String(255))
    price = db.Column('price', db.Float())
    date = db.Column('date', db.DateTime())

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
