<?php
/**
 * Email template: Donor thank-you receipt.
 *
 * Variables available:
 *   @var \Donatico\Models\Donation        $donation
 *   @var \Donatico\Emails\TemplateRenderer $renderer
 *
 * Body and footer copy come from Settings → Notifications (with merge tags).
 * Heading, subject etc. are stored as options and replaced via TemplateRenderer.
 *
 * @package Donatico
 */

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

// Variables are set by DonorThankYou::send() + TemplateRenderer::render() before include.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$site_name   = get_bloginfo( 'name' );
$site_url    = home_url( '/' );
$heading     = get_option( 'donatico_donor_email_heading', __( 'Thank you!', 'donatico' ) );
$body_text   = get_option( 'donatico_donor_email_body', '' );
$footer_text = get_option( 'donatico_donor_email_footer', $site_name . ' — ' . $site_url );
$currency    = $donation->getCurrency() ?: get_option( 'donatico_currency', 'USD' );
$symbol      = \Donatico\Helpers\Currency::getSymbol( $currency );
$amount      = $symbol . $donation->getFormattedAmount() . ' ' . $currency;
$date_label  = wp_date( get_option( 'date_format', 'F j, Y' ), strtotime( $donation->getCreatedAt() ) );

// Apply merge tags to settings-based copy (already loaded by TemplateRenderer).
if ( $renderer instanceof \Donatico\Emails\TemplateRenderer ) {
	$heading     = $renderer->replaceTags( $heading );
	$body_text   = $renderer->replaceTags( $body_text );
	$footer_text = $renderer->replaceTags( $footer_text );
}

// Default body when the setting is empty.
if ( trim( $body_text ) === '' ) {
	$body_text = sprintf(
		/* translators: 1: donor name, 2: amount */
		__( "Dear {donor_name},\n\nThank you so much for your generous donation of {amount}.\n\nYour support means the world to us and helps us continue our mission.\n\nWith gratitude,\n%s", 'donatico' ),
		$site_name
	);
	$body_text = $renderer instanceof \Donatico\Emails\TemplateRenderer
		? $renderer->replaceTags( $body_text )
		: $body_text;
}

// Convert plain text body to simple HTML paragraphs.
$body_html = nl2br( esc_html( $body_text ) );
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?php echo esc_html( $heading ); ?></title>
</head>
<body style="margin:0;padding:0;background-color:#f3f4f6;font-family:Arial,Helvetica,sans-serif;-webkit-font-smoothing:antialiased;">

<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f3f4f6;">
<tr><td align="center" style="padding:40px 16px;">

  <!-- Container -->
  <table width="600" cellpadding="0" cellspacing="0" border="0" style="max-width:600px;width:100%;">

    <!-- Header -->
    <tr>
      <td style="background:linear-gradient(135deg,#10b981,#059669);border-radius:8px 8px 0 0;padding:36px 32px;text-align:center;">
        <p style="margin:0 0 8px;font-size:36px;line-height:1;">💚</p>
        <h1 style="margin:0;color:#ffffff;font-size:26px;font-weight:700;letter-spacing:-.4px;">
          <?php echo esc_html( $heading ); ?>
        </h1>
        <p style="margin:8px 0 0;color:#d1fae5;font-size:14px;">
          <?php echo esc_html( $site_name ); ?>
        </p>
      </td>
    </tr>

    <!-- Body -->
    <tr>
      <td style="background:#ffffff;padding:32px;">

        <!-- Personalised copy -->
        <div style="font-size:15px;color:#374151;line-height:1.7;margin-bottom:28px;">
          <?php
          // $body_html = nl2br( esc_html( $body_text ) ) — only <br> tags present.
          echo wp_kses( $body_html, array( 'br' => array() ) );
          ?>
        </div>

        <!-- Receipt table -->
        <p style="margin:0 0 10px;font-size:13px;font-weight:700;color:#6b7280;text-transform:uppercase;letter-spacing:.06em;">
          <?php esc_html_e( 'Donation Receipt', 'donatico' ); ?>
        </p>
        <table width="100%" cellpadding="0" cellspacing="0" border="0"
               style="border:1px solid #e5e7eb;border-radius:6px;overflow:hidden;margin-bottom:28px;font-size:14px;">
          <tr style="background:#f9fafb;">
            <td style="padding:11px 16px;color:#6b7280;font-weight:600;width:40%;border-bottom:1px solid #e5e7eb;">
              <?php esc_html_e( 'Amount', 'donatico' ); ?>
            </td>
            <td style="padding:11px 16px;color:#059669;font-weight:700;border-bottom:1px solid #e5e7eb;">
              <?php echo esc_html( $amount ); ?>
            </td>
          </tr>
          <tr style="background:#ffffff;">
            <td style="padding:11px 16px;color:#6b7280;font-weight:600;border-bottom:1px solid #e5e7eb;">
              <?php esc_html_e( 'Date', 'donatico' ); ?>
            </td>
            <td style="padding:11px 16px;color:#1f2937;border-bottom:1px solid #e5e7eb;">
              <?php echo esc_html( (string) $date_label ); ?>
            </td>
          </tr>
          <?php if ( $donation->getStripeSessionId() ) : ?>
          <tr style="background:#f9fafb;">
            <td style="padding:11px 16px;color:#6b7280;font-weight:600;">
              <?php esc_html_e( 'Reference', 'donatico' ); ?>
            </td>
            <td style="padding:11px 16px;color:#9ca3af;font-size:12px;font-family:monospace;">
              <?php echo esc_html( $donation->getStripeSessionId() ); ?>
            </td>
          </tr>
          <?php endif; ?>
        </table>

        <?php if ( $donation->getMessage() ) : ?>
        <!-- Donor's own message, quoted back -->
        <table width="100%" cellpadding="0" cellspacing="0" border="0"
               style="border-left:3px solid #10b981;background:#f0fdf4;border-radius:0 4px 4px 0;margin-bottom:24px;">
          <tr>
            <td style="padding:14px 16px;font-size:14px;color:#374151;font-style:italic;">
              <?php echo esc_html( $donation->getMessage() ); ?>
            </td>
          </tr>
        </table>
        <?php endif; ?>

        <p style="margin:0;font-size:13px;color:#9ca3af;border-top:1px solid #f3f4f6;padding-top:20px;">
          <?php esc_html_e( 'If you have any questions about your donation, please reply to this email.', 'donatico' ); ?>
        </p>

      </td>
    </tr>

    <!-- Footer -->
    <tr>
      <td style="background:#f9fafb;border:1px solid #e5e7eb;border-radius:0 0 8px 8px;padding:20px 32px;text-align:center;">
        <p style="margin:0;font-size:12px;color:#9ca3af;">
          <?php echo wp_kses( $footer_text, array( 'a' => array( 'href' => array(), 'style' => array() ) ) ); ?>
        </p>
      </td>
    </tr>

  </table>
</td></tr>
</table>

</body>
</html>
