<?php
/**
 * Email template rendering.
 *
 * Renders HTML email content using WooCommerce email template
 * structure for visual consistency with store emails.
 *
 * @package Waitlytics
 */

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

/**
 * Class WTLY_Email_Templates
 */
class WTLY_Email_Templates {

	/**
	 * Render the back-in-stock notification email.
	 *
	 * @param object     $subscriber Subscriber row from database.
	 * @param WC_Product $product    WooCommerce product object.
	 * @param array      $settings   Plugin settings.
	 * @return string Rendered email HTML.
	 */
	public function render_back_in_stock_email( $subscriber, $product, $settings ) {
		$product_name  = $product->get_name();
		$product_url   = $product->get_permalink();
		$product_price = $product->get_price_html();
		$product_image = '';

		// Get product image URL.
		$image_id = $product->get_image_id();
		if ( $image_id ) {
			$image_data = wp_get_attachment_image_src( $image_id, 'woocommerce_thumbnail' );
			if ( $image_data ) {
				$product_image = $image_data[0];
			}
		}

		$show_image = ! empty( $settings['email_include_product_image'] );
		$show_price = ! empty( $settings['email_include_price'] );
		$show_cta   = ! empty( $settings['email_include_cta_button'] );

		// Build unsubscribe URL.
		$notification    = new WTLY_Notification();
		$unsubscribe_url = $notification->get_unsubscribe_url( $subscriber );

		// Email heading.
		$email_heading = ! empty( $settings['email_heading'] )
			? str_replace( '{product_name}', $product_name, $settings['email_heading'] )
			: sprintf(
				/* translators: %s: product name */
				__( 'Good news! %s is back.', 'waitlytics' ),
				$product_name
			);

		// Use null for $email parameter — we are not using WC_Email class.
		$email = null;

		ob_start();
		$template_path = WTLY_PLUGIN_DIR . 'templates/emails/back-in-stock.php';
		if ( file_exists( $template_path ) ) {
			include $template_path;
		}
		return ob_get_clean();
	}

	/**
	 * Render the subscription confirmed email.
	 *
	 * @param object     $subscriber Subscriber row from database.
	 * @param WC_Product $product    WooCommerce product object.
	 * @return string Rendered email HTML.
	 */
	public function render_subscription_confirmed_email( $subscriber, $product ) {
		$product_name = $product->get_name();
		$product_url  = $product->get_permalink();
		$site_name    = get_bloginfo( 'name' );

		// Build unsubscribe URL.
		$notification    = new WTLY_Notification();
		$unsubscribe_url = $notification->get_unsubscribe_url( $subscriber );

		$email_heading = sprintf(
			/* translators: %s: product name */
			__( "You're on the list for %s", 'waitlytics' ),
			$product_name
		);

		$email = null;

		ob_start();
		$template_path = WTLY_PLUGIN_DIR . 'templates/emails/subscription-confirmed.php';
		if ( file_exists( $template_path ) ) {
			include $template_path;
		}
		return ob_get_clean();
	}

	/**
	 * Send the subscription confirmed email.
	 *
	 * @param object     $subscriber Subscriber row from database.
	 * @param WC_Product $product    WooCommerce product object.
	 */
	public function send_subscription_confirmed( $subscriber, $product ) {
		$html = $this->render_subscription_confirmed_email( $subscriber, $product );

		if ( empty( $html ) ) {
			return;
		}

		$settings  = wtly_get_settings();
		$from_name = ! empty( $settings['email_from_name'] )
			? $settings['email_from_name']
			: get_bloginfo( 'name' );
		$from_email = ! empty( $settings['email_from_address'] )
			? $settings['email_from_address']
			: get_option( 'admin_email' );

		$subject = sprintf(
			/* translators: %s: product name */
			__( "You'll be notified when %s is back in stock", 'waitlytics' ),
			$product->get_name()
		);

		$headers = array(
			'Content-Type: text/html; charset=UTF-8',
			sprintf( 'From: %s <%s>', sanitize_text_field( $from_name ), sanitize_email( $from_email ) ),
		);

		wp_mail( $subscriber->email, $subject, $html, $headers );
	}
}
