<?php
declare(strict_types=1);

namespace Autogedal\BlackFriday\Block\Adminhtml\System\Config;

use Autogedal\BlackFriday\Model\SkuList;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

/** Read-only summary of the uploaded list under the upload field. */
class SkuListInfo extends Field
{
    public function __construct(Context $context, private readonly SkuList $skuList, array $data = [])
    {
        parent::__construct($context, $data);
    }

    protected function _getElementHtml(AbstractElement $element)
    {
        $import = $this->skuList->getLastImport();
        if (!$import) {
            return '<span>' . $this->escapeHtml(__('Nu a fost incarcata nicio lista.')) . '</span>';
        }
        $missing = (array)($import['missing'] ?? []);
        $html = $this->escapeHtml(__(
            '%1 SKU-uri in fisier, %2 gasite in catalog, incarcat la %3 (%4).',
            (int)($import['total'] ?? 0),
            (int)($import['imported'] ?? 0),
            (string)($import['date'] ?? ''),
            (string)($import['file'] ?? '')
        ));
        if ($missing) {
            $html .= '<br><strong>' . $this->escapeHtml(__('Inexistente in catalog (%1):', count($missing))) . '</strong> '
                . $this->escapeHtml(implode(', ', array_slice($missing, 0, 100)) . (count($missing) > 100 ? ' ...' : ''));
        }
        return '<span>' . $html . '</span>';
    }

    protected function _renderScopeLabel(AbstractElement $element)
    {
        return '';
    }
}
