<?php

declare(strict_types=1);

/**
 * Render the common branded email layout.
 * The body HTML must be produced by a trusted internal template.
 */
function iav_mail_layout(
    string $title,
    string $bodyHtml,
    string $buttonLabel = '',
    string $buttonUrl = ''
): string {
    $safeTitle = htmlspecialchars($title, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    $safeButtonLabel = htmlspecialchars($buttonLabel, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
    $safeButtonUrl = htmlspecialchars($buttonUrl, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');

    $button = '';
    if ($buttonLabel !== '' && $buttonUrl !== '') {
        $button = <<<HTML
          <tr>
            <td style="padding:8px 32px 32px;text-align:center">
              <a href="{$safeButtonUrl}" style="display:inline-block;background:#ed1b2f;color:#ffffff;text-decoration:none;font-weight:800;padding:14px 24px;border-radius:10px">{$safeButtonLabel}</a>
            </td>
          </tr>
        HTML;
    }

    return <<<HTML
    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <meta name="color-scheme" content="light dark">
        <meta name="supported-color-schemes" content="light dark">
        <title>{$safeTitle}</title>
      </head>
      <body style="margin:0;background:#eef2f7;color:#191d27;font-family:Arial,sans-serif">
        <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="background:#eef2f7;padding:28px 12px">
          <tr>
            <td align="center">
              <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="max-width:620px;background:#ffffff;border:1px solid #d9e0e9;border-radius:18px;overflow:hidden">
                <tr>
                  <td style="background:#07090d;padding:22px 32px;text-align:center">
                    <img src="https://instantaivirtuals.com/assets/branding/logo-email-dark.png" width="270" alt="Instant AI Virtuals" style="display:inline-block;max-width:100%;height:auto">
                  </td>
                </tr>
                <tr>
                  <td style="padding:34px 32px 12px">
                    <h1 style="margin:0;color:#191d27;font-size:26px;line-height:1.25">{$safeTitle}</h1>
                  </td>
                </tr>
                <tr>
                  <td style="padding:8px 32px 24px;color:#4b5565;font-size:16px;line-height:1.65">{$bodyHtml}</td>
                </tr>
                {$button}
                <tr>
                  <td style="border-top:1px solid #e7edf4;padding:20px 32px;color:#7b8494;font-size:12px;line-height:1.55;text-align:center">
                    Instant AI Virtuals · instantaivirtuals.com<br>
                    Analysis provides informational estimates and never guarantees an outcome.
                  </td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </body>
    </html>
    HTML;
}
