<?php
/**
 * File Path: includes/metaboxes/book-header/class-mbs-book-header-links.php
 *
 * Book Header Child Section -- External Links
 *
 * GoodReads and StoryGraph URLs are entered in the Global Buy Links metabox
 * (auto-detected by domain; rendered by section-goodreads.php). Kindle Preview
 * ASIN is extracted from the Amazon buy link URL by section-kindle-preview.php.
 * This class retains Landing Page Mode only.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Metaboxes/BookHeader
 * @since      1.0.0
 */

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

/**
 * Class MBS_Book_Header_Links
 */
// 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_Book_Header_Links { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	public function __construct() {
		add_action( 'mbs_book_header_render_links', array( $this, 'render' ) );
		add_action( 'mbs_book_header_save_links',   array( $this, 'save' ), 10, 2 );
	}

	// -------------------------------------------------------------------------
	// Render
	// -------------------------------------------------------------------------

	/**
	 * Render external-link fields inside the Book Header metabox.
	 *
	 * @since  1.0.0
	 * @param  WP_Post $post  Current post object.
	 * @return void
	 */
	public function render( $post ): void {
		$landing_mode = get_post_meta( $post->ID, 'mbs_landing_page_mode', true );
		?>

		<?php /* -- S14-8: Landing Page Mode (PRO) -------------------------------- */ ?>
		<tr>
			<th scope="row">
				<label for="mbs_landing_page_mode">
					<?php esc_html_e( 'Landing Page Mode', 'my-book-showroom' ); ?>
				</label>
			</th>
			<td>
				<label style="display:flex;align-items:flex-start;gap:8px;cursor:<?php echo mbs_is_pro() ? 'pointer' : 'not-allowed'; ?>;">
					<input type="checkbox"
					       id="mbs_landing_page_mode"
					       name="mbs_landing_page_mode"
					       value="1"
					       <?php checked( $landing_mode, '1' ); ?>
					       <?php disabled( ! mbs_is_pro() ); ?> />
					<span>
						<strong><?php esc_html_e( 'Use Landing Page template for this book', 'my-book-showroom' ); ?></strong>
						<br>
						<span class="description">
							<?php if ( mbs_is_pro() ) : ?>
								<?php esc_html_e( 'When enabled, this book uses a conversion-optimized landing page layout with a large cover hero and prominent buy buttons.', 'my-book-showroom' ); ?>
							<?php else : ?>
								<?php esc_html_e( 'PRO feature: a conversion-optimized landing page layout with a large cover hero and prominent buy buttons.', 'my-book-showroom' ); ?>
							<?php endif; ?>
						</span>
					</span>
				</label>
				<?php if ( ! mbs_is_pro() ) : ?>
					<!-- MBS: Landing Page Mode requires PRO license -->
				<?php endif; ?>
			</td>
		</tr>

		<?php
	}

	// -------------------------------------------------------------------------
	// Save
	// -------------------------------------------------------------------------

	/**
	 * Save external-link meta for the current post.
	 *
	 * @since  1.0.0
	 * @param  int     $post_id  Post ID.
	 * @param  WP_Post $post     Post object.
	 * @return void
	 */
	public function save( int $post_id, $post ): void {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		// -- Landing Page Mode (PRO) -------------------------------------------
		if ( mbs_is_pro() ) {
			$landing = isset( $_POST['mbs_landing_page_mode'] ) ? '1' : '';
			update_post_meta( $post_id, 'mbs_landing_page_mode', $landing );
		} else {
			// SEC-003: Downgrade guard -- remove PRO meta so a site downgraded
			// from PRO to FREE does not retain stale landing page mode data.
			delete_post_meta( $post_id, 'mbs_landing_page_mode' );
		}
	}
}
