<?php
/**
 * Email options content view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Parts
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Parts;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Settings\Options\Emails\FromNameOption;
use Awesomemotive\Rewardswp\Settings\Options\Emails\FromEmailOption;
use Awesomemotive\Rewardswp\Settings\Options\Emails\EmailLogoOption;
use Awesomemotive\Rewardswp\Factories\ViewsFactory;
use function Awesomemotive\Rewardswp\Helpers\get_option_value;

class EmailOptionsContentView extends AbstractView implements ViewInterface {

	/**
	 * Views factory instance.
	 *
	 * @since 1.0.0
	 *
	 * @var ViewsFactory
	 */
	private ViewsFactory $views_factory;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param ViewsFactory $views_factory Views factory instance.
	 */
	public function __construct( ViewsFactory $views_factory ) {
		$this->views_factory = $views_factory;
	}

	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {

		$from_name  = get_option_value( FromNameOption::class );
		$from_email = get_option_value( FromEmailOption::class );
		$email_logo = get_option_value( EmailLogoOption::class );

		// Extract logo data from array
		$logo_attachment_id = $email_logo['attachment_id'] ?? 0;
		$logo_size          = $email_logo['size'] ?? 'medium';
		$logo_preview_url   = '';

		// Get logo preview URL if attachment exists
		if ( $logo_attachment_id ) {
			$image_data = wp_get_attachment_image_src( $logo_attachment_id, $logo_size );
			if ( $image_data ) {
				$logo_preview_url = $image_data[0];
				// Force HTTPS if current site is HTTPS to avoid Mixed Content issues
				if ( is_ssl() && strpos( $logo_preview_url, 'http://' ) === 0 ) {
					$logo_preview_url = str_replace( 'http://', 'https://', $logo_preview_url );
				}
			}
		}

		?>
		<div class="divide-y divide-gray-200">

			<div class="py-5">
				<div class="gap-8 md:flex md:items-center">
					<div class="md:w-1/3 flex-none">
						<label for="email_from_name" class="text-sm font-semibold"><?php esc_html_e( 'From Name', 'rewardswp' ); ?></label>
						<p class="text-sm text-gray-500 mt-2"><?php esc_html_e( 'The name that emails come from. This is usually your site name.', 'rewardswp' ); ?></p>
					</div>
					<div class="md:w-2/3">
						<div class="sm:max-w-xs mt-2 xs:mt-0 flex items-center rounded-md bg-white px-3 outline-1 -outline-offset-1 outline-gray-300 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-blue-600">
							<input
								type="text"
								id="email_from_name"
								name="rewardswp_settings[emails][from_name]"
								class="block min-w-0 grow py-2 pr-3 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none sm:text-sm/6"
								x-model="fromName"
								placeholder="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"
							>
						</div>
					</div>
				</div>
			</div>

			<div class="py-5">
				<div class="gap-8 md:flex md:items-center">
					<div class="md:w-1/3 flex-none">
						<label for="email_from_email" class="text-sm font-semibold"><?php esc_html_e( 'From Email', 'rewardswp' ); ?></label>
						<p class="text-sm text-gray-500 mt-2"><?php esc_html_e( 'The email address to send emails from. This will act as the "from" and "reply-to" address.', 'rewardswp' ); ?></p>
					</div>
					<div class="md:w-2/3">
						<div class="sm:max-w-xs mt-2 xs:mt-0 flex items-center rounded-md bg-white px-3 outline-1 -outline-offset-1 outline-gray-300 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-blue-600">
							<input
								type="text"
								id="email_from_email"
								name="rewardswp_settings[emails][from_email]"
								class="block min-w-0 grow py-2 pr-3 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none sm:text-sm/6"
								x-model="fromEmail"
								placeholder="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>"
							>
						</div>
					</div>
				</div>
			</div>

			<div class="py-5">
				<div class="gap-8 md:flex md:items-start">
					<div class="md:w-1/3 flex-none">
						<label class="text-sm font-semibold"><?php esc_html_e( 'Email Logo', 'rewardswp' ); ?></label>
						<p class="text-sm text-gray-500 mt-2"><?php esc_html_e( 'Upload a logo to be displayed in all email notifications. This will appear at the top of emails.', 'rewardswp' ); ?></p>
					</div>
					<div class="md:w-2/3">
						<div class="mt-2 xs:mt-0">
							<?php
							// Get current logo data for PHP fallback values
							$logo_data     = get_option_value( EmailLogoOption::class );
							$attachment_id = $logo_data['attachment_id'] ?? 0;
							$size          = $logo_data['size'] ?? 'medium';
							$preview_url   = '';
							if ( $attachment_id ) {
								$image_data = wp_get_attachment_image_src( $attachment_id, $size );
								if ( $image_data ) {
									$preview_url = $image_data[0];
									if ( is_ssl() && strpos( $preview_url, 'http://' ) === 0 ) {
										$preview_url = str_replace( 'http://', 'https://', $preview_url );
									}
								}
							}

							$this->views_factory->get_view( \Awesomemotive\Rewardswp\Views\Admin\Components\MediaUploadComponentView::class )->render(
								[
									'label'         => '',
									'help_text'     => '',
									'x_model_id'    => 'logoAttachmentId',  // Alpine.js binding (preferred)
									'x_model_url'   => 'logoPreviewUrl',    // Alpine.js binding (preferred)
									'x_model_size'  => 'logoSize',          // Alpine.js binding (preferred)
									'attachment_id' => $attachment_id,      // PHP fallback
									'preview_url'   => $preview_url,        // PHP fallback
									'size'          => $size,               // PHP fallback
									'preview_size'  => EmailLogoOption::EMAIL_LOGO_DEFAULT_SIZE,
									'button_text'   => __( 'Upload Logo', 'rewardswp' ),
									'change_text'   => __( 'Change Logo', 'rewardswp' ),
									'remove_text'   => __( 'Remove Logo', 'rewardswp' ),
									'select_title'  => __( 'Select Email Logo', 'rewardswp' ),
									'select_button' => __( 'Use This Logo', 'rewardswp' ),
									'x_ref'         => 'emailLogoUpload',
								]
							);
							?>

							<!-- Hidden inputs for form submission -->
							<input type="hidden" name="rewardswp_settings[emails][email_logo][attachment_id]" x-bind:value="logoAttachmentId">
							<input type="hidden" name="rewardswp_settings[emails][email_logo][size]" x-bind:value="logoSize">
						</div>
					</div>
				</div>
			</div>

		</div>

		<?php
	}
}
