"""Import imaging metadata from various files to I2B2"""

from datetime import datetime, timedelta

from airflow import DAG

from common_steps import initial_step
from common_steps.prepare_pipeline import prepare_pipeline
from metadata_steps.metadata_to_i2b2 import metadata_to_i2b2_pipeline_cfg


def metadata_import_dag(dataset, section, email_errors_to, max_active_runs):

    # Define the DAG

    dag_name = '%s_metadata_import' % dataset.lower().replace(" ", "_")

    default_args = {
        'owner': 'airflow',
        'depends_on_past': False,
        'start_date': datetime.now(),
        'retries': 1,
        'retry_delay': timedelta(seconds=120),
        'email': email_errors_to,
        'email_on_failure': True,
        'email_on_retry': True
    }

    dag = DAG(
        dag_id=dag_name,
        default_args=default_args,
        schedule_interval=None,
        max_active_runs=max_active_runs)

    upstream_step = prepare_pipeline(dag, initial_step, False)
    metadata_to_i2b2_pipeline_cfg(dag, upstream_step, section)

    return dag
