<?php
/**
 * Admin dashboard pages.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor\Admin;

use ImpactAddons\ImpactWidgetsForElementor\Core\Widgets_Manager;

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

final class Dashboard {
	private string $menu_slug = 'iaew-dashboard';
	private string $legacy_grid_menu_slug = 'iapg-dashboard';

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ), 20 );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function register_menu(): void {
		$capability = 'manage_options';

		add_menu_page(
			esc_html__( 'Impact Addons', 'impact-widgets-for-elementor' ),
			esc_html__( 'Impact Addons', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug,
			array( $this, 'render_dashboard' ),
			'dashicons-screenoptions',
			58
		);

		add_submenu_page(
			$this->menu_slug,
			esc_html__( 'Dashboard', 'impact-widgets-for-elementor' ),
			esc_html__( 'Dashboard', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug,
			array( $this, 'render_dashboard' )
		);

		add_submenu_page(
			$this->menu_slug,
			esc_html__( 'Widgets', 'impact-widgets-for-elementor' ),
			esc_html__( 'Widgets', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug . '-widgets',
			array( $this, 'render_widgets_page' )
		);

		add_submenu_page(
			$this->menu_slug,
			esc_html__( 'Settings', 'impact-widgets-for-elementor' ),
			esc_html__( 'Settings', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug . '-settings',
			array( $this, 'render_settings_page' )
		);

		add_submenu_page(
			$this->menu_slug,
			esc_html__( 'Get Help', 'impact-widgets-for-elementor' ),
			esc_html__( 'Get Help', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug . '-help',
			array( $this, 'render_help_page' )
		);

		add_submenu_page(
			$this->menu_slug,
			esc_html__( 'Upgrade', 'impact-widgets-for-elementor' ),
			esc_html__( 'Upgrade', 'impact-widgets-for-elementor' ),
			$capability,
			$this->menu_slug . '-upgrade',
			array( $this, 'render_upgrade_page' )
		);

		add_submenu_page(
			null,
			esc_html__( 'Dashboard', 'impact-widgets-for-elementor' ),
			esc_html__( 'Dashboard', 'impact-widgets-for-elementor' ),
			$capability,
			$this->legacy_grid_menu_slug,
			array( $this, 'render_dashboard' )
		);
	}

	public function enqueue_assets( string $hook ): void {
		if ( false === strpos( $hook, $this->menu_slug ) && false === strpos( $hook, $this->legacy_grid_menu_slug ) ) {
			return;
		}

		wp_enqueue_style(
			'impact-widgets-elementor-admin-dashboard',
			IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/css/admin-dashboard.css',
			array(),
			$this->asset_version( 'assets/css/admin-dashboard.css' )
		);
	}

	public function render_dashboard(): void {
		$this->render_page( 'dashboard' );
	}

	public function render_widgets_page(): void {
		$this->render_page( 'widgets' );
	}

	public function render_settings_page(): void {
		$this->render_page( 'settings' );
	}

	public function render_help_page(): void {
		$this->render_page( 'help' );
	}

	public function render_upgrade_page(): void {
		$this->render_page( 'upgrade' );
	}

	private function render_page( string $active_tab ): void {
		$links        = $this->get_links();
		$widgets      = $this->get_widget_cards();
		$widget_count = count( Widgets_Manager::get_widget_map() );
		?>
		<div class="wrap iaew-admin">
			<?php $this->render_header( $links ); ?>
			<?php $this->render_tabs( $active_tab ); ?>

			<div class="iaew-admin__grid">
				<div class="iaew-admin__main">
					<?php
					if ( 'widgets' === $active_tab ) {
						$this->render_widgets_panel( $widgets, $links );
					} elseif ( 'settings' === $active_tab ) {
						$this->render_settings_panel( $links );
					} elseif ( 'help' === $active_tab ) {
						$this->render_help_panel( $links );
					} elseif ( 'upgrade' === $active_tab ) {
						$this->render_upgrade_panel( $links );
					} else {
						$this->render_dashboard_panels( $widget_count, $links );
					}
					?>
				</div>

				<?php $this->render_sidebar( $links, $active_tab ); ?>
			</div>
		</div>
		<?php
	}

	private function render_header( array $links ): void {
		?>
		<div class="iaew-admin__topbar">
			<div class="iaew-admin__brand">
				<div class="iaew-admin__logo">IA</div>
				<div>
					<h1><?php esc_html_e( 'Impact Addons Dashboard', 'impact-widgets-for-elementor' ); ?></h1>
					<p><?php esc_html_e( 'Manage free widgets and explore Impact Addons products from one place.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
			</div>
			<div class="iaew-admin__actions">
				<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-settings' ) ); ?>"><?php esc_html_e( 'Settings', 'impact-widgets-for-elementor' ); ?></a>
				<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Manage Widgets', 'impact-widgets-for-elementor' ); ?></a>
				<a class="button iaew-admin__button-pro" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Go Pro', 'impact-widgets-for-elementor' ); ?></a>
			</div>
		</div>
		<?php
	}

	private function render_tabs( string $active_tab ): void {
		?>
		<div class="iaew-admin__tabs">
			<?php foreach ( $this->get_tabs() as $tab ) : ?>
				<a class="iaew-admin__tab<?php echo $active_tab === $tab['slug'] ? ' is-active' : ''; ?>" href="<?php echo esc_url( $tab['url'] ); ?>"><?php echo esc_html( $tab['label'] ); ?></a>
			<?php endforeach; ?>
		</div>
		<?php
	}

	private function render_dashboard_panels( int $widget_count, array $links ): void {
		?>
		<div class="iaew-admin__panel iaew-admin__hero">
			<div>
				<h2><?php esc_html_e( 'Manage Impact Widgets from one dashboard', 'impact-widgets-for-elementor' ); ?></h2>
				<p><?php esc_html_e( 'Review the free Elementor widgets included with this plugin, open the widget list, and jump to Impact Addons resources when you need more tools.', 'impact-widgets-for-elementor' ); ?></p>
				<div class="iaew-admin__hero-actions">
					<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Open Widgets', 'impact-widgets-for-elementor' ); ?></a>
					<a class="button" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'View Plans', 'impact-widgets-for-elementor' ); ?></a>
					<a class="button" href="<?php echo esc_url( $links['contact'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Contact Us', 'impact-widgets-for-elementor' ); ?></a>
				</div>
			</div>
			<div class="iaew-admin__hero-card">
				<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Free Toolkit', 'impact-widgets-for-elementor' ); ?></div>
				<h3><?php echo esc_html( sprintf( _n( '%d widget available', '%d widgets available', $widget_count, 'impact-widgets-for-elementor' ), $widget_count ) ); ?></h3>
				<ul>
					<li><?php esc_html_e( 'Free Elementor widget modules', 'impact-widgets-for-elementor' ); ?></li>
					<li><?php esc_html_e( 'Shared Impact Addons admin layout', 'impact-widgets-for-elementor' ); ?></li>
					<li><?php esc_html_e( 'Upgrade links for premium products', 'impact-widgets-for-elementor' ); ?></li>
				</ul>
			</div>
		</div>

		<div class="iaew-admin__panel iaew-admin__widget-summary">
			<div class="iaew-admin__panel-head">
				<div>
					<h2><?php esc_html_e( 'Widgets / Features', 'impact-widgets-for-elementor' ); ?></h2>
					<p><?php esc_html_e( 'Open the Widgets page to review every free widget included with this plugin.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Open Widgets page', 'impact-widgets-for-elementor' ); ?></a>
			</div>
			<div class="iaew-admin__widget-preview">
				<div>
					<strong><?php echo esc_html( sprintf( _n( '%d free widget ready', '%d free widgets ready', $widget_count, 'impact-widgets-for-elementor' ), $widget_count ) ); ?></strong>
					<p><?php esc_html_e( 'Use Elementor to place these widgets on your pages, templates, and posts.', 'impact-widgets-for-elementor' ); ?></p>
					<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Go To Widgets', 'impact-widgets-for-elementor' ); ?></a>
				</div>
			</div>
		</div>

		<?php $this->render_conversion_boxes( $links ); ?>

		<div class="iaew-admin__panel">
			<h2><?php esc_html_e( 'What is next', 'impact-widgets-for-elementor' ); ?></h2>
			<p><?php esc_html_e( 'Explore premium addons when you need more Elementor widgets, layouts, form tools, and template builder features.', 'impact-widgets-for-elementor' ); ?></p>
			<div class="iaew-admin__feature-grid">
				<div class="iaew-admin__feature-card">
					<h3><?php esc_html_e( 'More Widget Packs', 'impact-widgets-for-elementor' ); ?></h3>
					<p><?php esc_html_e( 'Add premium widgets and design controls when your site needs a larger toolkit.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<div class="iaew-admin__feature-card">
					<h3><?php esc_html_e( 'Grid Layouts', 'impact-widgets-for-elementor' ); ?></h3>
					<p><?php esc_html_e( 'Build post grids, sliders, carousels, and layout-heavy sections with pro addons.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
			</div>
		</div>
		<?php
	}

	private function render_widgets_panel( array $widgets, array $links ): void {
		?>
		<div class="iaew-admin__panel">
			<h2><?php esc_html_e( 'Widgets / Features', 'impact-widgets-for-elementor' ); ?></h2>
			<p><?php esc_html_e( 'Review the free Elementor widgets available from this plugin.', 'impact-widgets-for-elementor' ); ?></p>

			<h3 class="iaew-admin__section-heading"><?php esc_html_e( 'Impact Widgets for Elementor', 'impact-widgets-for-elementor' ); ?></h3>
			<p class="iaew-admin__section-description"><?php esc_html_e( 'These widgets are included in the free plugin and are available in Elementor.', 'impact-widgets-for-elementor' ); ?></p>

			<div class="iaew-admin__widget-list">
				<?php foreach ( $widgets as $widget ) : ?>
					<div class="iaew-admin__widget-row">
						<span class="iaew-admin__widget-icon dashicons <?php echo esc_attr( $widget['icon'] ); ?>" aria-hidden="true"></span>
						<span>
							<strong><?php echo esc_html( $widget['title'] ); ?></strong>
							<small><?php echo esc_html( $widget['description'] ); ?></small>
						</span>
						<span class="iaew-admin__switch is-on" aria-hidden="true"></span>
					</div>
				<?php endforeach; ?>
			</div>

			<div class="iaew-admin__hero-actions iaew-admin__panel-actions">
				<a class="button button-primary" href="<?php echo esc_url( admin_url( 'edit.php?post_type=page' ) ); ?>"><?php esc_html_e( 'Open Pages', 'impact-widgets-for-elementor' ); ?></a>
				<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-settings' ) ); ?>"><?php esc_html_e( 'Open Settings', 'impact-widgets-for-elementor' ); ?></a>
			</div>
		</div>
		<?php $this->render_conversion_boxes( $links ); ?>
		<?php
	}

	private function render_settings_panel( array $links ): void {
		?>
		<div class="iaew-admin__panel">
			<h2><?php esc_html_e( 'Settings', 'impact-widgets-for-elementor' ); ?></h2>
			<p><?php esc_html_e( 'This free plugin does not require a license key. All included widgets are available after plugin activation.', 'impact-widgets-for-elementor' ); ?></p>

			<div class="iaew-admin__settings-request">
				<img src="<?php echo esc_url( IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-feature-request.svg' ); ?>" alt="" loading="lazy" />
				<div>
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Feature Request', 'impact-widgets-for-elementor' ); ?></div>
					<h3><?php esc_html_e( 'Need a widget we do not have yet?', 'impact-widgets-for-elementor' ); ?></h3>
					<p><?php esc_html_e( 'Tell us which Elementor widget, layout, or workflow would help your next build.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<a class="button button-primary" href="<?php echo esc_url( $links['feature_request'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Request Feature', 'impact-widgets-for-elementor' ); ?></a>
			</div>

			<div class="iaew-admin__feature-grid">
				<div class="iaew-admin__feature-card iaew-admin__settings-card">
					<img src="<?php echo esc_url( IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-knowledge-base.svg' ); ?>" alt="" loading="lazy" />
					<div class="iaew-admin__settings-card-body">
						<h3><?php esc_html_e( 'Free Widgets Enabled', 'impact-widgets-for-elementor' ); ?></h3>
						<p><?php esc_html_e( 'Widget availability is handled automatically by the plugin registry.', 'impact-widgets-for-elementor' ); ?></p>
						<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Manage Widgets', 'impact-widgets-for-elementor' ); ?></a>
					</div>
				</div>
				<div class="iaew-admin__feature-card iaew-admin__settings-card">
					<img src="<?php echo esc_url( IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-upgrade-widgets-pro.webp' ); ?>" alt="" loading="lazy" />
					<div class="iaew-admin__settings-card-body">
						<h3><?php esc_html_e( 'Premium Addons', 'impact-widgets-for-elementor' ); ?></h3>
						<p><?php esc_html_e( 'Use the Go Pro links when you want premium products with advanced controls.', 'impact-widgets-for-elementor' ); ?></p>
						<a class="button button-primary" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'View Pricing', 'impact-widgets-for-elementor' ); ?></a>
					</div>
				</div>
				<div class="iaew-admin__feature-card iaew-admin__settings-card">
					<img src="<?php echo esc_url( IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-centre.svg' ); ?>" alt="" loading="lazy" />
					<div class="iaew-admin__settings-card-body">
						<h3><?php esc_html_e( 'License Status', 'impact-widgets-for-elementor' ); ?></h3>
						<p><?php esc_html_e( 'This free plugin does not need a license key. Premium addons include their own license management after purchase.', 'impact-widgets-for-elementor' ); ?></p>
						<a class="button" href="<?php echo esc_url( $links['contact'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Contact Support', 'impact-widgets-for-elementor' ); ?></a>
					</div>
				</div>
			</div>
			<div class="iaew-admin__hero-actions iaew-admin__panel-actions">
				<a class="button button-primary" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Go Pro', 'impact-widgets-for-elementor' ); ?></a>
				<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ) ); ?>"><?php esc_html_e( 'Manage Widgets', 'impact-widgets-for-elementor' ); ?></a>
			</div>
		</div>
		<?php $this->render_conversion_boxes( $links ); ?>
		<?php
	}

	private function render_help_panel( array $links ): void {
		?>
		<div class="iaew-admin__panel iaew-admin__help-panel">
			<div class="iaew-admin__upgrade-head">
				<div>
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Support', 'impact-widgets-for-elementor' ); ?></div>
					<h2><?php esc_html_e( 'Get Help', 'impact-widgets-for-elementor' ); ?></h2>
					<p><?php esc_html_e( 'Use the right support path for product help, documentation, feature ideas, and WordPress.org reviews.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
			</div>

			<div class="iaew-admin__help-grid">
				<?php foreach ( $this->get_help_cards( $links ) as $card ) : ?>
					<article class="iaew-admin__help-card">
						<a class="iaew-admin__help-image-link" href="<?php echo esc_url( $card['url'] ); ?>" target="_blank" rel="noreferrer" aria-label="<?php echo esc_attr( $card['title'] ); ?>">
							<img src="<?php echo esc_url( $card['image'] ); ?>" alt="" loading="lazy" />
						</a>
						<div class="iaew-admin__help-body">
							<h3><?php echo esc_html( $card['title'] ); ?></h3>
							<p><?php echo esc_html( $card['description'] ); ?></p>
							<a class="button<?php echo ! empty( $card['primary'] ) ? ' button-primary' : ''; ?>" href="<?php echo esc_url( $card['url'] ); ?>" target="_blank" rel="noreferrer"><?php echo esc_html( $card['button'] ); ?></a>
						</div>
					</article>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	private function render_upgrade_panel( array $links ): void {
		?>
		<div class="iaew-admin__panel iaew-admin__upgrade-panel">
			<div class="iaew-admin__upgrade-head">
				<div>
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Upgrade', 'impact-widgets-for-elementor' ); ?></div>
					<h2><?php esc_html_e( 'Build more with Impact Addons Pro', 'impact-widgets-for-elementor' ); ?></h2>
					<p><?php esc_html_e( 'Choose the premium product that matches your next Elementor workflow.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<a class="button button-primary" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Compare Plans', 'impact-widgets-for-elementor' ); ?></a>
			</div>

			<div class="iaew-admin__upgrade-grid">
				<?php foreach ( $this->get_product_links( $links ) as $product ) : ?>
					<article class="iaew-admin__upgrade-card">
						<a class="iaew-admin__upgrade-image-link" href="<?php echo esc_url( $product['url'] ); ?>" target="_blank" rel="noreferrer" aria-label="<?php echo esc_attr( $product['label'] ); ?>">
							<img src="<?php echo esc_url( $product['image'] ); ?>" alt="" loading="lazy" />
						</a>
						<div class="iaew-admin__upgrade-body">
							<h3><?php echo esc_html( $product['label'] ); ?></h3>
							<p><?php echo esc_html( $product['description'] ); ?></p>
							<ul>
								<?php foreach ( $product['features'] as $feature ) : ?>
									<li><?php echo esc_html( $feature ); ?></li>
								<?php endforeach; ?>
							</ul>
							<div class="iaew-admin__upgrade-actions">
								<a class="button button-primary" href="<?php echo esc_url( $product['url'] ); ?>" target="_blank" rel="noreferrer"><?php echo esc_html( $product['cta'] ); ?></a>
								<a class="button" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'View Pricing', 'impact-widgets-for-elementor' ); ?></a>
							</div>
						</div>
					</article>
				<?php endforeach; ?>
			</div>

			<div class="iaew-admin__upgrade-footer">
				<div>
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Bundle Option', 'impact-widgets-for-elementor' ); ?></div>
					<h3><?php esc_html_e( 'Need more than one product?', 'impact-widgets-for-elementor' ); ?></h3>
					<p><?php esc_html_e( 'Compare plans and choose the best upgrade path for widgets, grids, forms, and template builder tools.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<a class="button button-primary" href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'View Pricing', 'impact-widgets-for-elementor' ); ?></a>
			</div>
		</div>
		<?php
	}

	private function render_conversion_boxes( array $links ): void {
		?>
		<div class="iaew-admin__panel iaew-admin__conversion-panel">
			<div class="iaew-admin__panel-head">
				<div>
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Go Pro', 'impact-widgets-for-elementor' ); ?></div>
					<h2><?php esc_html_e( 'Upgrade when your site needs more', 'impact-widgets-for-elementor' ); ?></h2>
					<p><?php esc_html_e( 'Pick the next product by the job you are trying to finish in Elementor.', 'impact-widgets-for-elementor' ); ?></p>
				</div>
				<a href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Compare all plans', 'impact-widgets-for-elementor' ); ?></a>
			</div>

			<div class="iaew-admin__conversion-grid">
				<?php foreach ( $this->get_conversion_cards( $links ) as $card ) : ?>
					<a class="iaew-admin__conversion-card" href="<?php echo esc_url( $card['url'] ); ?>" target="_blank" rel="noreferrer">
						<span class="dashicons <?php echo esc_attr( $card['icon'] ); ?>" aria-hidden="true"></span>
						<strong><?php echo esc_html( $card['title'] ); ?></strong>
						<small><?php echo esc_html( $card['description'] ); ?></small>
						<em><?php echo esc_html( $card['cta'] ); ?></em>
					</a>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	private function render_sidebar( array $links, string $active_tab ): void {
		?>
		<div class="iaew-admin__sidebar">
			<?php if ( 'upgrade' !== $active_tab ) : ?>
				<div class="iaew-admin__panel iaew-admin__promo-card">
					<div class="iaew-admin__hero-kicker"><?php esc_html_e( 'Get Pro', 'impact-widgets-for-elementor' ); ?></div>
					<h2><?php esc_html_e( 'Unlock more Impact Addons tools', 'impact-widgets-for-elementor' ); ?></h2>
					<p><?php esc_html_e( 'Explore premium widgets, grid layouts, forms, and template builder products for Elementor.', 'impact-widgets-for-elementor' ); ?></p>
					<div class="iaew-admin__quick-links">
						<a href="<?php echo esc_url( $links['pricing'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'View Pricing', 'impact-widgets-for-elementor' ); ?></a>
						<?php foreach ( $this->get_product_links( $links ) as $product ) : ?>
							<a href="<?php echo esc_url( $product['url'] ); ?>" target="_blank" rel="noreferrer"><?php echo esc_html( $product['label'] ); ?></a>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>

			<div class="iaew-admin__panel iaew-admin__support-card is-contact">
				<span class="dashicons dashicons-email-alt2" aria-hidden="true"></span>
				<h2><?php esc_html_e( 'Contact Us', 'impact-widgets-for-elementor' ); ?></h2>
				<p><?php esc_html_e( 'Need help choosing the right Impact Addons product?', 'impact-widgets-for-elementor' ); ?></p>
				<a class="button button-primary" href="<?php echo esc_url( $links['contact'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Contact Support', 'impact-widgets-for-elementor' ); ?></a>
			</div>

			<div class="iaew-admin__panel iaew-admin__support-card is-feature">
				<span class="dashicons dashicons-lightbulb" aria-hidden="true"></span>
				<h2><?php esc_html_e( 'Feature Request', 'impact-widgets-for-elementor' ); ?></h2>
				<p><?php esc_html_e( 'Tell us what widget, layout, or Elementor workflow you want next.', 'impact-widgets-for-elementor' ); ?></p>
				<a class="button" href="<?php echo esc_url( $links['feature_request'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Request Feature', 'impact-widgets-for-elementor' ); ?></a>
			</div>

			<div class="iaew-admin__panel">
				<h2><?php esc_html_e( 'Quick Access', 'impact-widgets-for-elementor' ); ?></h2>
				<div class="iaew-admin__quick-links">
					<a href="<?php echo esc_url( $links['website'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Knowledge Base', 'impact-widgets-for-elementor' ); ?></a>
					<a href="<?php echo esc_url( $links['contact'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Help Centre', 'impact-widgets-for-elementor' ); ?></a>
					<a href="<?php echo esc_url( $links['feature_request'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Request a Feature', 'impact-widgets-for-elementor' ); ?></a>
					<a href="<?php echo esc_url( $links['wordpress_org'] ); ?>" target="_blank" rel="noreferrer"><?php esc_html_e( 'Rate Us', 'impact-widgets-for-elementor' ); ?></a>
				</div>
			</div>
		</div>
		<?php
	}

	private function get_tabs(): array {
		return array(
			array(
				'slug'  => 'dashboard',
				'label' => __( 'Dashboard', 'impact-widgets-for-elementor' ),
				'url'   => admin_url( 'admin.php?page=' . $this->menu_slug ),
			),
			array(
				'slug'  => 'widgets',
				'label' => __( 'Widgets', 'impact-widgets-for-elementor' ),
				'url'   => admin_url( 'admin.php?page=' . $this->menu_slug . '-widgets' ),
			),
			array(
				'slug'  => 'settings',
				'label' => __( 'Settings', 'impact-widgets-for-elementor' ),
				'url'   => admin_url( 'admin.php?page=' . $this->menu_slug . '-settings' ),
			),
			array(
				'slug'  => 'help',
				'label' => __( 'Get Help', 'impact-widgets-for-elementor' ),
				'url'   => admin_url( 'admin.php?page=' . $this->menu_slug . '-help' ),
			),
			array(
				'slug'  => 'upgrade',
				'label' => __( 'Upgrade', 'impact-widgets-for-elementor' ),
				'url'   => admin_url( 'admin.php?page=' . $this->menu_slug . '-upgrade' ),
			),
		);
	}

	private function get_widget_cards(): array {
		$icon_map = array(
			'advanced-heading'   => 'dashicons-editor-textcolor',
			'advanced-button'    => 'dashicons-button',
			'iframe-embed'       => 'dashicons-embed-generic',
			'google-map'         => 'dashicons-location-alt',
			'advanced-accordion' => 'dashicons-admin-generic',
			'advanced-tabs'      => 'dashicons-index-card',
			'call-to-action'     => 'dashicons-megaphone',
			'logo-showcase'      => 'dashicons-images-alt2',
			'icon-info-list'     => 'dashicons-list-view',
			'post-categories'    => 'dashicons-category',
			'post-tags'          => 'dashicons-tag',
			'related-posts'      => 'dashicons-admin-post',
		);

		$cards = array();

		foreach ( Widgets_Manager::get_widget_map() as $slug => $class_name ) {
			$cards[] = array(
				'slug'        => $slug,
				'title'       => Widgets_Manager::get_widget_title( $slug ),
				'description' => esc_html__( 'Elementor widget module', 'impact-widgets-for-elementor' ),
				'icon'        => $icon_map[ $slug ] ?? 'dashicons-screenoptions',
			);
		}

		return $cards;
	}

	private function get_links(): array {
		return array(
			'website'         => 'https://impactaddons.com/',
			'pricing'         => 'https://impactaddons.com/pricing/',
			'contact'         => 'https://impactaddons.com/contact/',
			'feature_request' => 'https://impactaddons.com/feature-request/',
			'wordpress_org'   => 'https://wordpress.org/plugins/impact-widgets-for-elementor',
			'widgets_pro'     => 'https://impactaddons.com/impact-widgets-for-elementor/',
			'grid'            => 'https://impactaddons.com/impact-grid-addon-for-elementor/',
			'forms'           => 'https://impactaddons.com/impact-forms-for-elementor/',
			'template_builder'=> 'https://impactaddons.com/impact-template-builder/',
		);
	}

	private function get_help_cards( array $links ): array {
		return array(
			array(
				'title'       => __( 'Help Centre', 'impact-widgets-for-elementor' ),
				'description' => __( 'Get direct support for product questions, setup issues, and account guidance.', 'impact-widgets-for-elementor' ),
				'button'      => __( 'Contact Support', 'impact-widgets-for-elementor' ),
				'url'         => $links['contact'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-centre.svg',
				'primary'     => true,
			),
			array(
				'title'       => __( 'Feature Request', 'impact-widgets-for-elementor' ),
				'description' => __( 'Tell us what widget, layout, or workflow you want in a future release.', 'impact-widgets-for-elementor' ),
				'button'      => __( 'Request Feature', 'impact-widgets-for-elementor' ),
				'url'         => $links['feature_request'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-feature-request.svg',
				'primary'     => false,
			),
			array(
				'title'       => __( 'Knowledge Base', 'impact-widgets-for-elementor' ),
				'description' => __( 'Browse Impact Addons resources, product pages, and learning material.', 'impact-widgets-for-elementor' ),
				'button'      => __( 'Open Knowledge Base', 'impact-widgets-for-elementor' ),
				'url'         => $links['website'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-knowledge-base.svg',
				'primary'     => false,
			),
			array(
				'title'       => __( 'Rate Us', 'impact-widgets-for-elementor' ),
				'description' => __( 'Share your feedback on WordPress.org and help other Elementor users find the plugin.', 'impact-widgets-for-elementor' ),
				'button'      => __( 'Rate on WordPress.org', 'impact-widgets-for-elementor' ),
				'url'         => $links['wordpress_org'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-help-rate-us.svg',
				'primary'     => true,
			),
		);
	}

	private function get_conversion_cards( array $links ): array {
		return array(
			array(
				'title'       => __( 'More Elementor widgets', 'impact-widgets-for-elementor' ),
				'description' => __( 'Add premium content widgets and deeper design controls.', 'impact-widgets-for-elementor' ),
				'cta'         => __( 'View Widgets Pro', 'impact-widgets-for-elementor' ),
				'url'         => $links['widgets_pro'],
				'icon'        => 'dashicons-screenoptions',
			),
			array(
				'title'       => __( 'Post grids and sliders', 'impact-widgets-for-elementor' ),
				'description' => __( 'Create listing layouts, carousels, and editorial sections.', 'impact-widgets-for-elementor' ),
				'cta'         => __( 'View Grid Addon', 'impact-widgets-for-elementor' ),
				'url'         => $links['grid'],
				'icon'        => 'dashicons-grid-view',
			),
			array(
				'title'       => __( 'Forms and submissions', 'impact-widgets-for-elementor' ),
				'description' => __( 'Build form workflows and collect leads inside WordPress.', 'impact-widgets-for-elementor' ),
				'cta'         => __( 'View Impact Forms', 'impact-widgets-for-elementor' ),
				'url'         => $links['forms'],
				'icon'        => 'dashicons-feedback',
			),
		);
	}

	private function get_product_links( array $links ): array {
		return array(
			array(
				'label'       => __( 'Impact Widgets Pro', 'impact-widgets-for-elementor' ),
				'description' => __( 'Premium Elementor widgets and design controls.', 'impact-widgets-for-elementor' ),
				'url'         => $links['widgets_pro'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-upgrade-widgets-pro.webp',
				'cta'         => __( 'Explore Widgets Pro', 'impact-widgets-for-elementor' ),
				'features'    => array(
					__( 'More content widgets', 'impact-widgets-for-elementor' ),
					__( 'Advanced style controls', 'impact-widgets-for-elementor' ),
					__( 'Faster Elementor builds', 'impact-widgets-for-elementor' ),
				),
			),
			array(
				'label'       => __( 'Impact Grid Addon', 'impact-widgets-for-elementor' ),
				'description' => __( 'Post grids, sliders, carousels, and layout modules.', 'impact-widgets-for-elementor' ),
				'url'         => $links['grid'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-upgrade-grid-addon.png',
				'cta'         => __( 'Explore Grid Addon', 'impact-widgets-for-elementor' ),
				'features'    => array(
					__( 'Post grid layouts', 'impact-widgets-for-elementor' ),
					__( 'Carousel and slider modules', 'impact-widgets-for-elementor' ),
					__( 'Editorial listing designs', 'impact-widgets-for-elementor' ),
				),
			),
			array(
				'label'       => __( 'Impact Forms', 'impact-widgets-for-elementor' ),
				'description' => __( 'Elementor forms, submissions, and form workflows.', 'impact-widgets-for-elementor' ),
				'url'         => $links['forms'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-upgrade-forms.png',
				'cta'         => __( 'Explore Forms', 'impact-widgets-for-elementor' ),
				'features'    => array(
					__( 'Form widgets', 'impact-widgets-for-elementor' ),
					__( 'Saved submissions', 'impact-widgets-for-elementor' ),
					__( 'Lead capture workflows', 'impact-widgets-for-elementor' ),
				),
			),
			array(
				'label'       => __( 'Impact Template Builder', 'impact-widgets-for-elementor' ),
				'description' => __( 'WooCommerce and post template builder tools.', 'impact-widgets-for-elementor' ),
				'url'         => $links['template_builder'],
				'image'       => IMPACT_WIDGETS_ELEMENTOR_URL . 'assets/images/admin-upgrade-template-builder.png',
				'cta'         => __( 'Explore Template Builder', 'impact-widgets-for-elementor' ),
				'features'    => array(
					__( 'WooCommerce templates', 'impact-widgets-for-elementor' ),
					__( 'Single post layouts', 'impact-widgets-for-elementor' ),
					__( 'Theme builder workflows', 'impact-widgets-for-elementor' ),
				),
			),
		);
	}

	private function asset_version( string $relative_path ): string {
		$file = IMPACT_WIDGETS_ELEMENTOR_PATH . ltrim( $relative_path, '/\\' );

		if ( file_exists( $file ) ) {
			$modified = filemtime( $file );
			if ( false !== $modified ) {
				return (string) $modified;
			}
		}

		return IMPACT_WIDGETS_ELEMENTOR_VERSION;
	}
}
