from .dto.request import ApplyBenefitRequest
from ..service import BenefitApplierService
from ....model.repository.benefit_applier.exception import BenefitApplierNotFound
from ....repository.pos.utils import get_benefit_applier_by_name


class ApplyBenefitInteractor(object):

    def __init__(
        self,                           # type: ApplyBenefitInteractor
        benefit_applier_service,        # type: BenefitApplierService
    ):
        # type: (...) -> None

        self.benefit_applier_service = benefit_applier_service

    def apply_benefit(
        self,                   # type: ApplyBenefitInteractor
        apply_benefit_request,  # type: ApplyBenefitRequest
    ):
        # type: (...) -> None

        self._validate_benefit_applier(apply_benefit_request)

        self.benefit_applier_service.apply_benefit(
            benefit=apply_benefit_request.benefit,
            context=apply_benefit_request.context,
            benefit_applier=apply_benefit_request.context.applier,
        )

    @staticmethod
    def _validate_benefit_applier(apply_benefit_request):
        if apply_benefit_request.context.applier is None:
            raise BenefitApplierNotFound("")
        benefit_applier = get_benefit_applier_by_name(apply_benefit_request.context.applier)
        if benefit_applier is None:
            raise BenefitApplierNotFound(apply_benefit_request.context.applier)
