from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.inspection import inspect
from sqlalchemy.orm.exc import NoResultFound
from marshmallow import Schema, fields, ValidationError, pre_load

db = SQLAlchemy()

# deprecated Serializer. using marshmallow
#
# this Serializer class is needed to serialize a SQLAlchemy object to jsonify
# class Serializer(object):
#     def serialize(self):
#         return {c: getattr(self, c) for c in inspect(self).attrs.keys()}
#
#     @staticmethod
#     def serialize_list(l):
#         return [m.serialize() for m in l]

# Models
class CustomerDataModel(db.Model):
    __tablename__ = 'customer_data'
    id = db.Column(db.Integer, primary_key=True)
    customer_id  = db.Column(db.Integer)
    customername = db.Column(db.String(50))
    voucher_code = db.Column(db.String(50))
    mobile = db.Column(db.String(50))
    otp = db.Column(db.String(25))
    created_date = db.Column(db.DateTime)
    updated_date = db.Column(db.DateTime)
    ipaddress = db.Column(db.String(50))
    status = db.Column(db.Integer)
    campaign_id = db.Column(db.Integer)
    reward_id   = db.Column(db.Integer)

    def __init__(self, customername, voucher_code, mobile, otp, created_date, updated_date, ipaddress, status, campaign_id):
        self.customername = customername
        self.voucher_code = voucher_code
        self.mobile = mobile
        self.otp = otp
        self.created_date = created_date
        self.updated_date = updated_date
        self.ipaddress = ipaddress
        self.status = status
        self.campaign_id = campaign_id

    def save(self):
        db.session.add(self)
        db.session.commit()

    def update(self, data):
        for key, item in data.items():
            setattr(self, key, item)
        self.updated_date = datetime.datetime.utcnow()
        db.session.commit()

    def get_all_customerdata():
        return CustomerDataModel.query.all()

    def get_one_customerdata(id):
        return CustomerDataModel.query.get(id)

    def __repr__(self):
        return '<name {}>'.format(self.customername)

class MessageModel(db.Model):
    __tablename__ = 'messages'
    id = db.Column(db.Integer, primary_key=True)
    fromid  = db.Column(db.String(50))
    toid = db.Column(db.String(50))
    sub = db.Column(db.String(250))
    body = db.Column(db.String(1500))
    msgtype = db.Column(db.Integer)
    createdon = db.Column(db.DateTime)
    senton = db.Column(db.DateTime)
    status = db.Column(db.Integer)
    campaign_id = db.Column(db.Integer)

    def __init__(self, fromid, toid, sub, body, msgtype, createdon, status, campaign_id):
        self.fromid = fromid
        self.toid = toid
        self.sub = sub
        self.body = body
        self.msgtype = msgtype
        self.createdon = createdon
        self.status = status
        self.campaign_id = campaign_id

    def save(self):
        db.session.add(self)
        db.session.commit()

class VoucherModel(db.Model):
    __tablename__ = 'vouchers'
    id = db.Column(db.Integer, primary_key=True)
    campaign_id  = db.Column(db.Integer)
    code = db.Column(db.String(50))
    usedon = db.Column(db.DateTime)
    status = db.Column(db.Integer)

    def __init__(self, campaign_id, code, usedon, status):
        self.campaign_id = campaign_id
        self.code = code
        self.usedon = usedon
        self.status = status

    def save(self):
        db.session.add(self)
        db.session.commit()

    def voucher_validaiton_by_code(code):
        print('test')
        try:
            data = VoucherModel.query.filter_by(code=code,status=0).one()
            return True
        except NoResultFound:
            return False

class SentimentAnalysisDataModel(db.Model):
    __tablename__ = 'sentiment_analysis_data'
    id = db.Column(db.Integer, primary_key=True)
    campaign_id  = db.Column(db.Integer)
    no_of_posts_anlaysed = db.Column(db.Integer)
    cumulative_compound_score = db.Column(db.Numeric)
    created_timestamp = db.Column(db.DateTime)
    wall_id           = db.Column(db.Integer)
    customer_id       = db.Column(db.Integer)


    def __init__(self, campaign_id, no_of_posts_anlaysed, cumulative_compound_score, created_timestamp,wall_id,customer_id):
        self.campaign_id = campaign_id
        self.no_of_posts_anlaysed = no_of_posts_anlaysed
        self.cumulative_compound_score = cumulative_compound_score
        self.created_timestamp = created_timestamp
        self.wall_id           = wall_id
        self.customer_id       = customer_id

    def save(self):
        db.session.add(self)
        db.session.commit()
