<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for layout mode detection only
$instant_popup_builder_is_edit_mode = isset($_GET['edit_id']) && !empty(sanitize_text_field(wp_unslash($_GET['edit_id'])));
$instant_popup_builder_targeting_section_classes = 'ipb-section';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_targeting_section_classes .= ' ipb-new-popup-panel';
}
$instant_popup_builder_targeting_active_wrapper_classes = 'ipb-targeting-active-wrap';
if (! $instant_popup_builder_is_edit_mode) {
    $instant_popup_builder_targeting_active_wrapper_classes .= ' ipb-new-popup-panel';
}

// Check if Custom Targeting Extension is active

$instant_popup_builder_is_custom_targeting_active = class_exists('Instant_Popup_Custom_Targeting') ||
                             is_plugin_active('instant-popup-custom-targeting/instant-popup-custom-targeting.php') ||
                             defined('INSTANT_POPUP_CUSTOM_TARGETING_VERSION');

if ($instant_popup_builder_is_custom_targeting_active) {
    echo '<div class="' . esc_attr($instant_popup_builder_targeting_active_wrapper_classes) . '">';
    // If extension is active, let it handle the targeting interface
    // Check if we're being called through the action hook system
    if (has_action('instant_popup_builder_tab_content_condition')) {
        do_action('instant_popup_builder_tab_content_condition', 'targeting');
        echo '</div>';
        return;
    } else {
        // Fallback: include the extension's settings directly
        $instant_popup_builder_extension_file = WP_PLUGIN_DIR . '/instant-popup-custom-targeting/admin/partials/custom-targeting-settings.php';
        if (file_exists($instant_popup_builder_extension_file)) {
            include_once $instant_popup_builder_extension_file;
            echo '</div>';
            return;
        }
    }

    // If extension file not found, show error message
    ?>
    <div class="design_background">
        <div class="notice notice-error">
            <p><strong>Device Targeting Extension Error:</strong> Extension is active but settings file not found.</p>
            <p>Please check the Device Targeting extension installation.</p>
        </div>
    </div>
    <?php echo '</div>'; ?>
    <?php
    return;
} else {
    // If extension is not active, show upgrade notice
    ?>
    <div class="<?php echo esc_attr($instant_popup_builder_targeting_section_classes); ?>">
 <div class="ipb-section-header">
    <h3><?php esc_html_e( 'Device Targeting', 'instant-popup-builder' ); ?></h3>
</div>
<div class="ipb-section-content" data-section="display">
	<div class="ipb-upsell-card">
		<div class="ipb-upsell-card__title"><?php esc_html_e( 'Device Targeting', 'instant-popup-builder' ); ?></div>
		<p class="ipb-upsell-card__desc">
			<?php esc_html_e( 'Unlock advanced targeting rules (device, browser, behavior, and more) to show the right popup to the right audience.', 'instant-popup-builder' ); ?>
		</p>
		<div class="ipb-upsell-card__actions">
			<a class="button button-primary" target="_blank" href="https://instantpopupbuilder.com/extensions/custom-targeting/">
				<?php esc_html_e( 'Upgrade to Pro', 'instant-popup-builder' ); ?>
			</a>
		</div>
	</div>
 </div>
 </div>
    <?php
}