<?php

namespace Planaday\Config\Booking\Form\Address;

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\InputFields\BoolInput;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;

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

	protected bool $askForNumberExtension = true;

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

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

		return ( new BasicGroup() )
			->setIndex( AddressOptionsEnum::class )
			->addContents(
				(new BoolInput())
					->setIndex( AddressOptionsEnum::ASK_FOR_NUMBER_EXTENSION->value )
					->setName( $t( "Huisnummer extensie vragen?" ) )
					->setDescription( $t( "Moet er bij een adres, een huisnummer extensie gevraagd worden?" ) )
					->setDefaultValue( $this->askForNumberExtension )
					->addSetCallback( function ( $value ) {
						$this->askForNumberExtension = $value ?? $this->askForNumberExtension;
					})
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [
				AddressOptionsEnum::ASK_FOR_NUMBER_EXTENSION->value => &$this->askForNumberExtension,
			],
			"subGroups"       => [],
			"subGroupAliases" => []
		];
	}

	public function isAskForNumberExtension(): bool {
		$this->logUse( AddressOptionsEnum::ASK_FOR_NUMBER_EXTENSION );

		return $this->askForNumberExtension;
	}
}