<?php
// components/Form/Presentation/CouponPresenter.php

namespace Components\Form\Presentation;

class CouponPresenter implements Presenter
{
    public static function render($value, array $def): string
    {
        $id = (string)$value;
        if ($id === '') { return \Components\Base::h(\mh_lng('UI_EM_DASH','—')); }
        $tpl = null;
        try {
            $prov = \Components\Data\DataRegistry::templateCoupons();
            $rows = $prov ? $prov->templates() : [];
            foreach ((array)$rows as $row) {
                if (is_array($row) && (string)($row['id'] ?? '') === $id) { $tpl = $row; break; }
            }
        } catch (\Throwable $e) { $tpl = null; }



        $title = $tpl && isset($tpl['typeLabel']) ? (string)$tpl['typeLabel'] : $id;
        $desc = $tpl && isset($tpl['codeMain']) ? (string)$tpl['codeMain'] : ($tpl && isset($tpl['code']) ? str_replace('template_', '', (string)$tpl['code']) : '-');
        $label = \Components\Base::h($title);
        $subTitle = \Components\Base::h($desc);
        $type = $tpl && isset($tpl['type']) ? (string)$tpl['type'] : '';
        $iconData = \Components\Models\Coupon::getIcon($type);
        $icon = $iconData['svg'];
        $iconColorToken = $iconData['color'] ?? 'orange';
        $iconVariant = $iconData['variant'] ?? 'tint';

//        if ($iconVariant === 'solid') {
//            $colorClass = \Components\Support\Palette::dotBg($iconColorToken) . ' text-white';
//        } else {
//            $colorClass = \Components\Support\Palette::classes($iconColorToken);
//        }

        $colorClasses = [
            'green'  => 'bg-green-600/70 text-green-100',
            'orange' => 'bg-orange-600/70 text-orange-100',
            'blue'   => 'bg-blue-600/70 text-blue-100',
        ];
        $colorClass = $colorClasses[$iconColorToken] ?? 'bg-orange-100 text-orange-600';



        $html = <<<HTML
<div class="relative flex items-center bg-white ring ring-divider/70 shrink-0 text-left rounded-lg p-1.5 pr-3 space-x-2 leading-none" >
  <div class="relative flex items-center justify-center rounded-md size-8 p-1.5 {$colorClass}">{$icon}</div>
  <div class="flex flex-col justify-between min-w-0 h-8 pt-0.5  ">
    <p class="text-secondary text-[11px]">{$subTitle} USED??</p>
    <p class="text-primary text-xs font-medium  truncate max-w-[12rem]">{$label}</p>
  </div>
</div>
HTML;
        return $html;
    }
}
