from typing import (
    Any,
    Dict,
)

from nreports.builders import DataBuilder
from nreports_api.shared import JSON


class BaseLoyaltyCoupon(DataBuilder):

    def build_data(
        self,
        data_to_build: Dict[str, JSON],
        **kwargs: Any,
    ) -> Dict[str, JSON]:

        raise NotImplementedError()

    def fill_constant_values(
        self,
        pos_id: int,
        order_id: int,
        data_to_build: Dict[str, JSON],
    ) -> None:

        data_to_build["LOYALTY_TEXT"] = self.constants.get("LOYALTY_TEXT", "")
        data_to_build["PRINT_LOYALTY_COUPON"] = data_to_build["LOYALTY_TEXT"] != ""

        loyalty_coupon_before_danfe = self.constants.get("LOYALTY_COUPON_DANFE_POSITION", "before").lower() == "before"
        data_to_build["LOYALTY_COUPON_BEFORE_DANFE"] = loyalty_coupon_before_danfe
        data_to_build["LOYALTY_COUPON_AFTER_DANFE"] = not loyalty_coupon_before_danfe

        order_service = self.order_api_repository.get_order_service(pos_id=pos_id)
        order_xml = order_service.get_order_picture(order_id=order_id)

        is_delivery_order = order_xml.get("saleType", "") == "3"
        allow_delivery_orders = self.constants.get("LOYALTY_COUPON_FOR_DELIVERY_ORDER", "false").lower() == "true"
        if is_delivery_order and allow_delivery_orders is False:
            data_to_build["LOYALTY_COUPON_BEFORE_DANFE"] = False
            data_to_build["LOYALTY_COUPON_AFTER_DANFE"] = False
