<?php
/**
 * File path: widgets/class-mbs-widget-featured-book.php
 *
 * Featured Book Widget
 *
 * Displays a single highlighted book with cover, title, author, and Order Now button.
 * Supports PRO features (spinning video, format-specific covers, affiliate rewriting).
 *
 * Depends on: MBS_Cover_Renderer, MBS_Buy_Link_Renderer, MBS_Book_DAO
 *
 */

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

class MBS_Widget_Featured_Book extends WP_Widget {

	public function __construct() {
		parent::__construct(
			'mbs_featured_book',
			__( 'MBS Featured Book', 'my-book-showroom' ),
			array(
				'description' => __( 'Display a single featured book with cover and purchase options.', 'my-book-showroom' ),
			)
		);
	}

	public function widget( $args, $instance ) {
		if ( ! mbs_is_registered() ) {
			return;
		}

		$post_id = ! empty( $instance['book_id'] ) ? (int) $instance['book_id'] : 0;
		if ( ! $post_id || 'publish' !== get_post_status( $post_id ) ) {
			return;
		}

		echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated widget wrapper HTML

		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated title wrapper; content is esc_html()
		}

		// Use canonical cover renderer (4-level hierarchy)
		echo MBS_Cover_Renderer::render( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML

		echo '<h3 class="mbs-widget-book-title">' . esc_html( get_the_title( $post_id ) ) . '</h3>';

		$authors = wp_get_post_terms( $post_id, 'mbs_author', array( 'fields' => 'names' ) );
		if ( ! empty( $authors ) ) {
			echo '<div class="mbs-widget-book-author">' . esc_html( implode( ', ', $authors ) ) . '</div>';
		}

		// Order Now button (catalog context for modal support)
		echo MBS_Buy_Link_Renderer::render_order_now( $post_id, null, array( 'context' => 'catalog' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- trusted renderer returns pre-escaped HTML

		echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated widget wrapper HTML
	}

	public function form( $instance ) {
		$title   = ! empty( $instance['title'] ) ? $instance['title'] : '';
		$book_id = ! empty( $instance['book_id'] ) ? (int) $instance['book_id'] : 0;
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'my-book-showroom' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" 
			       name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" 
			       type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'book_id' ) ); ?>"><?php esc_html_e( 'Featured Book:', 'my-book-showroom' ); ?></label>
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'book_id' ) ); ?>" 
			        name="<?php echo esc_attr( $this->get_field_name( 'book_id' ) ); ?>">
				<option value=""><?php esc_html_e( '— Select Book —', 'my-book-showroom' ); ?></option>
				<?php
				$books = get_posts( array( 'post_type' => 'mbs_book', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );
				foreach ( $books as $book ) {
					printf(
						'<option value="%d" %s>%s</option>',
						(int) $book->ID,
						selected( $book_id, $book->ID, false ),
						esc_html( $book->post_title )
					);
				}
				?>
			</select>
		</p>
		<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title']   = sanitize_text_field( $new_instance['title'] );
		$instance['book_id'] = (int) $new_instance['book_id'];
		return $instance;
	}
}
