# 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.proto.context_pb2 import EndPointId
from .KpiSampleType import ORM_KpiSampleTypeEnum, grpc_to_enum__kpi_sample_type
from sqlalchemy import Column, ForeignKey, String, Enum, ForeignKeyConstraint
from sqlalchemy.dialects.postgresql import UUID, ARRAY
from context.service.database.Base import Base
from sqlalchemy.orm import relationship
LOGGER = logging.getLogger(__name__)

class EndPointModel(Base):
    __tablename__ = 'EndPoint'
    topology_uuid = Column(UUID(as_uuid=False), ForeignKey("Topology.topology_uuid"), primary_key=True)
    device_uuid = Column(UUID(as_uuid=False), ForeignKey("Device.device_uuid", ondelete='CASCADE'), primary_key=True)
    endpoint_uuid = Column(UUID(as_uuid=False), primary_key=True, unique=True)
    endpoint_type = Column(String)

    # Relationships
    kpi_sample_types = relationship("KpiSampleTypeModel", passive_deletes=True, back_populates="EndPoint")
    device = relationship("DeviceModel", back_populates="endpoints")

    @staticmethod
    def main_pk_name():
        return 'endpoint_uuid'

    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:
        result = {
            'device_id': self.device.dump_id(),
            'endpoint_uuid': {'uuid': self.endpoint_uuid},
        }
        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]
        response = []
        for a in self.kpi_sample_types:
            response.append(a.dump())
        return response

    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(Base): # pylint: disable=abstract-method
    __tablename__ = 'KpiSampleType'
    kpi_uuid = Column(UUID(as_uuid=False), primary_key=True)
    endpoint_uuid = Column(UUID(as_uuid=False), ForeignKey("EndPoint.endpoint_uuid",  ondelete='CASCADE'))
    kpi_sample_type = Column(Enum(ORM_KpiSampleTypeEnum, create_constraint=False,
                                                  native_enum=False))
    # __table_args__ = (ForeignKeyConstraint([endpoint_uuid], [EndPointModel.endpoint_uuid]), {})

    # Relationships
    EndPoint = relationship("EndPointModel", passive_deletes=True, back_populates="kpi_sample_types")

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

    def main_pk_name(self):
        return 'kpi_uuid'

"""
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
