from logging import getLogger
from typing import Dict, Optional, Type, Union
from unittest import TestCase

import samplehttpmodule
from dependencyinjection import DependencyLoader
from dihttpmessageprocessor import (AutoMappedDiRouterProcessorBuilder,
                                    AutoMappedWebApi)
from messageprocessor import MessageProcessor
from simplehttprouter import Route

import edpopenapibuilder
from edpopenapibuilder.interactor.dto import AutoOpenApiYamlBuilderRequest
from edpopenapibuilder.interactor.injection import (
    DependencyInjectionApiYamlBuilderInteractor
)

_LOGGER = getLogger(__name__)

class TestDependencyInjectionApiYamlBuilderInteractor(TestCase):
    def setUp(self):
        self.dependencies_loader = DependencyLoader()
        self.dependencies_loader.load_modules(
            modules=[
                samplehttpmodule,
                edpopenapibuilder
            ],
        )

    def _get_router_builder(
        self,
        route_definition: Optional[Dict[Route, Union[Type[MessageProcessor], AutoMappedWebApi]]] = None
    ) -> AutoMappedDiRouterProcessorBuilder:
        router_builder = AutoMappedDiRouterProcessorBuilder(
            dependency_context=self.dependencies_loader.build_context(),
            route_definition=route_definition or {},
        )
        return router_builder

    def test_execute(self):
        router_processor_builder = self._get_router_builder()
        interactor = DependencyInjectionApiYamlBuilderInteractor(
            router_processor_builder=router_processor_builder
        )
        result = interactor.execute(
            request=AutoOpenApiYamlBuilderRequest()
        )
        _LOGGER.info(f"\n{result}")
