<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Template-scope variables are confined to this partial.
/**
 * Core integration page.
 *
 * Core provides WordPress Mail only. External provider configuration belongs
 * to the separately distributed Subscription Pro extension.
 *
 * @package Instant_Popup_Builder
 */

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

$saved = false;
$request_method = isset($_SERVER['REQUEST_METHOD'])
	? sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD']))
	: '';

if ('POST' === $request_method && isset($_POST['ipb_core_integration_nonce'])) {
	check_admin_referer('ipb_core_integration_settings', 'ipb_core_integration_nonce');

	if (!current_user_can('manage_options')) {
		wp_die(esc_html__('You do not have permission to manage integrations.', 'instant-popup-builder'));
	}

	// WordPress Mail is the only integration implemented by the core plugin.
	update_option('ipb_active_integration', '');
	$saved = true;
}

$pro_url = 'https://instantpopupbuilder.com/extensions/subscription-popup/';
?>

<div class="wrap ipb-core-integration">
	<?php
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method returns trusted admin markup.
	echo Instant_Popup_Builder_Admin::ipb_top_header();
	?>

	<h1><?php esc_html_e('Email Marketing Integration', 'instant-popup-builder'); ?></h1>
	<p class="description">
		<?php esc_html_e('Use WordPress Mail, or unlock external email marketing providers with Subscription Pro.', 'instant-popup-builder'); ?>
	</p>

	<?php if ($saved) : ?>
		<div class="notice notice-success is-dismissible">
			<p><?php esc_html_e('WordPress Mail is now the active delivery method.', 'instant-popup-builder'); ?></p>
		</div>
	<?php endif; ?>

	<div class="ipb-core-integration-layout">
		<div class="card ipb-core-integration-card">
			<form method="post">
				<?php wp_nonce_field('ipb_core_integration_settings', 'ipb_core_integration_nonce'); ?>

				<div class="ipb-core-provider ipb-wordpress-mail-card">
					<span class="dashicons dashicons-wordpress" aria-hidden="true"></span>
					<span class="ipb-core-provider-copy">
						<strong><?php esc_html_e('WordPress Mail', 'instant-popup-builder'); ?></strong>
						<small><?php esc_html_e("Send through your site's default wp_mail configuration.", 'instant-popup-builder'); ?></small>
					</span>
				</div>

				<?php submit_button(__('Use WordPress Mail', 'instant-popup-builder')); ?>
			</form>
		</div>

		<div class="card ipb-core-integration-card">
			<div class="ipb-core-card-heading">
				<div>
					<h2><?php esc_html_e('Email marketing providers', 'instant-popup-builder'); ?></h2>
					<p class="description"><?php esc_html_e('Provider credentials and subscriber syncing are included in Subscription Pro.', 'instant-popup-builder'); ?></p>
				</div>
				<span class="ipb-pro-badge"><?php esc_html_e('Pro', 'instant-popup-builder'); ?></span>
			</div>

			<div class="ipb-core-provider-grid">
				<?php
				$providers = [
					['Brevo', 'dashicons-email-alt'],
					['ActiveCampaign', 'dashicons-megaphone'],
					['GetResponse', 'dashicons-chart-line'],
					['Mailchimp', 'dashicons-groups'],
				];

				foreach ($providers as $provider) :
					?>
					<a class="ipb-core-provider is-locked" href="<?php echo esc_url($pro_url); ?>" target="_blank" rel="noopener noreferrer">
						<span class="dashicons <?php echo esc_attr($provider[1]); ?>" aria-hidden="true"></span>
						<span class="ipb-core-provider-copy">
							<strong><?php echo esc_html($provider[0]); ?></strong>
							<small><?php esc_html_e('Available with Subscription Pro', 'instant-popup-builder'); ?></small>
						</span>
						<span class="dashicons dashicons-lock" aria-hidden="true"></span>
					</a>
				<?php endforeach; ?>
			</div>

			<p class="ipb-core-upsell-action">
				<a class="button button-primary" href="<?php echo esc_url($pro_url); ?>" target="_blank" rel="noopener noreferrer">
					<?php esc_html_e('Get Subscription Pro', 'instant-popup-builder'); ?>
				</a>
				<span><?php esc_html_e('Connect subscriber forms to your preferred email platform.', 'instant-popup-builder'); ?></span>
			</p>
		</div>
	</div>
</div>

<style>
	.ipb-core-integration{max-width:1200px}
	.ipb-core-integration>h1{margin-top:20px;margin-bottom:4px}
	.ipb-core-integration>.description{font-size:14px;margin-bottom:18px}
	.ipb-core-integration-layout{display:grid;grid-template-columns:minmax(300px,.8fr) minmax(480px,1.2fr);gap:20px;align-items:start}
	.ipb-core-integration-card{max-width:none;margin:0;padding:20px}
	.ipb-core-integration-card h2{margin:0 0 8px;font-size:16px}
	.ipb-core-card-heading{display:flex;justify-content:space-between;gap:16px;align-items:flex-start;margin-bottom:16px}
	.ipb-pro-badge{display:inline-block;padding:3px 8px;border-radius:10px;background:#d63638;color:#fff;font-size:11px;font-weight:600;text-transform:uppercase}
	.ipb-core-provider-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}
	.ipb-core-provider{display:grid;grid-template-columns:24px minmax(0,1fr) auto;gap:10px;align-items:center;padding:13px;border:1px solid #c3c4c7;background:#fff;color:#1d2327;text-decoration:none}
	.ipb-core-provider:hover{color:#1d2327;border-color:#8c8f94}
	.ipb-core-provider>.dashicons{color:#50575e}
	.ipb-core-provider-copy strong,.ipb-core-provider-copy small{display:block}
	.ipb-core-provider-copy small{margin-top:3px;color:#646970}
	.ipb-wordpress-mail-card{grid-template-columns:32px minmax(0,1fr);padding:18px}
	.ipb-wordpress-mail-card>.dashicons{font-size:26px;width:26px;height:26px}
	.ipb-core-provider.is-locked{background:#f6f7f7}
	.ipb-core-upsell-action{display:flex;align-items:center;gap:12px;margin:18px 0 0}
	.ipb-core-upsell-action span{color:#646970}
	@media(max-width:1000px){.ipb-core-integration-layout{grid-template-columns:1fr}}
	@media(max-width:650px){.ipb-core-provider-grid{grid-template-columns:1fr}.ipb-core-upsell-action{align-items:flex-start;flex-direction:column}}
</style>
