from typing import Dict, Optional

from dihttpmessageprocessor import AutoMappedDiRouterProcessorBuilder
from messageprocessor import MessageProcessor
from simplehttprouter import Route

from edpopenapibuilder.builder.dto import build_params
from ...interactor import AutoOpenApiYamlBuilderInteractor
from ...interactor.dto import AutoOpenApiYamlBuilderRequest


class DependencyInjectionApiYamlBuilderInteractor(AutoOpenApiYamlBuilderInteractor):
    def __init__(
        self,
        router_processor_builder: Optional[AutoMappedDiRouterProcessorBuilder] = None,
        routes: Optional[Dict[Route, MessageProcessor]] = None
    ):
        super().__init__(routes=routes)
        self.router_processor_builder = router_processor_builder

    def execute(self, request: AutoOpenApiYamlBuilderRequest) -> str:
        if self.router_processor_builder is not None:
            for route, definition in self.router_processor_builder.auto_mapped_definition.items():
                self.builder.add_interactor_path(
                    route_params=build_params(
                        route=route,
                        interactor_type=definition.interactor_type,
                        request_type=definition.request_type,
                        processor_type=definition.processor_type,
                        response_content_type=definition.response_content_type,
                        success_status=definition.success_status
                    )
                )

            for route, processor_type in self.router_processor_builder.route_definition.items():
                self.builder.add_processor_path(route=route, processor=processor_type)

        return super().execute(request=request)
