<?php

namespace Planaday\Config\General;

use Planaday\Config\BasicEnumMappedSetting;
use Planaday\Config\BasicSettingsLog;
use Planaday\Config\EnumMappedSettings;
use Planaday\Config\FormGroupable;
use Planaday\Config\General\Api\ApiOptionsEnum;
use Planaday\Config\General\Api\ApiSettings;
use Planaday\Config\General\Debug\DebugOptionsEnum;
use Planaday\Config\General\Debug\DebugSettings;
use Planaday\Config\General\Price\PriceOptionsEnum;
use Planaday\Config\General\Price\PriceSettings;
use Planaday\Config\GlobalSetting;
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\Structures\BasicGroup;
use Planaday\Utility\FormBuilder\Structures\TabbedGroups;

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

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

	public function __construct(
		protected LocalizationController $translator,
		protected SettingsLoader $settingsDatabase,
		protected PriceSettings $priceSettings,
		protected ApiSettings $apiSettings,
		protected DebugSettings $debugSettings
	) {
		$this->settingsDatabase->reloadData( $this, GeneralOptionsEnum::class );
	}

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

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

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

		return ( new BasicGroup() )
			->setIndex( GeneralOptionsEnum::class )
			->addContents(
				( new TabbedGroups() )
					->addTab(
						$t('API Instellingen'),
						$this->apiSettings->getFormGroup()
					)
					->addTab(
						$t('Prijs Instellingen'),
						$this->priceSettings->getFormGroup()
					)
					->addTab(
						$t('Debug Instellingen'),
						$this->debugSettings->getFormGroup()
					)
			);
	}

	/**
	 * @return array{values:mixed[], subGroups:array[], subGroupAliases:string[]}
	 */
	public function getEnumMapping(): array {
		return [
			"values"          => [],
			"subGroups"       => [
				GeneralOptionsEnum::PRICE_OPTIONS->value => $this->priceSettings->getEnumMapping(),
				GeneralOptionsEnum::API_SETTINGS->value  => $this->apiSettings->getEnumMapping(),
				GeneralOptionsEnum::DEBUG->value         => $this->debugSettings->getEnumMapping(),
			],
			"subGroupAliases" => [
				PriceOptionsEnum::class => GeneralOptionsEnum::PRICE_OPTIONS->value,
				ApiOptionsEnum::class   => GeneralOptionsEnum::API_SETTINGS->value,
				DebugOptionsEnum::class => GeneralOptionsEnum::DEBUG->value,
			]
		];
	}

	public function getUsageLog(): Collection {
		return new Collection(
			...$this->apiSettings->getUsageLog(),
			...$this->priceSettings->getUsageLog(),
			...$this->debugSettings->getUsageLog()
		);
	}
}