<?php

/**
 * Scan completed email template.
 *
 * Variables via $data array:
 * - $data['site_name'], $data['site_url'], $data['label']
 * - $data['issues_count'] (int)
 * - $data['scan_id'] (string, optional)
 * - $data['duration'] (int seconds, optional)
 * - $data['severity'] (string - max severity: critical, high, medium, low, none)
 * - $data['by_type'] (array - issue counts keyed by type, e.g. malware => 3)
 */

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

$sitefort_issues   = (int) ( $data['issues_count'] ?? 0 );
$sitefort_is_clean = $sitefort_issues === 0;
$sitefort_duration = isset( $data['duration'] ) ? (int) $data['duration'] : null;
$sitefort_severity = $data['severity'] ?? 'none';
$sitefort_by_type  = (array) ( $data['by_type'] ?? [] );

$sitefort_malware_count = (int) ( $sitefort_by_type['malware'] ?? 0 );
$sitefort_vuln_count    = (int) ( $sitefort_by_type['vulnerability'] ?? 0 );
$sitefort_other_count   = $sitefort_issues - $sitefort_malware_count - $sitefort_vuln_count;
?>

<?php
$sitefort_severity_alert_class = 'alert';
if ( in_array( $sitefort_severity, [ 'low', 'medium' ], true ) ) {
    $sitefort_severity_alert_class = 'alert-warn';
}

$sitefort_duration_label = '';
if ( $sitefort_duration !== null ) {
    $sitefort_mins = (int) floor( $sitefort_duration / 60 );
    $sitefort_secs = $sitefort_duration % 60;
    if ( $sitefort_mins > 0 ) {
        $sitefort_duration_label = $sitefort_mins . ' min ' . $sitefort_secs . 's';
    } else {
        $sitefort_duration_label = $sitefort_secs . ' seconds';
    }
}
?>

<?php if ( $sitefort_is_clean ) : ?>
<h1>All Clear</h1>
<div class="alert-info">
    No issues found on <strong><?php echo esc_html( $data['site_name'] ?? '' ); ?></strong>. Your site passed the security scan.
</div>
<?php else : ?>
<h1>Security Scan Completed</h1>
<div class="<?php echo esc_attr( $sitefort_severity_alert_class ); ?>">
    <strong><?php echo esc_html( (string) $sitefort_issues ); ?> issue<?php echo $sitefort_issues !== 1 ? 's' : ''; ?> found</strong>
    on <?php echo esc_html( $data['site_name'] ?? '' ); ?>
    (highest severity: <?php echo esc_html( ucfirst( $sitefort_severity ) ); ?>).
</div>

<?php if ( $sitefort_malware_count > 0 || $sitefort_vuln_count > 0 || $sitefort_other_count > 0 ) : ?>
<div style="margin-bottom: 20px;">
    <?php if ( $sitefort_malware_count > 0 ) : ?>
    <div class="issue-row issue-malware">
        <strong><?php echo esc_html( (string) $sitefort_malware_count ); ?></strong> malware detection<?php echo $sitefort_malware_count !== 1 ? 's' : ''; ?>
    </div>
    <?php endif; ?>
    <?php if ( $sitefort_vuln_count > 0 ) : ?>
    <div class="issue-row issue-vuln">
        <strong><?php echo esc_html( (string) $sitefort_vuln_count ); ?></strong> known vulnerabilit<?php echo $sitefort_vuln_count !== 1 ? 'ies' : 'y'; ?>
    </div>
    <?php endif; ?>
    <?php if ( $sitefort_other_count > 0 ) : ?>
    <div class="issue-row issue-other">
        <strong><?php echo esc_html( (string) $sitefort_other_count ); ?></strong> other finding<?php echo $sitefort_other_count !== 1 ? 's' : ''; ?>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<?php endif; ?>

<table class="meta-table">
    <tr>
        <td class="meta-label">Site</td>
        <td class="meta-value">
            <a href="<?php echo esc_url( $data['site_url'] ?? '' ); ?>"><?php echo esc_html( $data['site_url'] ?? '' ); ?></a>
        </td>
    </tr>
    <?php if ( $sitefort_duration_label ) : ?>
    <tr>
        <td class="meta-label">Duration</td>
        <td class="meta-value"><?php echo esc_html( $sitefort_duration_label ); ?></td>
    </tr>
    <?php endif; ?>
    <?php if ( ! empty( $data['scan_id'] ) ) : ?>
    <tr>
        <td class="meta-label">Scan ID</td>
        <td class="meta-value"><span class="mono-pill"><?php echo esc_html( $data['scan_id'] ); ?></span></td>
    </tr>
    <?php endif; ?>
</table>

<?php if ( ! $sitefort_is_clean ) : ?>
<div class="btn-container">
    <a href="<?php echo esc_url( admin_url( 'admin.php?page=sitefort' ) ); ?>" class="btn">Review Findings</a>
</div>
<?php else : ?>
<p style="text-align: center; color: #6b7280; font-size: 14px;">No action needed. Your next scheduled scan will run automatically.</p>
<?php endif; ?>
