from logging import Logger

from model.config import Config
from model.error_type import ExternalServicesErrorsType
from model.exception import ExternalServiceErrors
from model.repository import PosRepositoryModel
from interactor.route.store.get_status.dto import (
    GetStoreResponse,
    GetStoreRequest
)
from validation_util import validate_api_key


class GetStoreInteractor(object):
    def __init__(self, logger, config, pos_repository):
        # type: (Logger, Config, PosRepositoryModel) -> None

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

    def execute(self, request):
        # type: (GetStoreRequest) -> GetStoreResponse

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

        status = None
        if self.config.nf_type == 'NFCE':
            status = self.pos_repository.get_sefaz_status()
        elif self.config.nf_type == 'SAT':
            status = self.pos_repository.get_sat_status(pos_id)

        if not status:
            raise ExternalServiceErrors(ExternalServicesErrorsType.UnableToGetStatus)

        menu_id = self.pos_repository.get_catalog_version()
        if not menu_id:
            raise ExternalServiceErrors(ExternalServicesErrorsType.UnableToGetMenuId)

        return GetStoreResponse(status, menu_id)
