<?php

namespace Planaday\Config\OptionGroups\Booking;

use Planaday\Config\OptionGroups\Booking\Form\BookingFormOptionsValidator;
use Planaday\Config\OptionGroups\Booking\Submission\BookingSubmissionOptionsValidator;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Utility\DataFilters\InvalidFieldsException;
use Planaday\Utility\DataFilters\TypeValidator;
use Planaday\Utility\Debug\Log\Logging;

class BookingOptionsValidator {
	use Logging;

	/** @var BookingFormOptionsValidator */
	private $bookingFormOptionsValidator;

	/** @var BookingSubmissionOptionsValidator */
	private $submissionOptionsValidator;

	/** @var TypeValidator */
	private $typeValidator;

	/** @var LocalizationController */
	private $translator;

	public function __construct(
		TypeValidator $typeValidator,
		LocalizationController $translator,
		BookingFormOptionsValidator $bookingFormOptionsValidator,
		BookingSubmissionOptionsValidator $submissionOptionsValidator
	) {
		$this->bookingFormOptionsValidator            = $bookingFormOptionsValidator;
		$this->submissionOptionsValidator             = $submissionOptionsValidator;
		$this->typeValidator            = $typeValidator;
		$this->translator               = $translator;
	}

	/**
	 * @param array $data
	 * @param array|null $output if valid, clean data is returned here. if not, errors will be returned here
	 *
	 * @return bool true if valid, false if not
	 */
	public function validate( array $data, ?array &$output): bool {
		$errors = [];
		$tr = $this->translator;

		try {
			$data = $this->getSafeData( $data );
		} catch ( InvalidFieldsException $e ) {
			foreach ( $e->getInvalidFields() as $field => $fieldException ) {
				$errors[ $field ] = $tr->translate(
					'Het ' . strtolower($field) . ' veld bij de boeking opties is met ongeldige data ingevuld.'
				);
			}

			$output = $errors;
			return false;
		}

		if (!$this->bookingFormOptionsValidator->validate($data[BookingOptionsEnum::BOOKING_FORM] ?? [], $bookingFormOutput)) {
			$errors[BookingOptionsEnum::BOOKING_FORM . '-isValid'] = $tr->translate('De boekingsformulier opties zijn niet geldig.');
			$errors[BookingOptionsEnum::BOOKING_FORM] = $bookingFormOutput;
		} else {
			$data[BookingOptionsEnum::BOOKING_FORM] = $bookingFormOutput;
		}

		if (!$this->submissionOptionsValidator->validate($data[BookingOptionsEnum::SUBMISSION] ?? [], $submissionOutput)) {
			$errors[BookingOptionsEnum::SUBMISSION . '-isValid'] = $tr->translate('De boeking indien opties zijn niet geldig.');
			$errors[BookingOptionsEnum::SUBMISSION] = $submissionOutput;
		} else {
			$data[BookingOptionsEnum::SUBMISSION] = $submissionOutput;
		}

		if ( count( $errors ) > 0 ) {
			$output = $errors;
			$this->logWarning("data is not valid", $errors);
			return false;
		}

		$output = $data;
		return true;
	}

	/**
	 * @param array $data
	 *
	 * @return array
	 * @throws InvalidFieldsException
	 */
	private function getSafeData(array $data): array {
		$tv = $this->typeValidator;

		$arrays = [
			BookingOptionsEnum::BOOKING_FORM,
			BookingOptionsEnum::SUBMISSION
		];

		return $tv->getSafeData($data, [], $arrays);
	}

}