# Path: app/models.py
# class Respondent(db.Model):
# class Campaign(db.Model):
# class Audience(db.Model):
# class RandomBias(db.Model):
# class PossibleAbuses(db.Model):
# class BlockedClients(db.Model):
# class CallStatistics(db.Model):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
#     def __repr__(self):


from flask import current_app
from app.models import db, PossibleAbuses
from hashlib import sha256
from datetime import date


def abuse_detected(phone_number, duration=None, persist=False):
    """
    :param phone_number: a phone number to add to the abuse tracker.
    :param duration: the duration of the call.
    :param persist: iff persist is set to True abuse flag will be set permanently
    :return: true iff abuse is likely
    """

    hashed_phone_number = sha256(bytes(str(phone_number), 'utf-8')).hexdigest()
    short_calls = 0
    if duration is not None and int(duration) <= current_app.config['SHORT_CALL_THRESHOLD']:
        short_calls = 1

    abuse_case = PossibleAbuses().query.filter_by(id=hashed_phone_number).first()
    if abuse_case is None:

# Path: app/models.py
# class Respondent(db.Model):
# class Campaign(db.Model):
# class Audience(db.Model):
# class RandomBias(db.Model):
# class PossibleAbuses(db.Model):
# class BlockedClients(db.Model):
# class CallStatistics(db.Model):
# class Role(db.Model, RoleMixin):
# class User(db.Model, UserMixin):
#     def __repr__(self):


# Path: app/abuse/calls.py
from flask import current_app
from app.models import db, PossibleAbuses
from hashlib import sha256
from datetime import date



def abuse_detected(phone_number, duration=None, persist=False):
    """
    :param phone_number: a phone number to add to the abuse tracker.
    :param duration: the duration of the call.
    :param persist: iff persist is set to True abuse flag will be set permanently
    :return: true iff abuse is likely
    """

    hashed_phone_number = sha256(bytes(str(phone_number), 'utf-8')).hexdigest()
    short_calls = 0
    if duration is not None and int(duration) <= current_app.config['SHORT_CALL_THRESHOLD']:
        short_calls = 1

    abuse_case = PossibleAbuses().query.filter_by(id=hashed_phone_number).first()
    if abuse_case is None:

        abuse_case = PossibleAbuses()