from typing import Dict

from justificationdiscountreport.repository import JustificationDiscountRepository


class JustificationDiscountReport(object):
    def __init__(
        self,
        justification_discount_repository,   # type: JustificationDiscountRepository
    ):
        # type: (...) -> None

        self.justification_discount_repository = justification_discount_repository

    def get_justifications(
        self,
        initial_date,       # type: str
        end_date,           # type: str
        operator_id,        # type: str
    ):
        # type: (...) -> Dict

        justifications = self.justification_discount_repository.get_justifications(
            initial_date=initial_date,
            end_date=end_date,
            operator_id=operator_id,
        )

        result = {}
        list_order_date = list(dict.fromkeys([item.order_date for item in justifications]))
        for order_date in list_order_date:
            filtered_justifications = filter(
                (lambda items: items.order_date == order_date),
                justifications,
            )

            if len(filtered_justifications) > 0:
                result[str(order_date)] = filtered_justifications

        return result
