<?php

namespace Planaday\Entities\Booking\Validators;

use Planaday\Entities\Booking\DTO\Address;
use Planaday\Entities\Booking\PostRequest\AddressFieldEnum;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DataFilters\InvalidException;
use Planaday\Utility\DataFilters\TypeValidator;

class BookingAddressValidator {
	public function __construct(
		protected LocalizationController $localisationService,
		protected TypeValidator $typeValidator
	) {}

	public function getFromPost( array $postData, string $prefix = '', bool $isRequired = true ): Address|array {
		$t      = $this->localisationService;
		$type   = $this->typeValidator;
		$errors = [];

		try {
			$name = $type->str($postData[AddressFieldEnum::NAME] ?? null);
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::NAME] = $t->translate("De naam is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$street = $type->str($postData[AddressFieldEnum::STREET] ?? null);
			if ( $isRequired && empty($street)) {
				$errors[ AddressFieldEnum::STREET ] = $t->translate( 'Een straatnaam is verplicht bij het ' . $prefix . 'adres' );
			}
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::STREET] = $t->translate("De straat is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$houseNumber = $type->str($postData[AddressFieldEnum::HOUSE_NUMBER] ?? null);
			if ( $isRequired && empty($houseNumber) ) {
				$errors[ AddressFieldEnum::HOUSE_NUMBER ] = $t->translate( 'Een huisnummer is verplicht bij het ' . $prefix . 'adres' );
			}
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::HOUSE_NUMBER] = $t->translate("Het huisnummer is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$houseNumberExtension = $type->str($postData[AddressFieldEnum::HOUSE_NUMBER_EXTENSION] ?? null);
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::HOUSE_NUMBER_EXTENSION] = $t->translate("De huisnummer extensie is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$postalCode = $type->str($postData[AddressFieldEnum::POSTAL_CODE] ?? null);
			if ( $isRequired && empty($postalCode) ) {
				$errors[ AddressFieldEnum::POSTAL_CODE ] = $t->translate( 'Een postcode is verplicht bij het ' . $prefix . 'adres' );
			}
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::POSTAL_CODE] = $t->translate("De postcode is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$city = $type->str($postData[AddressFieldEnum::CITY] ?? null);
			if ( $isRequired && empty($city) ) {
				$errors[ AddressFieldEnum::CITY ] = $t->translate( 'Een stad is verplicht bij het ' . $prefix . 'adres' );
			}
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::CITY] = $t->translate("De stad is niet geldig bij het " . $prefix . "adres");
		}

		try {
			$country = $type->str($postData[AddressFieldEnum::COUNTRY] ?? null);
		} catch (InvalidException ) {
			$errors[AddressFieldEnum::COUNTRY] = $t->translate("Het land is niet geldig bij het " . $prefix . "adres");
		}


		if ( count( $errors ) > 0 ) {
			return $errors;
		}

		return (new Address())
			->setName($name ?? null)
			->setStreet($street ?? null)
			->setHouseNumber($houseNumber ?? null)
			->setHouseNumberExtension($houseNumberExtension ?? null)
			->setPostalCode($postalCode ?? null)
			->setCity($city ?? null)
			->setCountry($country ?? null);
	}
}