<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Variables are confined to this settings partial.

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

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET used only to match Add New vs edit layout (same as GEO / Scheduling).
$ipb_language_is_edit_mode = isset( $_GET['edit_id'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['edit_id'] ) ) );
$ipb_language_section_classes  = 'ipb-section';
$ipb_language_active_wrap_classes = 'ipb-language-active-wrap';
if ( ! $ipb_language_is_edit_mode ) {
	$ipb_language_section_classes .= ' ipb-new-popup-panel';
	$ipb_language_active_wrap_classes .= ' ipb-new-popup-panel';
}

// Check if Language Targeting extension is active.
$ipb_language_targeting_active = class_exists( 'Instant_Popup_Language_Targeting' ) ||
	is_plugin_active( 'instant-popup-language-targeting/instant-popup-language-targeting.php' ) ||
	defined( 'INSTANT_POPUP_LANGUAGE_TARGETING_VERSION' );

if ( $ipb_language_targeting_active ) {
	// If extension is active, let it handle the interface.
	if ( has_action( 'instant_popup_builder_tab_content_condition' ) ) {
		echo '<div class="' . esc_attr( $ipb_language_active_wrap_classes ) . '">';
		do_action( 'instant_popup_builder_tab_content_condition', 'language_targeting' );
		echo '</div>';
		return;
	}

	// Fallback: include the extension's settings directly.
	$ext_file = WP_PLUGIN_DIR . '/instant-popup-language-targeting/admin/partials/language-targeting-settings.php';
	if ( file_exists( $ext_file ) ) {
		echo '<div class="' . esc_attr( $ipb_language_active_wrap_classes ) . '">';
		include_once $ext_file;
		echo '</div>';
		return;
	}

	?>
	<div class="<?php echo esc_attr( $ipb_language_active_wrap_classes ); ?>">
		<div class="design_background">
			<div class="notice notice-error">
				<p><strong><?php esc_html_e( 'Language Targeting Extension Error:', 'instant-popup-builder' ); ?></strong>
					<?php esc_html_e( 'Extension is active but settings file not found.', 'instant-popup-builder' ); ?></p>
				<p><?php esc_html_e( 'Please check the Language Targeting Extension installation.', 'instant-popup-builder' ); ?></p>
			</div>
		</div>
	</div>
	<?php
	return;
}

// If extension is not active, show upgrade notice (core upsell only).
?>
<div class="<?php echo esc_attr( $ipb_language_section_classes ); ?>">
	<div class="ipb-section-header">
		<h3><?php esc_html_e( 'Language Targeting', 'instant-popup-builder' ); ?></h3>
	</div>
	<div class="ipb-section-content" data-section="language_targeting">
		<div class="ipb-upsell-card">
			<div class="ipb-upsell-card__title"><?php esc_html_e( 'Language Targeting', 'instant-popup-builder' ); ?></div>
			<p class="ipb-upsell-card__desc">
				<?php esc_html_e( 'Show popups based on your visitor’s browser/device language — independent of their physical location.', 'instant-popup-builder' ); ?>
			</p>
			<div class="ipb-upsell-card__actions">
				<a class="button button-primary" target="_blank" href="https://instantpopupbuilder.com/extensions/language-targeting/">
					<?php esc_html_e( 'Upgrade to Pro', 'instant-popup-builder' ); ?>
				</a>
			</div>
		</div>
	</div>
</div>
