from logging import Logger

from interactor.route.menu.get_product_info.dto import GetProductInfoRequest
from model.config import Config
from model.error_type import PosValidationErrorsType
from model.exception import ValidationError
from model.repository import PosRepositoryModel
from model.services import ProductInfoServiceModel
from validation_util import validate_api_key


class GetProductInfoInteractor(object):

    def __init__(
        self,
        logger,                 # type: Logger
        config,                 # type: Config
        pos_repository,         # type: PosRepositoryModel
        product_info_service    # type: ProductInfoServiceModel
    ):
        # type: (...) -> None

        self.logger = logger
        self.config = config
        self.pos_repository = pos_repository
        self.product_info_service = product_info_service

    def execute(
        self,
        request     # type: GetProductInfoRequest
    ):
        # type: (...) -> str

        validate_api_key(
            request=request,
            secure_api_key_list=self.config.api_key_mapping
        )

        product_info = self.product_info_service.get_product_info(language=request.language)

        if not product_info:
            raise ValidationError(PosValidationErrorsType.ProductInfoNotFound)

        return product_info
