{#
   Copyright 2021 Modelyst LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
#}

# External imports
from typing import Any
from airflow import DAG # type: ignore

from datetime import datetime

# Internal Imports
from dbgen import Gen
from dbgen.core.airflow_plugin import {{operator_name}} # type: ignore

# Written by {{ user }}

objs = dict({% for obj,(pk,ids,idfks) in objs.items() %} {{ obj }} = ('{{ pk }}', {{ ids }},{{ idfks }}),{% endfor %}) # type: D[str,T[str,L[str],L[str]]]

# Define DAG
default_args ={
    'owner'             : '{{ user }}',
    'start_date'        : datetime.strptime('{{ date }}','%Y-%m-%d'),
    'retries'           : 1,
    'backfill'          : False,
    'catchup'           : False
}

default_args.update({{ dag_args }})
with DAG('{{ modelname }}',schedule_interval = '{{ schedule_interval }}', default_args = default_args) as dag:
    {% for gen_name, gen_hash in gen_hash_dict.items() %}
    {{gen_name}}  = {{operator_name}}(
                               objs            = objs,
                               gen_name        = '{{gen_name}}',
                               gen_hash        = {{gen_hash}},
                               run_id          = {{run_id}},
                               db_conn_id      = '{{modelname}}',
                               mdb_conn_id     = '{{modelname}}_log',
                               retry           = {{retry}},
                               serial          = {{serial}},
                               bar             = {{bar}},
                               user_batch_size = {% if batch %}{{batch}}{% else %}None{%endif %}
                              )
    {% endfor %}

# Add dependencies
{% for child,parent in deps %}
    {{ parent }}.set_upstream({{ child}})
{% endfor %}

if __name__ == '__main__':
{% for gen_name in gen_hash_dict.keys() %}
    {{ gen_name }}.execute({})
{% endfor %}
