from app.main import main
from app.main.utils.common import response_message, read_data, read_rules, write_validations
from rulemall import RulesGroup
import pandas as pd
from datetime import datetime


@main.route('/validate/<rsid>/<rgid>', methods=['GET', 'POST'])
def validate(rsid, rgid):
    configs, rs = read_rules(rsid, rgid, where='db')
    rg = RulesGroup().from_dict(configs)
    # cols = rg.get_target()
    data = read_data(rsid, rs)
    rg.reset_data(data)
    validation, responsible = rg.validate()
    validation = pd.concat([validation.rename('validation'), responsible], axis=1)
    validation = validation.reset_index().rename(columns={'index': 'rid'})
    # TODO return rule group id
    validation['rgid'] = rgid
    validation['created_date'] = datetime.now()
    validation['modified_date'] = datetime.now()
    result = write_validations(validation)
    return response_message(result)
