<?php
/**
 * Settings page wrapper.
 *
 * Variables: $active_tab, $options, $sections, $sidebar
 *
 * @package Relatedly
 * @since   2.0.0
 * @since   2.3.0 Rebuilt the admin shell with a WordPress-native layout.
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
defined( 'ABSPATH' ) || exit;

use Relatedly\Premium\FeatureGate;
use Relatedly\Premium\LicenseManager;

$is_premium  = FeatureGate::is_premium();
$upgrade_url = LicenseManager::checkout_url();

$tab_config = [
	'general'          => [
		'label' => __( 'General', 'relatedly' ),
	],
	'inline'           => [
		'label' => __( 'Inline Posts', 'relatedly' ),
	],
	'end-content'      => [
		'label' => __( 'End of Post', 'relatedly' ),
	],
	'floating-sidebar' => [
		'label' => __( 'Floating Sidebar', 'relatedly' ),
		'pro'   => ! $is_premium,
	],
	'rss'              => [
		'label' => __( 'RSS Feeds', 'relatedly' ),
	],
	'email'            => [
		'label' => __( 'Email Reports', 'relatedly' ),
		'pro'   => ! $is_premium,
	],
];

$tab_url = static function ( string $tab ): string {
	return add_query_arg(
		[
			'page' => 'relatedly',
			'tab'  => $tab,
		],
		admin_url( 'admin.php' )
	);
};

$locked_tabs       = [ 'floating-sidebar', 'email' ];
$active_tab_locked = ! $is_premium && in_array( $active_tab, $locked_tabs, true );
?>
<div class="wrap relatedly-admin-wrap">
	<h1 class="screen-reader-text"><?php esc_html_e( 'Relatedly Settings', 'relatedly' ); ?></h1>

	<div class="relatedly-admin-shell">
		<header class="relatedly-admin-header">
			<div class="relatedly-admin-brand">
				<div class="relatedly-admin-logo" aria-hidden="true">
					<img src="<?php echo esc_url( RELATEDLY_PLUGIN_URL . 'assets/images/relatedly-icon.svg' ); ?>" alt="" width="32" height="32">
				</div>
				<div>
					<h1><?php esc_html_e( 'Relatedly', 'relatedly' ); ?></h1>
					<p><?php esc_html_e( 'Configure your content recommendation engine and display themes.', 'relatedly' ); ?></p>
				</div>
			</div>

			<div class="relatedly-admin-status">
				<span class="relatedly-version">
					<?php
					printf(
						/* translators: %s: plugin version number. */
						esc_html__( 'v%s', 'relatedly' ),
						esc_html( RELATEDLY_VERSION )
					);
					?>
				</span>
				<span class="relatedly-plan-badge"><?php echo $is_premium ? esc_html__( 'Premium Active', 'relatedly' ) : esc_html__( 'Free', 'relatedly' ); ?></span>
				<?php if ( ! $is_premium ) : ?>
					<a class="button button-primary" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener noreferrer">
						<?php esc_html_e( 'Upgrade', 'relatedly' ); ?>
					</a>
				<?php endif; ?>
			</div>
		</header>

		<nav class="relatedly-admin-tabs" aria-label="<?php esc_attr_e( 'Relatedly settings sections', 'relatedly' ); ?>">
			<?php foreach ( $tab_config as $key => $cfg ) : ?>
				<a href="<?php echo esc_url( $tab_url( $key ) ); ?>" class="relatedly-admin-tab <?php echo $active_tab === $key ? 'is-active' : ''; ?>">
					<?php echo esc_html( $cfg['label'] ); ?>
					<?php if ( ! empty( $cfg['pro'] ) ) : ?>
						<span class="relatedly-admin-tab-badge"><?php esc_html_e( 'Pro', 'relatedly' ); ?></span>
					<?php endif; ?>
				</a>
			<?php endforeach; ?>
		</nav>

		<div class="relatedly-admin-layout">
			<main class="relatedly-admin-main">
				<?php if ( ! $active_tab_locked ) : ?>
					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" id="relatedly-settings-form">
						<input type="hidden" name="action" value="relatedly_save_settings">
						<input type="hidden" name="relatedly_active_tab" value="<?php echo esc_attr( $active_tab ); ?>">
						<?php wp_nonce_field( 'relatedly_save_settings' ); ?>

						<?php $sections->render( 'tab-' . $active_tab . '.php' ); ?>
					</form>
				<?php else : ?>
					<?php $sections->render( 'tab-' . $active_tab . '.php' ); ?>
				<?php endif; ?>
			</main>

			<aside class="relatedly-admin-sidebar">
				<?php $sidebar->render(); ?>
			</aside>
		</div>
	</div>
</div>
