<?php
/**
 * Upgrade and Pro waitlist helpers.
 *
 * @package Vareon_Calculator_Suite_Lite
 */

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

/**
 * Get settings tab configuration.
 *
 * @param bool $is_pro Whether Pro is active.
 * @return array<string, array<string, string>>
 */
function vareoncalc_get_settings_tabs( $is_pro = false ) {
	$tabs = array(
		'general'      => array(
			'label'    => __( 'General', 'vareon-calculator-suite-lite' ),
			'panel_id' => 'tab-general',
			'icon'     => 'admin-generic',
		),
		'docs'         => array(
			'label'    => __( 'Docs', 'vareon-calculator-suite-lite' ),
			'panel_id' => 'tab-docs',
			'icon'     => 'book-alt',
		),
		'pro-waitlist' => array(
			'label'    => __( 'Pro Waitlist', 'vareon-calculator-suite-lite' ),
			'panel_id' => 'tab-pro-waitlist',
			'icon'     => 'star-filled',
		),
	);

	if ( $is_pro ) {
		$tabs['colors'] = array(
			'label'    => __( 'Colors & Theme', 'vareon-calculator-suite-lite' ),
			'panel_id' => 'tab-colors',
			'icon'     => 'admin-appearance',
		);
	}

	return $tabs;
}

/**
 * Normalize the requested settings tab to a supported value.
 *
 * @param string $tab   Requested tab.
 * @param bool   $is_pro Whether Pro is active.
 * @return string
 */
function vareoncalc_normalize_settings_tab( $tab = '', $is_pro = false ) {
	$tab         = sanitize_key( (string) $tab );
	$allowed     = array_keys( vareoncalc_get_settings_tabs( $is_pro ) );
	$allowed[]   = 'welcome';
	$default_tab = 'general';

	if ( in_array( $tab, $allowed, true ) ) {
		return $tab;
	}

	return $default_tab;
}

/**
 * Get the settings page URL for a tab.
 *
 * @param string $tab Tab slug.
 * @return string
 */
function vareoncalc_get_settings_tab_url( $tab ) {
	return add_query_arg(
		array(
			'page' => 'vareoncalc',
			'tab'  => sanitize_key( (string) $tab ),
		),
		admin_url( 'options-general.php' )
	);
}

/**
 * Get the canonical site URL for the waitlist CTA.
 *
 * @return string
 */
function vareoncalc_get_pro_waitlist_url() {
	return 'https://vareon.dev/waitlist';
}

/**
 * Render the Pro waitlist tab.
 *
 * @return void
 */
function vareoncalc_render_pro_waitlist_tab() {
	$waitlist_url = vareoncalc_get_pro_waitlist_url();
	?>
	<div class="vareoncalc-waitlist">
		<div class="vareoncalc-waitlist__hero">
			<h2 class="vareoncalc-waitlist__headline"><?php esc_html_e( 'Join the waitlist for Vareon Pro', 'vareon-calculator-suite-lite' ); ?></h2>
			<p class="vareoncalc-waitlist__subhead"><?php esc_html_e( 'Vareon Pro adds the full composable engine — Design Studio and the complete calculator library. Join the waitlist and we\'ll let you know when it\'s ready.', 'vareon-calculator-suite-lite' ); ?></p>
			<a class="vareoncalc-waitlist__cta-btn" href="<?php echo esc_url( $waitlist_url ); ?>" target="_blank" rel="noopener noreferrer">
				<?php esc_html_e( 'Join Waitlist', 'vareon-calculator-suite-lite' ); ?>
				<span class="dashicons dashicons-arrow-right-alt2" aria-hidden="true"></span>
			</a>
			<p class="vareoncalc-waitlist__fine"><?php esc_html_e( '* After Pro launches, waitlist subscribers are converted to our regular newsletter. You can unsubscribe anytime.', 'vareon-calculator-suite-lite' ); ?></p>
		</div>
	</div>
	<?php
}
