<?php
/**
 * Base admin page: renders the shared Divi-like layout (sidebar + gradient header).
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Admin\Pages;

use SuiteMkt3k\Admin\Admin_Menu;
use SuiteMkt3k\Security\Capabilities;
use SuiteMkt3k\Compat\WooCommerce_Check;

defined( 'ABSPATH' ) || exit;

abstract class Base_Page {

	protected static string $slug         = 'suitemkt3k';
	protected static string $cap          = Capabilities::MANAGE;
	protected static bool   $requires_wc  = false;
	protected static string $page_title   = '';
	protected static string $page_subtitle = '';

	public static function render(): void {
		Capabilities::ensure( static::$cap );
		self::layout_open( static::$slug );
		if ( static::$requires_wc && ! WooCommerce_Check::is_active() ) {
			WooCommerce_Check::render_notice();
		}
		static::header( static::$page_title, static::$page_subtitle );
		static::content();
		self::layout_close();
	}

	abstract protected static function content(): void;

	protected static function header( string $title, string $subtitle = '', array $actions = array() ): void {
		?>
		<div class="wmk-header-banner">
			<div class="wmk-header-left">
				<?php /* Use <p> not <h1>/<h2>: WP core moves .notice after the first .wrap h1/h2. */ ?>
				<p class="wmk-header-title"><?php echo esc_html( $title ); ?></p>
				<?php if ( $subtitle ) : ?>
					<p class="wmk-header-subtitle"><?php echo esc_html( $subtitle ); ?></p>
				<?php endif; ?>
			</div>
			<?php if ( $actions ) : ?>
				<div class="wmk-header-actions">
					<?php foreach ( $actions as $action ) : ?>
						<a class="wmk-btn <?php echo esc_attr( $action['class'] ?? 'wmk-btn-primary' ); ?>"
						   href="<?php echo esc_url( $action['url'] ?? '#' ); ?>"
						   <?php echo ! empty( $action['attrs'] ) ? wp_kses_post( $action['attrs'] ) : ''; ?>
						>
							<?php echo esc_html( $action['label'] ?? '' ); ?>
						</a>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}

	protected static function layout_open( string $current_slug ): void {
		$pages = Admin_Menu::pages();
		$notice_title = static::$page_title !== '' ? static::$page_title : 'SuiteMkt3k';
		?>
		<div class="wrap wmk-wrap">
			<div class="wmk-app">
				<?php
				$current_lang = \SuiteMkt3k\Modules\Settings\Settings_Manager::get_locale();
				if ( ! in_array( $current_lang, array( 'es_ES', 'en_US' ), true ) ) {
					$current_lang = 'es_ES';
				}
				$languages = array(
					'es_ES' => array( 'label' => __( 'Spanish', 'suitemkt3k' ), 'flag' => SUITEMKT3K_URL . 'assets/frontend/img/es.png' ),
					'en_US' => array( 'label' => __( 'English', 'suitemkt3k' ), 'flag' => SUITEMKT3K_URL . 'assets/frontend/img/en.png' ),
				);
				$switch_nonce = wp_create_nonce( 'suitemkt3k_switch_language' );
				$current_uri  = self::admin_request_uri( $current_slug );
				?>
				<aside class="wmk-sidebar" aria-label="<?php esc_attr_e( 'SuiteMkt3k navigation', 'suitemkt3k' ); ?>">
					<div class="wmk-logo">
						<div class="wmk-logo-text">
							<strong>SuiteMkt<span class="wmk-logo-accent">3k</span></strong>
							<small><?php esc_html_e( 'Marketing Suite', 'suitemkt3k' ); ?></small>
						</div>
					</div>
					<nav class="wmk-nav">
						<?php foreach ( $pages as $page ) : ?>
							<?php if ( ! empty( $page['hidden'] ) ) { continue; } ?>
							<?php if ( ! current_user_can( $page['cap'] ) ) { continue; } ?>
							<a class="wmk-nav-item <?php echo $page['slug'] === $current_slug ? 'is-active' : ''; ?>"
							   href="<?php echo esc_url( admin_url( 'admin.php?page=' . $page['slug'] ) ); ?>">
								<?php echo self::nav_icon( $page['icon'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
								<span><?php echo esc_html( $page['label'] ); ?></span>
							</a>
						<?php endforeach; ?>
					</nav>

					<div class="wmk-lang-switcher" x-data="{ open: false }" @click.outside="open = false">
						<button type="button" class="wmk-lang-button" @click="open = !open" aria-haspopup="listbox" :aria-expanded="open">
							<img class="wmk-lang-flag" src="<?php echo esc_url( $languages[ $current_lang ]['flag'] ); ?>" alt="">
							<span><?php echo esc_html( $languages[ $current_lang ]['label'] ); ?></span>
							<svg class="wmk-lang-caret" :class="open ? 'is-open' : ''" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 9l6 6 6-6"/></svg>
						</button>
						<ul class="wmk-lang-menu" x-show="open" x-cloak role="listbox">
							<?php foreach ( $languages as $locale => $lang ) : ?>
								<li>
									<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
										<input type="hidden" name="action" value="suitemkt3k_switch_language">
										<input type="hidden" name="locale" value="<?php echo esc_attr( $locale ); ?>">
										<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( $switch_nonce ); ?>">
										<input type="hidden" name="_wp_http_referer" value="<?php echo esc_attr( $current_uri ); ?>">
										<button type="submit" class="wmk-lang-option <?php echo $locale === $current_lang ? 'is-active' : ''; ?>" role="option" aria-selected="<?php echo $locale === $current_lang ? 'true' : 'false'; ?>">
											<img class="wmk-lang-flag" src="<?php echo esc_url( $lang['flag'] ); ?>" alt="">
											<span><?php echo esc_html( $lang['label'] ); ?></span>
										</button>
									</form>
								</li>
							<?php endforeach; ?>
						</ul>
					</div>

					<div class="wmk-nav-footer">
						<small>
							<?php /* translators: %s: plugin version. */ ?>
							<?php echo esc_html( sprintf( __( 'Version %s', 'suitemkt3k' ), SUITEMKT3K_VERSION ) ); ?>
							·
							<?php esc_html_e( 'Author:', 'suitemkt3k' ); ?>
							<a href="https://www.31416k.com" target="_blank" rel="noopener" class="wmk-author-link">31416k</a>
						</small>
					</div>
				</aside>
				<main class="wmk-main">
					<?php
					/*
					 * Anchor for WordPress core admin-notice relocation (wp-admin/js/common.js):
					 * notices are moved after .wp-header-end or the first .wrap h1/h2.
					 * Keep this h1 outside the blue banner so notices never land inside it.
					 */
					?>
					<h1 class="screen-reader-text"><?php echo esc_html( $notice_title ); ?></h1>
					<div class="wp-header-end"></div>
		<?php
	}

	protected static function layout_close(): void {
		?>
				</main>
			</div>
		</div>
		<?php
	}

	/**
	 * Build the current admin screen URL for the language switcher referer.
	 *
	 * Uses admin_url() + query args instead of home_url( REQUEST_URI ), which duplicates
	 * the site subdirectory on installs like /31416k2026/.
	 *
	 * @param string $default_page Admin menu slug fallback.
	 */
	protected static function admin_request_uri( string $default_page ): string {
		$args = \SuiteMkt3k\Utils\Sanitizer::admin_screen_query_args( $default_page );
		return esc_url_raw( add_query_arg( $args, admin_url( 'admin.php' ) ) );
	}

	protected static function nav_icon( string $key ): string {
		$icons = array(
			'home'     => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 11l9-8 9 8"/><path d="M5 10v10h14V10"/></svg>',
			'ticket'   => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 8a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2v2a2 2 0 0 0 0 4v2a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-2a2 2 0 0 0 0-4z"/></svg>',
			'mail'     => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></svg>',
			'cart'     => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="9" cy="20" r="1"/><circle cx="18" cy="20" r="1"/><path d="M3 3h2l3 12h11l2-8H6"/></svg>',
			'users'    => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="9" cy="8" r="4"/><path d="M2 21a7 7 0 0 1 14 0"/><circle cx="17" cy="7" r="3"/><path d="M22 21a5 5 0 0 0-6-4.9"/></svg>',
			'chart'    => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 3v18h18"/><path d="M7 15l4-4 3 3 5-7"/></svg>',
			'settings' => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3h.1a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8v.1a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/></svg>',
			'help'     => '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M9.1 9a3 3 0 0 1 5.8 1c0 2-3 2-3 4"/><line x1="12" y1="17" x2="12" y2="17.01"/></svg>',
		);
		return '<span class="wmk-nav-icon">' . ( $icons[ $key ] ?? '' ) . '</span>';
	}
}
