<?php
/**
 * Base class for locked (Pro) widgets
 * 
 * @package Softech_Addons_for_Elementor
 * @since 1.0.0
 */

namespace Softech_Addons_for_Elementor\Widgets;

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

// Exit if Elementor is not loaded
if (!did_action('elementor/loaded')) {
    return;
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

abstract class Base_Locked_Widget extends Widget_Base {

    /**
     * Get widget metadata
     * Override this in child classes
     */
    abstract public function get_widget_metadata();

    /**
     * Check if pro is active
     */
    protected function is_pro_active() {
        return apply_filters('sits_elementor_addons_is_pro_active', false);
    }

    public function get_title() {
        $metadata = $this->get_widget_metadata();
        return $metadata['title'] ?? __('Locked Widget', 'softech-addons-for-elementor');
    }

    public function get_icon() {
        $metadata = $this->get_widget_metadata();
        return $metadata['icon'] ?? 'eicon-star';
    }

    public function get_categories() {
        $metadata = $this->get_widget_metadata();
        return $metadata['categories'] ?? ['sits-category'];
    }

    public function get_keywords() {
        $metadata = $this->get_widget_metadata();
        $keywords = $metadata['keywords'] ?? [];
        
        if (!$this->is_pro_active()) {
            $keywords = array_merge($keywords, ['locked', 'premium', 'pro']);
        }
        
        return $keywords;
    }

    public function get_description() {
        $metadata = $this->get_widget_metadata();
        return $metadata['description'] ?? '';
    }

    public function show_in_panel() {
        return true;
    }

    public function is_editable() {
        return $this->is_pro_active();
    }

    protected function register_controls() {
        if (!$this->is_pro_active()) {
            return;
        }
        $this->register_pro_controls();
    }

    protected function register_pro_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => esc_html__('Content', 'softech-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'pro_active_notice',
            [
                'type' => Controls_Manager::RAW_HTML,
                'raw' => '<div class="elementor-panel-alert elementor-panel-alert-success">' . 
                         esc_html__('Pro version is active! This widget is now unlocked.', 'softech-addons-for-elementor') . 
                         '</div>',
            ]
        );

        $this->end_controls_section();
    }

    protected function render() {
        if (!$this->is_pro_active()) {
            return;
        }
        $this->render_pro();
    }

    protected function render_pro() {
        echo '<div class="sitsel-pro-widget">';
        echo esc_html__('Pro widget content goes here', 'softech-addons-for-elementor');
        echo '</div>';
    }

    protected function content_template() {
        if (!$this->is_pro_active()) {
            return;
        }
        $this->content_template_pro();
    }

    protected function content_template_pro() {
        ?>
        <div class="sitsel-pro-widget">
            <?php echo esc_html__('Pro widget template', 'softech-addons-for-elementor'); ?>
        </div>
        <?php
    }

    public function before_render() {
        if (!$this->is_pro_active() && \Elementor\Plugin::$instance->editor->is_edit_mode()) {
            return false;
        }
        parent::before_render();
    }
   // Add BOTH methods (Elementor checks different methods in different versions)

// Add BOTH methods (Elementor checks different methods in different versions)

protected function get_upsale_data() {
    if ($this->is_pro_active()) {
        return false;
    }

    $metadata = $this->get_widget_metadata();
    
    return [
        'condition' => true,
        /* translators: %s: Widget title */
        'title' => sprintf(__('Unlock %s', 'softech-addons-for-elementor'), $metadata['title'] ?? 'Widget'),
        'description' => __('This is a premium widget. Upgrade to unlock it!', 'softech-addons-for-elementor'),
        // 'upgrade_url' => admin_url('admin.php?page=softech-addons-for-elementor&tab=pro'),
        'upgrade_url' => admin_url('admin.php?page=softech-addons-for-elementor&tab=pro'),
        'upgrade_text' => __('Upgrade Now', 'softech-addons-for-elementor'),
    ];
}

// Also add this method (different spelling)
public function get_promotion_data() {
    if ($this->is_pro_active()) {
        return [];
    }

    $metadata = $this->get_widget_metadata();
    
    return [
        /* translators: %s: Widget title */
        'title' => sprintf(__('Unlock %s', 'softech-addons-for-elementor'), $metadata['title'] ?? 'Widget'),
        'description' => __('Upgrade to Sitsel Pro to unlock this premium widget.', 'softech-addons-for-elementor'),
        'url' => admin_url('admin.php?page=softech-addons-for-elementor&tab=pro'),
        'cta_text' => __('Upgrade Now', 'softech-addons-for-elementor'),
    ];
}
}
