<?php
/**
 * File Path: includes/blocks/class-mbs-block-book-grid.php
 *
 * Book Grid Block — Render
 *
 * PHP render_callback for the mbs/book-grid Gutenberg block.
 * Registered by MBS_Blocks_Registry::register_book_grid_block().
 *
 * Delegates to MBS_Shortcode_Books::render() so the block and the
 * [mbs_books] shortcode share identical output logic.
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

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

class MBS_Block_Book_Grid { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render the Book Grid block.
	 *
	 * Called by WordPress as the register_block_type render_callback.
	 * Maps block attributes to MBS_Shortcode_Books::render() parameter
	 * format so both the block and the shortcode share output logic.
	 *
	 * @param array $attributes Block attributes from the editor.
	 * @return string Rendered HTML.
	 */
	public static function render( array $attributes ): string {
		// BLK-GRID-1: Always pass layout='grid' so the block uses the grid layout.
		// Without this the shortcode defaults to mbs_get_setting('formatting',
		// 'default_layout', 'hero'), which renders each book as a full-width hero
		// row — appearing as a single column regardless of the columns setting.
		//
		// Per-block column count: read from the block attribute if the editor set it,
		// otherwise fall back to the site-wide appearance setting. Emitted as an
		// inline --mbs-columns CSS variable so the block overrides the root value
		// without affecting other blocks on the same page.
		$columns_attr = absint( $attributes['columns'] ?? 0 );
		$columns      = ( $columns_attr >= 1 && $columns_attr <= 6 )
			? $columns_attr
			: (int) mbs_get_setting( 'appearance', 'columns', 3 );
		if ( $columns < 1 ) {
			$columns = 3;
		}

		// BLK-GRID-2: Suppress pagination in the editor preview.
		// ServerSideRender pagination links are real <a> tags; clicking them
		// navigates the editor page to the paginated URL, producing a REST 404.
		$in_editor = defined( 'REST_REQUEST' ) && REST_REQUEST;

		$shortcode_atts = array(
			'layout'          => 'grid',
			'show_pagination' => ! $in_editor,
			'limit'   => (int) ( $attributes['limit'] ?? 6 ),
			'orderby' => sanitize_key( $attributes['orderby'] ?? 'date' ),
			// Taxonomy filters — pass through only when non-empty.
			'author'  => sanitize_text_field( $attributes['author']  ?? '' ),
			'series'  => sanitize_text_field( $attributes['series']  ?? '' ),
			'genre'   => sanitize_text_field( $attributes['genre']   ?? '' ),
			'tag'     => sanitize_text_field( $attributes['tag']     ?? '' ),
		);

		$html = MBS_Shortcode_Books::render( $shortcode_atts );

		// Wrap with inline --mbs-columns so per-block column count overrides the
		// site-wide value set on :root .mbs-wrapper by class-mbs-front-assets.php.
		if ( '' !== $html ) {
			$html = '<div style="--mbs-columns:' . absint( $columns ) . '">' . $html . '</div>';
		}

		return $html;
	}
}
