<?php

namespace Planaday\Config\Booking;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\Booking\Form\BookingFormOptionsEnum;
use Planaday\Config\Booking\Form\FormSettings;
use Planaday\Config\Booking\Submission\BookingSubmissionOptionsEnum;
use Planaday\Config\Booking\Submission\SubmissionSettings;
use Planaday\Config\Booking\Voucher\VoucherSettings;
use Planaday\Config\Booking\Voucher\VoucherSettingsEnum;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FeatureToggles\FeatureToggleSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\GlobalSetting;
use Planaday\Config\SettingsLoader;
use Planaday\Config\SettingsWithForm;
use Planaday\Config\SettingsWithUsageLog;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\DataGrouping\Collection;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Structures\TabbedGroups;

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

	public static function getIndex(): string {
		return '-booking-options';
	}

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase,
		protected UrlHandler $urlHandler,
		protected FormSettings $formSettings,
		protected SubmissionSettings $submissionSettings,
		protected VoucherSettings $voucherSettings,
		protected FeatureToggleSettings $featureToggleSettings
	) {
		$this->settingsDatabase->reloadData( $this, BookingOptionsEnum::class );
	}

	public function getTitle(): string {
		return $this->translator->translate('Boeking Instellingen');
	}

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

		$tabs = new TabbedGroups();

		if ($this->featureToggleSettings->showShortcodeSettings()) $tabs->addTab(
			$t('Boekingsformulier Instellingen'),
			$this->formSettings->getFormGroup()
		);

		$tabs->addTab(
			$t('Boeking Indien Instellingen'),
			$this->submissionSettings->getFormGroup()
		);

		$tabs->addTab(
		     $t('Kortingscode Instellingen'),
		     $this->voucherSettings->getFormGroup()
		);

		return ( new BasicGroup() )
			->setIndex( BookingOptionsEnum::class )
			->addContents(
				$tabs
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [],
			"subGroups"       => [
				BookingOptionsEnum::BOOKING_FORM->value => $this->formSettings->getEnumMapping(),
				BookingOptionsEnum::SUBMISSION->value   => $this->submissionSettings->getEnumMapping(),
				BookingOptionsEnum::Voucher->value      => $this->voucherSettings->getEnumMapping(),
			],
			"subGroupAliases" => [
				BookingFormOptionsEnum::class => BookingOptionsEnum::BOOKING_FORM->value,
				BookingSubmissionOptionsEnum::class   => BookingOptionsEnum::SUBMISSION->value,
				VoucherSettingsEnum::class => BookingOptionsEnum::Voucher->value,
			]
		];
	}

	public function save(): void {
		$this->settingsDatabase->saveData( $this );
		$this->urlHandler->flushRewriteRules();
	}

	public function getFormSettings(): FormSettings {
		return $this->formSettings;
	}

	public function getSubmissionSettings(): SubmissionSettings {
		return $this->submissionSettings;
	}

	public function getUsageLog(): Collection {
		return new Collection(
			...$this->formSettings->getUsageLog(),
			...$this->submissionSettings->getUsageLog(),
			...$this->voucherSettings->getUsageLog()
		);
	}
}