<?php
/**
 * Admin Field Helpers Trait
 *
 * Shared form field rendering helpers for admin settings pages.
 *
 * @package Niwa_AI
 * @since   0.10.25
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

trait Niwa_AI_Admin_Field_Helpers {

	/**
	 * Render a checkbox row.
	 *
	 * @param string $n     Option name prefix.
	 * @param string $key   Field key.
	 * @param string $label Row label.
	 * @param array  $opts  Current options.
	 * @param string $desc  Optional description.
	 */
	protected function checkbox_row( $n, $key, $label, $opts, $desc = '' ) {
		$checked = ! empty( $opts[ $key ] ) ? 'checked' : '';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><label><input type="checkbox" name="' . esc_attr( $n . '[' . $key . ']' ) . '" value="1" ' . $checked . ' /> ';
		echo esc_html( $desc ?: $label ) . '</label></td></tr>';
	}

	/**
	 * Render a text input row.
	 *
	 * @param string $n     Option name prefix.
	 * @param string $key   Field key.
	 * @param string $label Row label.
	 * @param array  $opts  Current options.
	 * @param string $desc  Optional description.
	 */
	protected function text_row( $n, $key, $label, $opts, $desc = '' ) {
		$value = isset( $opts[ $key ] ) ? $opts[ $key ] : '';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><input type="text" name="' . esc_attr( $n . '[' . $key . ']' ) . '" value="' . esc_attr( $value ) . '" class="regular-text" />';
		if ( $desc ) {
			echo '<p class="description">' . esc_html( $desc ) . '</p>';
		}
		echo '</td></tr>';
	}

	/**
	 * Render a number input row.
	 *
	 * @param string $n     Option name prefix.
	 * @param string $key   Field key.
	 * @param string $label Row label.
	 * @param array  $opts  Current options.
	 * @param int    $min   Minimum value.
	 * @param int    $max   Maximum value.
	 */
	protected function number_row( $n, $key, $label, $opts, $min = 0, $max = 9999 ) {
		$value = isset( $opts[ $key ] ) ? (int) $opts[ $key ] : 0;
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><input type="number" name="' . esc_attr( $n . '[' . $key . ']' ) . '" value="' . esc_attr( $value ) . '" min="' . esc_attr( $min ) . '" max="' . esc_attr( $max ) . '" class="small-text" /></td></tr>';
	}

	/**
	 * Render a textarea row.
	 *
	 * @param string $n     Option name prefix.
	 * @param string $key   Field key.
	 * @param string $label Row label.
	 * @param array  $opts  Current options.
	 * @param string $desc  Optional description.
	 */
	protected function textarea_row( $n, $key, $label, $opts, $desc = '' ) {
		$value = isset( $opts[ $key ] ) ? $opts[ $key ] : '';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><textarea name="' . esc_attr( $n . '[' . $key . ']' ) . '" rows="3" class="large-text">' . esc_textarea( $value ) . '</textarea>';
		if ( $desc ) {
			echo '<p class="description">' . esc_html( $desc ) . '</p>';
		}
		echo '</td></tr>';
	}

	/**
	 * Render a select row.
	 *
	 * @param string $n       Option name prefix.
	 * @param string $key     Field key.
	 * @param string $label   Row label.
	 * @param array  $opts    Current options.
	 * @param array  $choices Associative array of value => label.
	 */
	protected function select_row( $n, $key, $label, $opts, $choices ) {
		$current = isset( $opts[ $key ] ) ? $opts[ $key ] : '';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><select name="' . esc_attr( $n . '[' . $key . ']' ) . '">';
		foreach ( $choices as $val => $text ) {
			echo '<option value="' . esc_attr( $val ) . '" ' . selected( $current, $val, false ) . '>' . esc_html( $text ) . '</option>';
		}
		echo '</select></td></tr>';
	}

	/**
	 * Render a color input row.
	 *
	 * @param string $n     Option name prefix.
	 * @param string $key   Field key.
	 * @param string $label Row label.
	 * @param array  $opts  Current options.
	 */
	protected function color_row( $n, $key, $label, $opts ) {
		$value = isset( $opts[ $key ] ) ? $opts[ $key ] : '#374151';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><input type="color" name="' . esc_attr( $n . '[' . $key . ']' ) . '" value="' . esc_attr( $value ) . '" /></td></tr>';
	}

	/**
	 * Render a URL input row.
	 *
	 * @param string $n           Option name prefix.
	 * @param string $key         Field key.
	 * @param string $label       Row label.
	 * @param array  $opts        Current options.
	 * @param string $placeholder Optional placeholder text.
	 */
	protected function url_row( $n, $key, $label, $opts, $placeholder = 'https://example.com/' ) {
		$value = isset( $opts[ $key ] ) ? $opts[ $key ] : '';
		echo '<tr><th scope="row">' . esc_html( $label ) . '</th>';
		echo '<td><input type="url" name="' . esc_attr( $n . '[' . $key . ']' ) . '" value="' . esc_attr( $value ) . '" class="large-text" placeholder="' . esc_attr( $placeholder ) . '" /></td></tr>';
	}
}
