<?php

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

use Planaday\Entities\Booking\DTO\Address;
use Planaday\Entities\Booking\DTO\BookingInvoice;
use Planaday\Entities\Booking\Form\Address\AddressFormFactory;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Conditions\InputValueCondition;
use Planaday\Utility\FormBuilder\DataTypes\BoolType;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\InputFields\RadioInput;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\LayoutItems\Header;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Validators\Required;

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

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

		$outputGroup = (new BasicGroup());

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

			$addressGroup = ( new BasicGroup() )
				->setIndex( InvoiceFieldsEnum::Address->value )
				->addContents(
					new Header( $t( "Factuuradres"), 5)
				);

			$addressForm = $this->addressFormFactory->getFormForObject($address, $this->settings->isAddressRequired(), "work billing");

			if ( $this->settings->isShowSameAdressOption() ){
				$showSameAddressOption = ( new RadioInput( new BoolType() ) )
					->setIndex("toggle")
					->setName($t("Factuuradres hetzelfde?"))
					->setDescription($t("Moet het adres ook gebruikt worden als factuuradres?"))
					->setOptions([
						["value" => false, "label" => $t("Een ander factuuradres")],
						["value" => true, "label" => $t("Hetzelfde adres")],
					])
					->setDefaultValue( false )
					->addSetCallback( function ( $value ) use ( $bookingInvoice ) {
						if ( $value ) $bookingInvoice->setAddress( null );
					});

				$addressGroup->addContents($showSameAddressOption);
				$addressForm->addConditions(
					(new InputValueCondition($showSameAddressOption))->equals()->value(false)
				);
			}

			$addressGroup->addContents($addressForm);
			$outputGroup->addContents($addressGroup);
		}

		if ( $this->settings->isAskForEmail() ){
			$isRequired = $this->settings->isEmailRequired();
			$outputGroup->addContents( $email =
				(new StringInput() )
					->setIndex(InvoiceFieldsEnum::Email->value)
					->setName( $t( "Factuur email" ) . ( $isRequired ? " *" : "") )
					->setDescription( $t( "Het e-mailadres van de factuur" ) )
					->setPlaceholder( $t( "Factuur email" ) )
					->setAutocomplete( "work billing email" )
					->addSetCallback( function ( $value ) use ( $bookingInvoice ) {
						$bookingInvoice->setEmail( $value );
					})
			);

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

		return $outputGroup;
	}
}