class Account(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    account_number = db.Column(db.String(20), index=True, unique=True)
    owner_id = db.Column(db.Integer, db.ForeignKey('customer.id'))
    account_name = db.Column(db.String(128), index=True)
    balance = db.Column(db.Float)
    transactions = db.relationship('Transaction',
                                   backref="account",
                                   cascade="all, delete-orphan",
                                   lazy='dynamic')
    pending_transactions = db.relationship('PendingTransaction',
                                           backref="pt_account",
                                           cascade="all, delete-orphan",
                                           lazy='dynamic')

    def as_dict(self):
        #Urgh, this totally won't come back to bite me in the arse...
        #In theory, there's an issue serialising datetime objects... not a problem right now...
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    #Reduce Balance
    def debit(self, amount):
        self.balance = self.balance - amount
        return True

    #Increase Balance
    def credit(self, amount):
        self.balance = self.balance + amount
        return True
class oAuthRefreshToken(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, index=True)
    token_value = db.Column(db.String(64), index=True, unique=True)
    grant = db.Column(db.String(128), index=True)
    creation_date = db.Column(db.DateTime(),
                              server_default=db.func.current_timestamp())
class APIKey(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    key_name = db.Column(db.String(64), index=True, unique=True)
    key_value = db.Column(db.String(128), index=True, unique=True)
    creation_date = db.Column(db.DateTime(timezone=True),
                              server_default=func.now())

    def __repr__(self):
        return '<Token %r>' % (self.key_name)
class PendingTransaction(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    to_account = db.Column(db.Integer)
    tran_type = db.Column(db.String(128), index=True)
    amount = db.Column(db.Float)

    def as_dict(self):
        #Urgh, this totally won't come back to bite me in the arse...
        #In theory, there's an issue serialising datetime objects... not a problem right now...
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Customer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    customer_number = db.Column(db.String(20), index=True, unique=True)
    user_id = db.Column(db.Integer, index=True, unique=True)
    email = db.Column(db.String(128), index=True)
    street_address = db.Column(db.String(128), index=True)
    city = db.Column(db.String(128), index=True)
    country = db.Column(db.String(128), index=True)
    state = db.Column(db.String(128), index=True)
    postcode = db.Column(db.Integer)
    accounts = db.relationship('Account',
                               backref="owner",
                               cascade="all, delete-orphan",
                               lazy='dynamic')

    def as_dict(self):
        #Urgh, this totally won't come back to bite me in the arse...
        #In theory, there's an issue serialising datetime objects... not a problem right now...
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    def __json__(self):
        return [
            'id', 'user_id', 'email', 'street_address', 'city', 'state',
            'country', 'postcode'
        ]
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    password = db.Column(db.String(128), index=True)
    creation_date = db.Column(db.DateTime(timezone=True),
                              server_default=func.now())