<?php
/**
 * Premium Component Base Class for GGM Commerce.
 *
 * Extends the base widget class with license verification for premium features.
 *
 * @package GGM_Commerce
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class GGM_Commerce_Premium_Widget
 *
 * Base class for all premium Elementor widgets that require license activation.
 */
abstract class GGM_Commerce_Premium_Widget extends GGM_Commerce_Base_Widget {

	/**
	 * The slug used for license activation check.
	 * Each child class must define this.
	 *
	 * @var string
	 */
	protected $premium_widget_slug = '';

	/**
	 * Get the categories for this widget.
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return array( 'ggmc-widgets' );
	}

	/**
	 * Check if this widget is activated via license.
	 *
	 * @return bool Whether the widget is activated.
	 */
	protected function is_widget_activated() {
		if ( empty( $this->premium_widget_slug ) ) {
			return false;
		}

		$license = ggm_commerce_license();
		return $license->is_widget_activated( $this->premium_widget_slug );
	}

	/**
	 * Render the widget output.
	 * Checks activation status before rendering actual content.
	 */
	protected function render() {
		// Always allow rendering in editor for preview purposes.
		$is_editor = \Elementor\Plugin::$instance->editor->is_edit_mode();

		if ( ! $this->is_widget_activated() && ! $is_editor ) {
			// Widget not activated - show nothing on frontend.
			return;
		}

		if ( ! $this->is_widget_activated() && $is_editor ) {
			// Show activation required message in editor.
			$this->render_activation_required();
			return;
		}

		// Widget is activated - render the actual content.
		$this->render_premium_content();
	}

	/**
	 * Render the activation required message for the editor.
	 */
	protected function render_activation_required() {
		$widget_name = $this->get_title();
		?>
		<div class="ggmc-premium-widget-locked" style="
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			color: white;
			padding: 30px;
			text-align: center;
			border-radius: 8px;
			font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
		">
			<div style="font-size: 48px; margin-bottom: 15px;">
				<span class="dashicons dashicons-lock" style="font-size: 48px; width: 48px; height: 48px;"></span>
			</div>
			<h3 style="color: white; margin: 0 0 10px 0; font-size: 20px;">
				<?php echo esc_html( $widget_name ); ?> - Premium Component
			</h3>
			<p style="margin: 0 0 20px 0; opacity: 0.9;">
				This widget requires activation in your Goose Commerce settings.
			</p>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=ggmc-settings&tab=premium-components' ) ); ?>"
			   target="_blank"
			   style="
				   display: inline-block;
				   background: rgba(255,255,255,0.2);
				   color: white;
				   padding: 10px 25px;
				   text-decoration: none;
				   border-radius: 5px;
				   font-weight: bold;
			   ">
				Activate Widget
			</a>
		</div>
		<?php
	}

	/**
	 * Render the premium content.
	 * Child classes must implement this method.
	 */
	abstract protected function render_premium_content();

	/**
	 * Get the keywords for search.
	 *
	 * @return array Keywords.
	 */
	public function get_keywords() {
		return array( 'ggm', 'commerce', 'premium', 'pro' );
	}

	/**
	 * Get widget icon.
	 * Can be overridden by child classes.
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-lock';
	}

	/**
	 * Whether to show this widget in the Elementor panel.
	 * Only show if the widget is activated via license.
	 *
	 * @return bool
	 */
	public function show_in_panel() {
		return $this->is_widget_activated();
	}
}
