# Copyright (C) 2021 OpenMotics BV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from peewee import (
    Model, Database, SqliteDatabase,
    AutoField, CharField, BooleanField, IntegerField, ForeignKeyField,
    FloatField
)
from peewee_migrate import Migrator
import constants

if False:  # MYPY
    from typing import Dict, Any, List, Type


def migrate(migrator, database, fake=False, **kwargs):
    # type: (Migrator, Database, bool, Dict[Any, Any]) -> None

    class BaseModel(Model):
        class Meta:
            database = SqliteDatabase(constants.get_gateway_database_file(),
                                      pragmas={'foreign_keys': 1})

    class Schedule(BaseModel):
        id = AutoField()
        source = CharField()  # Options: 'gateway' or 'thermostats'
        external_id = CharField(null=True)
        name = CharField()
        start = FloatField()
        repeat = CharField(null=True)
        duration = FloatField(null=True)
        end = FloatField(null=True)
        action = CharField()
        arguments = CharField(null=True)
        status = CharField()

        class Sources:
            GATEWAY = 'gateway'
            THERMOSTATS = 'thermostats'

        class Meta:
            indexes = (
                (('source', 'external_id'), True),
            )

    Schedule.delete() \
        .where(Schedule.source == Schedule.Sources.THERMOSTATS) \
        .execute()


def rollback(migrator, database, fake=False, **kwargs):
    # type: (Migrator, Database, bool, Dict[Any, Any]) -> None
    pass


add migration that removes thermostat entries from the schedules