<?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;
}

$securwp_issues   = (int) ( $data['issues_count'] ?? 0 );
$securwp_is_clean = $securwp_issues === 0;
$securwp_duration = isset( $data['duration'] ) ? (int) $data['duration'] : null;
$securwp_severity = $data['severity'] ?? 'none';
$securwp_by_type  = (array) ( $data['by_type'] ?? [] );

$securwp_malware_count = (int) ( $securwp_by_type['malware'] ?? 0 );
$securwp_vuln_count    = (int) ( $securwp_by_type['vulnerability'] ?? 0 );
$securwp_other_count   = $securwp_issues - $securwp_malware_count - $securwp_vuln_count;
?>

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

$securwp_duration_label = '';
if ( $securwp_duration !== null ) {
    $securwp_mins = (int) floor( $securwp_duration / 60 );
    $securwp_secs = $securwp_duration % 60;
    if ( $securwp_mins > 0 ) {
        $securwp_duration_label = $securwp_mins . ' min ' . $securwp_secs . 's';
    } else {
        $securwp_duration_label = $securwp_secs . ' seconds';
    }
}
?>

<?php if ( $securwp_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( $securwp_severity_alert_class ); ?>">
    <strong><?php echo esc_html( (string) $securwp_issues ); ?> issue<?php echo $securwp_issues !== 1 ? 's' : ''; ?> found</strong>
    on <?php echo esc_html( $data['site_name'] ?? '' ); ?>
    (highest severity: <?php echo esc_html( ucfirst( $securwp_severity ) ); ?>).
</div>

<?php if ( $securwp_malware_count > 0 || $securwp_vuln_count > 0 || $securwp_other_count > 0 ) : ?>
<div style="margin-bottom: 20px;">
    <?php if ( $securwp_malware_count > 0 ) : ?>
    <div class="issue-row issue-malware">
        <strong><?php echo esc_html( (string) $securwp_malware_count ); ?></strong> malware detection<?php echo $securwp_malware_count !== 1 ? 's' : ''; ?>
    </div>
    <?php endif; ?>
    <?php if ( $securwp_vuln_count > 0 ) : ?>
    <div class="issue-row issue-vuln">
        <strong><?php echo esc_html( (string) $securwp_vuln_count ); ?></strong> known vulnerabilit<?php echo $securwp_vuln_count !== 1 ? 'ies' : 'y'; ?>
    </div>
    <?php endif; ?>
    <?php if ( $securwp_other_count > 0 ) : ?>
    <div class="issue-row issue-other">
        <strong><?php echo esc_html( (string) $securwp_other_count ); ?></strong> other finding<?php echo $securwp_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 ( $securwp_duration_label ) : ?>
    <tr>
        <td class="meta-label">Duration</td>
        <td class="meta-value"><?php echo esc_html( $securwp_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 ( ! $securwp_is_clean ) : ?>
<div class="btn-container">
    <a href="<?php echo esc_url( admin_url( 'admin.php?page=securwp' ) ); ?>" 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; ?>
