# global imports (dont't worry, red is normal)
import network_constants as netconst
from database.MongoRepositoryBase import MongoRepositoryBase
from db.entities.layer_adapter import LayerAdapter
from db.entities.use_case import UseCase

import pymongo
import json
from typing import List, Dict

class Repository(MongoRepositoryBase):
    '''This is a repository for MongoDb.'''

    def __init__(self):
        super().__init__(netconst.BUSINESS_LOGIC_DB_HOSTNAME,
                         netconst.BUSINESS_LOGIC_DB_PORT,
                         'rest-gateway-db')

        self._adapter_collection = 'layer_adapters'
        self._use_case_collection = 'use_cases'

    def delete_all_use_cases_with_name(self, name: str):
        collection = self._database[self._use_case_collection]
        collection.delete_many({"name": name})

    def delete_all_layers(self):
        collection = self._database[self._adapter_collection]
        collection.delete_many({})

    def delete_all_use_cases(self):
        collection = self._database[self._use_case_collection]
        collection.delete_many({})

    def all_use_cases(self) -> List[UseCase]:
        dicts = list(super().get_entries(self._use_case_collection, projection={'_id': False}))

        return [UseCase.from_serializable_dict(d) for d in dicts]

    def put_use_case(self, use_case_name: str):
        use_cases = self.all_use_cases()

        existing_use_cases = list(filter(lambda use_case: use_case.name == use_case_name, use_cases))

        if len(existing_use_cases) == 0:
            use_case = UseCase(use_case_name)
            super().insert_entry(self._use_case_collection, use_case.to_serializable_dict())

    def add(self, adapter : LayerAdapter):
        self.put_use_case(adapter.use_case)
        super().insert_entry(self._adapter_collection, adapter.to_serializable_dict())
    
    def one_by_name_and_usecase(self, name : str, use_case: str) -> LayerAdapter:
        return list(super().get_entries(self._adapter_collection, selection={"name": name, "use_case": use_case}))

    def update_use_case(self, adapter : LayerAdapter, use_case: str):
        collection = self._database[self._adapter_collection]
        collection.update_one({"name":adapter.name, "use_case": use_case}, {"$set": adapter.to_serializable_dict()})
    
    def update(self, adapter : LayerAdapter):
        collection = self._database[self._adapter_collection]
        collection.update_one({"name":adapter.name, "use_case": adapter.use_case}, {"$set": adapter.to_serializable_dict()})
    
    def delete_all_with_name_and_use_case(self, name: str, use_case: str):
        collection = self._database[self._adapter_collection]
        collection.delete_many({"name": name, "use_case": use_case})

    def all(self) -> List[Dict]:
        result = super().get_entries(self._adapter_collection, projection={'_id': False})

        return list(result)
    
    def all_for_use_case(self, use_case: str) -> List[Dict]:
        result = super().get_entries(self._adapter_collection, projection={'_id': False}, selection={"use_case": use_case})

        return list(result)