from application.model.configuration import Configurations
from application.model.customexception import build_message_exception
from application.repository.api import APIRepository
from typing import Optional


class GetLoyaltyIdInteractor(object):
    def __init__(self, configs, api_repository):
        # type: (Configurations, APIRepository) -> None

        self.logger = configs.logger
        self.configs = configs
        self.api_repository = api_repository

    def get_loyalty_id(self, loyalty_customer_id):
        # type: (str) -> Optional[str]

        try:
            self.logger.info("Requesting customer loyaltySystemId")
            loyalty_id = self.api_repository.retrieve_loyalty_customer_info(loyalty_customer_id)
            if not loyalty_id:
                self.logger.info("No customer loyalty data retrieved")
            else:
                self.logger.info("Retrieved customer loyalty data: {}".format(loyalty_id))

            return loyalty_id
        except Exception as ex:
            self.logger.error("Error at get_loyalty_id: {}".format(ex))
            build_message_exception(ex)
            raise
