<?php
/**
 * File path: widgets/class-mbs-widget-series-list.php
 *
 * Series List Widget
 *
 * Displays a list of book series with optional book count and links to series archives.
 *
 * Depends on: MBS_Book_DAO (for counts), taxonomy 'mbs_series'
 *
 */

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

class MBS_Widget_Series_List extends WP_Widget {

	public function __construct() {
		parent::__construct(
			'mbs_series_list',
			__( 'MBS Series List', 'my-book-showroom' ),
			array(
				'description' => __( 'Display a list of book series with optional post counts.', 'my-book-showroom' ),
			)
		);
	}

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

		$title      = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Series', 'my-book-showroom' );
		$show_count = ! empty( $instance['show_count'] );

		echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated widget wrapper HTML
		echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated title wrapper; content is esc_html()

		$series = get_terms( array(
			'taxonomy'   => 'mbs_series',
			'hide_empty' => true,
		) );

		if ( ! empty( $series ) && ! is_wp_error( $series ) ) {
			echo '<ul class="mbs-widget-series-list">';
			foreach ( $series as $s ) {
				$count = $show_count ? ' <span class="mbs-count">(' . (int) $s->count . ')</span>' : '';
				$link  = get_term_link( $s );
				printf(
					'<li><a href="%s">%s%s</a></li>',
					esc_url( $link ),
					esc_html( $s->name ),
					$count // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- \$count is empty string or static HTML span with (int)-cast term count
				);
			}
			echo '</ul>';
		}

		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'] : '';
		$show_count = ! empty( $instance['show_count'] );
		?>
		<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>
			<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" 
			       name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" 
			       value="1" <?php checked( $show_count ); ?>>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php esc_html_e( 'Show book count', 'my-book-showroom' ); ?></label>
		</p>
		<?php
	}

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