<?php
/**
 * SettingsApiRegistrar class.
 *
 * Registers setting sections and fields to the WordPress Settings API
 * using group and field data from the SettingsRegistry.
 *
 * @package Ilabs\Firevert\Core\Settings\Registrar
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Core\Settings\Registrar;

use Ilabs\Firevert\Core\Settings\Registry\SettingsRegistry;
use Ilabs\Firevert\Core\Settings\UI\SettingsFieldRenderer;

/**
 * Registers settings sections and fields using the WordPress Settings API.
 *
 * Relies on SettingsRegistry for group/field data,
 * and SettingsFieldRenderer for rendering fields.
 */
final class SettingsApiRegistrar {

	/**
	 * Settings registry instance.
	 *
	 * @var SettingsRegistry
	 */
	private SettingsRegistry $registry;

	/**
	 * Constructor.
	 *
	 * @param SettingsRegistry $registry Get from DI container.
	 */
	public function __construct( SettingsRegistry $registry ) {
		$this->registry = $registry;
	}

	/**
	 * Registers all sections and fields from SettingsRegistry to the WordPress Settings API.
	 *
	 * @return void
	 */
	public function register(): void {
		foreach ( $this->registry->get_groups() as $group_slug => $group ) {
			$this->register_section( $group_slug, $group );
			$this->register_fields( $group_slug, $group['fields'] ?? array() );
		}
	}

	/**
	 * Registers a single settings section.
	 *
	 * @param string $slug  Section slug.
	 * @param array  $group Group configuration array.
	 *
	 * @return void
	 */
	private function register_section( string $slug, array $group ): void {
		$is_custom = ! empty( $group['custom_view'] );

		if ( $is_custom ) {
			$view_name = is_string( $group['custom_view'] ) ? $group['custom_view'] : null;
			if ( ! $view_name && ! empty( $group['view'] ) && is_string( $group['view'] ) ) {
				$view_name = $group['view'];
			}
			if ( ! $view_name ) {
				$view_name = $slug . '.view.php';
			}

			$view_name = basename( $view_name );
			if ( ! preg_match( '/^[a-z0-9\-_\.]+$/i', $view_name ) ) {
				return;
			}

			if ( ! empty( $group['enqueue'] ) && is_callable( $group['enqueue'] ) ) {
				call_user_func( $group['enqueue'], $slug, $group );
			}

			add_settings_section(
				$slug . '_section',
				$group['title'] ?? '',
				static function () use ( $view_name ) {
					firevert_view( $view_name );
				},
				'firevert_' . $slug
			);
			return;
		}

		add_settings_section(
			$slug . '_section',
			$group['title'] ?? '',
			static function () use ( $group ) {
				if ( ! empty( $group['description'] ) ) {
					echo '<p>' . esc_html( $group['description'] ) . '</p>';
				}
			},
			'firevert_' . $slug
		);
	}

	/**
	 * Registers fields for a given group.
	 *
	 * @param string $group_slug Group slug.
	 * @param array  $fields     Array of field definitions.
	 *
	 * @return void
	 */
	private function register_fields( string $group_slug, array $fields ): void {
		foreach ( $fields as $field ) {
			add_settings_field(
				$field['id'],
				$field['label'],
				static function () use ( $field ) {
					SettingsFieldRenderer::render( $field );
				},
				'firevert_' . $group_slug,
				$group_slug . '_section'
			);
		}
	}
}
