<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Block\Info;

use Magento\Framework\DataObject;
use Magento\Payment\Block\Info;

/**
 * Payment info block for the SimplyIN method.
 *
 * Surfaces the actual payment details captured on the order (method e.g. BLIK/card, provider e.g.
 * PayU, status, transaction id) in the "Payment Information" section — admin order view, storefront
 * order view, invoices and order e-mails — instead of only the method title. All values come from
 * the payment additional_information; no card number/PAN is stored or shown (no PCI exposure).
 */
class Simplyin extends Info
{
    /**
     * Human-readable labels for the known payment-method slugs.
     *
     * @var array<string, string>
     */
    private const METHOD_LABELS = [
        'blik' => 'BLIK',
        'card' => 'Card',
        'pay_by_link' => 'Pay by link',
        'transfer' => 'Bank transfer',
    ];

    /**
     * Append SimplyIN payment details to the rendered payment information.
     *
     * @param DataObject|null $transport
     * @return DataObject
     */
    protected function _prepareSpecificInformation($transport = null): DataObject
    {
        $transport = parent::_prepareSpecificInformation($transport);
        $info = $this->getInfo();

        $rows = [];
        $this->addRow($rows, (string)__('Payment method'), $this->resolveMethod($info));
        $this->addRow($rows, (string)__('Payment provider'), (string)$info->getAdditionalInformation('simplyin_payment_provider_name'));
        $this->addRow($rows, (string)__('Payment status'), (string)$info->getAdditionalInformation('simplyin_payment_status'));
        $this->addRow(
            $rows,
            (string)__('Transaction ID'),
            (string)($info->getAdditionalInformation('simplyin_transaction_id')
                ?: $info->getAdditionalInformation('simplyin_provider_payment_id'))
        );

        return $transport->setData(array_merge($transport->getData(), $rows));
    }

    /**
     * Resolve the display name of the payment method (BLIK/card/…) from additional_information.
     *
     * @param \Magento\Payment\Model\InfoInterface $info
     * @return string
     */
    private function resolveMethod($info): string
    {
        $name = (string)$info->getAdditionalInformation('simplyin_payment_method_name');
        if ($name !== '') {
            return $name;
        }

        $slug = strtolower((string)$info->getAdditionalInformation('simplyin_payment_method'));

        return self::METHOD_LABELS[$slug] ?? ($slug !== '' ? ucfirst($slug) : '');
    }

    /**
     * Add a label/value row only when the value is non-empty.
     *
     * @param array $rows
     * @param string $label
     * @param string $value
     * @return void
     */
    private function addRow(array &$rows, string $label, string $value): void
    {
        if ($value !== '') {
            $rows[$label] = $value;
        }
    }
}
