<?php
/**
 * File Path: includes/admin/tabs/class-mbs-tab-setup.php
 *
 * Setup Tab -- Settings IA Aggregator (UX-017)
 *
 * Groups General, Formatting, and Appearance settings into a single
 * Setup tab that serves as the primary landing view for all users.
 *
 * Save mechanics per child:
 *   . General    -- self-contained <form> with admin_init nonce handler;
 *                   render() is fully encapsulated.
 *   . Formatting -- relies on outer form; wrapped here with
 *                   settings_fields( 'mbs_formatting' ).
 *   . Appearance -- relies on outer form; wrapped here with
 *                   settings_fields( 'mbs_appearance' ).
 *
 * Enqueue strategy: delegates to each child using the child's own slug so
 * the child's internal guard check passes without modification.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Admin/Tabs
 * @since      1.0.0
 */

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

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

	/** @var MBS_Tab_General */
	private MBS_Tab_General $general;

	/** @var MBS_Tab_Formatting */
	private MBS_Tab_Formatting $formatting;

	/** @var MBS_Tab_Appearance */
	private MBS_Tab_Appearance $appearance;

	/**
	 * @param MBS_Tab_General    $general    General settings child instance.
	 * @param MBS_Tab_Formatting $formatting Formatting settings child instance.
	 * @param MBS_Tab_Appearance $appearance Appearance settings child instance.
	 */
	public function __construct(
		MBS_Tab_General $general,
		MBS_Tab_Formatting $formatting,
		MBS_Tab_Appearance $appearance
	) {
		$this->general    = $general;
		$this->formatting = $formatting;
		$this->appearance = $appearance;
	}

	/**
	 * Enqueue Setup tab assets when this tab is active.
	 *
	 * Delegates to each child using the child's own slug so each child's
	 * internal guard (e.g. 'general' !== $current_tab) passes correctly.
	 *
	 * @param string $current_tab The active tab slug.
	 */
	public function enqueue( string $current_tab ): void {
		if ( 'setup' !== $current_tab ) {
			return;
		}
		$this->general->enqueue( 'general' );
		$this->formatting->enqueue( 'formatting' );
		$this->appearance->enqueue( 'appearance' );
	}

	/**
	 * Render the Setup tab.
	 *
	 * Outputs General, Formatting, and Appearance sections in sequence.
	 * General is fully self-contained (owns its own <form> and nonce).
	 * Formatting and Appearance are each wrapped in a Settings API form.
	 */
	public function render(): void {
		?>
		<div class="mbs-setup-tab">

			<h2 class="mbs-section-heading"><?php esc_html_e( 'General', 'my-book-showroom' ); ?></h2>
			<?php $this->general->render(); ?>

			<h2 class="mbs-section-heading"><?php esc_html_e( 'Formatting', 'my-book-showroom' ); ?></h2>
			<form method="post" action="options.php">
				<?php settings_fields( 'mbs_formatting' ); ?>
				<div class="mbs-save-top"><?php submit_button( null, 'primary', 'submit', true ); ?></div>
				<?php $this->formatting->render(); ?>
				<?php submit_button(); ?>
			</form>

			<h2 class="mbs-section-heading"><?php esc_html_e( 'Appearance', 'my-book-showroom' ); ?></h2>
			<form method="post" action="options.php">
				<?php settings_fields( 'mbs_appearance' ); ?>
				<div class="mbs-save-top"><?php submit_button( null, 'primary', 'submit', true ); ?></div>
				<?php $this->appearance->render(); ?>
				<?php submit_button(); ?>
			</form>

		</div>
		<?php
	}
}
