<?php
/**
 * Email template: On-demand log review completion
 * Sent when a manually requested log review finishes processing.
 *
 * @param array $data Associative array with keys:
 *   - site_name (string)
 *   - result_summary (string)
 *   - findings (array of arrays, each with: severity, title, recommendation)
 *   - dashboard_url (string)
 * @return string Rendered HTML email
 */
function render_review_complete(array $data): string {
    $site_name      = htmlspecialchars($data['site_name'] ?? '', ENT_QUOTES, 'UTF-8');
    $result_summary = htmlspecialchars($data['result_summary'] ?? '', ENT_QUOTES, 'UTF-8');
    $findings       = $data['findings'] ?? [];
    $dashboard_url  = htmlspecialchars($data['dashboard_url'] ?? '#', ENT_QUOTES, 'UTF-8');

    // Severity badge colors
    $severity_colors = [
        'critical' => ['bg' => '#dc3545', 'text' => '#ffffff'],
        'warning'  => ['bg' => '#fd7e14', 'text' => '#ffffff'],
        'info'     => ['bg' => '#0d6efd', 'text' => '#ffffff'],
    ];

    $findings_count = count($findings);
    $findings_label = $findings_count === 1 ? '1 finding' : $findings_count . ' findings';

    // Build findings rows
    $findings_html = '';
    if ($findings_count === 0) {
        $findings_html = <<<ROW
<tr>
<td style="padding:20px; text-align:center; color:#868e96; font-size:14px;">No issues were found during this review.</td>
</tr>
ROW;
    } else {
        foreach ($findings as $index => $finding) {
            $f_severity      = strtolower(trim($finding['severity'] ?? 'info'));
            $f_title         = htmlspecialchars($finding['title'] ?? '', ENT_QUOTES, 'UTF-8');
            $f_recommendation = htmlspecialchars($finding['recommendation'] ?? '', ENT_QUOTES, 'UTF-8');

            $badge = $severity_colors[$f_severity] ?? $severity_colors['info'];
            $f_severity_label = ucfirst($f_severity);

            $border_top = $index > 0 ? 'border-top:1px solid #e9ecef;' : '';

            $findings_html .= <<<ROW
<tr>
<td style="padding:16px 20px; {$border_top}">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td style="padding-bottom:6px;">
            <span style="display:inline-block; padding:3px 8px; border-radius:3px; font-size:11px; font-weight:700; text-transform:uppercase; letter-spacing:0.4px; color:{$badge['text']}; background-color:{$badge['bg']};">{$f_severity_label}</span>
        </td>
    </tr>
    <tr>
        <td style="padding-bottom:4px;">
            <span style="font-size:15px; font-weight:600; color:#212529;">{$f_title}</span>
        </td>
    </tr>
    <tr>
        <td>
            <span style="font-size:13px; color:#6c757d; line-height:1.5;">{$f_recommendation}</span>
        </td>
    </tr>
    </table>
</td>
</tr>
ROW;
        }
    }

    $html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Review Complete - {$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;">Review Complete</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;">Log Review Complete</h1>
</td>
</tr>

<!-- Summary -->
<tr>
<td style="padding:8px 32px 24px 32px;">
    <p style="margin:0; font-size:15px; color:#495057; line-height:1.6;">{$result_summary}</p>
</td>
</tr>

<!-- Findings header -->
<tr>
<td style="padding:0 32px 12px 32px;">
    <h2 style="margin:0; font-size:14px; font-weight:700; color:#495057; text-transform:uppercase; letter-spacing:0.3px;">Findings ({$findings_label})</h2>
</td>
</tr>

<!-- Findings list -->
<tr>
<td style="padding:0 32px 24px 32px;">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background-color:#f8f9fa; border-radius:6px; border:1px solid #e9ecef;">
    {$findings_html}
    </table>
</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 Report</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 enabled AI monitoring for {$site_name}.</p>
</td>
</tr>

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

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

</body>
</html>
HTML;

    return $html;
}
