<?php

namespace Planaday\Entities\Booking\Form\Voucher;

use Planaday\Entities\Booking\DTO\BookingForm;
use Planaday\Entities\Booking\Form\BookingFieldsEnum;
use Planaday\Entities\Course\DTO\Course;
use Planaday\Platform\PlatformAbstraction\LocalizationController;
use Planaday\Platform\PlatformAbstraction\UrlHandler;
use Planaday\Utility\DependencyInjection\Attributes\Scoped;
use Planaday\Utility\FormBuilder\Form;
use Planaday\Utility\FormBuilder\InputFields\StringInput;
use Planaday\Utility\FormBuilder\LayoutItems\MessageDisplay;
use Planaday\Utility\FormBuilder\LayoutItems\SubmitButton;
use Planaday\Utility\FormBuilder\Structure;
use Planaday\Utility\FormBuilder\SubmitStrategies\RestSubmit;

#[Scoped]
class VoucherFormFactory {
	public function __construct(
		protected LocalizationController $localizationController,
		protected UrlHandler $urlHandler,
	) {}

	public function getFormForVoucher(BookingForm $bookingForm, Course $course): Structure{
		$t = function (string $text) {
			return $this->localizationController->translate($text);
		};

		return (new Form())
			->setSubmitBehaviour(
				(new RestSubmit())
					->setUrl($this->urlHandler->getRestApiPrefix() . 'voucher/check/course/' . $course->getId() . "/")
					->setSaveDataOnSuccess(true)
			)
			->setName('voucherForm')
			->addContents(
				new MessageDisplay(),
				(new StringInput())
					->setIndex(BookingFieldsEnum::Voucher->value)
					->setName($t("Kortingscode"))
					->setDescription($t("Voer hier uw kortingscode in"))
					->setPlaceholder($t("Kortingscode"))
					->addSetCallback(function (string|null $value) use ($bookingForm) {
						$bookingForm->setVoucherCode($value);
					}),
				new SubmitButton($t("Kortingscode toepassen")),
			);
	}
}