<?php
/**
 * AJAX handler for sending test emails.
 *
 * @package MailDoctor
 */

use MailDoctor\WPMD_Email_Templates;

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

/**
 * AJAX handler for sending test emails.
 *
 * @return void
 */
function mail_doctor_ajax_send_test(): void {
	if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'You do not have permission to send test emails.', 'mail-doctor' ),
			),
			403
		);
	}

	check_ajax_referer( 'mail_doctor_send_test', 'nonce' );

	$email_type = isset( $_POST['email_type'] ) ? sanitize_key( wp_unslash( $_POST['email_type'] ) ) : '';
	$order_id   = isset( $_POST['order_id'] ) ? absint( wp_unslash( $_POST['order_id'] ) ) : 0;
	if ( ! $order_id ) {
		$order_id = mail_doctor_find_sample_order_id();
	}
	$recipient = isset( $_POST['recipient'] ) ? sanitize_email( wp_unslash( $_POST['recipient'] ) ) : '';

	if ( '' === $email_type || '' === $recipient ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Recipient email and email type are required.', 'mail-doctor' ),
			),
			400
		);
	}

	if ( ! is_email( $recipient ) ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Please provide a valid recipient email address.', 'mail-doctor' ),
			),
			400
		);
	}

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized individually below.
	$template_input = isset( $_POST['template'] ) && is_array( $_POST['template'] ) ? wp_unslash( $_POST['template'] ) : array();

	$template = array(
		'subject' => WPMD_Email_Templates::sanitize_line_field( isset( $template_input['subject'] ) ? (string) $template_input['subject'] : '' ),
		'heading' => WPMD_Email_Templates::sanitize_line_field( isset( $template_input['heading'] ) ? (string) $template_input['heading'] : '' ),
		'html'    => WPMD_Email_Templates::sanitize_html_field( isset( $template_input['html'] ) ? (string) $template_input['html'] : '' ),
		'css'     => WPMD_Email_Templates::sanitize_css_field( isset( $template_input['css'] ) ? (string) $template_input['css'] : '' ),
	);

	$prepared = mail_doctor_prepare_email_template(
		$email_type,
		$order_id,
		array(
			'template'  => $template,
			'test_send' => true,
		)
	);

	if ( empty( $prepared['html'] ) ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Unable to render the email for sending.', 'mail-doctor' ),
			),
			500
		);
	}

	$headers = array(
		'Content-Type: text/html; charset=UTF-8',
		'X-WPMD-Test: 1',
	);

	$sent = wp_mail(
		$recipient,
		$prepared['subject'] ?: sprintf(
			/* translators: %s: email type */
			esc_html__( 'WooCommerce email: %s', 'mail-doctor' ),
			$email_type
		),
		$prepared['html'],
		$headers
	);

	if ( ! $sent ) {
		wp_send_json_error(
			array(
				'message' => esc_html__( 'Failed to send the test email.', 'mail-doctor' ),
			),
			500
		);
	}

	wp_send_json_success(
		array(
			'message' => esc_html__( 'Test email sent successfully.', 'mail-doctor' ),
		)
	);
}
