<?php
/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Header
 *
 * This class handles the rendering of the header section for the Pixelio Cart Abandon Recovery More plugin.
 * It displays the logo, slogan, mode switch (AI/Manual), settings menu, quota usage, and beta signup UI.
 *
 * It also fetches plugin configuration and quota information from the API and manages the display
 * of interactive elements like beta signup, quota bars, and settings submenu.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Views\Reusable;

use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Header
 *
 * This class handles the rendering of the header section for the Pixelio Cart Abandon Recovery More plugin.
 * It displays the logo, slogan, mode switch (AI/Manual), settings menu, quota usage, and beta signup UI.
 *
 * It also fetches plugin configuration and quota information from the API and manages the display
 * of interactive elements like beta signup, quota bars, and settings submenu.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Views\Reusable
 * @author  Pixelio
 * @since   0.1
 */
class Pixelio_Cart_Abandon_Recovery_More_Header
{


	/**
	 * Current admin page slug.
	 *
	 * @var string
	 */
	private $current_page;

	/**
	 * Current mode retrieved from API ('basic', 'advanced', etc.).
	 *
	 * @var string|null
	 */
	private $current_mode;

	/**
	 * Type of mode usage ('auto' for AI, 'manual' for manual).
	 *
	 * @var string|null
	 */
	private $auto_mode;

	/**
	 * Label displayed when auto mode (AI) is enabled.
	 *
	 * @var string
	 */
	private $checked_auto_mode_label;

	/**
	 * Label displayed when manual mode is selected.
	 *
	 * @var string
	 */
	private $unchecked_auto_mode_label;
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;
	/**
	 * @var mixed|\WP_Error
	 */
	private mixed $body;

	/**
	 * Pixelio_Cart_Abandon_Recovery_More_Header constructor.
	 *
	 * Initializes current page, retrieves plugin configuration from the API, and
	 * sets up default labels for AI/manual mode switch.
	 */
	public function __construct($pixelio_util)
	{
		$this->pixelio_util = $pixelio_util;
		$this->current_page = '';
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we are in submenu - no nonce is required
		if (isset($_GET['page'])) {
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we are in submenu - no nonce is required
			$this->current_page = sanitize_text_field(wp_unslash($_GET['page'])) ?? '';

		}
		$url = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'get_config';

		$this->body = $this->pixelio_util->pixelio_cart_abandon_recovery_more_call_api($url);
		if ('pixelio-cart-abandon-recovery-more-settings' === $this->current_page) {


			if (is_wp_error($this->body )) {
				$this->current_mode = null;
				$this->auto_mode = null;
			} else {
				$data = $this->body ;

				if (is_array($data)) {
					$this->current_mode = $data['level'] ?? null;
					$this->auto_mode = $data['type_of_use'] ?? null;
				} else {
					$this->current_mode = null;
					$this->auto_mode = null;
				}
			}
		}
		$this->checked_auto_mode_label = 'AI mode';
		$this->unchecked_auto_mode_label = 'Manual mode';
	}

	/**
	 * Render the header views.
	 *
	 * This method outputs the HTML for the plugin header including:
	 * - Logo
	 * - Slogan
	 * - AI/Manual mode switch
	 * - Settings menu
	 * - Quota usage bars
	 * - Beta signup section
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_get_views(): void
	{
		$configured = esc_html(get_option('pixelio_first_config_done', false));
		?>


		<div class="pixelio-cart-abandon-recovery-more-header-card-1">
			<div class="pixelio-cart-abandon-recovery-more-logo">
				<img
					src="<?php echo esc_attr(Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_url . Pixelio_Cart_Abandon_Recovery_More_Util::$logo_blue); ?>"
					alt="Rivio Logo"/>
			</div>

			<div
				class="pixelio-cart-abandon-recovery-more-slogan <?php echo (strpos($this->current_page, 'email') !== false || strpos($this->current_page, 'settings') !== false) ? esc_attr('center') : esc_attr('right'); ?>">
				<span><?php echo esc_attr(Pixelio_Cart_Abandon_Recovery_More_Util::$slogan); ?></span>
			</div>
			<?php if ('rivio-ai-cart-recovery-marketing-email-automation-email' === $this->current_page) : ?>
				<div>
					<?php

					$pixelio_cart_abandon_recovery_more_checkbox = new Pixelio_Cart_Abandon_Recovery_More_Checkbox('', '', 'ia-setup-mode', '', false, false, '', '', '', true, $this->checked_auto_mode_label, $this->unchecked_auto_mode_label);
					$pixelio_cart_abandon_recovery_more_checkbox->pixelio_cart_abandon_recovery_more_get_views();
					?>

				</div>
			<?php elseif ('rivio-ai-cart-recovery-marketing-email-automation-settings' === $this->current_page) : ?>

			<?php endif; ?>

			<?php if (!('first-config' === $this->current_page) && $this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key("pixelio_api_key")) : ?>
				<div class="pixelio-cart-abandon-recovery-more-settings-menu">
					<button id="pixelio-cart-abandon-recovery-more-settings-btn"
							title="<?php esc_html_e('Settings', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>">

						<img width="100%"
							 src="<?php echo esc_attr(plugins_url('../../assets/icons/settings-icon.svg', __FILE__)); ?>"
							 alt="Icone settings Pixelio"
							 class="pixelio-settings-icon"
						/>
					</button>

					<div id="pixelio-cart-abandon-recovery-more-submenu"
						 class="pixelio-cart-abandon-recovery-more-submenu" style="display:none;">
						<a href="#" id="pixelio-cart-abandon-recovery-more-reset-first-config">
							<?php esc_html_e('Reconfigure your profile', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</a>
						<a href="#" id="pixelio-cart-abandon-recovery-more-disconnect-first-config">
							<?php esc_html_e('Disconnect your domain', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</a>
					</div>

				</div>
			<?php endif; ?>
		</div>
		<?php if (!('rivio-first-config' === $this->current_page)) : ?>
		<?php

		if ($this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key("pixelio_api_key")): ?>
			<div class="pixelio-cart-abandon-recovery-more-header-card pixelio-quota-card pixelio">
				<div class="pixelio-quota-card-header">
					<div class="pixelio-quota-card-icon">⚡</div>
					<div>
						<h2><?php esc_html_e('Quota Usage', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></h2>
						<p><?php esc_html_e('Track your current automation usage and smart recovery limits.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></p>
					</div>
				</div>

				<div class="pixelio-quota-usage">
					<div class="pixelio-cart-abandon-recovery-more-quota-horizontal">
						<div class="pixelio-cart-abandon-recovery-more-quota-label">
							<span><?php esc_html_e('Emails Sent', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
							<strong id="pixelio-cart-abandon-recovery-more-emails-count">0 / &#8734;</strong>
						</div>
						<div class="pixelio-cart-abandon-recovery-more-quota-bar">
							<div class="pixelio-cart-abandon-recovery-more-quota-fill"
								 id="pixelio-cart-abandon-recovery-more-emails-bar"></div>
						</div>
					</div>

					<div class="pixelio-cart-abandon-recovery-more-quota-horizontal">
						<div class="pixelio-cart-abandon-recovery-more-quota-label">
							<span><?php esc_html_e('Revenue Recovered', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
							<strong id="pixelio-cart-abandon-recovery-more-revenus-count">0€ / &#8734;</strong>
						</div>
						<div class="pixelio-cart-abandon-recovery-more-quota-bar">
							<div class="pixelio-cart-abandon-recovery-more-quota-fill"
								 id="pixelio-cart-abandon-recovery-more-revenus-bar"></div>
						</div>
					</div>
				</div>

				<div class="pixelio-cart-abandon-recovery-more-quota-footer">
					<div class="pixelio-quota-warning-icon">!</div>
					<div>
						<strong><?php echo esc_html__('Your email quota has been reached.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></strong>
						<p>
							<?php echo esc_html__('You can continue sending emails for free in manual mode by disconnecting the API via the settings icon.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
							<?php echo esc_html__('Disconnecting the API will remove AI-generated emails and advanced algorithm content such as segmentation scoring.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</p>

						<a href="<?php echo esc_attr(Pixelio_Cart_Abandon_Recovery_More_Util::$pack_page_url); ?>"
						   class="pixelio-cart-abandon-recovery-more-subscribe-btn">
							<?php esc_html_e('Subscribe to increase quota', 'rivio-ai-cart-recovery-marketing-email-automation'); ?> →
						</a>
					</div>
				</div>
			</div>
		<?php endif;
		if ($configured) :
			?>
			<div id="pixelio-cart-abandon-recovery-more-beta-signup-container" style="display:none; margin-top:20px;">
				<div class="pixelio-cart-abandon-recovery-more-beta-signup-wrapper"
					 style="padding:15px; background-color:#f5f8ff; border-left:4px solid #0D367A; border-radius:6px;">
					<div class="beta-intro">
						<strong><?php esc_html_e('Do you want to use all the features without limits FOR FREE ?', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></strong>
						<a href="#" id="pixelio-cart-abandon-recovery-more-beta-faq-toggle"
						   style="margin-left:10px; font-size:0.9em;"><?php esc_html_e('Know more', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></a>
					</div>

					<div class="pixelio-cart-abandon-recovery-more-beta-faq" style="display:none; margin-top:10px;">
						<label style="font-weight:600; display:block; margin-bottom:10px;">
							<input type="checkbox" id="pixelio-cart-abandon-recovery-more-join-beta" name="join_beta"/>
							<?php
							esc_html_e(
								'I agree to provide weekly feedback to report bugs and help improve the plugin, in exchange for free access to paid features.',
								'rivio-ai-cart-recovery-marketing-email-automation'
							);
							?>
						</label>

						<div class="pixelio-cart-abandon-recovery-more-beta-preference"
							 style="margin-top:10px; display:none;">
							<p><?php esc_attr_e('Select your preferred communication method:', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></p>

							<label style="margin-left:20px;">
								<input type="radio" name="comm_method"
									   value="email"/> <?php esc_attr_e('Email', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
							</label>
						</div>

						<div class="pixelio-cart-abandon-recovery-more-beta-extra-fields"
							 style="display:none; margin-top:10px;">

							<input type="email" name="email_username"
								   placeholder="<?php esc_attr_e('Your email', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>"
								   class="pixelio-cart-abandon-recovery-more-beta-input"
								   style="width:100%; padding:8px; border-radius:4px; background:none; border : 0; border-bottom:1px solid #ccc; display:none;"/>
						</div>

						<button id="pixelio-cart-abandon-recovery-more-beta-signup-btn" class="button">
							<?php esc_attr_e('Sign Up', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</button>
					</div>
				</div>
			</div>

		<?php elseif (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key("pixelio_api_key")): ?>
			<div id="continue-config-container" style=" margin-top:20px;">
				<div class="pixelio-cart-abandon-recovery-more-beta-signup-wrapper"
					 style="padding:15px; background-color:#f5f8ff; border-left:4px solid #0D367A; border-radius:6px;">
					<div class="continue-config" style="    display: flex;
	flex-direction: row;
	justify-content: space-between;">
						<strong><?php esc_attr_e("You haven't finished the initial configuration of the plugin. To take advantage of AI emailing & analytics, please get your FREE API KEY", 'rivio-ai-cart-recovery-marketing-email-automation'); ?></strong>
						<a href="<?php echo esc_url(Pixelio_Cart_Abandon_Recovery_More_Util::$first_config_page_url); ?>"
						   id="continue-config"
						   style="margin-left:10px; font-size:0.9em;"><?php esc_attr_e('Continue the configuration', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></a>
					</div>
				</div>
			</div>
		<?php endif; ?>
		<?php if (('rivio-ai-cart-recovery-marketing-email-automation-settings' === $this->current_page
				|| 'rivio-ai-cart-recovery-marketing-email-automation-email' === $this->current_page
				|| 'rivio-ai-cart-recovery-marketing-email-automation' === $this->current_page
				|| 'rivio-ai-cart-recovery-marketing-email-automation-faq' === $this->current_page
				|| 'rivio-ai-cart-recovery-marketing-email-automation-integrations' === $this->current_page ||
				'rivio-ai-cart-recovery-marketing-email-automation-integrations' === $this->current_page) &&
			$this->pixelio_util->pixelio_cart_abandon_recovery_more_is_unreachable($this->body)) : ?>
			<div id="pixelio-maintenance-container">
				<div class="pixelio-maintenance-wrapper">

					<div class="pixelio-maintenance-content">

						<div class="pixelio-maintenance-left">

							<div class="pixelio-maintenance-icon">
								⚡
							</div>

							<div class="pixelio-maintenance-text">

								<div class="pixelio-maintenance-title">
									<?php esc_attr_e(
										'Temporary maintenance in progress',
										'rivio-ai-cart-recovery-marketing-email-automation'
									); ?>
								</div>

								<div class="pixelio-maintenance-description">
									<?php esc_attr_e(
										'Rivio AI services are currently undergoing maintenance or deployment. Some features may be temporarily unavailable for a few minutes. Your settings and data remain completely safe.',
										'rivio-ai-cart-recovery-marketing-email-automation'
									); ?>
								</div>

							</div>

						</div>

						<div class="pixelio-maintenance-status">
                <span class="pixelio-maintenance-badge">
                    <span class="pixelio-maintenance-dot"></span>

                    <?php esc_attr_e(
						'Service temporarily unavailable',
						'rivio-ai-cart-recovery-marketing-email-automation'
					); ?>
                </span>
						</div>

					</div>

				</div>
			</div>
		<?php endif; ?>
	<?php endif;
	}
}
