<?php
/**
 * File Path: includes/admin/tabs/class-mbs-tab-promote.php
 *
 * Promote Tab -- Settings IA Aggregator (UX-019)
 *
 * PRO-gated. Groups Affiliate (Earn More) and Email / Newsletter settings
 * into a single promotion hub.
 *
 * Save mechanics per child:
 *   . Affiliate -- save_settings() in MBS_Tab_Affiliate handles admin_init save.
 *                  Form posts to action="" (same-page PRG pattern).
 *   . Email     -- AJAX-driven; render() is fully self-contained.
 *
 * Enqueue strategy: delegates to each child using the child's own slug.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Admin/Tabs
 * @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_Tab_Promote {

	/** @var MBS_Tab_Affiliate */
	private MBS_Tab_Affiliate $affiliate;

	/** @var MBS_Tab_Email */
	private MBS_Tab_Email $email;

	/**
	 * @param MBS_Tab_Affiliate $affiliate Affiliate child instance.
	 * @param MBS_Tab_Email     $email     Email child instance.
	 */
	public function __construct(
		MBS_Tab_Affiliate $affiliate,
		MBS_Tab_Email $email
	) {
		$this->affiliate = $affiliate;
		$this->email     = $email;
	}

	/**
	 * Enqueue Promote tab assets when this tab is active.
	 *
	 * @param string $current_tab The active tab slug.
	 */
	public function enqueue( string $current_tab ): void {
		if ( 'promote' !== $current_tab ) {
			return;
		}
		$this->affiliate->enqueue( 'affiliate' );
		$this->email->enqueue( 'email' );
	}

	/**
	 * Render the Promote tab.
	 *
	 * Affiliate is wrapped in a Settings API form (its render() outputs
	 * settings_fields + submit_button but does not own the <form> tag).
	 * Email is AJAX-driven and fully self-contained.
	 */
	public function render(): void {
		?>
		<div class="mbs-promote-tab">

			<?php if ( isset( $_GET['updated'] ) ) : ?>
			<div class="notice notice-success is-dismissible">
				<p><?php esc_html_e( 'Settings saved.', 'my-book-showroom' ); ?></p>
			</div>
			<?php endif; ?>

			<h2 class="mbs-section-heading"><?php esc_html_e( 'Earn More (Affiliate)', 'my-book-showroom' ); ?></h2>
			<form method="post" action="">
				<div class="mbs-save-top"><?php submit_button( null, 'primary', 'submit', true ); ?></div>
				<?php $this->affiliate->render(); ?>
			</form>

			<h2 class="mbs-section-heading"><?php esc_html_e( 'Email / Newsletter', 'my-book-showroom' ); ?></h2>
			<form method="post" action="">
				<?php $this->email->render(); ?>
			</form>

		</div>
		<?php
	}
}
