<?php

namespace PortalPeople\OrderWebhook\Ui\Component\Listing\Column;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

/**
 * Renders the NGauge status column in the admin orders grid.
 *
 * ✓  (green, links to NGauge)   — when ngauge_order_no is populated.
 * ✗  (red,   links to resend)   — when ngauge_order_no is missing.
 */
class NgaugeStatus extends Column
{
    /**
     * @var UrlInterface
     */
    private $urlBuilder;

    /**
     * @param ContextInterface   $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface       $urlBuilder
     * @param array              $components
     * @param array              $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * @inheritDoc
     */
    public function prepareDataSource(array $dataSource): array
    {
        if (!isset($dataSource['data']['items'])) {
            return $dataSource;
        }

        foreach ($dataSource['data']['items'] as &$item) {
            $orderId    = $item['entity_id'] ?? null;
            $ngaugeNo   = $item['ngauge_order_no']   ?? null;
            $ngaugeLink = $item['ngauge_order_link']  ?? null;

            if (!empty($ngaugeNo)) {
                // Synced — show green tick, linked to NGauge if a URL is available
                $label = htmlspecialchars($ngaugeNo, ENT_QUOTES, 'UTF-8');
                if (!empty($ngaugeLink)) {
                    $url = htmlspecialchars($ngaugeLink, ENT_QUOTES, 'UTF-8');
                    $html = sprintf(
                        '<a href="%s" target="_blank" title="%s" style="color:#28a745;font-size:20px;text-decoration:none;">&#10003;</a>',
                        $url,
                        $label
                    );
                } else {
                    $html = sprintf(
                        '<span title="%s" style="color:#28a745;font-size:20px;">&#10003;</span>',
                        $label
                    );
                }
            } else {
                // Not synced — show red cross linked to the resend action
                $sendUrl = $this->urlBuilder->getUrl(
                    'portalpeople_orderwebhook/order/resendwebhook',
                    ['order_id' => (int)$orderId]
                );
                $html = sprintf(
                    '<a href="%s" title="%s" style="color:#dc3545;font-size:20px;text-decoration:none;">&#10007;</a>',
                    htmlspecialchars($sendUrl, ENT_QUOTES, 'UTF-8'),
                    __('Send to NGauge')
                );
            }

            $item[$this->getData('name')] = $html;
        }

        return $dataSource;
    }
}
