{#- Copyright 2020 Curtin University
#
# 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.
# Author: Tuan Chien -#}
import logging
from typing import List

import pendulum
from observatory.platform.workflows.workflow import Release, Workflow


class {{ workflow_class }}Release(Release):
    def __init__(self, *, dag_id: str, release_date: pendulum.DateTime):
        """Construct a Release instance
        :param dag_id: the id of the DAG.
        :param release_id: the id of the release.
        """

        self.release_date = release_date
        release_id = f'{dag_id}_{self.release_date.strftime("%Y_%m_%d")}'
        super().__init__(dag_id=dag_id, release_id=release_id)


class {{ workflow_class }}(Workflow):
    DAG_ID = "{{ workflow_module }}"

    def __init__(
        self,
        *,
        dag_id: str = DAG_ID,
        start_date: pendulum.DateTime = pendulum.datetime(2021, 7, 1),
        schedule_interval="@weekly",
        catchup=False,
    ):
        """Construct a {{ workflow_class }}Workflow instance.

        :param dag_id: the id of the DAG.
        :param start_date: the start date of the DAG.
        :param schedule_interval: the schedule interval of the DAG.
        :param catchup: whether to catchup the DAG or not.
        """

        super().__init__(
            dag_id=dag_id,
            start_date=start_date,
            schedule_interval=schedule_interval,
            catchup=catchup,
        )

        # Add sensor tasks
        # self.add_sensor(some_airflow_sensor)

        # Add setup tasks
        # self.add_setup_task(self.some_setup_task)

        # Add generic tasks
        self.add_task(self.task1)
        self.add_task(self.cleanup)

    def make_release(self, **kwargs):
        """Make a release instance.
        :param kwargs: the context passed from the PythonOperator.
        :return: A release instance or list of release instances
        """

        releases = list()
        release = {{ workflow_class }}Release(dag_id=self.dag_id, release_date=self.start_date)
        releases.append(release)
        return releases

    def task1(self, releases: List[{{ workflow_class }}Release], **kwargs):
        """ Add your own comments. """

        pass

    def cleanup(self, releases: List[{{ workflow_class }}Release], **kwargs):
        """ Delete downloaded, extracted and transformed files of the release.
        :param releases: List of {{ workflow_class }}Release instances
        :param kwargs: The context passed from the PythonOperator.
        :return: None.
        """

        for release in releases:
            release.cleanup()