<?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\Provider;

use PrestaShop\PrestaShop\Adapter\LegacyContext;
use Symfony\Contracts\Translation\TranslatorInterface;

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

/**
 * Provides data for Payment Settings UI.
 * Responsible for generating options for selects, tabs configuration, etc.
 */
class PaymentSettingsDataProvider
{
    private int $languageId;

    public function __construct(
        private TranslatorInterface $translator,
        LegacyContext $legacyContext
    ) {
        $this->languageId = (int) $legacyContext->getContext()->language->id;
    }

    /**
     * Get tabs configuration for Payment Settings page.
     *
     * @return array<array{id: string, label: string, content_id: string}>
     */
    public function getSettingsTabs(): array
    {
        return [
            [
                'id' => 'main_settings',
                'label' => $this->trans('Main settings'),
                'content_id' => 'tab-content-main-settings',
            ],
            [
                'id' => 'order_status',
                'label' => $this->trans('Order status'),
                'content_id' => 'tab-content-order-status',
            ],
            [
                'id' => 'display_settings',
                'label' => $this->trans('Display settings'),
                'content_id' => 'tab-content-display-settings',
            ],
        ];
    }

    /**
     * Get order status choices for "Awaiting Confirmation" mapping.
     *
     * @return array<string, string> Label => Value format for Symfony ChoiceType
     */
    public function getStatusAwaitingConfirmationChoices(): array
    {
        return $this->getAllOrderStatusChoices();
    }

    public function getStatusConfirmedPaymentChoices(): array
    {
        return $this->getAllOrderStatusChoices();
    }

    public function getStatusAwaitingPaymentChoices(): array
    {
        return $this->getAllOrderStatusChoices();
    }

    public function getPaymentTitleChoices(): array
    {
        return [
            $this->trans('Pay with Paysera') => 'pay_with_paysera',
            $this->trans('Paysera Payment') => 'paysera_payment',
            $this->trans('Pay Online') => 'pay_online',
            $this->trans('Card Payment') => 'card_payment',
        ];
    }

    private function getAllOrderStatusChoices(): array
    {
        if (!class_exists('OrderState')) {
            return [];
        }

        $orderStates = \OrderState::getOrderStates($this->languageId);

        $choices = [];
        foreach ($orderStates as $state) {
            $choices[$state['name']] = (string) $state['id_order_state'];
        }

        return $choices;
    }

    private function trans(string $string): string
    {
        return $this->translator->trans($string, [], 'Modules.Payserapayments.Global');
    }
}
