<?php
/**
 * Newsletter Block - Server-side render
 *
 * @package kairo
 * @since 1.0.0
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Block content.
 * @var WP_Block $block      Block instance.
 */

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

$heading          = ! empty( $attributes['heading'] ) ? $attributes['heading'] : __( 'Join the Newsletter', 'kairo' );
$description      = ! empty( $attributes['description'] ) ? $attributes['description'] : __( 'Get weekly insights on personal branding, productivity, and building an audience.', 'kairo' );
$button_text      = ! empty( $attributes['buttonText'] ) ? $attributes['buttonText'] : __( 'Subscribe', 'kairo' );
$placeholder      = ! empty( $attributes['placeholder'] ) ? $attributes['placeholder'] : __( 'Enter your email', 'kairo' );
$show_name_field  = ! empty( $attributes['showNameField'] );
$name_placeholder = ! empty( $attributes['namePlaceholder'] ) ? $attributes['namePlaceholder'] : __( 'Your First Name', 'kairo' );
$privacy_text     = isset( $attributes['privacyText'] ) ? $attributes['privacyText'] : __( 'No spam. Unsubscribe anytime.', 'kairo' );
$use_jetpack      = isset( $attributes['useJetpack'] ) ? $attributes['useJetpack'] : true;
$form_action      = ! empty( $attributes['formAction'] ) ? $attributes['formAction'] : '';
$form_layout      = ! empty( $attributes['formLayout'] ) ? $attributes['formLayout'] : 'inline';
$background_style = ! empty( $attributes['backgroundStyle'] ) ? $attributes['backgroundStyle'] : 'dark';

// Check if Jetpack Subscriptions is available and enabled.
$jetpack_available = function_exists( 'Jetpack' ) && class_exists( 'Jetpack_Subscriptions_Widget' );

// Use Jetpack when: useJetpack is true, no custom form action, and Jetpack is available.
$render_jetpack = $use_jetpack && empty( $form_action ) && $jetpack_available;

$class_names = array(
	'kairo-newsletter',
	'kairo-newsletter--' . esc_attr( $background_style ),
	'kairo-newsletter--' . esc_attr( $form_layout ),
);

if ( $render_jetpack ) {
	$class_names[] = 'kairo-newsletter--jetpack';
}

$wrapper_attributes = get_block_wrapper_attributes(
	array(
		'class' => implode( ' ', $class_names ),
	)
);
?>
<div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
	<div class="kairo-newsletter__inner">
		<?php if ( $heading ) : ?>
			<h3 class="kairo-newsletter__heading"><?php echo wp_kses_post( $heading ); ?></h3>
		<?php endif; ?>

		<?php if ( $description ) : ?>
			<p class="kairo-newsletter__description"><?php echo wp_kses_post( $description ); ?></p>
		<?php endif; ?>

		<?php if ( $render_jetpack ) : ?>
			<?php
			// Render Jetpack subscription form via shortcode.
			$shortcode_atts = array(
				'show_subscribers_total' => '0',
				'button_on_newline'      => 'stacked' === $form_layout ? '1' : '0',
				'submit_button_text'     => $button_text,
				'subscribe_placeholder'  => $placeholder,
			);

			$shortcode_string = '[jetpack_subscription_form';
			foreach ( $shortcode_atts as $key => $value ) {
				$shortcode_string .= ' ' . $key . '="' . esc_attr( $value ) . '"';
			}
			$shortcode_string .= ']';

			echo do_shortcode( $shortcode_string );
			?>
		<?php else : ?>
			<form class="kairo-newsletter__form" action="<?php echo esc_url( $form_action ? $form_action : '#' ); ?>" method="post">
				<?php if ( $show_name_field ) : ?>
					<input 
						type="text" 
						name="first_name" 
						class="kairo-newsletter__input kairo-newsletter__input--name" 
						placeholder="<?php echo esc_attr( $name_placeholder ); ?>"
					>
				<?php endif; ?>
				<input 
					type="email" 
					name="email" 
					class="kairo-newsletter__input" 
					placeholder="<?php echo esc_attr( $placeholder ); ?>" 
					required
				>
				<button type="submit" class="kairo-newsletter__button">
					<?php echo esc_html( $button_text ); ?>
				</button>
			</form>
		<?php endif; ?>

		<?php if ( $privacy_text ) : ?>
			<p class="kairo-newsletter__privacy"><?php echo esc_html( $privacy_text ); ?></p>
		<?php endif; ?>
	</div>
</div>
