<?php
/**
 * Classic (pre-Gutenberg) sidebar widget that wraps the [findastra] shortcode.
 *
 * Lets buyers drop FindAstra into any registered sidebar without editing
 * theme files. Outputs through the shortcode renderer so styling stays consistent.
 *
 * @package FindAstra
 */

defined( 'ABSPATH' ) || exit;

final class FindAstra_Widget extends WP_Widget {

	public function __construct() {
		parent::__construct(
			'findastra_widget',
			__( 'FindAstra — Semantic Search', 'findastra' ),
			array(
				'description' => __( 'AI semantic product search box for WooCommerce.', 'findastra' ),
				'classname'   => 'widget_findastra',
			)
		);
	}

	public function widget( $args, $instance ) {
		$title = ! empty( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
		$placeholder = ! empty( $instance['placeholder'] ) ? (string) $instance['placeholder'] : __( 'Search products…', 'findastra' );
		$limit = isset( $instance['limit'] ) ? max( 1, min( 50, (int) $instance['limit'] ) ) : 8;

		echo wp_kses_post( (string) $args['before_widget'] );
		if ( '' !== $title ) {
			echo wp_kses_post( $args['before_title'] . esc_html( $title ) . $args['after_title'] );
		}
		echo do_shortcode( sprintf(
			'[findastra placeholder="%s" limit="%d"]',
			esc_attr( $placeholder ),
			(int) $limit
		) );
		echo wp_kses_post( (string) $args['after_widget'] );
	}

	public function form( $instance ) {
		$title = $instance['title'] ?? '';
		$placeholder = $instance['placeholder'] ?? __( 'Search products…', 'findastra' );
		$limit = isset( $instance['limit'] ) ? (int) $instance['limit'] : 8;
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'findastra' ); ?></label>
			<input class="widefat" type="text"
				id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
				name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
				value="<?php echo esc_attr( $title ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"><?php esc_html_e( 'Placeholder:', 'findastra' ); ?></label>
			<input class="widefat" type="text"
				id="<?php echo esc_attr( $this->get_field_id( 'placeholder' ) ); ?>"
				name="<?php echo esc_attr( $this->get_field_name( 'placeholder' ) ); ?>"
				value="<?php echo esc_attr( $placeholder ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php esc_html_e( 'Max results:', 'findastra' ); ?></label>
			<input class="tiny-text" type="number" min="1" max="50"
				id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"
				name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>"
				value="<?php echo esc_attr( (string) $limit ); ?>" />
		</p>
		<?php
		return '';
	}

	public function update( $new_instance, $old_instance ) {
		return array(
			'title'       => sanitize_text_field( (string) ( $new_instance['title'] ?? '' ) ),
			'placeholder' => sanitize_text_field( (string) ( $new_instance['placeholder'] ?? '' ) ),
			'limit'       => max( 1, min( 50, (int) ( $new_instance['limit'] ?? 8 ) ) ),
		);
	}
}
