<?php
/**
 * Footer newsletter shell (architecture — no ESP integration).
 *
 * @package Kutby
 * @since   2.7.0
 */

declare(strict_types=1);

namespace Kutby\Footer\Components;

use Kutby\Footer\Contracts\FooterComponentInterface;
use Kutby\Footer\Support\FooterContext;

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

/**
 * Class Newsletter
 */
final class Newsletter implements FooterComponentInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( FooterContext $ctx, array $args = array() ): void {
		if ( ! $ctx->bool( 'show_newsletter' ) ) {
			return;
		}

		$title       = $ctx->string( 'newsletter_title', 'النشرة البريدية' );
		$placeholder = $ctx->string( 'newsletter_placeholder', 'بريدك الإلكتروني' );
		$field_id    = isset( $args['id'] ) ? (string) $args['id'] : wp_unique_id( 'kutby-footer-newsletter-' );

		/**
		 * Newsletter form action URL (default: current page — shell only).
		 *
		 * @since 2.7.0
		 *
		 * @param string $action Action URL.
		 */
		$action = (string) apply_filters( 'kutby_footer_newsletter_action', '' );
		?>
		<div class="kutby-footer-newsletter">
			<?php if ( '' !== $title ) : ?>
				<p class="kutby-footer-newsletter__title"><?php echo esc_html( $title ); ?></p>
			<?php endif; ?>
			<form
				class="kutby-footer-newsletter__form"
				method="post"
				action="<?php echo esc_url( $action ); ?>"
				<?php echo '' === $action ? ' data-kutby-newsletter-shell="1"' : ''; ?>
			>
				<label class="kutby-sr-only" for="<?php echo esc_attr( $field_id ); ?>"><?php echo esc_html( $placeholder ); ?></label>
				<input
					type="email"
					id="<?php echo esc_attr( $field_id ); ?>"
					name="kutby_newsletter_email"
					class="kutby-footer-newsletter__input"
					placeholder="<?php echo esc_attr( $placeholder ); ?>"
					autocomplete="email"
					required
				/>
				<button type="submit" class="kutby-btn kutby-footer-newsletter__submit">
					اشتراك
				</button>
			</form>
		</div>
		<?php
	}
}
