<?php

namespace Planaday\Entities\Booking\Form\Company\ContactPerson;

use Planaday\Entities\Booking\DTO\Address;
use Planaday\Entities\Booking\DTO\ContactPerson;
use Planaday\Entities\Booking\Form\Address\AddressFormFactory;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\LayoutItems\Header;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Validators\RegexValidator;
use Planaday\Utility\FormBuilder\Validators\Required;
use Planaday\Utility\RegexUtility;

#[Scoped]
class ContactPersonFormFactory {
	public function __construct(
		protected LocalizationController $localizationController,
		protected AddressFormFactory $addressFormFactory,
		protected ContactPersonSettings $settings,
	) {}

	/**
	 * @param ContactPerson $contactPerson
	 *
	 * @return BasicGroup
	 */
	public function getFormForObject(ContactPerson $contactPerson): BasicGroup{
		$t = function (string $text) {
			return $this->localizationController->translate($text);
		};

		$outputGroup = (new BasicGroup());

		$outputGroup->addContents(
			( new BasicGroup() )
			->addContents(
				( new StringInput() )
					->setIndex(ContactPersonFieldsEnum::FirstName->value)
					->setName( $t( "Voornaam" ) . " *" )
					->setDescription( $t( "De voornaam van het contactpersoon" ) )
					->setPlaceholder( $t( "Voornaam" ) )
					->setAutocomplete( "section-contact-person given-name" )
					->addSetCallback( function ( $value ) use ( $contactPerson ) {
						$contactPerson->setFirstName( $value ?? '' );
					})
					->addValidator(
						new Required( $t( "Voornaam is verplicht" ) )
					),
				( new StringInput() )
					->setIndex(ContactPersonFieldsEnum::Prefix->value)
					->setName( $t( "Tussenvoegsel" ) )
					->setDescription( $t( "Het tussenvoegsel van het contactpersoon" ) )
					->setPlaceholder( $t( "Tussenvoegsel" ) )
					->addSetCallback( function ( $value ) use ( $contactPerson ) {
						$contactPerson->setPrefix( $value ?? '' );
					}),
				( new StringInput() )
					->setIndex(ContactPersonFieldsEnum::LastName->value)
					->setName( $t( "Achternaam" ) . " *" )
					->setDescription( $t( "De achternaam van het contactpersoon" ) )
					->setPlaceholder( $t( "Achternaam" ) )
					->setAutocomplete( "section-contact-person family-name" )
					->addSetCallback( function ( $value ) use ( $contactPerson ) {
						$contactPerson->setLastName( $value ?? '' );
					})
					->addValidator(
						new Required( $t( "Achternaam is verplicht" ) )
					),
			)->addClasses("pad-full-name"),
		);

		if ( $this->settings->isAskForAddress() ){
			$address = $contactPerson->getAddress() ?? new Address();
			$contactPerson->setAddress($address);

			$outputGroup->addContents(
				( new BasicGroup() )
					->setIndex( ContactPersonFieldsEnum::Address->value )
					->addContents(
						new Header( $t( "Adres" ), 5),
						$this->addressFormFactory->getFormForObject($address, $this->settings->isAddressRequired(), "section-contact-person"),
					)
			);
		}

		$outputGroup->addContents(
			( new StringInput() )
				->setIndex(ContactPersonFieldsEnum::Email->value)
				->setName( $t( "E-mailadres" ) . " *" )
				->setDescription( $t( "Het e-mailadres van het contactpersoon" ) )
				->setPlaceholder( $t( "E-mailadres" ) )
				->setAutocomplete( "section-contact-person email" )
				->addSetCallback( function ( $value ) use ( $contactPerson ) {
					$contactPerson->setEmail( $value ?? '' );
				})
			->addValidator(
				new Required( $t( "E-mailadres is verplicht" ) ),
				new RegexValidator("email", RegexUtility::SIMPLE_EMAIL_REGEX, $t( "Het e-mailadres is niet geldig" ))
			)
		);

		if ( $this->settings->isAskForPhoneNumber() ){
			$isRequired = $this->settings->isPhoneNumberRequired();

			$outputGroup->addContents( $telefoonNummer =
				( new StringInput() )
					->setIndex(ContactPersonFieldsEnum::PhoneNumber->value)
					->setName( $t( "Telefoonnummer" ) . ( $isRequired ? " *" : "") )
					->setDescription( $t( "Het telefoonnummer van het contactpersoon" ) )
					->setPlaceholder( $t( "Telefoonnummer" ) )
					->setAutocomplete( "section-contact-person tel" )
					->addSetCallback( function ( $value ) use ( $contactPerson ) {
						$contactPerson->setPhoneNumber( $value ?? '' );
					})
			);

			if ( $isRequired ){
				$telefoonNummer->addValidator(
					new Required( $t( "Telefoonnummer is verplicht" ) )
				);
			}
		}

		return $outputGroup;
	}
}