import typing

from .product import product
from .reservation import reservation

class reservations_collection:
    def __init__(self) -> None:
        self.__content = list()

    def append(self, target: reservation) -> object:
        self.__content.append(target)
        return self

    def _filter(self, action: typing.Callable) -> object:
        new = list()

        for item in self.__content:
            if action(item):
                new.append(item)

        self.__content = new
        return self

    def by_target(self, target: product) -> object:
        if not target.in_database:
            raise reservation_exception("Target product must be in database.")

        return self._filter(lambda item: item.target_id == target.id)

    def by_email(self, target: str) -> object:
        return self._filter(lambda item: item.email == target)

    def by_phone_number(self, target: str) -> object:
        return self._filter(lambda item: item.phone_number == target)
    
    def results(self) -> list:
        return self.__content
