<?php
/**
 * Email template: On-demand AI traffic analysis completion.
 * Sent when a manually requested traffic analysis finishes processing.
 *
 * @param array $data Associative array with keys:
 *   - site_name (string)
 *   - analysis_text (string) AI's free-text traffic analysis
 *   - dashboard_url (string)
 * @return string Rendered HTML email
 */
function render_traffic_analysis_complete(array $data): string {
    $site_name     = htmlspecialchars($data['site_name'] ?? '', ENT_QUOTES, 'UTF-8');
    $analysis_raw  = (string)($data['analysis_text'] ?? '');
    $dashboard_url = htmlspecialchars($data['dashboard_url'] ?? '#', ENT_QUOTES, 'UTF-8');

    // Render the AI's free-text analysis as paragraphs. The AI returns plain
    // text (no markdown contract for traffic flow), so split on blank lines
    // and escape each block.
    $blocks = preg_split('/\n{2,}/', trim($analysis_raw)) ?: [];
    $analysis_html = '';
    foreach ($blocks as $block) {
        $block = trim($block);
        if ($block === '') continue;
        $escaped = nl2br(htmlspecialchars($block, ENT_QUOTES, 'UTF-8'));
        $analysis_html .= "<p style=\"margin:0 0 14px 0; font-size:14px; color:#495057; line-height:1.6;\">{$escaped}</p>\n";
    }
    if ($analysis_html === '') {
        $analysis_html = '<p style="margin:0; font-size:14px; color:#868e96;">No analysis text was returned.</p>';
    }

    $html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Traffic Analysis Ready - {$site_name}</title>
</head>
<body style="margin:0; padding:0; background-color:#f4f5f7; font-family:Arial, Helvetica, sans-serif; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;">

<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background-color:#f4f5f7;">
<tr><td align="center" style="padding:24px 16px;">

<!-- Main container -->
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="max-width:600px; width:100%; background-color:#ffffff; border-radius:8px; overflow:hidden; box-shadow:0 1px 3px rgba(0,0,0,0.08);">

<!-- Header -->
<tr>
<td style="background-color:#1a1a2e; padding:24px 32px;">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td style="color:#ffffff; font-size:20px; font-weight:bold; letter-spacing:0.5px;">WHP AI Monitor</td>
        <td align="right" style="color:#a0a0b8; font-size:13px;">Traffic Analysis</td>
    </tr>
    </table>
</td>
</tr>

<!-- Site name bar -->
<tr>
<td style="background-color:#f0f1f3; padding:12px 32px; border-bottom:1px solid #e2e3e5;">
    <span style="font-size:13px; color:#6c757d;">Site:</span>
    <span style="font-size:13px; color:#212529; font-weight:600;">{$site_name}</span>
</td>
</tr>

<!-- Status message -->
<tr>
<td style="padding:28px 32px 8px 32px;">
    <h1 style="margin:0; font-size:20px; font-weight:700; color:#212529; line-height:1.4;">Traffic Analysis Ready</h1>
</td>
</tr>

<!-- Analysis body -->
<tr>
<td style="padding:8px 32px 24px 32px;">
    {$analysis_html}
</td>
</tr>

<!-- Dashboard button -->
<tr>
<td align="center" style="padding:0 32px 32px 32px;">
    <a href="{$dashboard_url}" style="display:inline-block; padding:12px 28px; background-color:#1a1a2e; color:#ffffff; font-size:14px; font-weight:600; text-decoration:none; border-radius:6px;">View Full Analysis</a>
</td>
</tr>

<!-- Footer -->
<tr>
<td style="background-color:#f8f9fa; padding:20px 32px; border-top:1px solid #e9ecef;">
    <p style="margin:0; font-size:12px; color:#868e96; line-height:1.5; text-align:center;">You're receiving this because you requested a traffic analysis for {$site_name}.</p>
</td>
</tr>

</table>
<!-- /Main container -->

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

</body>
</html>
HTML;

    return $html;
}
