from customer_services.interactor.add_customer.dto import AddCustomerRequest
from customer_services.model import CustomerExistsException, Customer, Address, CustomerNonExistsException
from customer_services.model.repository import CustomerRepository
from customer_services.service import GetCustomerService


class AddCustomerInteractor:
    def __init__(self, customer_repository, get_customer_service):
        # type: (CustomerRepository, GetCustomerService) -> None

        self.customer_repository = customer_repository
        self.get_customer_service = get_customer_service
    
    def add_customer(self, request):
        # type: (AddCustomerRequest) -> None
        
        customer = None
        try:
            customer = self.get_customer_service.get_customer(request.phone)
        except CustomerNonExistsException as _:
            pass
        
        if customer:
            raise CustomerExistsException()
        
        address = Address(
            request.address_id,
            request.zip_code,
            request.state,
            request.city,
            request.street,
            request.neighborhood,
            request.number,
            request.complement,
            request.reference_point
        )
        customer = Customer(request.phone, request.document, request.name, address)
        self.customer_repository.add_customer(customer)
