# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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.

import logging
from typing import Dict, List, Optional, Tuple
from common.orm.Database import Database
from common.orm.HighLevel import get_object
from common.orm.backend.Tools import key_to_str
from common.orm.fields.EnumeratedField import EnumeratedField
from common.orm.fields.ForeignKeyField import ForeignKeyField
from common.orm.fields.PrimaryKeyField import PrimaryKeyField
from common.orm.fields.StringField import StringField
from common.orm.model.Model import Model
from common.proto.context_pb2 import EndPointId
from .DeviceModel import DeviceModel
from .KpiSampleType import ORM_KpiSampleTypeEnum, grpc_to_enum__kpi_sample_type
from .TopologyModel import TopologyModel

LOGGER = logging.getLogger(__name__)

class EndPointModel(Model):
    pk = PrimaryKeyField()
    topology_fk = ForeignKeyField(TopologyModel, required=False)
    device_fk = ForeignKeyField(DeviceModel)
    endpoint_uuid = StringField(required=True, allow_empty=False)
    endpoint_type = StringField()

    def delete(self) -> None:
        for db_kpi_sample_type_pk,_ in self.references(KpiSampleTypeModel):
            KpiSampleTypeModel(self.database, db_kpi_sample_type_pk).delete()
        super().delete()

    def dump_id(self) -> Dict:
        device_id = DeviceModel(self.database, self.device_fk).dump_id()
        result = {
            'device_id': device_id,
            'endpoint_uuid': {'uuid': self.endpoint_uuid},
        }
        if self.topology_fk is not None:
            result['topology_id'] = TopologyModel(self.database, self.topology_fk).dump_id()
        return result

    def dump_kpi_sample_types(self) -> List[int]:
        db_kpi_sample_type_pks = self.references(KpiSampleTypeModel)
        return [KpiSampleTypeModel(self.database, pk).dump() for pk,_ in db_kpi_sample_type_pks]

    def dump(   # pylint: disable=arguments-differ
            self, include_kpi_sample_types=True
        ) -> Dict:
        result = {
            'endpoint_id': self.dump_id(),
            'endpoint_type': self.endpoint_type,
        }
        if include_kpi_sample_types: result['kpi_sample_types'] = self.dump_kpi_sample_types()
        return result

class KpiSampleTypeModel(Model): # pylint: disable=abstract-method
    pk = PrimaryKeyField()
    endpoint_fk = ForeignKeyField(EndPointModel)
    kpi_sample_type = EnumeratedField(ORM_KpiSampleTypeEnum, required=True)

    def dump(self) -> Dict:
        return self.kpi_sample_type.value

def set_kpi_sample_types(database : Database, db_endpoint : EndPointModel, grpc_endpoint_kpi_sample_types):
    db_endpoint_pk = db_endpoint.pk
    for kpi_sample_type in grpc_endpoint_kpi_sample_types:
        orm_kpi_sample_type = grpc_to_enum__kpi_sample_type(kpi_sample_type)
        str_endpoint_kpi_sample_type_key = key_to_str([db_endpoint_pk, orm_kpi_sample_type.name])
        db_endpoint_kpi_sample_type = KpiSampleTypeModel(database, str_endpoint_kpi_sample_type_key)
        db_endpoint_kpi_sample_type.endpoint_fk = db_endpoint
        db_endpoint_kpi_sample_type.kpi_sample_type = orm_kpi_sample_type
        db_endpoint_kpi_sample_type.save()

def get_endpoint(
    database : Database, grpc_endpoint_id : EndPointId,
    validate_topology_exists : bool = True, validate_device_in_topology : bool = True
) -> Tuple[str, EndPointModel]:
    endpoint_uuid                  = grpc_endpoint_id.endpoint_uuid.uuid
    endpoint_device_uuid           = grpc_endpoint_id.device_id.device_uuid.uuid
    endpoint_topology_uuid         = grpc_endpoint_id.topology_id.topology_uuid.uuid
    endpoint_topology_context_uuid = grpc_endpoint_id.topology_id.context_id.context_uuid.uuid
    str_endpoint_key = key_to_str([endpoint_device_uuid, endpoint_uuid])

    if len(endpoint_topology_context_uuid) > 0 and len(endpoint_topology_uuid) > 0:
        # check topology exists
        str_topology_key = key_to_str([endpoint_topology_context_uuid, endpoint_topology_uuid])
        if validate_topology_exists:
            from .TopologyModel import TopologyModel
            get_object(database, TopologyModel, str_topology_key)

        # check device is in topology
        str_topology_device_key = key_to_str([str_topology_key, endpoint_device_uuid], separator='--')
        if validate_device_in_topology:
            from .RelationModels import TopologyDeviceModel
            get_object(database, TopologyDeviceModel, str_topology_device_key)

        str_endpoint_key = key_to_str([str_endpoint_key, str_topology_key], separator=':')

    db_endpoint : EndPointModel = get_object(database, EndPointModel, str_endpoint_key)
    return str_endpoint_key, db_endpoint
