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

from dependencyinjection import DependencyLoader
from dihttpmessageprocessor import (
    AutoMappedDiRouterProcessorBuilder
)

import edpopenapibuilder.injection
import samplehttpmodule
from edpopenapibuilder.injection.interactor import DependencyInjectionApiYamlBuilderInteractor
from edpopenapibuilder.interactor.dto import AutoOpenApiYamlBuilderRequest
from samplehttpmodule.route import add_descriptors

_LOGGER = getLogger(__name__)


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

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

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