<?php
/**
 * Tabbed settings page renderer for RSM plugins.
 *
 * @package Rsmspark_Suite
 */

defined( 'ABSPATH' ) || exit;

/**
 * Shared tabbed settings UI.
 */
final class Rsmspark_Settings_Page {

	/**
	 * Render a tabbed settings page.
	 *
	 * @param array<string, mixed> $args {
	 *     @type string   $page_title   H1 title.
	 *     @type string   $menu_slug    Settings page slug (for tab query arg).
	 *     @type string   $active_tab   Current tab id.
	 *     @type array    $tabs         Tab id => [ 'label' => string, 'callback' => callable ].
	 *     @type string   $form_action  Form action URL (empty = no form wrapper).
	 *     @type string   $form_method  GET|POST.
	 *     @type callable $form_open    Optional extra output inside opening form tag.
	 *     @type string   $wrap_class   Additional wrap CSS class.
	 * }
	 * @return void
	 */
	public static function render( $args ) {
		$defaults = array(
			'page_title'  => '',
			'menu_slug'   => '',
			'active_tab'  => '',
			'tabs'        => array(),
			'form_action' => '',
			'form_method' => 'post',
			'form_open'   => null,
			'wrap_class'  => '',
			'skip_wrap'   => false,
		);

		$args = wp_parse_args( $args, $defaults );

		if ( empty( $args['tabs'] ) || ! is_array( $args['tabs'] ) ) {
			return;
		}

		$tab_ids = array_keys( $args['tabs'] );
		if ( empty( $args['active_tab'] ) || ! isset( $args['tabs'][ $args['active_tab'] ] ) ) {
			$args['active_tab'] = $tab_ids[0];
		}

		$wrap_classes = trim( 'rsmspark-suite-admin rsmspark-suite-admin rsm-settings-page rsmspark-suite-ui ' . $args['wrap_class'] );
		if ( empty( $args['skip_wrap'] ) ) {
			$wrap_classes = trim( 'wrap ' . $wrap_classes );
		}
		?>
		<div class="<?php echo esc_attr( $wrap_classes ); ?>">
			<?php if ( '' !== (string) $args['page_title'] ) : ?>
				<h2 class="rsm-cw-screen-title"><?php echo esc_html( $args['page_title'] ); ?></h2>
			<?php endif; ?>

			<nav class="nav-tab-wrapper wp-clearfix" aria-label="<?php esc_attr_e( 'Settings sections', 'rsm-content-widgets' ); ?>">
				<?php foreach ( $args['tabs'] as $tab_id => $tab ) : ?>
					<?php
					$url = add_query_arg(
						array(
							'page' => $args['menu_slug'],
							'tab'  => $tab_id,
						),
						admin_url( 'admin.php' )
					);
					$classes = 'nav-tab';
					if ( $tab_id === $args['active_tab'] ) {
						$classes .= ' nav-tab-active';
					}
					?>
					<a class="<?php echo esc_attr( $classes ); ?>" href="<?php echo esc_url( $url ); ?>">
						<?php echo esc_html( $tab['label'] ); ?>
					</a>
				<?php endforeach; ?>
			</nav>

			<div class="rsm-settings-tab-panel">
				<?php if ( ! empty( $args['form_action'] ) ) : ?>
					<form method="<?php echo esc_attr( $args['form_method'] ); ?>" action="<?php echo esc_url( $args['form_action'] ); ?>">
						<?php
						if ( is_callable( $args['form_open'] ) ) {
							call_user_func( $args['form_open'] );
						}
						?>
				<?php endif; ?>

				<?php
				$callback = $args['tabs'][ $args['active_tab'] ]['callback'] ?? null;
				if ( is_callable( $callback ) ) {
					call_user_func( $callback );
				}
				?>

				<?php if ( ! empty( $args['form_action'] ) ) : ?>
						<?php submit_button(); ?>
					</form>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Read active tab from request with fallback.
	 *
	 * @param string $menu_slug Settings page slug.
	 * @param string $default   Default tab id.
	 * @return string
	 */
	public static function get_active_tab( $menu_slug, $default = 'general' ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( isset( $_GET['page'] ) && $menu_slug === sanitize_key( wp_unslash( $_GET['page'] ) ) && isset( $_GET['tab'] ) ) {
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended
			return sanitize_key( wp_unslash( $_GET['tab'] ) );
		}

		return $default;
	}
}
