/**
 * AskAny Pro Modal JavaScript API
 * 
 * Provides a reusable API for showing Pro upgrade modals
 * 
 * @package Askany
 * @since 1.9.0
 */

(function($) {
	'use strict';
	
	/**
	 * AskanyPro namespace
	 */
	window.AskanyPro = window.AskanyPro || {};
	
	/**
	 * Open the Pro upgrade modal
	 * 
	 * @param {Object} options - Modal options
	 * @param {string} options.title - Modal title (optional)
	 * @param {string} options.description - Modal description (optional)
	 * @param {Array} options.features - Array of feature strings (optional)
	 * @param {string} options.upgradeUrl - Custom upgrade URL (optional)
	 * @param {boolean} options.useDynamicPricing - Use dynamic pricing modal if available (default: true)
	 */
	AskanyPro.openModal = function(options) {
		options = options || {};
		
		// Check if dynamic pricing is available and should be used
		if (typeof AskanyPricing !== 'undefined' && options.useDynamicPricing !== false) {
			// Use dynamic pricing modal (it will check topbar_active internally)
			AskanyPricing.showModal(options);
			return;
		}
		
		// Fallback to default Pro modal
		var $modal = $('#askany-pro-modal');
		
		// If modal doesn't exist, create it
		if ($modal.length === 0) {
			$modal = AskanyPro.createModal();
		}
		
		// Update modal content if custom options provided
		if (options.title) {
			$modal.find('.askany-pro-modal-header h2').text(options.title);
		}
		
		if (options.description) {
			$modal.find('.askany-pro-modal-header p').text(options.description);
		}
		
		if (options.features && options.features.length > 0) {
			var $featuresList = $modal.find('.askany-pro-features');
			$featuresList.empty();
			
			options.features.forEach(function(feature) {
				$featuresList.append(
					'<li>' +
					'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
					'<polyline points="20 6 9 17 4 12"></polyline>' +
					'</svg>' +
					'<span>' + feature + '</span>' +
					'</li>'
				);
			});
		}
		
		if (options.upgradeUrl) {
			$modal.find('.askany-pro-upgrade-btn').attr('href', options.upgradeUrl);
		}
		
		// Show modal
		$modal.fadeIn(300);
		$('body').css('overflow', 'hidden');
	};
	
	/**
	 * Close the Pro upgrade modal
	 */
	AskanyPro.closeModal = function() {
		$('#askany-pro-modal').fadeOut(300);
		$('body').css('overflow', '');
	};
	
	/**
	 * Create the Pro modal HTML structure
	 * 
	 * @returns {jQuery} The modal element
	 */
	AskanyPro.createModal = function() {
		var modalHtml = 
			'<div id="askany-pro-modal" class="askany-pro-modal" style="display: none;">' +
			'<div class="askany-pro-modal-overlay"></div>' +
			'<div class="askany-pro-modal-content">' +
			'<button class="askany-pro-modal-close" aria-label="Close">' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<line x1="18" y1="6" x2="6" y2="18"></line>' +
			'<line x1="6" y1="6" x2="18" y2="18"></line>' +
			'</svg>' +
			'</button>' +
			'<div class="askany-pro-modal-header">' +
			'<div class="askany-pro-icon">' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>' +
			'</svg>' +
			'</div>' +
			'<h2>Upgrade to Pro</h2>' +
			'<p>Unlock unlimited chat sessions and premium features</p>' +
			'</div>' +
			'<div class="askany-pro-modal-body">' +
			'<ul class="askany-pro-features">' +
			'<li>' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polyline points="20 6 9 17 4 12"></polyline>' +
			'</svg>' +
			'<span>Unlimited chat sessions</span>' +
			'</li>' +
			'<li>' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polyline points="20 6 9 17 4 12"></polyline>' +
			'</svg>' +
			'<span>Priority support</span>' +
			'</li>' +
			'<li>' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polyline points="20 6 9 17 4 12"></polyline>' +
			'</svg>' +
			'<span>Advanced analytics</span>' +
			'</li>' +
			'<li>' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polyline points="20 6 9 17 4 12"></polyline>' +
			'</svg>' +
			'<span>Custom branding</span>' +
			'</li>' +
			'<li>' +
			'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
			'<polyline points="20 6 9 17 4 12"></polyline>' +
			'</svg>' +
			'<span>Premium AI models</span>' +
			'</li>' +
			'</ul>' +
			'</div>' +
			'<div class="askany-pro-modal-footer">' +
			'<a href="#" class="askany-pro-upgrade-btn">Upgrade Now</a>' +
			'<p class="askany-pro-note">30-day money-back guarantee</p>' +
			'</div>' +
			'</div>' +
			'</div>';
		
		var $modal = $(modalHtml);
		$('body').append($modal);
		
		return $modal;
	};
	
	/**
	 * Initialize Pro modal event handlers
	 */
	AskanyPro.init = function() {
		// Close modal on close button or overlay click
		$(document).on('click', '.askany-pro-modal-close, .askany-pro-modal-overlay', function() {
			AskanyPro.closeModal();
		});
		
		// Close modal on escape key
		$(document).on('keydown', function(e) {
			if (e.key === 'Escape' && $('#askany-pro-modal').is(':visible')) {
				AskanyPro.closeModal();
			}
		});
		
		// Open modal when clicking pro badge
		$(document).on('click', '.askany-pro-badge', function(e) {
			e.preventDefault();
			e.stopPropagation();
			AskanyPro.openModal();
		});
		
		// Open modal when clicking locked feature
		$(document).on('click', '.askany-locked-feature', function(e) {
			e.preventDefault();
			e.stopPropagation();
			AskanyPro.openModal();
		});
	};
	
	// Auto-initialize when DOM is ready
	$(document).ready(function() {
		AskanyPro.init();
	});
	
})(jQuery);
