<?php
/**
 * Template Renderer Helper
 *
 * Provides reusable rendering utilities for admin page templates.
 * All output is properly escaped using WordPress escaping functions.
 *
 * @package Biscotti_CMP
 * @since   4.1.0
 */

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

/**
 * Class Biscotti_Template_Renderer
 *
 * Lightweight helper class providing reusable rendering utilities
 * for the local admin UI templates. All methods produce escaped HTML output.
 */
class Biscotti_Template_Renderer {

	/**
	 * Render a stat card (used on Dashboard).
	 *
	 * Outputs a card element displaying a label, value, and optional dashicon.
	 *
	 * @since 4.1.0
	 *
	 * @param string $label Card label (will be escaped).
	 * @param mixed  $value Card value (will be escaped).
	 * @param string $icon  Optional. Dashicon class name (e.g. 'dashicons-chart-bar'). Default empty.
	 * @return void
	 */
	public static function stat_card( $label, $value, $icon = '' ) {
		?>
		<div class="biscotti-stat-card">
			<?php if ( ! empty( $icon ) ) : ?>
				<span class="biscotti-stat-icon dashicons <?php echo esc_attr( $icon ); ?>"></span>
			<?php endif; ?>
			<span class="biscotti-stat-value"><?php echo esc_html( $value ); ?></span>
			<span class="biscotti-stat-label"><?php echo esc_html( $label ); ?></span>
		</div>
		<?php
	}

	/**
	 * Render a data table from an array of rows.
	 *
	 * Outputs an HTML table with column headers and escaped row data.
	 * Supports an optional CSS class for custom styling.
	 *
	 * @since 4.1.0
	 *
	 * @param array  $columns     Column definitions as associative array ['key' => 'Label'].
	 * @param array  $rows        Array of associative arrays with data matching column keys.
	 * @param string $table_class Optional. Additional CSS class for the table element. Default empty.
	 * @return void
	 */
	public static function data_table( $columns, $rows, $table_class = '' ) {
		$classes = 'widefat biscotti-data-table';
		if ( ! empty( $table_class ) ) {
			$classes .= ' ' . sanitize_html_class( $table_class );
		}
		?>
		<table class="<?php echo esc_attr( $classes ); ?>">
			<thead>
				<tr>
					<?php foreach ( $columns as $key => $label ) : ?>
						<th scope="col"><?php echo esc_html( $label ); ?></th>
					<?php endforeach; ?>
				</tr>
			</thead>
			<tbody>
				<?php if ( empty( $rows ) ) : ?>
					<tr>
						<td colspan="<?php echo esc_attr( count( $columns ) ); ?>">
							<?php esc_html_e( 'No data available.', 'biscotti-cmp' ); ?>
						</td>
					</tr>
				<?php else : ?>
					<?php foreach ( $rows as $row ) : ?>
						<tr>
							<?php foreach ( $columns as $key => $label ) : ?>
								<td><?php echo esc_html( $row[ $key ] ?? '' ); ?></td>
							<?php endforeach; ?>
						</tr>
					<?php endforeach; ?>
				<?php endif; ?>
			</tbody>
		</table>
		<?php
	}

	/**
	 * Render a form field.
	 *
	 * Supports text, select, toggle (checkbox), color, textarea, and number field types.
	 * All values are properly escaped for their rendering context.
	 *
	 * @since 4.1.0
	 *
	 * @param string $type    Field type: 'text', 'select', 'toggle', 'color', 'textarea', 'number'.
	 * @param string $name    Field name attribute.
	 * @param mixed  $value   Current field value.
	 * @param array  $options {
	 *     Optional. Additional field options.
	 *
	 *     @type string $label       Field label text.
	 *     @type string $description Help text displayed below the field.
	 *     @type string $placeholder Placeholder text for text/textarea fields.
	 *     @type array  $choices     Associative array of value => label for select fields.
	 *     @type string $class       Additional CSS class for the input element.
	 *     @type int    $min         Minimum value for number fields.
	 *     @type int    $max         Maximum value for number fields.
	 * }
	 * @return void
	 */
	public static function form_field( $type, $name, $value, $options = array() ) {
		$label       = $options['label'] ?? '';
		$description = $options['description'] ?? '';
		$placeholder = $options['placeholder'] ?? '';
		$choices     = $options['choices'] ?? array();
		$class       = $options['class'] ?? '';
		?>
		<tr class="biscotti-form-field">
			<?php if ( ! empty( $label ) ) : ?>
				<th scope="row">
					<label for="biscotti-field-<?php echo esc_attr( $name ); ?>">
						<?php echo esc_html( $label ); ?>
					</label>
				</th>
			<?php endif; ?>
			<td>
				<?php
				switch ( $type ) {
					case 'text':
						self::render_text_field( $name, $value, $placeholder, $class );
						break;
					case 'number':
						self::render_number_field( $name, $value, $options );
						break;
					case 'select':
						self::render_select_field( $name, $value, $choices, $class );
						break;
					case 'toggle':
						self::render_toggle_field( $name, $value, $class );
						break;
					case 'color':
						self::render_color_field( $name, $value, $class );
						break;
					case 'textarea':
						self::render_textarea_field( $name, $value, $placeholder, $class );
						break;
				}

				if ( ! empty( $description ) ) :
					?>
					<p class="description"><?php echo esc_html( $description ); ?></p>
					<?php
				endif;
				?>
			</td>
		</tr>
		<?php
	}

	/**
	 * Render a premium link page.
	 *
	 * Displays a feature description and a link to the Biscotti Dashboard.
	 * No API calls, no gated UI, no disabled buttons.
	 *
	 * @since 4.1.0
	 *
	 * @param string $feature_name  Feature title (will be escaped).
	 * @param string $description   Feature description (will be escaped).
	 * @param string $dashboard_path Path on app.biscotti-cmp.com (e.g. 'ab-testing').
	 * @return void
	 */
	public static function premium_link_page( $feature_name, $description, $dashboard_path ) {
		$dashboard_url = 'https://app.biscotti-cmp.com/' . ltrim( $dashboard_path, '/' );
		?>
		<div class="biscotti-page biscotti-premium-info">
			<h1><?php echo esc_html( $feature_name ); ?></h1>
			<div class="biscotti-info-card">
				<p><?php echo esc_html( $description ); ?></p>
				<a href="<?php echo esc_url( $dashboard_url ); ?>" target="_blank" rel="noopener" class="button button-primary">
					<?php esc_html_e( 'Open in Biscotti Dashboard', 'biscotti-cmp' ); ?> &rarr;
				</a>
			</div>
		</div>
		<?php
	}

	/**
	 * Render an admin notice.
	 *
	 * Outputs a WordPress-standard admin notice with the specified type and message.
	 *
	 * @since 4.1.0
	 *
	 * @param string $message Notice text (will be escaped).
	 * @param string $type    Optional. Notice type: 'info', 'success', 'warning', 'error'. Default 'info'.
	 * @return void
	 */
	public static function admin_notice( $message, $type = 'info' ) {
		$allowed_types = array( 'info', 'success', 'warning', 'error' );
		if ( ! in_array( $type, $allowed_types, true ) ) {
			$type = 'info';
		}
		?>
		<div class="notice notice-<?php echo esc_attr( $type ); ?> is-dismissible">
			<p><?php echo esc_html( $message ); ?></p>
		</div>
		<?php
	}

	/**
	 * Render a text input field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name        Field name attribute.
	 * @param string $value       Current value.
	 * @param string $placeholder Placeholder text.
	 * @param string $class       Additional CSS class.
	 * @return void
	 */
	private static function render_text_field( $name, $value, $placeholder, $class ) {
		$input_class = 'regular-text';
		if ( ! empty( $class ) ) {
			$input_class .= ' ' . sanitize_html_class( $class );
		}
		?>
		<input
			type="text"
			id="biscotti-field-<?php echo esc_attr( $name ); ?>"
			name="<?php echo esc_attr( $name ); ?>"
			value="<?php echo esc_attr( $value ); ?>"
			placeholder="<?php echo esc_attr( $placeholder ); ?>"
			class="<?php echo esc_attr( $input_class ); ?>"
		/>
		<?php
	}

	/**
	 * Render a number input field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name    Field name attribute.
	 * @param mixed  $value   Current value.
	 * @param array  $options Field options including min, max.
	 * @return void
	 */
	private static function render_number_field( $name, $value, $options ) {
		$class = 'small-text';
		if ( ! empty( $options['class'] ) ) {
			$class .= ' ' . sanitize_html_class( $options['class'] );
		}
		$min = isset( $options['min'] ) ? intval( $options['min'] ) : '';
		$max = isset( $options['max'] ) ? intval( $options['max'] ) : '';
		?>
		<input
			type="number"
			id="biscotti-field-<?php echo esc_attr( $name ); ?>"
			name="<?php echo esc_attr( $name ); ?>"
			value="<?php echo esc_attr( $value ); ?>"
			class="<?php echo esc_attr( $class ); ?>"
			<?php if ( '' !== $min ) : ?>
				min="<?php echo esc_attr( $min ); ?>"
			<?php endif; ?>
			<?php if ( '' !== $max ) : ?>
				max="<?php echo esc_attr( $max ); ?>"
			<?php endif; ?>
		/>
		<?php
	}

	/**
	 * Render a select dropdown field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name    Field name attribute.
	 * @param string $value   Current selected value.
	 * @param array  $choices Associative array of value => label.
	 * @param string $class   Additional CSS class.
	 * @return void
	 */
	private static function render_select_field( $name, $value, $choices, $class ) {
		$select_class = '';
		if ( ! empty( $class ) ) {
			$select_class = sanitize_html_class( $class );
		}
		?>
		<select
			id="biscotti-field-<?php echo esc_attr( $name ); ?>"
			name="<?php echo esc_attr( $name ); ?>"
			<?php if ( ! empty( $select_class ) ) : ?>
				class="<?php echo esc_attr( $select_class ); ?>"
			<?php endif; ?>
		>
			<?php foreach ( $choices as $choice_value => $choice_label ) : ?>
				<option value="<?php echo esc_attr( $choice_value ); ?>" <?php selected( $value, $choice_value ); ?>>
					<?php echo esc_html( $choice_label ); ?>
				</option>
			<?php endforeach; ?>
		</select>
		<?php
	}

	/**
	 * Render a toggle (checkbox) field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name  Field name attribute.
	 * @param mixed  $value Current value (truthy = checked).
	 * @param string $class Additional CSS class.
	 * @return void
	 */
	private static function render_toggle_field( $name, $value, $class ) {
		$input_class = 'biscotti-toggle';
		if ( ! empty( $class ) ) {
			$input_class .= ' ' . sanitize_html_class( $class );
		}
		?>
		<label class="<?php echo esc_attr( $input_class ); ?>">
			<input
				type="checkbox"
				id="biscotti-field-<?php echo esc_attr( $name ); ?>"
				name="<?php echo esc_attr( $name ); ?>"
				value="1"
				<?php checked( ! empty( $value ) ); ?>
			/>
			<span class="biscotti-toggle-slider"></span>
		</label>
		<?php
	}

	/**
	 * Render a color picker field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name  Field name attribute.
	 * @param string $value Current hex color value.
	 * @param string $class Additional CSS class.
	 * @return void
	 */
	private static function render_color_field( $name, $value, $class ) {
		$input_class = 'biscotti-color-picker';
		if ( ! empty( $class ) ) {
			$input_class .= ' ' . sanitize_html_class( $class );
		}
		?>
		<input
			type="text"
			id="biscotti-field-<?php echo esc_attr( $name ); ?>"
			name="<?php echo esc_attr( $name ); ?>"
			value="<?php echo esc_attr( $value ); ?>"
			class="<?php echo esc_attr( $input_class ); ?>"
			data-default-color="<?php echo esc_attr( $value ); ?>"
		/>
		<?php
	}

	/**
	 * Render a textarea field.
	 *
	 * @since 4.1.0
	 *
	 * @param string $name        Field name attribute.
	 * @param string $value       Current value.
	 * @param string $placeholder Placeholder text.
	 * @param string $class       Additional CSS class.
	 * @return void
	 */
	private static function render_textarea_field( $name, $value, $placeholder, $class ) {
		$textarea_class = 'large-text';
		if ( ! empty( $class ) ) {
			$textarea_class .= ' ' . sanitize_html_class( $class );
		}
		?>
		<textarea
			id="biscotti-field-<?php echo esc_attr( $name ); ?>"
			name="<?php echo esc_attr( $name ); ?>"
			placeholder="<?php echo esc_attr( $placeholder ); ?>"
			class="<?php echo esc_attr( $textarea_class ); ?>"
			rows="4"
		><?php echo esc_textarea( $value ); ?></textarea>
		<?php
	}
}
