import logging

from application.repository.api import APIRepository
from typing import Optional

logger = logging.getLogger(__name__)


class GetAndLockCouponInteractor(object):
    def __init__(
        self,            # type: GetAndLockCouponInteractor
        api_repository,  # type: APIRepository
    ):
        # type: (...) -> None

        self.api_repository = api_repository

    def get_and_lock_voucher(
        self,       # type: GetAndLockCouponInteractor
        pos_id,     # type: int
        coupon_id,  # type: str
    ):
        # type: (...) -> Optional[str]

        logger.info("Requesting and locking benefit for couponId: {}; PosId: {}".format(coupon_id, pos_id))
        benefit = self.api_repository.retrieve_benefit(
            pos_id=pos_id,
            coupon_id=coupon_id,
        )
        logger.info("Retrieved benefit:\n{}".format(benefit or ""))
        return benefit
