<?php
namespace WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\SiteHealth\Collectors;

/**
 * Abstract Collector Base Class
 *
 * Base class for all site health data collectors
 */
abstract class AbstractCollector
{
	/**
	 * Collect data for this category
	 *
	 * @return array Array of label/value pairs
	 */
	abstract public function collect(): array;

	/**
	 * Get the name of this collector
	 *
	 * @return string Collector name
	 */
	abstract public function getName(): string;

	/**
	 * Get the display label for this collector
	 *
	 * @return string Display label
	 */
	abstract public function getLabel(): string;

	/**
	 * Format a value for display
	 *
	 * @param mixed $value Value to format
	 * @return string Formatted value
	 */
	protected function formatValue($value): string
	{
		if (is_bool($value)) {
			return $value ? __('Yes', 'ultimate-security') : __('No', 'ultimate-security');
		}

		if (is_array($value)) {
			return implode(', ', $value);
		}

		if (is_null($value)) {
			return __('Not available', 'ultimate-security');
		}

		return (string) $value;
	}

	/**
	 * Create a data item
	 *
	 * @param string $label Label
	 * @param mixed $value Value
	 * @return array
	 */
	protected function item(string $label, $value): array
	{
		return [
			'label' => $label,
			'value' => $this->formatValue($value),
		];
	}

	/**
	 * Check if a value is available
	 *
	 * @param mixed $value Value to check
	 * @return bool
	 */
	protected function isAvailable($value): bool
	{
		return !is_null($value) && $value !== '' && $value !== false;
	}
}
