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

namespace Simplyin\Payment\Block\Adminhtml\System\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Module\PackageInfo;
use Simplyin\Payment\Model\Config\EnvironmentConfig;

/**
 * Read-only admin field showing the installed plugin version, prefixed with the backend environment
 * for non-production builds (e.g. "dev 1.3.10", "stage 1.3.10", "preprod 1.3.10"); production shows
 * the bare version ("1.3.10"). Environment comes from {@see EnvironmentConfig} (build-time, pinned by
 * the deploy pipeline); the version from the module's composer.json via {@see PackageInfo}.
 */
class PluginVersion extends Field
{
    private const MODULE_NAME = 'Simplyin_Payment';

    /**
     * @param Context $context
     * @param PackageInfo $packageInfo
     * @param EnvironmentConfig $environmentConfig
     * @param array $data
     */
    public function __construct(
        Context $context,
        private readonly PackageInfo $packageInfo,
        private readonly EnvironmentConfig $environmentConfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    /**
     * Render the installed plugin version, environment-prefixed for non-production builds.
     *
     * @param AbstractElement $element
     * @return string
     */
    protected function _getElementHtml(AbstractElement $element): string
    {
        $version = (string)$this->packageInfo->getVersion(self::MODULE_NAME);
        $environment = $this->environmentConfig->getEnvironment();
        $label = $environment === 'prod' ? $version : $environment . ' ' . $version;

        return '<span class="simplyin-plugin-version" style="font-weight:600;">'
            . $this->escapeHtml($label)
            . '</span>';
    }
}
