import random


def less_more_handler(rowA, fieldA, rowB, fieldB, op, df, t1, t2):
    """
    Handler function for the case of the less or more operators (< or >).
    In case the condition is of the form t.A<t'.B or t.A>t'.B, the function will update the value of t.A to a different value
    from the active domain of A that sarisfies the condition with t'.B, if such a value exists, and to a random value otherwise.

    Parameters
    ----------
    rowA : string
        either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
        or t2 (in which case the tuple t2 is considered on the left-hand side of the = operator)
    rowB : string
        either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
        or t2 (in which case the tuple t2 is considered on the right-hand side of the = operator)
    fieldA : string
        the attribute of the first tuple
    fieldB : string
        the attribute of the second tuple
    op: string
    	the comparison operator
    t1 : 
    	the database tuple corresponding to rowA
    t2 : 
    	the database tuple corresponding to rowB
    df : dataframe
        the database frame

    Returns
    -------
    the updated tuples t1 and t2
    """

    if rowA == "t1":
        if op == ">":
            # in case the violation already exists
            if getattr(t1, fieldA) > getattr(t2, fieldB):
                return t1,t2
            attr_set = df[fieldA].unique()
            comp = getattr(t2, fieldB)
            attr_set = attr_set[attr_set > comp]
            if len(attr_set) > 0:
                val = random.choice(attr_set)
            else:
                val = random.uniform(comp+1, comp+100)
            setattr(t1, fieldA, val)
            return t1,t2

        if op == "<":
            # in case the violation already exists
            if getattr(t1, fieldA) < getattr(t2, fieldB):
                return t1,t2
            attr_set = df[fieldA].unique()
            comp = getattr(t2, fieldB)
            attr_set = attr_set[attr_set < comp]
            if len(attr_set) > 0:
                val = random.choice(attr_set)
            else:
                val = random.uniform(comp-100, comp-1)
            setattr(t1, fieldA, val)
            return t1,t2

    if rowA == "t2":
        if op == ">":
            # in case the violation already exists
            if getattr(t2, fieldA) > getattr(t1, fieldB):
                return t1,t2
            attr_set = df[fieldA].unique()
            comp = getattr(t1, fieldB)
            attr_set = attr_set[attr_set > comp]
            if len(attr_set) > 0:
                val = random.choice(attr_set)
            else:
                val = random.uniform(comp+1, comp+100)
            setattr(t2, fieldA, val)
            return t1,t2

        if op == "<":
            # in case the violation already exists
            if getattr(t2, fieldA) < getattr(t1, fieldB):
                return t1,t2
            attr_set = df[fieldA].unique()
            comp = getattr(t1, fieldB)
            attr_set = attr_set[attr_set < comp]
            if len(attr_set) > 0:
                val = random.choice(attr_set)
            else:
                val = random.uniform(comp-100, comp-1)
            setattr(t2, fieldA, val)
            return t1,t2