<?php

namespace WPEventGenius\Common\Services;

use WPEventGenius\Common\Utils\Icon;

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

/**
 * Upsell Service
 * 
 * Handles upsell functionality for the free version of Event Genius.
 * Provides methods to generate Pro upsell links with UTM tracking.
 * 
 * @package WPEventGenius
 * @since 1.0.0
 */
class UpsellService {

	/**
	 * Base URL for the pricing page
	 * 
	 * @var string
	 */
	private $pricing_url = 'https://wpeventgenius.com/pricing/';

	/**
	 * Constructor
	 */
	public function __construct() {}

	/**
	 * Initialize hooks
	 * 
	 * Sets up WordPress hooks for the upsell service.
	 * 
	 * @return void
	 */
	public function init_hooks() {
		if ( ! $this->is_free_tier() ) {
			return;
		}

		// Add "Add New" button to forms page that opens an upsell modal
		add_filter( 'evge_form_builder_action_button', array( $this, 'add_new_form_button' ), 10, 2 );

		// Add Series tab to All Events navigation (free version - opens upsell modal)
		add_filter( 'evge_all_events_navigation_args', array( $this, 'add_series_tab_to_all_events_navigation' ), 10, 1 );

		// Add upsell sections to single event settings page
		add_filter( 'evge_single_event_sections_registration', array( $this, 'add_event_settings_upsells' ), 100, 1 );

		// Add upsell tabs to single registration sub-navigation
		add_filter( 'evge_single_registration_sub_navigation_args', array( $this, 'add_registration_sub_navigation_upsells' ), 10, 2 );

		// Add Payments tab to Settings navigation (free version - opens upsell modal)
		add_filter( 'evge_settings_navigation_args', array( $this, 'add_payments_tab_to_settings_navigation' ), 10, 2 );

		// Add advanced form fields upsell link next to Type select field
		add_action( 'evge_form_field_type_select_after', array( $this, 'add_advanced_fields_upsell_link' ), 10, 1 );

		// Register AJAX handler for upsell modal
		add_action( 'wp_ajax_evge_get_upsell_modal', array( $this, 'ajax_get_upsell_modal' ) );

		// Registration Types upsell panel: styles + expand/collapse (free tier event editor only)
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_registration_types_upsell_assets' ), 15 );

		// After Repeat / End repeat — skip dates teaser on free tier (Learn more → upsell modal)
		add_action( 'evge_event_editor_after_recurrence_selector', array( $this, 'event_editor_custom_recurrence_upsell' ), 10, 1 );
	}

	/**
	 * Free tier: same skip-dates layout as Pro; "Learn more" opens Pro upsell modal.
	 *
	 * @param string $recurrence_type Current `evge_recurrence_type` value.
	 */
	public function event_editor_custom_recurrence_upsell( $recurrence_type ) {
		EVGE()->modal_service()->request_modal();

		$wrapper_class = 'evge-recurrence-skip-dates evge-recurrence-skip-dates--free-gated';
		if ( 'none' === $recurrence_type ) {
			$wrapper_class .= ' evge-hidden';
		}

		$modal_ajax = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'custom_recurrence_scheduling',
			'location' => 'event-editor',
		);
		$modal_settings = array(
			'width' => 'small',
		);
		?>
		<div id="evge-event-editor-after-recurrence-panel" class="<?php echo esc_attr( $wrapper_class ); ?>">
			<div class="evge-recurrence-skip-dates-inner">
				<div class="evge-single-setting-label evge-recurrence-skip-dates-label-row">
					<span class="evge-main-setting-label">
						<?php esc_html_e( 'Skip Specific Dates', 'event-genius' ); ?>
						<a href="#"
						   class="evge-upsell-pro-badge evge-modal-trigger"
						   data-evge-modal-content="ajax"
						   data-evge-ajax="<?php echo esc_attr( wp_json_encode( $modal_ajax ) ); ?>"
						   data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>">
							<?php esc_html_e( 'Pro', 'event-genius' ); ?>
						</a>
					</span>
					<span class="evge-tooltip-wrap">
						<a href="javascript:void(0);" class="evge-tooltip-link">
							<?php
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							echo Icon::get( 'tooltip' );
							?>
						</a>
						<div class="evge-tooltip evge-shadow evge-tooltip-right">
							<p><?php esc_html_e( 'Choose dates when this event should not occur.', 'event-genius' ); ?></p>
						</div>
					</span>
				</div>
				<ul class="evge-recurrence-skip-dates-list" aria-live="polite"></ul>
				<div class="evge-recurrence-skip-dates-add-row">
					<label for="evge_recurrence_skip_date_picker_free" class="screen-reader-text"><?php esc_html_e( 'Date to skip', 'event-genius' ); ?></label>
					<input type="date"
						   class="evge-recurrence-skip-date-input"
						   id="evge_recurrence_skip_date_picker_free"
						   disabled
						   aria-label="<?php esc_attr_e( 'Date to skip (Pro)', 'event-genius' ); ?>">
				</div>
				<input type="hidden" name="evge_recurrence_skip_dates" id="evge_recurrence_skip_dates" value="[]">
			</div>
		</div>
		<?php
	}

	/**
	 * Get a Pro upsell link with UTM tracking parameters
	 * 
	 * @param array $args {
	 *     Optional. Configuration for the upsell link.
	 * 
	 *     @type string $source   The source location (e.g., 'settings-page', 'dashboard-page')
	 *     @type string $medium   The medium type (e.g., 'upsell-link', 'feature-notice')
	 *     @type string $content  Optional. Specific content identifier for tracking
	 *     @type string $url      Optional. Custom URL to use instead of default pricing page
	 * }
	 * @return string The complete URL with UTM parameters, or empty string if not free tier
	 */
	public function get_pro_upsell_link( $args = array() ) {


		$defaults = array(
			'source'  => 'plugin',
			'medium'  => 'upsell-link',
			'content' => '',
			'url'     => $this->pricing_url,
		);

		$args = wp_parse_args( $args, $defaults );

		// Get the current tier for UTM campaign
		$tier = evge_get_tier();
		$utm_campaign = 'evge-' . $tier;

		// Build UTM parameters
		$utm_params = array(
			'utm_campaign' => $utm_campaign,
			'utm_source'   => sanitize_key( $args['source'] ),
			'utm_medium'   => sanitize_key( $args['medium'] ),
		);

		// Add content parameter if provided
		if ( ! empty( $args['content'] ) ) {
			$utm_params['utm_content'] = sanitize_key( $args['content'] );
		}

		// Build the complete URL with UTM parameters
		$url = add_query_arg( $utm_params, $args['url'] );

		return esc_url( $url );
	}

	/**
	 * Filter callback to add "Add New" button to forms page (free version)
	 * 
	 * Returns a button that opens an upsell modal instead of creating a new form.
	 * This is used as an upsell to encourage users to upgrade to Pro.
	 * 
	 * @param string $button_html Existing button HTML
	 * @param object $page The page instance
	 * @return string Button HTML
	 */
	public function add_new_form_button( $button_html, $page ) {
		// Only add button on the forms tab
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$tab = ! empty( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : '';
		
		if ( $tab !== 'forms' ) {
			return $button_html;
		}

		// Request modal since we're adding an upsell link
		EVGE()->modal_service()->request_modal();

		// Generate button with upsell modal
		$add_new_button = $this->get_upsell_button( array(
			'type'     => 'add_new_form',
			'location' => 'forms-tab',
		) );

		return $button_html . $add_new_button;
	}

	/**
	 * Get an upsell button HTML with modal trigger
	 * 
	 * @param array $args {
	 *     Configuration for the upsell button.
	 * 
	 *     @type string $type     The type of upsell (e.g., 'add_new_form', 'multiple_forms', etc.)
	 *     @type string $location The location of the button (e.g., 'forms-tab', 'settings-page', etc.)
	 *     @type string $text     Optional. Button text. Defaults to '+ Add New'
	 *     @type string $class    Optional. Additional CSS classes
	 * }
	 * @return string Button HTML
	 */
	public function get_upsell_button( $args = array() ) {
		$defaults = array(
			'type'     => '',
			'location' => '',
			'text'     => __( 'Add New', 'event-genius' ),
			'class'    => 'button evge-admin-secondary-button',
		);

		$args = wp_parse_args( $args, $defaults );

		if ( empty( $args['type'] ) || empty( $args['location'] ) ) {
			return '';
		}

		// Build AJAX data for the modal
		$ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'      => sanitize_key( $args['type'] ),
			'location'  => sanitize_key( $args['location'] ),
		);

		// Build modal settings
		$modal_settings = array(
			'width' => 'small',
		);

		$button_html = sprintf(
			'<button type="button" class="%s evge-modal-trigger" data-evge-modal-content="ajax" data-evge-ajax="%s" data-evge-modal-settings="%s">
				+ %s <span class="evge-upsell-pro-badge">%s</span>
			</button>',
			esc_attr( $args['class'] ),
			esc_attr( wp_json_encode( $ajax_data ) ),
			esc_attr( wp_json_encode( $modal_settings ) ),
			esc_html( $args['text'] ),
			esc_html__( 'Pro', 'event-genius' )
		);

		return $button_html;
	}

	/**
	 * AJAX handler for getting upsell modal content
	 * 
	 * @return void
	 */
	public function ajax_get_upsell_modal() {
		// Verify nonce
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'evge-admin' ) ) {
			wp_send_json_error( array( 'message' => __( 'Security check failed', 'event-genius' ) ) );
		}

		// Check user capabilities
		if ( ! current_user_can( 'manage_evge_registrations' ) ) {
			wp_send_json_error( array( 'message' => __( 'Insufficient permissions', 'event-genius' ) ) );
		}

		// Get parameters
		$upsell_type = isset( $_POST['type'] ) ? sanitize_key( wp_unslash( $_POST['type'] ) ) : '';
		$location    = isset( $_POST['location'] ) ? sanitize_key( wp_unslash( $_POST['location'] ) ) : '';

		if ( empty( $upsell_type ) || empty( $location ) ) {
			wp_send_json_error( array( 'message' => __( 'Invalid parameters', 'event-genius' ) ) );
		}

		// Get upsell link with UTM tracking
		// Include feature type in medium for better tracking of which features users are interested in
		$upsell_link = $this->get_pro_upsell_link( array(
			'source'  => $location,
			'medium'  => 'upsell-modal-' . $upsell_type,
			'content' => 'upgrade-to-pro',
		) );

		// Get upsell data (will be parameterized later)
		$upsell_data = $this->get_upsell_data( $upsell_type, $location );

		// Start output buffering to capture the modal HTML
		ob_start();
		$this->render_upsell_modal( $upsell_data, $upsell_link );
		$html = ob_get_clean();

		wp_send_json_success( array( 'html' => $html ) );
	}

	/**
	 * Get upsell data based on type and location
	 * 
	 * @param string $type The type of upsell
	 * @param string $location The location of the button
	 * @return array Upsell data (title, description, image, etc.)
	 */
	private function get_upsell_data( $type, $location ) {
		// Default generic upsell data
		// This will be parameterized later to accept custom content
		$defaults = array(
			'title'              => __( 'Upgrade to Pro', 'event-genius' ),
			'description'        => __( 'This feature is available in Event Genius Pro. Upgrade now to unlock this and many other powerful features!', 'event-genius' ),
			'image'              => '',
			'image_2x'           => '',
			'features'           => array(),
			'additional_features' => array(),
		);

		// Type-specific data (can be expanded later)
		$type_data = array();

		switch ( $type ) {
			case 'add_new_form':
				$type_data = array(
					'title'       => __( 'Multiple Registration Forms', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to create multiple registration forms', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-forms.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-forms-2x.png',
					'features'    => array(
						__( 'Create unique custom registration forms for each event.', 'event-genius' ),
						__( 'Assign forms to specific events, customizing confirmations and notifications for different audiences.', 'event-genius' ),
						__( 'Reuse custom form fields across multiple forms for efficiency.', 'event-genius' ),
						__( 'Allow for additional guests and bulk event registration.', 'event-genius' ),
					),
				);
				break;
			case 'event_series_management':
				$type_data = array(
					'title'       => __( 'Custom Event Series', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to create and manage event series', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-series.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-series-2x.png',
					'features'    => array(
						__( 'Group related events into series for better organization.', 'event-genius' ),
						__( 'Allow attendees to register for the full series or just a single date.', 'event-genius' ),
						__( 'Edit each recurrence individually or make bulk changes.', 'event-genius' ),
						__( 'Dedicated pages for each series with registration options.', 'event-genius' ),
					),
				);
				break;
			case 'accept_payments':
				$type_data = array(
					'title'       => __( 'Accept Payments & Sell Tickets', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to accept payments and sell tickets', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-payments.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-payments-2x.png',
					'features'    => array(
						__( 'Stripe and PayPal payment gateways.', 'event-genius' ),
						__( 'Flexible offline or manual payment tracking and instructions.', 'event-genius' ),
						__( 'Allow attendees to pay for additional guests or multiple events at once.', 'event-genius' ),
						__( 'Complete control over payment records.', 'event-genius' ),
						__( 'Contact individual attendess as needed.', 'event-genius' ),
					),
				);
				break;
			case 'scheduled_event_reminders':
				$type_data = array(
					'title'       => __( 'Scheduled Event Messages', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to send scheduled messages and reminders to your attendees', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-scheduled.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-scheduled-2x.png',
					'features'    => array(
						__( 'Automatically send reminder emails before your event to improve attendance.', 'event-genius' ),
						__( 'Send follow-up and thank youmessages after events to keep attendees engaged.', 'event-genius' ),
						__( 'Schedule relative to the event start and end times or a specific date and time.', 'event-genius' ),
						__( 'Automate post event surveys to gather feedback and improve future events.', 'event-genius' ),
					),
				);
				break;
			case 'custom_recurrence_scheduling':
				$type_data = array(
					'title'       => __( 'Custom recurrence & schedules', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to go beyond simple presets with flexible repeat rules, skip dates, and advanced scheduling options.', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-custom-recurrence.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-custom-recurrence-2x.png',
					'features'    => array(
						__( 'Build custom repeat patterns with intervals and rules that match real-world schedules.', 'event-genius' ),
						__( 'Skip specific calendar days (holidays, closures) without managing individual events.', 'event-genius' ),
						__( 'Works with full event series management and registration.', 'event-genius' ),
					),
				);
				break;
			case 'registration_types':
				$type_data = array(
					'title'       => __( 'Registration Types', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to add multiple registration types and ticket-style options for your events', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-types.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-types.png',
					'features'    => array(
						__( 'Create multiple types (e.g. General, VIP, early bird) with separate pricing and capacity.', 'event-genius' ),
						__( 'Set per-type sales windows or inherit the event registration window.', 'event-genius' ),
						__( 'Let attendees choose their type at registration.', 'event-genius' ),
						__( 'Combine with paid plans for payments, series, and more.', 'event-genius' ),
					),
				);
				break;
			case 'attendance_tracking':
				$type_data = array(
					'title'       => __( 'Check-in & Attendance Tracking', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to track attendance and check-in status for your events', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-attendance.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-attendance-2x.png',
					'features'    => array(
						__( 'Look up attendees by name, email, or confirmation code and record attendance with a single click.', 'event-genius' ),
						__( 'Track attendance status including attended, no-show, and excused.', 'event-genius' ),
						__( 'Allow event helpers to check in attendees with a simple interface.', 'event-genius' ),
						__( 'Schedule follow up emails based on attendance status.', 'event-genius' ),
					),
				);
				break;
			case 'advanced_form_fields':
				$type_data = array(
					'title'       => __( 'Advanced Form Fields', 'event-genius' ),
					'description' => __( 'Upgrade to Event Genius Pro to unlock advanced form field types', 'event-genius' ),
					'image'       => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-fields.png',
					'image_2x'    => EVGE_PLUGIN_URL . 'assets/images/admin/features/feature-fields-2x.png',
					'features'    => array(
						__( 'File upload fields for documents, images, and other attachments.', 'event-genius' ),
						__( 'Date picker fields with customizable date formats and restrictions.', 'event-genius' ),
						__( 'Attendee number fields to allow multiple guest registration.', 'event-genius' ),
						__( 'Hidden admin fields for storing custom data visible only to administrators.', 'event-genius' ),
					),
				);
				break;
		}

		// Get all additional features and exclude the current type
		$all_additional_features = $this->get_all_additional_features();
		$additional_features = array();
		
		foreach ( $all_additional_features as $feature_type => $feature_text ) {
			if ( $feature_type !== $type ) {
				$additional_features[] = $feature_text;
			}
		}

		// Merge additional features into type data
		$type_data['additional_features'] = $additional_features;

		return wp_parse_args( $type_data, $defaults );
	}

	/**
	 * Get all additional features keyed by upsell type
	 * 
	 * @return array Array of features where keys are upsell types and values are feature descriptions
	 */
	private function get_all_additional_features() {
		return array(
			'accept_payments' 			=> __( 'Accept payments & sell tickets', 'event-genius' ),
			'add_new_form'              => __( 'Multiple registration forms', 'event-genius' ),
			'attendance_tracking'    	=> __( 'Check-in & attendance tracking', 'event-genius' ),
			'event_series_management'   => __( 'Event series management', 'event-genius' ),
			'multiple_email_templates'    => __( 'Multiple email templates', 'event-genius' ),
			'scheduled_event_reminders' => __( 'Scheduled event reminders', 'event-genius' ),
			'multiple_guests'         => __( 'Register multiple guests', 'event-genius' ),
			'registered_events'    => __( 'List events registered for', 'event-genius' ),
			'manual_email'    			=> __( 'Manually email registrants', 'event-genius' ),
			'priority_support'     => __( 'Priority support', 'event-genius' ),
			'blocks_and_shortcodes' => __( 'Additional blocks and shortcodes', 'event-genius' ),
			'multiple_event_registration'     => __( 'Multiple event registration', 'event-genius' ),
			'offline_payment_tracking'      => __( 'Offline payment tracking', 'event-genius' ),
			'registration_types'            => __( 'Multiple registration types', 'event-genius' ),
			'custom_recurrence_scheduling'  => __( 'Custom recurrence & schedules', 'event-genius' ),
		);
	}

	/**
	 * Render the upsell modal template
	 * 
	 * @param array  $upsell_data Upsell data (title, description, etc.)
	 * @param string $upsell_link The Pro upgrade link with UTM tracking
	 * @return void
	 */
	private function render_upsell_modal( $upsell_data, $upsell_link ) {
		$template_path = trailingslashit( EVGE_ADMIN_TEMPLATE_PATH ) . 'evge/upsell/modal.php';

		if ( file_exists( $template_path ) ) {
			include $template_path;
		} else {
			// Fallback if template doesn't exist
			echo '<div class="evge-upsell-modal-content">';
			echo '<h2>' . esc_html( $upsell_data['title'] ) . '</h2>';
			echo '<p>' . esc_html( $upsell_data['description'] ) . '</p>';
			echo '<a href="' . esc_url( $upsell_link ) . '" class="button button-primary" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Upgrade to Pro', 'event-genius' ) . '</a>';
			echo '</div>';
		}
	}

	/**
	 * Add Series tab to All Events navigation (free version - opens upsell modal)
	 * 
	 * @param array $navigation_args The navigation arguments array
	 * @return array Modified navigation arguments with Series tab
	 */
	public function add_series_tab_to_all_events_navigation( $navigation_args ) {

		// Only add Series tab if user has appropriate capability
		if ( ! current_user_can( 'manage_options' ) ) {
			return $navigation_args;
		}

		// Check if Series tab already exists (added by Pro version)
		$series_exists = array_search( 'series', array_column( $navigation_args['nav_items'], 'id' ) ) !== false;
		if ( $series_exists ) {
			return $navigation_args;
		}

		// Add Series tab after Venues tab
		$venues_index = array_search( 'venues', array_column( $navigation_args['nav_items'], 'id' ) );
		if ( $venues_index !== false ) {
			// Build AJAX data for the modal
			$ajax_data = array(
				'action'   => 'evge_get_upsell_modal',
				'type'     => 'event_series_management',
				'location' => 'all-events-navigation',
			);

			// Build modal settings
			$modal_settings = array(
				'width' => 'small',
			);

			// Get the Series navigation item with modal trigger
			$series_nav_item = array(
				'id'                => 'series',
				'title'             => __( 'Series', 'event-genius' ),
				'url'               => '#', // Placeholder URL
				'capability'        => 'manage_options',
				'modal_trigger'     => true,
				'modal_ajax_data'   => $ajax_data,
				'modal_settings'    => $modal_settings,
			);
			
			// Insert Series tab after Venues tab
			$navigation_args['nav_items'] = array_merge(
				array_slice( $navigation_args['nav_items'], 0, $venues_index + 1 ),
				array( $series_nav_item ),
				array_slice( $navigation_args['nav_items'], $venues_index + 1 )
			);
					// Request modal since we're adding an upsell link
			EVGE()->modal_service()->request_modal();
		}

		return $navigation_args;
	}

	/**
	 * Add upsell sections to single event settings page
	 * 
	 * @param array $sections The event sections array
	 * @return array Modified sections array
	 */
	public function add_event_settings_upsells( $sections ) {
		// Request modal since we're adding upsell links
		EVGE()->modal_service()->request_modal();

		$modal_settings = array(
			'width' => 'small',
		);

		$registration_types_ajax = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'registration_types',
			'location' => 'event-settings',
		);

		$registration_types_upsell_section = array(
			'title'       => __( 'Types', 'event-genius' ),
			'description' => __( 'Registration types work like ticket options: each type can have its own name, price, capacity, and when it is available. Attendees choose a type when registering (e.g. General Admission, VIP).', 'event-genius' ),
			'pro_badge'   => array(
				'ajax_data'      => $registration_types_ajax,
				'modal_settings' => $modal_settings,
			),
			'subsections' => array(
				'registration_types_upsell_panel' => array(
					'priority'    => 10,
					'id'          => 'registration_types_upsell_panel',
					'type'        => 'registration_types_upsell',
					'callback'    => array( $this, 'registration_types_upsell_display' ),
					'label'       => '',
					'description' => '',
					'settings'    => array(),
				),
			),
		);

		// Place Types immediately above Payments (after Blocks & Shortcodes from priority 99; Payments are appended below).
		$after_key = array_key_exists( 'blocks_shortcodes', $sections ) ? 'blocks_shortcodes' : 'attendee_list';
		$sections  = $this->insert_array_key_after( $sections, $after_key, 'registration_types_upsell', $registration_types_upsell_section );

		// Build AJAX data for the modal
		$ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'accept_payments',
			'location' => 'event-settings',
		);

		// Add Payments upsell section (Types upsell is inserted just above this)
		$sections['payments_upsell'] = array(
			'title' => __( 'Payments', 'event-genius' ),
			'pro_badge' => array(
				'ajax_data' => $ajax_data,
				'modal_settings' => $modal_settings,
			),
			'subsections' => array(
				'enable_payments_upsell' => array(
					'priority' => 10,
					'id' => 'enable_payments_upsell',
					'type' => 'payment_enabled_upsell',
					'callback' => array( $this, 'payment_enabled_upsell_display' ),
					'label' => __( 'Enable Payments', 'event-genius' ),
					'description' => '',
					'settings' => array()
				)
			)
		);

		// Build AJAX data for Scheduled Messages modal
		$scheduled_messages_ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'scheduled_event_reminders',
			'location' => 'event-settings',
		);

		// Add Scheduled Messages upsell section after payments
		$sections['scheduled_messages_upsell'] = array(
			'title' => __( 'Scheduled Messages', 'event-genius' ),
			'pro_badge' => array(
				'ajax_data' => $scheduled_messages_ajax_data,
				'modal_settings' => $modal_settings,
			),
			'subsections' => array(
				'enable_scheduled_messages_upsell' => array(
					'priority' => 10,
					'id' => 'enable_scheduled_messages_upsell',
					'type' => 'scheduled_messages_enabled_upsell',
					'callback' => array( $this, 'scheduled_messages_enabled_upsell_display' ),
					'label' => __( 'Enable Scheduled Messages', 'event-genius' ),
					'description' => '',
					'settings' => array()
				)
			)
		);

		return $sections;
	}

	/**
	 * Display the payment enable/disable upsell toggle
	 * 
	 * @param array $section The section data
	 */
	public function payment_enabled_upsell_display( $section ) {
		// Build AJAX data for the modal
		$ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'accept_payments',
			'location' => 'event-settings',
		);

		// Build modal settings
		$modal_settings = array(
			'width' => 'small',
		);

		?>
		<div id="evge-single-setting-<?php echo esc_attr( $section['id'] ); ?>" class="evge-payment-enabled-upsell-wrap evge-single-section evge-upsell-disabled">
			<div class="evge-flex evge-flex-center">
				<div class="evge-toggle-setting evge-upsell-toggle">
					<input class="evge-toggle-setting-enabled" 
						   type="hidden" 
						   name="evge_payment_status_upsell" 
						   value="disabled">
					<a class="evge-settings-toggle-wrap evge-modal-trigger" 
					   href="#"
					   data-evge-modal-content="ajax"
					   data-evge-ajax="<?php echo esc_attr( wp_json_encode( $ajax_data ) ); ?>"
					   data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>">
						<span class="evge-settings-toggle evge-input-toggle--disabled" 
							  aria-label="<?php esc_attr_e( 'Payments are disabled - Click to learn more', 'event-genius' ); ?>">
							<?php esc_html_e( 'No', 'event-genius' ); ?>
						</span>
					</a>
					<?php if ( ! empty( $section['label'] ) ) : ?>
						<label><?php echo esc_html( $section['label'] ); ?></label>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Display the scheduled messages enable/disable upsell toggle
	 * 
	 * @param array $section The section data
	 */
	public function scheduled_messages_enabled_upsell_display( $section ) {
		// Build AJAX data for the modal
		$ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'scheduled_event_reminders',
			'location' => 'event-settings',
		);

		// Build modal settings
		$modal_settings = array(
			'width' => 'small',
		);

		?>
		<div id="evge-single-setting-<?php echo esc_attr( $section['id'] ); ?>" class="evge-scheduled-messages-enabled-upsell-wrap evge-single-section evge-upsell-disabled">
			<div class="evge-flex evge-flex-center">
				<div class="evge-toggle-setting evge-upsell-toggle">
					<input class="evge-toggle-setting-enabled" 
						   type="hidden" 
						   name="evge_scheduled_messages_status_upsell" 
						   value="disabled">
					<a class="evge-settings-toggle-wrap evge-modal-trigger" 
					   href="#"
					   data-evge-modal-content="ajax"
					   data-evge-ajax="<?php echo esc_attr( wp_json_encode( $ajax_data ) ); ?>"
					   data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>">
						<span class="evge-settings-toggle evge-input-toggle--disabled" 
							  aria-label="<?php esc_attr_e( 'Scheduled messages are disabled - Click to learn more', 'event-genius' ); ?>">
							<?php esc_html_e( 'No', 'event-genius' ); ?>
						</span>
					</a>
					<?php if ( ! empty( $section['label'] ) ) : ?>
						<label><?php echo esc_html( $section['label'] ); ?></label>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Add upsell tabs to single registration sub-navigation
	 * 
	 * @param array $sub_navigation_args The sub-navigation arguments array
	 * @param object $event The event object
	 * @return array Modified sub-navigation arguments with upsell tabs
	 */
	public function add_registration_sub_navigation_upsells( $sub_navigation_args, $event ) {
		// Request modal since we're adding upsell links
		EVGE()->modal_service()->request_modal();
		// Build AJAX data for Payments modal
		$payments_ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'accept_payments',
			'location' => 'registration-subnav',
		);

		// Build AJAX data for Attendance modal
		$attendance_ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'attendance_tracking',
			'location' => 'registration-subnav',
		);

		// Build modal settings
		$modal_settings = array(
			'width' => 'small',
		);

		// Add Payments tab
		$sub_navigation_args['nav_items'][] = array(
			'id'                => 'payments',
			'title'             => __( 'Payments', 'event-genius' ),
			'url'               => '#',
			'modal_trigger'     => true,
			'modal_ajax_data'   => $payments_ajax_data,
			'modal_settings'    => $modal_settings,
			'pro_badge'         => true,
		);

		// Add Attendance tab
		$sub_navigation_args['nav_items'][] = array(
			'id'                => 'attendance',
			'title'             => __( 'Attendance', 'event-genius' ),
			'url'               => '#',
			'modal_trigger'     => true,
			'modal_ajax_data'   => $attendance_ajax_data,
			'modal_settings'    => $modal_settings,
			'pro_badge'         => true,
		);

		return $sub_navigation_args;
	}

	/**
	 * Add Payments tab to Settings navigation (free version - opens upsell modal)
	 * 
	 * @param array $navigation_args The navigation arguments array
	 * @param string $active_tab The active tab
	 * @return array Modified navigation arguments with Payments tab
	 */
	public function add_payments_tab_to_settings_navigation( $navigation_args, $active_tab ) {
		// Check if Payments tab already exists (added by Pro version)
		$payments_exists = array_search( 'payments', array_column( $navigation_args['nav_items'], 'id' ) ) !== false;
		if ( $payments_exists ) {
			return $navigation_args;
		}

		// Add Payments tab after Registration tab
		$registration_index = array_search( 'registration', array_column( $navigation_args['nav_items'], 'id' ) );
		if ( $registration_index !== false ) {
			// Build AJAX data for the modal
			$ajax_data = array(
				'action'   => 'evge_get_upsell_modal',
				'type'     => 'accept_payments',
				'location' => 'settings-navigation',
			);

			// Build modal settings
			$modal_settings = array(
				'width' => 'small',
			);

			// Request modal since we're adding an upsell link
			EVGE()->modal_service()->request_modal();

			// Get the Payments navigation item with modal trigger
			$payments_nav_item = array(
				'id'                => 'payments',
				'title'             => __( 'Payments', 'event-genius' ),
				'url'               => '#', // Placeholder URL
				'modal_trigger'     => true,
				'modal_ajax_data'   => $ajax_data,
				'modal_settings'    => $modal_settings,
			);
			
			// Insert Payments tab after Registration tab
			$navigation_args['nav_items'] = array_merge(
				array_slice( $navigation_args['nav_items'], 0, $registration_index + 1 ),
				array( $payments_nav_item ),
				array_slice( $navigation_args['nav_items'], $registration_index + 1 )
			);
		}

		return $navigation_args;
	}

	/**
	 * Add advanced form fields upsell link next to Type label
	 * 
	 * @param object $field The field object
	 * @return void
	 */
	public function add_advanced_fields_upsell_link( $field ) {
		// Only show for free tier users
		if ( ! $this->is_free_tier() ) {
			return;
		}

		// Build AJAX data for the modal
		$ajax_data = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'advanced_form_fields',
			'location' => 'form-builder-type-setting',
		);

		// Build modal settings
		$modal_settings = array(
			'width' => 'small',
		);

		// Request modal since we're adding an upsell link
		EVGE()->modal_service()->request_modal();

		?>
		<a href="#" 
		   class="evge-advanced-fields-upsell-link evge-modal-trigger"
		   data-evge-modal-content="ajax"
		   data-evge-ajax="<?php echo esc_attr( wp_json_encode( $ajax_data ) ); ?>"
		   data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>">
			<?php esc_html_e( 'All field types', 'event-genius' ); ?>
		</a>
		<?php
	}

	/**
	 * Insert a key after an existing key in an associative array (preserves order).
	 *
	 * @param array  $array   Source array.
	 * @param string $after_key Key to insert after.
	 * @param string $new_key   New key.
	 * @param mixed  $new_value New value.
	 * @return array
	 */
	private function insert_array_key_after( array $array, $after_key, $new_key, $new_value ) {
		if ( ! array_key_exists( $after_key, $array ) ) {
			$array[ $new_key ] = $new_value;
			return $array;
		}
		$result = array();
		foreach ( $array as $k => $v ) {
			$result[ $k ] = $v;
			if ( $k === $after_key ) {
				$result[ $new_key ] = $new_value;
			}
		}
		return $result;
	}

	/**
	 * Enqueue inline card + registration type styles and minimal JS for the upsell panel (free tier, event editor).
	 *
	 * @param string $hook_suffix Current admin screen.
	 */
	public function enqueue_registration_types_upsell_assets( $hook_suffix ) {
		if ( ! $this->is_free_tier() ) {
			return;
		}
		if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) {
			return;
		}
		global $post;
		$post_type = '';
		if ( $post && isset( $post->post_type ) ) {
			$post_type = $post->post_type;
		} elseif ( isset( $_GET['post_type'] ) ) {
			$post_type = sanitize_text_field( wp_unslash( $_GET['post_type'] ) );
		} else {
			global $typenow;
			if ( isset( $typenow ) ) {
				$post_type = $typenow;
			}
		}
		if ( $post_type !== EVGE_EVENT_POST_TYPE ) {
			return;
		}

		EVGE()->style_service()->enqueue_style( 'evge-inline-cards-admin' );
		EVGE()->style_service()->enqueue_style( 'evge-registration-types-admin' );

		wp_add_inline_script(
			'jquery',
			'jQuery(function($){$(document).on("click",".evge-registration-types-upsell-panel .evge-registration-type-toggle",function(e){e.preventDefault();$(this).closest(".evge-registration-type-item").toggleClass("evge-collapsed");});});'
		);
	}

	/**
	 * Registration Types upsell: preview UI matching Standard tier with Pro locks (free tier).
	 *
	 * @param array $section Subsection config.
	 */
	public function registration_types_upsell_display( $section ) {
		$ajax_data      = array(
			'action'   => 'evge_get_upsell_modal',
			'type'     => 'registration_types',
			'location' => 'event-settings',
		);
		$modal_settings = array( 'width' => 'small' );
		$event_cpt      = new \WPEventGenius\Admin\CustomPostTypes\Event();
		?>
		<div id="evge-single-setting-<?php echo esc_attr( $section['id'] ); ?>" class="evge-registration-types-panel evge-registration-types-upsell-panel evge-single-section" data-evge-upsell="1">
			<div id="evge-registration-types-list-upsell" class="evge-registration-types-list evge-inline-list">
				<div class="evge-registration-type-item evge-inline-card evge-collapsed" data-index="0" data-type-id="" data-block-remove="1" data-block-disable="1">
					<div class="evge-registration-type-header evge-inline-card-header">
						<span class="evge-drag-handle evge-registration-type-drag-handle" aria-hidden="true" style="opacity:0.35;" title="<?php esc_attr_e( 'Drag to reorder', 'event-genius' ); ?>">
							<svg width="18" height="18" viewBox="0 0 18 18" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><circle cx="6" cy="5" r="1.25"/><circle cx="12" cy="5" r="1.25"/><circle cx="6" cy="9" r="1.25"/><circle cx="12" cy="9" r="1.25"/><circle cx="6" cy="13" r="1.25"/><circle cx="12" cy="13" r="1.25"/></svg>
						</span>
						<h4 class="evge-registration-type-summary evge-inline-card-summary">
							<span class="evge-registration-type-name"><?php esc_html_e( 'General', 'event-genius' ); ?></span>
						</h4>
						<button type="button" class="button-link evge-registration-type-toggle evge-inline-card-toggle" aria-expanded="false" aria-label="<?php esc_attr_e( 'Toggle registration type', 'event-genius' ); ?>">
							<?php
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							echo Icon::get( 'down-chevron' );
							?>
						</button>
					</div>
					<div class="evge-registration-type-fields evge-inline-card-fields">
						<div class="evge-field">
							<?php $this->registration_types_upsell_lock_button( $ajax_data, $modal_settings ); ?>
							<label><?php esc_html_e( 'Name', 'event-genius' ); ?></label>
							<input type="text" value="<?php echo esc_attr( __( 'General', 'event-genius' ) ); ?>" class="regular-text" disabled>
						</div>
						<div class="evge-field">
							<?php $this->registration_types_upsell_lock_button( $ajax_data, $modal_settings ); ?>
							<label><?php esc_html_e( 'Description', 'event-genius' ); ?></label>
							<textarea rows="2" class="large-text" disabled></textarea>
						</div>
						<div class="evge-field evge-field-price">
							<?php $this->registration_types_upsell_lock_button( $ajax_data, $modal_settings ); ?>
							<?php
							$event_cpt->toggle_setting_display(
								array(
									'name'          => '',
									'value'         => 'enabled',
									'enabled_text'  => __( 'Yes', 'event-genius' ),
									'disabled_text' => __( 'No', 'event-genius' ),
									'enabled_aria'  => __( 'Use event price', 'event-genius' ),
									'disabled_aria' => __( 'Set custom price', 'event-genius' ),
									'label'         => __( 'Use event price', 'event-genius' ),
									'wrapper_class' => 'evge-reg-type-use-event-price-wrap evge-upsell-fake-toggle',
								)
							);
							?>
							<div class="evge-reg-type-price-custom-wrap evge-reg-type-revealed-settings" style="display:none;">
								<label for="evge_rt_upsell_price"><?php esc_html_e( 'Price', 'event-genius' ); ?></label>
								<input id="evge_rt_upsell_price" type="number" value="0" min="0" step="0.01" class="small-text" disabled>
							</div>
						</div>
						<div class="evge-field evge-field-capacity evge-capacity-settings-inline">
							<?php $this->registration_types_upsell_lock_button( $ajax_data, $modal_settings ); ?>
							<?php
							$event_cpt->toggle_setting_display(
								array(
									'name'          => '',
									'value'         => 'enabled',
									'enabled_text'  => __( 'Yes', 'event-genius' ),
									'disabled_text' => __( 'No', 'event-genius' ),
									'enabled_aria'  => __( 'Use event capacity', 'event-genius' ),
									'disabled_aria' => __( 'Set custom capacity', 'event-genius' ),
									'label'         => __( 'Use event capacity', 'event-genius' ),
									'wrapper_class' => 'evge-reg-type-use-event-capacity-wrap evge-upsell-fake-toggle',
								)
							);
							?>
							<div class="evge-reg-type-capacity-custom-wrap evge-reg-type-revealed-settings" style="display:none;">
								<label for="evge_rt_upsell_cap" class="evge-main-setting-label"><?php esc_html_e( 'Capacity', 'event-genius' ); ?></label>
								<div class="evge-flex evge-flex-center">
									<div class="evge-capacity-input">
										<input id="evge_rt_upsell_cap" type="number" value="50" min="0" class="small-text" disabled>
									</div>
									<?php
									$event_cpt->toggle_setting_display(
										array(
											'name'          => '',
											'value'         => 'enabled',
											'enabled_text'  => __( 'Yes', 'event-genius' ),
											'disabled_text' => __( 'No', 'event-genius' ),
											'enabled_aria'  => __( 'Capacity is unlimited', 'event-genius' ),
											'disabled_aria' => __( 'Capacity is limited', 'event-genius' ),
											'label'         => __( 'Unlimited Capacity', 'event-genius' ),
											'wrapper_class' => 'evge-reg-type-unlimited-wrap evge-capacity-toggle evge-upsell-fake-toggle',
										)
									);
									?>
								</div>
							</div>
						</div>
						<div class="evge-field evge-field-availability">
							<?php $this->registration_types_upsell_lock_button( $ajax_data, $modal_settings ); ?>
							<?php
							$event_cpt->toggle_setting_display(
								array(
									'name'          => '',
									'value'         => 'enabled',
									'enabled_text'  => __( 'Yes', 'event-genius' ),
									'disabled_text' => __( 'No', 'event-genius' ),
									'enabled_aria'  => __( 'Use event registration window', 'event-genius' ),
									'disabled_aria' => __( 'Set custom availability window', 'event-genius' ),
									'label'         => __( 'Use event registration window', 'event-genius' ),
									'wrapper_class' => 'evge-restrict-availability-wrap evge-upsell-fake-toggle',
								)
							);
							?>
							<div class="evge-reg-type-timeline-wrap evge-timeline-settings-wrap evge-reg-type-revealed-settings" style="display:none;">
								<div class="evge-timeline-grid">
									<div class="evge-timeline-field">
										<label for="evge_rt_upsell_open" class="evge-main-setting-label"><?php esc_html_e( 'Availablility Opens', 'event-genius' ); ?></label>
										<select id="evge_rt_upsell_open" class="evge-reg-type-open-type" disabled>
											<option><?php esc_html_e( 'Immediately', 'event-genius' ); ?></option>
										</select>
									</div>
									<div class="evge-timeline-field">
										<label for="evge_rt_upsell_close" class="evge-main-setting-label"><?php esc_html_e( 'Availablilty Closes', 'event-genius' ); ?></label>
										<select id="evge_rt_upsell_close" class="evge-reg-type-close-type" disabled>
											<option><?php esc_html_e( 'Never', 'event-genius' ); ?></option>
										</select>
									</div>
								</div>
							</div>
						</div>
						<div class="evge-field evge-field-status">
							<label for="evge_rt_upsell_status"><?php esc_html_e( 'Status', 'event-genius' ); ?></label>
							<select id="evge_rt_upsell_status" class="evge-reg-type-status-select" disabled>
								<option selected><?php esc_html_e( 'Active', 'event-genius' ); ?></option>
							</select>
						</div>
						<span class="evge-remove-registration-type evge-reg-type-remove-button evge-reg-type-remove-disabled evge-inline-card-remove" aria-disabled="true" title="<?php esc_attr_e( 'At least one registration type is required.', 'event-genius' ); ?>">
							<?php
							// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							echo Icon::get( 'trash' );
							?>
						</span>
					</div>
				</div>
			</div>
			<button type="button" class="button evge-admin-secondary-button evge-left-icon evge-modal-trigger" data-evge-modal-content="ajax" data-evge-ajax="<?php echo esc_attr( wp_json_encode( $ajax_data ) ); ?>" data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>">
				<span class="evge-icon-text">
					<?php
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					echo Icon::get( 'plus' );
					?>
					<?php esc_html_e( 'Add registration type', 'event-genius' ); ?>
					<span class="evge-upsell-pro-badge"><?php esc_html_e( 'Pro', 'event-genius' ); ?></span>
				</span>
			</button>
		</div>
		<?php
	}

	/**
	 * Lock control that opens the Pro upsell modal (registration types preview).
	 *
	 * @param array $ajax_data      Modal AJAX payload.
	 * @param array $modal_settings Modal options.
	 */
	private function registration_types_upsell_lock_button( $ajax_data, $modal_settings ) {
		$this->render_upsell_lock_button(
			$ajax_data,
			$modal_settings,
			__( 'Upgrade to unlock registration types', 'event-genius' )
		);
	}

	/**
	 * Lock control that opens a Pro upsell modal (shared by registration types, recurrence previews).
	 *
	 * @param array  $ajax_data      Modal AJAX payload.
	 * @param array  $modal_settings Modal options.
	 * @param string $aria_label     Accessible label for the control.
	 */
	private function render_upsell_lock_button( $ajax_data, $modal_settings, $aria_label ) {
		?>
		<button type="button" class="evge-upsell-registration-type-lock evge-modal-trigger" data-evge-modal-content="ajax" data-evge-ajax="<?php echo esc_attr( wp_json_encode( $ajax_data ) ); ?>" data-evge-modal-settings="<?php echo esc_attr( wp_json_encode( $modal_settings ) ); ?>" aria-label="<?php echo esc_attr( $aria_label ); ?>">
			<?php
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo Icon::get( 'lock' );
			?>
		</button>
		<?php
	}

	/**
	 * Check if the current tier is free
	 * 
	 * @return bool True if free tier, false otherwise
	 */
	private function is_free_tier() {
		return function_exists( 'evge_is_free_tier' ) && evge_is_free_tier();
	}
}

