<?php
/**
 * SettingsFieldRenderer class.
 *
 * Responsible for rendering settings fields
 *
 * @package Ilabs\Firevert\Core\Settings\UI
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Settings\UI;

use Ilabs\Firevert\Core\Settings\Form\SettingsStorageService;

/**
 * Renders individual fields in the Firevert settings panel.
 */
final class SettingsFieldRenderer {

	/**
	 * Renders a single field based on its type.
	 *
	 * @param array $field Field definition (must include 'type', 'id', 'slug').
	 *
	 * @return void
	 */
	public static function render( array $field ): void {
		if ( ! isset( $field['type'], $field['id'], $field['slug'] ) ) {
			return;
		}

		if ( 'checkbox' !== $field['type'] ) {
			return;
		}

		/**
		 * Get from DI container.
		 *
		 * @var SettingsStorageService $storage
		 */
		$storage     = firevert_container()->get( 'settings_storage' );
		$module_slug = $field['slug'];
		$key         = $field['id'];

		$value = $storage->get_module_setting( $module_slug, $key, '' );

		printf(
			'<input type="checkbox" name="firevert_settings[modules][%s][%s]" value="1" %s />',
			esc_attr( $module_slug ),
			esc_attr( $key ),
			checked( $value, '1', false )
		);
	}
}
