<?php
/**
 * Coupon opt-in email template editor + live preview.
 *
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

use SuiteMkt3k\Admin\Email_Preview_Renderer;
use SuiteMkt3k\Modules\Coupons\Coupon_Email_Template_Manager;
use SuiteMkt3k\Utils\Sanitizer;

$tpl         = Coupon_Email_Template_Manager::get_template();
$test_status = Sanitizer::get( 'test', 'key' );
$updated     = Sanitizer::flag( INPUT_GET, 'updated' );
$design      = Coupon_Email_Template_Manager::merge_design( (array) ( $tpl['design'] ?? array() ) );

$company  = (array) \SuiteMkt3k\Modules\Settings\Settings_Manager::get( 'company', '', array() );
$logo_id  = isset( $company['logo_id'] ) ? (int) $company['logo_id'] : 0;
$logo_url = $logo_id ? (string) ( wp_get_attachment_image_url( $logo_id, 'medium' ) ?: '' ) : '';
$brand    = (string) ( $company['name'] ?? get_bloginfo( 'name' ) );

$state = array(
	'subject'           => (string) ( $tpl['subject'] ?? Coupon_Email_Template_Manager::default_subject() ),
	'heading'           => (string) ( $design['heading'] ?? '' ),
	'preheader'         => (string) ( $design['preheader'] ?? '' ),
	'inbox_preview'     => (string) ( $design['inbox_preview'] ?? '' ),
	'body'              => (string) ( $design['body'] ?? '' ),
	'cta_label'         => (string) ( $design['cta_label'] ?? '' ),
	'accent'            => (string) ( $design['accent'] ?? '#2AB2E5' ),
	'unsubscribe_text'  => (string) ( $design['unsubscribe_text'] ?? '' ),
	'unsubscribe_cta'   => (string) ( $design['unsubscribe_cta'] ?? '' ),
	'cta_radius'        => (string) ( $design['cta_radius'] ?? '10' ),
	'logo_url'          => $logo_url,
	'brand'             => $brand,
);

$alpine_payload = wp_json_encode( $state );

$preview_data = array_merge(
	$state,
	\SuiteMkt3k\Email\Email_Renderer::footer_context(),
	array(
		'sample_name'   => __( 'there', 'suitemkt3k' ),
		'body_fallback' => __( 'Your email body will appear here.', 'suitemkt3k' ),
	)
);
?>
<?php if ( $updated ) : ?><div class="wmk-flash wmk-flash-success"><?php esc_html_e( 'Template saved.', 'suitemkt3k' ); ?></div><?php endif; ?>
<?php if ( 'sent' === $test_status ) : ?><div class="wmk-flash wmk-flash-success"><?php esc_html_e( 'Test email sent.', 'suitemkt3k' ); ?></div><?php endif; ?>
<?php if ( 'failed' === $test_status ) : ?><div class="wmk-flash wmk-flash-error"><?php esc_html_e( 'Test email failed. Verify SMTP settings.', 'suitemkt3k' ); ?></div><?php endif; ?>

<div class="wmk-designer-grid" data-wmk-designer="coupon" data-wmk-initial="<?php echo esc_attr( wp_json_encode( $state ) ); ?>">
	<div class="wmk-designer-main wmk-alpine-pending"
		x-data="<?php echo esc_attr( $alpine_payload ); ?>"
		x-init="$el.classList.remove('wmk-alpine-pending')">
	<form class="wmk-card" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="suitemkt3k_coupon_email_template_save">
		<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>

		<h3><?php esc_html_e( 'Edit coupon opt-in email template', 'suitemkt3k' ); ?></h3>
		<p class="wmk-help"><?php esc_html_e( 'Email sent when a visitor signs up through a coupon popup. Campaign marketing text is inserted via {{marketing}}.', 'suitemkt3k' ); ?></p>

		<div class="wmk-grid wmk-grid-2">
			<label class="wmk-field wmk-field-full"><span><?php esc_html_e( 'Subject', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="subject" required x-model="subject"></label>
			<label class="wmk-field"><span><?php esc_html_e( 'Heading', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[heading]" x-model="heading">
				<small class="wmk-help"><?php esc_html_e( 'Placeholders: {{coupon_name}}, {{code}}, {{name}}.', 'suitemkt3k' ); ?></small>
			</label>
			<label class="wmk-field"><span><?php esc_html_e( 'Inbox preview line', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[inbox_preview]" x-model="inbox_preview">
				<small class="wmk-help"><?php esc_html_e( 'Hidden preview text in the inbox (not shown in the email body).', 'suitemkt3k' ); ?></small>
			</label>
			<label class="wmk-field wmk-field-full"><span><?php esc_html_e( 'Body', 'suitemkt3k' ); ?></span>
				<textarea class="wmk-input" name="design[body]" rows="6" x-model="body"></textarea>
				<small class="wmk-help"><?php esc_html_e( 'Placeholders: {{marketing}}, {{code}}, {{coupon_name}}, {{name}}. HTML allowed for {{code}}.', 'suitemkt3k' ); ?></small>
			</label>
			<label class="wmk-field"><span><?php esc_html_e( 'CTA label', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[cta_label]" x-model="cta_label"></label>
			<label class="wmk-field"><span><?php esc_html_e( 'Accent color', 'suitemkt3k' ); ?></span>
				<?php \SuiteMkt3k\Admin\UI::color_input( 'design[accent]', (string) ( $design['accent'] ?? '#2AB2E5' ), 'x-model="accent"' ); ?>
			</label>
			<label class="wmk-field wmk-field-narrow"><span><?php esc_html_e( 'CTA border radius', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" type="number" min="0" max="99" name="design[cta_radius]" x-model="cta_radius"></label>
			<label class="wmk-field wmk-field-full"><span><?php esc_html_e( 'Intro line', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[preheader]" x-model="preheader">
				<small class="wmk-help"><?php esc_html_e( 'Short line shown below the CTA button (e.g. “Your discount is ready”).', 'suitemkt3k' ); ?></small>
			</label>
			<label class="wmk-field wmk-field-full"><span><?php esc_html_e( 'Unsubscribe sentence', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[unsubscribe_text]" x-model="unsubscribe_text"></label>
			<label class="wmk-field"><span><?php esc_html_e( 'Unsubscribe link text', 'suitemkt3k' ); ?></span>
				<input class="wmk-input" name="design[unsubscribe_cta]" x-model="unsubscribe_cta"></label>
		</div>

		<div class="wmk-form-actions">
			<a class="wmk-btn wmk-btn-ghost" href="<?php echo esc_url( admin_url( 'admin.php?page=suitemkt3k-coupons' ) ); ?>"><?php esc_html_e( 'Back to list', 'suitemkt3k' ); ?></a>
			<button class="wmk-btn wmk-btn-primary" type="submit"><?php esc_html_e( 'Save template', 'suitemkt3k' ); ?></button>
		</div>
	</form>
	</div>

	<?php Email_Preview_Renderer::coupon_optin( $preview_data ); ?>
</div>

<form class="wmk-card wmk-card-soft" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin-top:20px;">
	<input type="hidden" name="action" value="suitemkt3k_coupon_email_test">
	<?php wp_nonce_field( \SuiteMkt3k\Security\Nonces::ADMIN_ACTION ); ?>
	<h4><?php esc_html_e( 'Send a test of this template', 'suitemkt3k' ); ?></h4>
	<div class="wmk-grid wmk-grid-2">
		<label class="wmk-field"><span><?php esc_html_e( 'Recipient', 'suitemkt3k' ); ?></span>
			<input class="wmk-input" type="email" name="test_email" value="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>" required></label>
	</div>
	<div class="wmk-form-actions">
		<button class="wmk-btn wmk-btn-secondary" type="submit"><?php esc_html_e( 'Send test email', 'suitemkt3k' ); ?></button>
	</div>
</form>
