<?php

namespace Easymailing\App\Core\Provider\Woocommerce\Service;

use Easymailing\App\Core\Provider\Woocommerce\Model\SynchronizationStatus;

class SynchronizationViewHandler
{
	public function getWidgetContent($status, $wpId, $type): string
	{
		$statusAlert = $this->getTableContent($status, $wpId, $type, true);
		return sprintf(
			'<div class="em-synchronization-widget-status">%s</div>',
			$statusAlert
		);
	}

	public function getTableContent($status, $wpItemId, $resource, bool $withButton = false): string
	{
		$alertType = SynchronizationStatus::getClass($status);
		$message   = SynchronizationStatus::getTitle($status);

		// Construir la parte del botón solo si se requiere.
		$buttonHtml = '';
		if ($withButton) {
			$buttonHtml = sprintf(
				'<div><button data-type="%s" data-id="%s" class="button em-synchronize-resource-button">%s</button></div>',
				esc_attr($resource),
				esc_attr($wpItemId),
				esc_html(__('Sincronizar', 'easymailing'))
			);
		}

		// Construir el HTML de alerta junto con el botón opcional.
		return sprintf(
			'<div class="em-table-widget">
                <div class="em-synchronization-alert em-synchronization-alert-%s" role="alert">%s</div>
                %s
            </div>',
			esc_attr($alertType),
			esc_html($message),
			$buttonHtml
		);
	}
}
