<?php

namespace Unlockafe_addons\Widgets;
use Elementor\Widget_Base;
if (! defined('ABSPATH')) exit; // Exit if accessed directly


/**
 * Advance Coupons
 *
 * @since 1.0.0
 */

class Advance_Coupons extends Widget_Base
{
	public function get_name()
	{
		return 'unlockafe-advance-coupons';
	}

	public function get_title()
	{
		return __('Advance Coupons', 'unlock-addons-for-elementor');
	}

	public function get_icon()
	{
		return 'eicon-coupon ulicon';
	}

	public function get_keywords()
	{
		return ['unlock', 'coupon', 'advance'];
	}

	public function get_categories()
	{
		return ['unlock-addons-pro'];
	}

	public function get_script_depends()
	{
		return [
			'unlockafe-advance-coupons-scripts'
		];
	}

	public function get_style_depends()
	{
		return [
			'unlockafe-advance-coupons-style'
		];
	}

    protected function get_coupon_categories() {
        // Use the Coupon_Categories class for consistent categories
        if (class_exists('\Unlockafe_addons\Classes\Coupon_Categories')) {
            $coupon_categories = new \Unlockafe_addons\Classes\Coupon_Categories();
            return $coupon_categories->get_available_categories();
        }
        
        // Fallback categories
        return [
            'seasonal' => __('Seasonal', 'unlock-addons-for-elementor'),
            'new-customer' => __('New Customer', 'unlock-addons-for-elementor'),
            'loyalty' => __('Loyalty Program', 'unlock-addons-for-elementor'),
            'flash-sale' => __('Flash Sale', 'unlock-addons-for-elementor'),
            'bulk-discount' => __('Bulk Discount', 'unlock-addons-for-elementor'),
            'free-shipping' => __('Free Shipping', 'unlock-addons-for-elementor'),
        ];
    }

    protected function register_controls()
    {
        // Content Tab
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'unlock-addons-for-elementor'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'coupon_categories',
            [
                'label' => __('Filter by Category', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => true,
                'options' => $this->get_coupon_categories(),
                'label_block' => true,
                'description' => __('Show coupons from specific categories. Leave empty to show all.', 'unlock-addons-for-elementor'),
            ]
        );

        $this->end_controls_section();

        // Style Tab
        $this->start_controls_section(
            'style_section',
            [
                'label' => __('Styling', 'unlock-addons-for-elementor'),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'heading_color',
            [
                'label' => __('Heading Color', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-coupon-list-heading' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'heading_typography',
                'label' => __('Heading Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .ua-coupon-list-heading',
            ]
        );

        $this->add_control(
            'coupon_code_color',
            [
                'label' => __('Coupon Code Color', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-coupon-code' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' => 'coupon_code_typography',
                'label' => __('Coupon Code Typography', 'unlock-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .ua-coupon-code',
            ]
        );

        $this->add_control(
            'apply_button_color',
            [
                'label' => __('Apply Button Color', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-apply-coupon' => 'color: {{VALUE}}; border-color: {{VALUE}}',
                ],
            ]
        );

        $this->add_control(
            'apply_button_bg_color',
            [
                'label' => __('Apply Button Background', 'unlock-addons-for-elementor'),
                'type' => \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .ua-apply-coupon' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $categories = !empty($settings['coupon_categories']) ? $settings['coupon_categories'] : [];

        if (!class_exists('Unlockafe_addons\Classes\Coupons_Controller')) {
            if (is_admin()) {
                echo '<div class="ua-alert ua-alert-warning">' . esc_html__('Coupons Controller not found.', 'unlock-addons-for-elementor') . '</div>';
            }
            return;
        }

        $coupons_controller = new \Unlockafe_addons\Classes\Coupons_Controller();
        $available_coupons = $coupons_controller->get_available_coupons($categories);

        if (empty($available_coupons)) {
            if (is_admin()) {
                echo '<p>' . esc_html__('No coupons available for the selected criteria.', 'unlock-addons-for-elementor') . '</p>';
            }
            return;
        }
        ?>
        <div class="ua-coupon-list-wrapper" 
             data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" 
             data-nonce="<?php echo esc_attr(wp_create_nonce('unlockafe_coupon_nonce')); ?>">
            <div class="ua-coupon-message" style="display: none;"></div>
            <h3 class="ua-coupon-list-heading"><?php echo esc_html__('Available Coupons', 'unlock-addons-for-elementor'); ?></h3>
            <ul class="ua-coupon-list">
                <?php foreach ($available_coupons as $coupon) : ?>
                    <li class="ua-coupon-item">
                        <div class="ua-coupon-info">
                            <strong class="ua-coupon-code"><?php echo esc_html($coupon->get_code()); ?></strong>
                            <?php if ($coupon->get_description()) : ?>
                                <span class="ua-coupon-description"><?php echo esc_html($coupon->get_description()); ?></span>
                            <?php endif; ?>
                        </div>
                        <a href="#" class="ua-apply-coupon unlockafe-apply-coupon-link" data-coupon-code="<?php echo esc_attr($coupon->get_code()); ?>">
                            <?php echo esc_html__('Apply Coupon', 'unlock-addons-for-elementor'); ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
            <div class="ua-coupon-message" style="display:none;"></div>
        </div>
        <?php
    }
}