import prefect
from datamonk.utils import functions as utils
from datamonk.scriptorium.process import monitoring
import datamonk.scriptorium.apps.dataform as dataform
import os

import logging

@prefect.task
def dataform_transform(project_id=os.environ.get("DATAFORM_PROJECT_ID"),
                       token=os.environ.get("DATAFORM_API_KEY"),
                       schedule=os.environ.get("DATAFORM_SCHEDULE_NAME")
                       ):
       df_inst=dataform.instance(project_id=project_id,
                                 token=token)
       return df_inst.run(schedule=schedule)


@prefect.task
def data_monitoring(config):
    return monitoring.run(rules_list=config["rules"])

def prefect_parameter_read_json(path,param_name):
        return prefect.Parameter(param_name,
                                 default=utils.local.read_configJSON(path,"dict")
                                 )


@prefect.task
def generate_time_range(start,end,output_format='%Y-%m-%dT%H:%M:%S'):
    logger = logging.getLogger(__name__)
    generated_time_range = utils.time(output_format=output_format).get_time_range(start=start,end=end)

    logger.info(f"generated_time_range{generated_time_range}")
    return generated_time_range
