<?php
/**
 * Table Reservation AJAX handler.
 * Saves reservation and sends emails. Used by Table Reservation widget.
 *
 * @package Unlock_Addons_For_Elementor
 */

namespace Unlockafe_addons\Classes;

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

class Table_Reservation_Handler {

	const OPTION_KEY = 'unlockafe_table_reservations';
	const OPTION_EMAIL_KEY = 'unlockafe_reservation_email_settings';

	/** Last mail error message (set when wp_mail fails) for display to admin. */
	protected static $last_mail_error = '';

	public static function init() {
		add_action('wp_ajax_unlockafe_table_reservation_submit', [__CLASS__, 'handle_submit']);
		add_action('wp_ajax_nopriv_unlockafe_table_reservation_submit', [__CLASS__, 'handle_submit']);
	}

	/**
	 * Get reservation email sender settings (from name, from email, app password, SMTP).
	 *
	 * @return array
	 */
	public static function get_email_settings() {
		$defaults = [
			'from_name'               => get_bloginfo('name') ?: 'WordPress',
			'from_email'              => get_option('admin_email'),
			'smtp_password'           => '',
			'smtp_host'               => '',
			'smtp_port'               => '587',
			'smtp_encryption'         => 'tls',
			'smtp_disable_ssl_verify' => '',
		];
		$saved = get_option(self::OPTION_EMAIL_KEY, []);
		if (!is_array($saved)) {
			$saved = [];
		}
		return array_merge($defaults, $saved);
	}

	/**
	 * Configure PHPMailer for reservation emails (custom From + optional SMTP).
	 * Hooked to phpmailer_init only when sending reservation emails.
	 */
	public static function configure_phpmailer($phpmailer) {
		$s = self::get_email_settings();
		$from_email = !empty($s['from_email']) && is_email($s['from_email']) ? $s['from_email'] : get_option('admin_email');
		$from_name  = !empty($s['from_name']) ? $s['from_name'] : get_bloginfo('name');
		$phpmailer->SetFrom($from_email, $from_name);
		$phpmailer->CharSet = 'UTF-8';

		$password = isset($s['smtp_password']) ? $s['smtp_password'] : '';
		if ($password !== '') {
			$phpmailer->isSMTP();
			$phpmailer->Host       = !empty($s['smtp_host']) ? $s['smtp_host'] : 'smtp.gmail.com';
			$phpmailer->Port       = !empty($s['smtp_port']) ? (int) $s['smtp_port'] : 587;
			$phpmailer->SMTPAuth   = true;
			$phpmailer->Username   = $from_email;
			$phpmailer->Password   = $password;
			$encryption = isset($s['smtp_encryption']) ? $s['smtp_encryption'] : 'tls';
			$phpmailer->SMTPSecure = ($encryption === 'ssl') ? 'ssl' : 'tls';
			$phpmailer->Timeout    = 15;
			// Fix "certificate verify failed" on localhost / dev when SSL verify is disabled in settings.
			if (!empty($s['smtp_disable_ssl_verify']) && $s['smtp_disable_ssl_verify'] === 'yes') {
				$phpmailer->SMTPOptions = [
					'ssl' => [
						'verify_peer'       => false,
						'verify_peer_name'  => false,
					],
				];
			}
		}
	}

	/**
	 * Capture wp_mail_failed error for display.
	 *
	 * @param \WP_Error $wp_error
	 */
	public static function capture_mail_failed($wp_error) {
		if (is_wp_error($wp_error)) {
			self::$last_mail_error = $wp_error->get_error_message();
		}
	}

	/**
	 * Get and clear the last mail error message.
	 *
	 * @return string
	 */
	public static function get_last_mail_error() {
		$err = self::$last_mail_error;
		self::$last_mail_error = '';
		return $err;
	}

	public static function handle_submit() {
		check_ajax_referer('unlockafe_table_reservation_submit', 'nonce');

		$name     = isset($_POST['guest_name']) ? sanitize_text_field(wp_unslash($_POST['guest_name'])) : '';
		$email    = isset($_POST['guest_email']) ? sanitize_email(wp_unslash($_POST['guest_email'])) : '';
		$phone    = isset($_POST['guest_phone']) ? sanitize_text_field(wp_unslash($_POST['guest_phone'])) : '';
		$location = isset($_POST['guest_location']) ? sanitize_text_field(wp_unslash($_POST['guest_location'])) : '';
		$party    = isset($_POST['party_size']) ? absint($_POST['party_size']) : 0;
		$date     = isset($_POST['reservation_date']) ? sanitize_text_field(wp_unslash($_POST['reservation_date'])) : '';
		$time     = isset($_POST['reservation_time']) ? sanitize_text_field(wp_unslash($_POST['reservation_time'])) : '';
		$notes    = isset($_POST['special_requests']) ? sanitize_textarea_field(wp_unslash($_POST['special_requests'])) : '';
		$min_guest = isset($_POST['min_guest']) ? absint($_POST['min_guest']) : null;
		$max_guest = isset($_POST['max_guest']) ? absint($_POST['max_guest']) : null;

		$custom_data = [];
		foreach ($_POST as $key => $value) {
			if (strpos($key, 'custom_type_') === 0 && is_string($value)) {
				$idx = str_replace('custom_type_', '', $key);
				if (is_numeric($idx)) {
					$custom_data[$idx]['type'] = sanitize_text_field(wp_unslash($value));
				}
			}
			if (strpos($key, 'custom_qty_') === 0) {
				$idx = str_replace('custom_qty_', '', $key);
				if (is_numeric($idx)) {
					$custom_data[$idx]['qty'] = absint($value);
				}
			}
		}
		ksort($custom_data);

		if (!empty($email) && !is_email($email)) {
			wp_send_json_error(['message' => __('Please enter a valid email address.', 'unlock-addons-for-elementor')]);
		}

		if (empty($date) || empty($time)) {
			wp_send_json_error(['message' => __('Please select date and time.', 'unlock-addons-for-elementor')]);
		}

		if (isset($_POST['party_size']) && ($party < 1 || $party > 50)) {
			wp_send_json_error(['message' => __('Please select a valid party size.', 'unlock-addons-for-elementor')]);
		}

		if (isset($_POST['min_guest']) && ($min_guest < 1 || $min_guest > 50)) {
			wp_send_json_error(['message' => __('Please enter a valid minimum guest (1–50).', 'unlock-addons-for-elementor')]);
		}
		if (isset($_POST['max_guest']) && ($max_guest < 1 || $max_guest > 50)) {
			wp_send_json_error(['message' => __('Please enter a valid maximum guest (1–50).', 'unlock-addons-for-elementor')]);
		}

		$reservation = [
			'id'          => uniqid('res_', true),
			'name'        => $name,
			'email'       => $email,
			'phone'       => $phone,
			'location'    => $location,
			'party'       => $party,
			'min_guest'   => $min_guest,
			'max_guest'   => $max_guest,
			'date'        => $date,
			'time'        => $time,
			'notes'       => $notes,
			'custom_data' => $custom_data,
			'created'     => current_time('mysql'),
			'status'      => 'pending',
		];

		$saved = self::save_reservation($reservation);
		if (!$saved) {
			wp_send_json_error(['message' => __('Could not save reservation. Please try again.', 'unlock-addons-for-elementor')]);
		}

		$admin_email = isset($_POST['admin_email']) ? sanitize_email(wp_unslash($_POST['admin_email'])) : get_option('admin_email');
		$send_customer = !empty($_POST['send_customer_email']) && $_POST['send_customer_email'] === 'yes';
		$success_msg = isset($_POST['success_message']) ? sanitize_textarea_field(wp_unslash($_POST['success_message'])) : '';

		if ($admin_email && is_email($admin_email)) {
			self::send_admin_notification($reservation, $admin_email);
		}

		if ($send_customer && $email) {
			self::send_customer_confirmation($reservation);
		}

		$summary = [
			'date'       => $date,
			'time'       => $time,
			'party_size' => $party,
			'name'       => $name,
		];
		if ($location) {
			$summary['location'] = $location;
		}
		if ($min_guest !== null && $min_guest > 0) {
			$summary['min_guest'] = $min_guest;
		}
		if ($max_guest !== null && $max_guest > 0) {
			$summary['max_guest'] = $max_guest;
		}
		if (!empty($custom_data)) {
			$summary['custom'] = $custom_data;
		}
		wp_send_json_success([
			'message' => $success_msg ?: __('Thank you! Your table has been reserved.', 'unlock-addons-for-elementor'),
			'summary' => $summary,
			'id'      => $reservation['id'],
		]);
	}

	protected static function save_reservation(array $reservation) {
		$all = get_option(self::OPTION_KEY, []);
		if (!is_array($all)) {
			$all = [];
		}
		$all[] = $reservation;
		return update_option(self::OPTION_KEY, $all);
	}

	protected static function get_mail_headers() {
		$s = self::get_email_settings();
		$from_email = !empty($s['from_email']) && is_email($s['from_email']) ? $s['from_email'] : get_option('admin_email');
		$from_name  = !empty($s['from_name']) ? $s['from_name'] : (get_bloginfo('name') ?: 'WordPress');
		return [
			'Content-Type: text/html; charset=UTF-8',
			'From: ' . $from_name . ' <' . $from_email . '>',
		];
	}

	/** Add phpmailer_init hook, send mail, then remove hook so reservation emails use custom From/SMTP. */
	protected static function send_mail($to, $subject, $body) {
		self::$last_mail_error = '';
		add_action('phpmailer_init', [__CLASS__, 'configure_phpmailer'], 10, 1);
		add_action('wp_mail_failed', [__CLASS__, 'capture_mail_failed'], 10, 1);
		$sent = wp_mail($to, $subject, $body, self::get_mail_headers());
		remove_action('phpmailer_init', [__CLASS__, 'configure_phpmailer'], 10);
		remove_action('wp_mail_failed', [__CLASS__, 'capture_mail_failed'], 10);
		return $sent;
	}

	/**
	 * Send a test email (e.g. from Reservation List page). Uses same From/SMTP settings.
	 *
	 * @param string $to      Recipient email.
	 * @param string $subject Subject.
	 * @param string $body    Body.
	 * @return bool
	 */
	public static function send_test_email($to, $subject, $body) {
		return self::send_mail($to, $subject, $body);
	}

	protected static function send_admin_notification(array $reservation, $admin_email) {
		$subject = sprintf(
			/* translators: %s: site name */
			__('[%s] New Table Reservation', 'unlock-addons-for-elementor'),
			get_bloginfo('name')
		);
		$body = self::format_reservation_email_html($reservation, 'admin');
		self::send_mail($admin_email, $subject, $body);
	}

	protected static function send_customer_confirmation(array $reservation) {
		if (empty($reservation['email']) || !is_email($reservation['email'])) {
			return;
		}
		$subject = sprintf(
			/* translators: %s: site name */
			__('Your reservation at %s is confirmed!', 'unlock-addons-for-elementor'),
			get_bloginfo('name')
		);
		$body = self::format_reservation_email_html($reservation, 'customer');
		self::send_mail($reservation['email'], $subject, $body);
	}

	/**
	 * Build HTML email body (invoice/order style) for reservation.
	 *
	 * @param array  $r   Reservation data.
	 * @param string $ctx 'admin' or 'customer'.
	 * @return string HTML.
	 */
	protected static function format_reservation_email_html(array $r, $ctx = 'customer') {
		$site_name = get_bloginfo('name');
		$title = $ctx === 'admin'
			? __('New Table Reservation', 'unlock-addons-for-elementor')
			: __('Your reservation is confirmed', 'unlock-addons-for-elementor');
		$rows = [
			__('Name', 'unlock-addons-for-elementor')           => esc_html($r['name']),
			__('Email', 'unlock-addons-for-elementor')          => '<a href="mailto:' . esc_attr($r['email']) . '" style="color:#2563eb;text-decoration:none;">' . esc_html($r['email']) . '</a>',
			__('Phone', 'unlock-addons-for-elementor')          => esc_html($r['phone']),
			__('Party size', 'unlock-addons-for-elementor')      => esc_html((string) (isset($r['party']) ? $r['party'] : '—')),
			__('Date', 'unlock-addons-for-elementor')           => esc_html(isset($r['date']) ? $r['date'] : '—'),
			__('Time', 'unlock-addons-for-elementor')           => esc_html(isset($r['time']) ? $r['time'] : '—'),
		];
		if (!empty($r['location'])) {
			$rows[__('Location', 'unlock-addons-for-elementor')] = esc_html($r['location']);
		}
		if (!empty($r['min_guest'])) {
			$rows[__('Minimum guest', 'unlock-addons-for-elementor')] = esc_html($r['min_guest']);
		}
		if (!empty($r['max_guest'])) {
			$rows[__('Maximum guest', 'unlock-addons-for-elementor')] = esc_html($r['max_guest']);
		}
		if (!empty($r['custom_data']) && is_array($r['custom_data'])) {
			foreach ($r['custom_data'] as $row) {
				$label = isset($row['type']) ? $row['type'] : __('Item', 'unlock-addons-for-elementor');
				$qty = isset($row['qty']) ? $row['qty'] : '';
				$rows[esc_html($label)] = esc_html((string) $qty);
			}
		}
		$notes = isset($r['notes']) && $r['notes'] !== '' ? $r['notes'] : '—';
		$rows[__('Special requests', 'unlock-addons-for-elementor')] = esc_html($notes);
		if (!empty($r['status'])) {
			$rows[__('Status', 'unlock-addons-for-elementor')] = '<span style="background:#e5e7eb;color:#374151;padding:4px 10px;border-radius:6px;font-weight:600;">' . esc_html($r['status']) . '</span>';
		}
		if (!empty($r['status_reason'])) {
			$rows[__('Reason', 'unlock-addons-for-elementor')] = esc_html($r['status_reason']);
		}

		$trs = '';
		$i = 0;
		foreach ($rows as $label => $value) {
			$bg = $i % 2 === 0 ? '#ffffff' : '#f9fafb';
			$trs .= '<tr><td style="padding:12px 16px;border-bottom:1px solid #e5e7eb;color:#6b7280;font-size:14px;width:40%;background:' . $bg . ';">' . $label . '</td><td style="padding:12px 16px;border-bottom:1px solid #e5e7eb;color:#111827;font-size:14px;font-weight:500;background:' . $bg . ';">' . $value . '</td></tr>';
			$i++;
		}

		$html = '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"></head><body style="margin:0;padding:0;background:#f3f4f6;font-family:\'Segoe UI\',Tahoma,Geneva,Verdana,sans-serif;-webkit-font-smoothing:antialiased;">';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="background:#f3f4f6;padding:24px 16px;">';
		$html .= '<tr><td align="center">';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="max-width:560px;background:#ffffff;border-radius:12px;overflow:hidden;box-shadow:0 4px 6px -1px rgba(0,0,0,0.1),0 2px 4px -2px rgba(0,0,0,0.1);">';
		$html .= '<tr><td style="background:linear-gradient(135deg,#1f2937 0%,#374151 100%);padding:28px 24px;text-align:center;">';
		$html .= '<h1 style="margin:0;color:#ffffff;font-size:22px;font-weight:700;letter-spacing:-0.02em;">' . esc_html($site_name) . '</h1>';
		$html .= '<p style="margin:8px 0 0;color:#d1d5db;font-size:14px;">' . esc_html($title) . '</p>';
		$html .= '</td></tr>';
		$html .= '<tr><td style="padding:24px;">';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="border:1px solid #e5e7eb;border-radius:8px;overflow:hidden;">';
		$html .= '<thead><tr><th style="padding:12px 16px;text-align:left;background:#f9fafb;color:#6b7280;font-size:12px;text-transform:uppercase;letter-spacing:0.05em;border-bottom:1px solid #e5e7eb;">' . __('Details', 'unlock-addons-for-elementor') . '</th><th style="padding:12px 16px;text-align:left;background:#f9fafb;color:#6b7280;font-size:12px;text-transform:uppercase;letter-spacing:0.05em;border-bottom:1px solid #e5e7eb;"></th></tr></thead>';
		$html .= '<tbody>' . $trs . '</tbody></table>';
		$html .= '</td></tr>';
		$html .= '<tr><td style="padding:16px 24px;background:#f9fafb;border-top:1px solid #e5e7eb;text-align:center;">';
		$html .= '<p style="margin:0;color:#6b7280;font-size:13px;">' . esc_html__('Thank you for your reservation.', 'unlock-addons-for-elementor') . '</p>';
		$html .= '<p style="margin:6px 0 0;color:#9ca3af;font-size:12px;">' . esc_html($site_name) . '</p>';
		$html .= '</td></tr></table></td></tr></table></body></html>';
		return $html;
	}

	protected static function format_reservation_email(array $r) {
		$lines = [
			'Name: ' . $r['name'],
			'Email: ' . $r['email'],
			'Phone: ' . $r['phone'],
			'Party size: ' . $r['party'],
			'Date: ' . $r['date'],
			'Time: ' . $r['time'],
		];
		if (!empty($r['location'])) {
			$lines[] = 'Location: ' . $r['location'];
		}
		if (!empty($r['min_guest'])) {
			$lines[] = 'Minimum Guest: ' . $r['min_guest'];
		}
		if (!empty($r['max_guest'])) {
			$lines[] = 'Maximum Guest: ' . $r['max_guest'];
		}
		if (!empty($r['custom_data']) && is_array($r['custom_data'])) {
			foreach ($r['custom_data'] as $row) {
				$lines[] = (isset($row['type']) ? $row['type'] : '') . ' / Qty: ' . (isset($row['qty']) ? $row['qty'] : '');
			}
		}
		$lines[] = 'Special requests: ' . $r['notes'];
		if (!empty($r['status'])) {
			$lines[] = 'Status: ' . $r['status'];
		}
		if (!empty($r['status_reason'])) {
			$lines[] = 'Reason: ' . $r['status_reason'];
		}
		return implode("\n", $lines);
	}

	/**
	 * Get all reservations (for admin list).
	 *
	 * @return array
	 */
	public static function get_all_reservations() {
		$all = get_option(self::OPTION_KEY, []);
		if (!is_array($all)) {
			return [];
		}
		usort($all, function ($a, $b) {
			$t1 = isset($a['created']) ? strtotime($a['created']) : 0;
			$t2 = isset($b['created']) ? strtotime($b['created']) : 0;
			return $t2 - $t1;
		});
		return $all;
	}

	/**
	 * Update reservation status and optionally send email to customer.
	 *
	 * @param string $id     Reservation ID.
	 * @param string $status One of: pending, complete, cancel.
	 * @param string $reason Optional reason (required when status is cancel).
	 * @return array|false Updated reservation or false on failure.
	 */
	public static function update_reservation_status($id, $status, $reason = '') {
		$all = get_option(self::OPTION_KEY, []);
		if (!is_array($all)) {
			return false;
		}
		$found_index = null;
		$previous    = null;
		foreach ($all as $index => $res) {
			if (isset($res['id']) && $res['id'] === $id) {
				$found_index = $index;
				$previous    = $all[$index];
				break;
			}
		}
		if ($found_index === null) {
			return false;
		}
		$all[$found_index]['status'] = $status;
		$all[$found_index]['status_reason'] = $reason;
		$all[$found_index]['status_updated'] = current_time('mysql');
		if (!update_option(self::OPTION_KEY, $all)) {
			return false;
		}
		$updated = $all[$found_index];
		if (!empty($updated['email']) && is_email($updated['email'])) {
			self::send_status_change_email($updated, $status, $reason);
		}
		return $updated;
	}

	/**
	 * Send email to customer when reservation status is changed.
	 *
	 * @param array  $reservation Updated reservation.
	 * @param string $new_status  New status.
	 * @param string $reason       Optional reason (e.g. cancel reason).
	 */
	public static function send_status_change_email(array $reservation, $new_status, $reason = '') {
		$site = get_bloginfo('name');
		$status_label = $new_status === 'complete' ? __('Completed', 'unlock-addons-for-elementor') : ($new_status === 'cancel' ? __('Cancelled', 'unlock-addons-for-elementor') : __('Pending', 'unlock-addons-for-elementor'));
		$subject = sprintf(
			/* translators: 1: site name, 2: status */
			__('[%1$s] Your reservation status: %2$s', 'unlock-addons-for-elementor'),
			$site,
			$status_label
		);
		$body = self::format_status_change_email_html($reservation, $new_status, $status_label, $reason);
		self::send_mail($reservation['email'], $subject, $body);
	}

	/**
	 * HTML email body for status change notification (invoice style).
	 *
	 * @param array  $r            Reservation.
	 * @param string $status_key   complete|cancel|pending.
	 * @param string $status_label Translated status label.
	 * @param string $reason       Optional reason.
	 * @return string HTML.
	 */
	protected static function format_status_change_email_html(array $r, $status_key, $status_label, $reason = '') {
		$site_name = get_bloginfo('name');
		$status_bg = $status_key === 'complete' ? '#d1fae5' : ($status_key === 'cancel' ? '#fee2e2' : '#fef3c7');
		$status_color = $status_key === 'complete' ? '#065f46' : ($status_key === 'cancel' ? '#991b1b' : '#92400e');
		$rows = [
			__('Hello', 'unlock-addons-for-elementor')       => esc_html($r['name']),
			__('New status', 'unlock-addons-for-elementor')   => '<span style="background:' . $status_bg . ';color:' . $status_color . ';padding:6px 12px;border-radius:6px;font-weight:600;">' . esc_html($status_label) . '</span>',
			__('Date', 'unlock-addons-for-elementor')        => esc_html(isset($r['date']) ? $r['date'] : '—'),
			__('Time', 'unlock-addons-for-elementor')        => esc_html(isset($r['time']) ? $r['time'] : '—'),
			__('Party size', 'unlock-addons-for-elementor')  => esc_html((string) (isset($r['party']) ? $r['party'] : '—')),
		];
		if ($reason !== '') {
			$rows[__('Reason', 'unlock-addons-for-elementor')] = esc_html($reason);
		}
		$trs = '';
		$i = 0;
		foreach ($rows as $label => $value) {
			$bg = $i % 2 === 0 ? '#ffffff' : '#f9fafb';
			$trs .= '<tr><td style="padding:12px 16px;border-bottom:1px solid #e5e7eb;color:#6b7280;font-size:14px;width:40%;background:' . $bg . ';">' . $label . '</td><td style="padding:12px 16px;border-bottom:1px solid #e5e7eb;color:#111827;font-size:14px;font-weight:500;background:' . $bg . ';">' . $value . '</td></tr>';
			$i++;
		}
		$intro = sprintf(__('Your table reservation at %s has been updated.', 'unlock-addons-for-elementor'), $site_name);
		$html = '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"></head><body style="margin:0;padding:0;background:#f3f4f6;font-family:\'Segoe UI\',Tahoma,Geneva,Verdana,sans-serif;-webkit-font-smoothing:antialiased;">';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="background:#f3f4f6;padding:24px 16px;">';
		$html .= '<tr><td align="center">';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="max-width:560px;background:#ffffff;border-radius:12px;overflow:hidden;box-shadow:0 4px 6px -1px rgba(0,0,0,0.1),0 2px 4px -2px rgba(0,0,0,0.1);">';
		$html .= '<tr><td style="background:linear-gradient(135deg,#1f2937 0%,#374151 100%);padding:28px 24px;text-align:center;">';
		$html .= '<h1 style="margin:0;color:#ffffff;font-size:22px;font-weight:700;">' . esc_html($site_name) . '</h1>';
		$html .= '<p style="margin:8px 0 0;color:#d1d5db;font-size:14px;">' . esc_html__('Reservation status update', 'unlock-addons-for-elementor') . '</p>';
		$html .= '</td></tr>';
		$html .= '<tr><td style="padding:24px;"><p style="margin:0 0 16px;color:#4b5563;font-size:15px;line-height:1.5;">' . esc_html($intro) . '</p>';
		$html .= '<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="border:1px solid #e5e7eb;border-radius:8px;overflow:hidden;">';
		$html .= '<thead><tr><th colspan="2" style="padding:12px 16px;text-align:left;background:#f9fafb;color:#6b7280;font-size:12px;text-transform:uppercase;letter-spacing:0.05em;border-bottom:1px solid #e5e7eb;">' . __('Details', 'unlock-addons-for-elementor') . '</th></tr></thead><tbody>' . $trs . '</tbody></table>';
		$html .= '</td></tr>';
		$html .= '<tr><td style="padding:16px 24px;background:#f9fafb;border-top:1px solid #e5e7eb;text-align:center;"><p style="margin:0;color:#6b7280;font-size:13px;">' . esc_html__('Thank you.', 'unlock-addons-for-elementor') . '</p><p style="margin:6px 0 0;color:#9ca3af;font-size:12px;">' . esc_html($site_name) . '</p></td></tr></table></td></tr></table></body></html>';
		return $html;
	}
}
