<?php
/**
 * Email template: Batched issue digest
 * Sent when multiple issues are batched into a single notification.
 *
 * @param array $data Associative array with keys:
 *   - site_name (string)
 *   - issues (array of arrays, each with: severity, title, diagnosis, recommendation)
 *   - issue_count (int)
 *   - dashboard_url (string)
 * @return string Rendered HTML email
 */
function render_issue_digest(array $data): string {
    $site_name     = htmlspecialchars($data['site_name'] ?? '', ENT_QUOTES, 'UTF-8');
    $issues        = $data['issues'] ?? [];
    $issue_count   = (int)($data['issue_count'] ?? count($issues));
    $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'],
    ];

    // Count by severity
    $counts = ['critical' => 0, 'warning' => 0, 'info' => 0];
    foreach ($issues as $issue) {
        $sev = strtolower(trim($issue['severity'] ?? 'info'));
        if (isset($counts[$sev])) {
            $counts[$sev]++;
        } else {
            $counts['info']++;
        }
    }

    // Build severity summary pills
    $summary_pills = '';
    if ($counts['critical'] > 0) {
        $summary_pills .= '<span style="display:inline-block; padding:4px 12px; border-radius:4px; font-size:13px; font-weight:600; color:#ffffff; background-color:#dc3545; margin-right:8px;">' . $counts['critical'] . ' Critical</span>';
    }
    if ($counts['warning'] > 0) {
        $summary_pills .= '<span style="display:inline-block; padding:4px 12px; border-radius:4px; font-size:13px; font-weight:600; color:#ffffff; background-color:#fd7e14; margin-right:8px;">' . $counts['warning'] . ' Warning</span>';
    }
    if ($counts['info'] > 0) {
        $summary_pills .= '<span style="display:inline-block; padding:4px 12px; border-radius:4px; font-size:13px; font-weight:600; color:#ffffff; background-color:#0d6efd; margin-right:8px;">' . $counts['info'] . ' Info</span>';
    }

    $issue_label = $issue_count === 1 ? '1 Issue' : $issue_count . ' Issues';

    // Build issue rows
    $issues_html = '';
    foreach ($issues as $index => $issue) {
        $i_severity       = strtolower(trim($issue['severity'] ?? 'info'));
        $i_title          = htmlspecialchars($issue['title'] ?? '', ENT_QUOTES, 'UTF-8');
        $i_diagnosis      = htmlspecialchars($issue['diagnosis'] ?? '', ENT_QUOTES, 'UTF-8');
        $i_recommendation = htmlspecialchars($issue['recommendation'] ?? '', ENT_QUOTES, 'UTF-8');

        $badge = $severity_colors[$i_severity] ?? $severity_colors['info'];
        $i_severity_label = ucfirst($i_severity);

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

        $issues_html .= <<<ROW
<tr>
<td style="padding:20px; {$border_top}">
    <table role="presentation" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td style="padding-bottom:8px;">
            <span style="font-size:12px; color:#868e96; font-weight:600;">#{$item_number}</span>
            <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']}; margin-left:8px;">{$i_severity_label}</span>
        </td>
    </tr>
    <tr>
        <td style="padding-bottom:6px;">
            <span style="font-size:15px; font-weight:600; color:#212529;">{$i_title}</span>
        </td>
    </tr>
    <tr>
        <td style="padding-bottom:6px;">
            <span style="font-size:13px; color:#495057; line-height:1.5;"><strong style="color:#495057;">Diagnosis:</strong> {$i_diagnosis}</span>
        </td>
    </tr>
    <tr>
        <td>
            <span style="font-size:13px; color:#6c757d; line-height:1.5;"><strong style="color:#495057;">Recommendation:</strong> {$i_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>Issue Digest - {$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;">Issue Digest</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>

<!-- Title and count -->
<tr>
<td style="padding:28px 32px 8px 32px;">
    <h1 style="margin:0; font-size:20px; font-weight:700; color:#212529; line-height:1.4;">{$issue_label} Detected</h1>
</td>
</tr>

<!-- Severity summary -->
<tr>
<td style="padding:12px 32px 24px 32px;">
    {$summary_pills}
</td>
</tr>

<!-- Issues list 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;">All Issues</h2>
</td>
</tr>

<!-- Issues 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;">
    {$issues_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 All in Dashboard</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;
}
