<?php
/**
 * 2026 Paysera
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author Paysera <plugins@paysera.com>
 * @copyright 2026 Paysera
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of Paysera
 */

declare(strict_types=1);

namespace Paysera\PrestashopPayments\Form;

use Paysera\PrestashopPayments\Provider\PaymentSettingsDataProvider;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\NotBlank;

if (!defined('_PS_VERSION_')) {
    exit;
}

final class PaymentSettingsFormType extends TranslatorAwareType
{
    private PaymentSettingsDataProvider $dataProvider;

    public function __construct(
        PaymentSettingsDataProvider $dataProvider,
        $translator,
        array $locales = []
    ) {
        parent::__construct($translator, $locales);
        $this->dataProvider = $dataProvider;
    }

    /**
     * @param array<string, mixed> $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $this->buildMainSettingsFields($builder);
        $this->buildOrderStatusFields($builder);
        $this->buildDisplaySettingsFields($builder);
    }

    private function buildMainSettingsFields(FormBuilderInterface $builder): void
    {
        $builder
            ->add('enable_payments', SwitchType::class, [
                'label' => $this->trans('Enable payments', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'When disabled, Paysera will be hidden from the checkout page and no payments can be processed.',
                    'Modules.Payserapayments.Global'
                ),
                'required' => false,
                'choices' => [
                    $this->trans('Yes', 'Modules.Payserapayments.Global') => true,
                    $this->trans('No', 'Modules.Payserapayments.Global') => false,
                ],
            ])
            ->add('payment_window_mode', ChoiceType::class, [
                'label' => $this->trans('Payment window mode', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'Determine how the payment page will be displayed when a customer proceeds to payment.',
                    'Modules.Payserapayments.Global'
                ),
                'choices' => [
                    $this->trans('New tab', 'Modules.Payserapayments.Global') => PaymentSettingsDataConfiguration::PAYMENT_WINDOW_MODE_SAME_PAGE,
                    $this->trans('Pop-up window', 'Modules.Payserapayments.Global') => PaymentSettingsDataConfiguration::PAYMENT_WINDOW_MODE_NEW_WINDOW,
                ],
                'expanded' => true,
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'Payment window mode is required.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                    new Choice([
                        'choices' => PaymentSettingsDataConfiguration::ALLOWED_PAYMENT_WINDOW_MODES,
                        'message' => $this->trans(
                            'Invalid payment window mode selected.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                ],
            ])
        ;
    }

    private function buildOrderStatusFields(FormBuilderInterface $builder): void
    {
        $builder
            ->add('status_awaiting_confirmation', ChoiceType::class, [
                'label' => $this->trans('Awaiting Paysera Confirmation', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'Order status after finishing checkout',
                    'Modules.Payserapayments.Global'
                ),
                'choices' => $this->dataProvider->getStatusAwaitingConfirmationChoices(),
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'Awaiting confirmation status is required.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                ],
            ])
            ->add('status_confirmed_payment', ChoiceType::class, [
                'label' => $this->trans('Paysera confirmed payment', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'Order status for confirmed payment',
                    'Modules.Payserapayments.Global'
                ),
                'choices' => $this->dataProvider->getStatusConfirmedPaymentChoices(),
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'Confirmed payment status is required.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                ],
            ])
            ->add('status_awaiting_payment', ChoiceType::class, [
                'label' => $this->trans('Awaiting Paysera payment', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'Order status for pending payment',
                    'Modules.Payserapayments.Global'
                ),
                'choices' => $this->dataProvider->getStatusAwaitingPaymentChoices(),
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'Awaiting payment status is required.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                ],
            ])
        ;
    }

    private function buildDisplaySettingsFields(FormBuilderInterface $builder): void
    {
        $builder
            ->add('payment_title', TextType::class, [
                'label' => $this->trans('Payment title', 'Modules.Payserapayments.Global'),
                'help' => $this->trans(
                    'This title will be displayed on the checkout page',
                    'Modules.Payserapayments.Global'
                ),
                'required' => true,
                'constraints' => [
                    new NotBlank([
                        'message' => $this->trans(
                            'Payment title is required.',
                            'Modules.Payserapayments.Global'
                        ),
                    ]),
                ],
            ])
        ;
    }
}
