# This file is part of Karabo.
#
# http://www.karabo.eu
#
# Copyright (C) European XFEL GmbH Schenefeld. All rights reserved.
#
# Karabo is free software: you can redistribute it and/or modify it under
# the terms of the MPL-2 Mozilla Public License.
#
# You should have received a copy of the MPL-2 Public License along with
# Karabo. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
#
# Karabo 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.
#
# Model classes generated from the existing Sqlite database by pwiz,
# a model generator script bundled with peewee. The command to generate
# the model is:
#
#     python -m pwiz -e sqlite ${path_to_sqlite_db}
#
# All the STRING(max_size) fields in the database were generated by pwiz
# as UnknownField instances. This wouldn't work, so they were manually
# changed to be of type peewee.CharField(max_length=max_size) and the
# UnknownField class, which was only an empty bodied class generated by
# pwiz to work as a placeholder, has been removed.
#
# An auxiliary model class generated by pwiz, SqliteSequence, wasn't actually
# being used (probably required by some older peewee version?) and has been
# removed.
#
# pwiz also generates a BaseModel class that binds the models statically to
# the database connection. That was not suitable for the ConfigurationManager
# and has been replaced by a dynamic binding to the database from within
# 'ConfigurationManager.dbHandle.__enter__(self)'.
#
import datetime

from peewee import (
    SQL, BooleanField, CharField, Check, DateTimeField, ForeignKeyField,
    IntegerField, Model, TextField)


class ConfigSchema(Model):
    digest = CharField(max_length=40, unique=True)
    schema_data = TextField()

    class Meta:
        table_name = 'ConfigSchema'


class ConfigSet(Model):
    config_name = CharField(max_length=80, index=True)
    description = TextField(null=True)
    device_set_id = CharField(max_length=40, index=True)
    overwritable = BooleanField(constraints=[SQL("DEFAULT FALSE")])
    priority = IntegerField(constraints=[SQL("DEFAULT 1")],
                            index=True, null=True)
    user = CharField(max_length=64,
                     constraints=[SQL("DEFAULT '.'")])

    class Meta:
        table_name = 'ConfigSet'
        indexes = (
            (('device_set_id', 'config_name'), True),
        )
        constraints = [
            Check('priority > 0 AND priority < 4'),
        ]


class DeviceConfig(Model):
    config_data = TextField()
    config_set = ForeignKeyField(column_name='config_set_id',
                                 field='id', model=ConfigSet)
    device_id = CharField(max_length=1024, index=True)
    schema = ForeignKeyField(column_name='schema_id',
                             field='id', model=ConfigSchema)
    timestamp = DateTimeField(default=datetime.datetime.now)

    class Meta:
        table_name = 'DeviceConfig'
        indexes = (
            (('config_set', 'device_id'), True),
        )
