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 GetAndLockVoucherInteractor(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_and_lock_voucher(self, pos_id, voucher_id):
        # type: (int, str) -> Optional[str]

        try:
            self.logger.info("Requesting and locking benefit for voucherId: {}; PosId: {}".format(voucher_id, pos_id))
            benefit = self.api_repository.retrieve_benefit(pos_id, voucher_id)
            self.logger.info("Retrieved benefit:\n{}".format(benefit or ""))
            return benefit
        except Exception as ex:
            self.logger.error("Error at get_and_lock_voucher: {}".format(ex))
            build_message_exception(ex)
            raise
