<?php

/**
 * Number Field
 *
 * @package AmbassadorHub\CustomFields
 */

namespace AmbassadorHub\Fields\Fields;

use InvalidArgumentException;
use function esc_attr;
use function printf;
use function sprintf;

/**
 * Class representing a number custom field.
 */
final class NumberField extends AbstractField {

	/**
	 * Constructor.
	 *
	 * Initializes the number field with ID and label.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->id    = 'number';
		$this->label = __( 'Number Field', 'ambassadorhub' );

		parent::__construct();
	}

	/**
	 * Sanitize a number value.
	 *
	 * @param mixed $value The raw value to sanitize.
	 *
	 * @return ?int|float The sanitized number value.
	 */
	public function sanitize_value( $value ) {
		if ( is_numeric( $value ) ) {
			return (float) $value;
		}
		return null;
	}

	public function get_settings_fields(): array {
		return array(
			array(
				'type'  => 'number',
				'id'    => $this->id . '_min',
				'label' => __( 'Minimum Value', 'ambassadorhub' ),
			),
			array(
				'type'  => 'number',
				'id'    => $this->id . '_max',
				'label' => __( 'Maximum Value', 'ambassadorhub' ),
			),
			array(
				'type'  => 'number',
				'id'    => $this->id . '_step',
				'label' => __( 'Step Value', 'ambassadorhub' ),
			),
		);
	}

	/**
	 * Validate and process number field configuration.
	 *
	 * @param array $field Field configuration.
	 * @param array $data Raw input data.
	 *
	 * @return void Modifies $field array in-place.
	 *
	 * @throws InvalidArgumentException If validation fails.
	 */
	public function validate_settings( array $settings ): void {

		if ( isset( $settings[ $this->id . '_min' ] ) && ! is_numeric( $settings[ $this->id . '_min' ] ) ) {
			throw new InvalidArgumentException( esc_html__( 'Minimum value must be a number.', 'ambassadorhub' ) );
		}
		if ( isset( $settings[ $this->id . '_max' ] ) && ! is_numeric( $settings[ $this->id . '_max' ] ) ) {
			throw new InvalidArgumentException( esc_html__( 'Maximum value must be a number.', 'ambassadorhub' ) );
		}
		if ( isset( $settings[ $this->id . '_step' ] ) && ! is_numeric( $settings[ $this->id . '_step' ] ) ) {
			throw new InvalidArgumentException( esc_html__( 'Step value must be a number.', 'ambassadorhub' ) );
		}

		if ( isset( $settings[ $this->id . '_min' ] ) && isset( $settings[ $this->id . '_max' ] ) && $settings[ $this->id . '_min' ] > $settings[ $this->id . '_max' ] ) {
			throw new InvalidArgumentException( esc_html__( 'Minimum value cannot be greater than maximum value.', 'ambassadorhub' ) );
		}
	}

	/**
	 * Render frontend input for number fields.
	 *
	 * @param array  $field         Field definition.
	 * @param string $id            HTML id attribute.
	 * @param string $name          HTML name attribute.
	 * @param string $value         Current value.
	 * @param string $placeholder   Placeholder text.
	 * @param string $required_attr Required attribute string.
	 * @param string $class_attr    CSS class attribute string.
	 *
	 * @return void
	 */
	public function render_field_input(
		array $field,
		string $id,
		string $name,
		string $value,
		string $placeholder,
		bool $required,
		string $class_attr
	): void {
		$min_attr  = '';
		$max_attr  = '';
		$step_attr = '';
		if ( isset( $field['settings']['number_min'] ) && '' !== (string) $field['settings']['number_min'] && null !== $field['settings']['number_min'] ) {
			$min_attr = ' min="' . esc_attr( (string) $field['settings']['number_min'] ) . '"';
		}
		if ( isset( $field['settings']['number_max'] ) && '' !== (string) $field['settings']['number_max'] && null !== $field['settings']['number_max'] ) {
			$max_attr = ' max="' . esc_attr( (string) $field['settings']['number_max'] ) . '"';
		}
		if ( isset( $field['settings']['number_step'] ) && '' !== (string) $field['settings']['number_step'] && null !== $field['settings']['number_step'] ) {
			$step_attr = ' step="' . esc_attr( (string) $field['settings']['number_step'] ) . '"';
		}

		$class_attribute = '';
		if ( '' !== trim( $class_attr ) ) {
			$class_attribute = ' class="' . esc_attr( $class_attr ) . '"';
		}

		echo '<input type="number"'
			. ' id="' . esc_attr( $id ) . '"'
			. ' name="' . esc_attr( $name ) . '"'
			. $class_attribute // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped with esc_attr().
			. ' value="' . esc_attr( $value ) . '"'
			. ' placeholder="' . esc_attr( $placeholder ) . '"'
			. $min_attr // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped with esc_attr().
			. $max_attr // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped with esc_attr().
			. $step_attr // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped with esc_attr().
			. ( $required ? ' required' : '' )
			. ' />';
	}
}
