<?php

/**
 * Vulnerability alert email template.
 *
 * $data keys:
 * - site_name, site_url, label
 * - count (int)              Number of new vulnerabilities.
 * - severity (string)        Highest severity across the batch.
 * - vulnerabilities (array)  Items with plugin_name, cve_id, severity.
 */

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

$sitefort_severity = $data['severity'] ?? 'medium';
$sitefort_alert_class = in_array( $sitefort_severity, [ 'high', 'critical' ], true ) ? 'alert' : 'alert-warn';
$sitefort_vulns = isset( $data['vulnerabilities'] ) && is_array( $data['vulnerabilities'] ) ? $data['vulnerabilities'] : [];
$sitefort_count = isset( $data['count'] ) ? (int) $data['count'] : count( $sitefort_vulns );
$sitefort_brand_name = isset( $data['brand_name'] ) && is_scalar( $data['brand_name'] )
    ? sanitize_text_field( (string) $data['brand_name'] )
    : 'SiteFort';
$sitefort_site_name = isset( $data['site_name'] ) && is_scalar( $data['site_name'] )
    ? sanitize_text_field( (string) $data['site_name'] )
    : 'your site';
?>
<h1><?php echo $sitefort_count > 1 ? esc_html( sprintf( '%d Vulnerabilities Found', $sitefort_count ) ) : 'Vulnerability Found'; ?></h1>

<p class="intro-copy">
    <?php echo esc_html( $sitefort_brand_name ); ?> found software with known vulnerabilities on <?php echo esc_html( $sitefort_site_name ); ?>. Updating the affected components usually fixes this.
</p>

<div class="<?php echo esc_attr( $sitefort_alert_class ); ?>">
    <?php if ( $sitefort_count > 1 ) : ?>
        <strong><?php echo esc_html( $sitefort_count ); ?> known vulnerabilities</strong>
        affect components on <?php echo esc_html( $sitefort_site_name ); ?>.
        Highest severity: <?php echo esc_html( ucfirst( $sitefort_severity ) ); ?>.
    <?php else : ?>
        <strong>A known vulnerability</strong> affects a component on <?php echo esc_html( $sitefort_site_name ); ?>.
    <?php endif; ?>
</div>

<div class="detail-box">
    <strong>Site:</strong> <?php echo esc_html( $data['site_url'] ?? '' ); ?>
</div>

<?php if ( ! empty( $sitefort_vulns ) ) : ?>
<table cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%;margin-top:12px;">
    <thead>
        <tr style="background:#f3f4f6;text-align:left;">
            <th>Component</th>
            <th>CVE</th>
            <th>Severity</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ( $sitefort_vulns as $sitefort_vuln ) : ?>
        <tr style="border-top:1px solid #e5e7eb;">
            <td><?php echo esc_html( (string) ( $sitefort_vuln['plugin_name'] ?? 'Unknown' ) ); ?></td>
            <td><?php echo esc_html( (string) ( $sitefort_vuln['cve_id'] ?? '' ) ); ?></td>
            <td><?php echo esc_html( ucfirst( (string) ( $sitefort_vuln['severity'] ?? 'medium' ) ) ); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>

<p>
    Check if updates are available for the affected components. If no patch exists yet,
    consider deactivating them until a fix is released.
</p>
