<?php

namespace Planaday\Config\Booking\Submission;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\Booking\Submission\Mail\MailOptionsEnum;
use Planaday\Config\Booking\Submission\Mail\MailSettings;
use Planaday\Config\Booking\Submission\Redirecting\RedirectOptionsEnum;
use Planaday\Config\Booking\Submission\Redirecting\RedirectSettings;
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\DataGrouping\Collection;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\LayoutItems\Text;
use Planaday\Utility\FormBuilder\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Structures\CollapsibleGroup;

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

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase,
		protected MailSettings $mailSettings,
		protected RedirectSettings $redirectSettings
	) {
		$this->settingsDatabase->reloadData($this, BookingSubmissionOptionsEnum::class );
	}

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

		return ( new BasicGroup() )
			->setIndex( BookingSubmissionOptionsEnum::class )
			->addContents(
				new Text($t("Instellingen voor de acties na het indienen van een boeking")),
				(new CollapsibleGroup())
					->setHeader($t("E-mail"))
					->setDescription($t("Instellingen voor de mails die mogelijk na een boeking gestuurd kunnen worden"))
					->addContents(
						$this->mailSettings->getFormGroup()
					),
				(new CollapsibleGroup())
					->setHeader($t("Redirect"))
					->setDescription($t("Instellingen voor waar de cursist naar wordt gestuurd na het indienen van een boeking."))
					->addContents(
						$this->redirectSettings->getFormGroup()
					)
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [],
			"subGroups"       => [
				BookingSubmissionOptionsEnum::MAIL->value => $this->mailSettings->getEnumMapping(),
				BookingSubmissionOptionsEnum::REDIRECT->value => $this->redirectSettings->getEnumMapping()
			],
			"subGroupAliases" => [
				MailOptionsEnum::class => BookingSubmissionOptionsEnum::MAIL->value,
				RedirectOptionsEnum::class => BookingSubmissionOptionsEnum::REDIRECT->value,
			]
		];
	}

	public function getMailSettings(): MailSettings {
		return $this->mailSettings;
	}

	public function getRedirectSettings(): RedirectSettings {
		return $this->redirectSettings;
	}

	public function getUsageLog(): Collection{
		return new Collection(
			...$this->mailSettings->getUsageLog(),
			...$this->redirectSettings->getUsageLog()
		);
	}
}