<?php
/**
 * File Path: includes/metaboxes/class-mbs-metabox-endorsements.php
 *
 * Endorsements Metabox
 *
 * Handles the endorsements/blurbs repeater on the mbs_book edit screen.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

// phpcs:disable WordPress.Security.NonceVerification -- nonce verified in parent class verify_save() / check_admin_referer() before $_POST access; PHPCS cannot trace cross-method nonce verification
class MBS_Metabox_Endorsements extends MBS_Metabox_Base { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/** Register hooks. */
	public function __construct() {
	}

	/** Add the Endorsements metabox. */

	/** Render the endorsements repeater metabox. */
	public function render( WP_Post $post ): void {
		wp_nonce_field( 'mbs_endorsements_nonce', 'mbs_endorsements_nonce' );

		// Admin uses get_all_for_post() so hidden endorsements remain editable (E-002).
		$endorsements = MBS_Endorsement_DAO::get_all_for_post( $post->ID );
		?>
		<div class="mbs-repeater mbs-endorsements-repeater">
			<div class="mbs-repeater-rows">
				<?php if ( ! empty( $endorsements ) ) : ?>
					<?php foreach ( $endorsements as $index => $endorsement ) : ?>
						<div class="mbs-repeater-row">
							<input type="hidden" 
							       name="mbs_endorsement_id[]" 
							       value="<?php echo esc_attr( $endorsement->id ); ?>">

							<textarea name="mbs_endorsement_quote[]" 
							          class="large-text" 
							          rows="3"
							          placeholder="Quote text..."><?php echo esc_textarea( $endorsement->quote_text ); ?></textarea>

							<input type="text" 
							       name="mbs_endorsement_attributed_to[]" 
							       class="widefat" 
							       placeholder="Attributed to (name)"
							       value="<?php echo esc_attr( $endorsement->attributed_to ); ?>">

							<input type="text" 
							       name="mbs_endorsement_attribution_title[]" 
							       class="widefat" 
							       placeholder="Title or publication"
							       value="<?php echo esc_attr( $endorsement->attribution_title ); ?>">

							<label>
								<input type="checkbox" 
								       name="mbs_endorsement_visible[]" 
								       value="1" 
								       <?php checked( $endorsement->is_visible, 1 ); ?>>
								Visible on front-end
							</label>

							<button type="button" 
							        class="button button-link-delete mbs-remove-row"><?php esc_html_e( 'Remove', 'my-book-showroom' ); ?></button>
						</div>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>

			<button type="button" 
			        class="button button-primary mbs-add-endorsement-row">+ Add Endorsement</button>
		</div>
		<?php
	}

	/** Save endorsements on post save. */
	public function save( int $post_id, WP_Post $post ): void {
		if ( ! $this->verify_save( $post_id, 'mbs_endorsements_nonce', 'mbs_endorsements_nonce' ) ) {
			return;
		}

		// Delegate actual save to the DAO
		MBS_Endorsement_DAO::save_from_post( $post_id, $_POST );
	}

	/**
	 * Render one editor section within the unified book editor shell.
	 *
	 * Passthrough — single-section handler (Sprint 169).
	 *
	 * @since  1.0.0
	 * @param  string  $section_key  Section key (ignored for single-section handlers).
	 * @param  WP_Post $post         Current post object.
	 * @return void
	 */
	public function render_section( string $section_key, WP_Post $post ): void {
		$this->render( $post );
	}

	/**
	 * Save one editor section within the unified book editor shell.
	 *
	 * Passthrough — single-section handler (Sprint 169).
	 *
	 * @since  1.0.0
	 * @param  string  $section_key  Section key (ignored for single-section handlers).
	 * @param  int     $post_id      Post ID.
	 * @param  WP_Post $post         Post object.
	 * @return void
	 */
	public function save_section( string $section_key, int $post_id, WP_Post $post ): void {
		$this->save( $post_id, $post );
	}

}
