<?php

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

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Conditions\InputValueCondition;
use Planaday\Utility\FormBuilder\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;

#[Scoped]
class InvoiceSettings implements EnumMappedSettings, SettingsWithForm, SettingsWithUsageLog {
	use FormGroupable;
	use BasicEnumMappedSetting;
	use BasicSettingsLog;

	protected bool $askForAddress = true;
	protected bool $askForEmail = true;
	protected bool $emailRequired = false;

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase
	) {
		$this->settingsDatabase->reloadData($this, InvoiceOptionsEnum::class );
	}

	protected function createFormGroup(): BasicGroup {
		$t = function ( string $string ) {
			return $this->translator->translate( $string );
		};

		return ( new BasicGroup() )
			->setIndex( InvoiceOptionsEnum::class )
			->addContents(
				(new BoolInput())
					->setIndex( InvoiceOptionsEnum::ASK_FOR_ADDRESS->value )
					->setName( $t( "Factuuradres vragen?" ) )
					->setDescription( $t( "Moet er een factuuradres gevraagd worden?" ) )
					->setDefaultValue( $this->askForAddress )
					->addSetCallback( function ( $value ) {
						$this->askForAddress = $value ?? $this->askForAddress;
					}),
				$emailAsked = (new BoolInput())
					->setIndex( InvoiceOptionsEnum::ASK_FOR_EMAIL->value )
					->setName( $t( "Factuur email vragen?" ) )
					->setDescription( $t( "Moet er een factuur email gevraagd worden?" ) )
					->setDefaultValue( $this->askForEmail )
					->addSetCallback( function ( $value ) {
						$this->askForEmail = $value ?? $this->askForEmail;
					}),
				(new BasicGroup())
					->addConditions(
						(new InputValueCondition($emailAsked))->equals()->value(true)
					)->addContents(
						(new BoolInput())
							->setIndex( InvoiceOptionsEnum::EMAIL_REQUIRED->value )
							->setName( $t( "Factuur email verplicht?" ) )
							->setDescription( $t( "Moet de factuur email verplicht zijn?" ) )
							->setDefaultValue( $this->emailRequired )
							->addSetCallback( function ( $value ) {
								$this->emailRequired = $value ?? $this->emailRequired;
							})
					)->addClasses("pad-subgroup")
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				InvoiceOptionsEnum::ASK_FOR_ADDRESS->value => &$this->askForAddress,
				InvoiceOptionsEnum::ASK_FOR_EMAIL->value => &$this->askForEmail,
				InvoiceOptionsEnum::EMAIL_REQUIRED->value => &$this->emailRequired,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isAskForAddress(): bool {
		$this->logUse( InvoiceOptionsEnum::ASK_FOR_ADDRESS );
		return $this->askForAddress;
	}

	public function isAskForEmail(): bool {
		$this->logUse( InvoiceOptionsEnum::ASK_FOR_EMAIL );
		return $this->askForEmail;
	}

	public function isEmailRequired(): bool {
		$this->logUse( InvoiceOptionsEnum::EMAIL_REQUIRED );
		return $this->emailRequired;
	}
}