<?php

/**
 * Base email template for SiteFort notifications.
 * Matches the Console's emerald brand style.
 *
 * Variables available:
 * - $content (string) - inner HTML content from event-specific template
 * - $data['site_name'] (string) - WordPress site name
 * - $data['site_url'] (string) - WordPress site URL
 * - $data['label'] (string) - Human-readable event label
 */

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

$sitefort_year = gmdate( 'Y' );
$sitefort_site_host = wp_parse_url( $data['site_url'] ?? home_url(), PHP_URL_HOST ) ?: 'your site';
$sitefort_brand_name = isset( $data['brand_name'] ) && is_scalar( $data['brand_name'] ) ? sanitize_text_field( (string) $data['brand_name'] ) : 'SiteFort';
$sitefort_company_name = isset( $data['company_name'] ) && is_scalar( $data['company_name'] ) ? sanitize_text_field( (string) $data['company_name'] ) : $sitefort_brand_name;
$sitefort_hide_attribution = ! empty( $data['hide_attribution'] );
$sitefort_has_custom_brand = strcasecmp( $sitefort_brand_name, 'SiteFort' ) !== 0;
$sitefort_brand_logo_url = ! empty( $data['logo_url'] ) ? esc_url( $data['logo_url'], [ 'https' ] ) : '';
if ( '' === $sitefort_brand_logo_url && ! $sitefort_hide_attribution && ! $sitefort_has_custom_brand && defined( 'SITEFORT_PLUGIN_URL' ) ) {
    $sitefort_brand_logo_url = SITEFORT_PLUGIN_URL . 'assets/images/icon.svg';
}
$sitefort_brand_initial = strtoupper( substr( $sitefort_brand_name, 0, 1 ) );
$sitefort_support_url = ! empty( $data['support_url'] ) ? esc_url( $data['support_url'], [ 'https' ] ) : '';
$sitefort_default_companies = [ 'sitefort', 'sitefort security' ];
$sitefort_show_company = $sitefort_company_name !== '' && ( ! $sitefort_hide_attribution || ! in_array( strtolower( $sitefort_company_name ), $sitefort_default_companies, true ) );
?>
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php echo esc_html( $data['label'] ?? $sitefort_brand_name . ' Notification' ); ?></title>
    <style>
        body {
            margin: 0; padding: 0; min-width: 100%;
            font-family: 'Segoe UI', 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
            font-size: 16px; line-height: 1.6;
            background-color: #f0f2f5; color: #333333;
            -webkit-font-smoothing: antialiased;
        }
        a { color: #059669; text-decoration: none; font-weight: 500; }
        a:hover { text-decoration: underline; }
        .wrapper { width: 100%; table-layout: fixed; background-color: #f0f2f5; padding: 40px 0; }
        .card {
            background-color: #ffffff; margin: 0 auto; max-width: 580px;
            border-radius: 12px; box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            overflow: hidden; border: 1px solid #e5e7eb;
        }
        .header { background-color: #ffffff; padding: 32px 40px 24px; text-align: center; border-bottom: 1px solid #f1f5f9; }
        .header-logo {
            display: block; width: 58px; height: 58px; margin: 0 auto;
        }
        .header-mark {
            width: 58px; height: 58px; margin: 0 auto;
            border-radius: 16px; background: linear-gradient(135deg, #071c2c 0%, #0b2a43 100%);
            color: #ffffff; font-size: 24px; font-weight: 700; line-height: 58px; text-align: center;
            font-family: 'Segoe UI', 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;
        }
        .header-brand { margin: 12px 0 0; font-size: 13px; letter-spacing: 0.02em; color: #0f172a; font-weight: 700; }
        .header-caption { margin: 4px 0 0; font-size: 12px; color: #64748b; letter-spacing: 0.01em; }
        .inner { padding: 40px; }
        h1 {
            margin-top: 0; font-size: 22px; font-weight: 600; color: #111827;
            margin-bottom: 20px; text-align: center; letter-spacing: -0.3px;
        }
        p { margin-bottom: 20px; color: #4b5563; font-size: 15px; }
        .btn {
            display: inline-block; background-color: #059669; color: #ffffff !important;
            padding: 14px 32px; border-radius: 8px; font-weight: 600;
            text-align: center; text-decoration: none; font-size: 15px;
            box-shadow: 0 4px 6px -1px rgba(5,150,105,0.2);
        }
        .btn:hover { background-color: #047857; text-decoration: none; }
        .btn-container { text-align: center; margin: 30px 0; }
        .alert {
            background-color: #fef2f2; border-left: 4px solid #ef4444;
            padding: 14px 16px; border-radius: 4px; font-size: 14px;
            color: #991b1b; margin-bottom: 20px;
        }
        .alert-warn {
            background-color: #fffbeb; border-left: 4px solid #f59e0b;
            padding: 14px 16px; border-radius: 4px; font-size: 14px;
            color: #92400e; margin-bottom: 20px;
        }
        .alert-info {
            background-color: #f0fdf4; border-left: 4px solid #10b981;
            padding: 14px 16px; border-radius: 4px; font-size: 14px;
            color: #065f46; margin-bottom: 20px;
        }
        .detail-box {
            background: #f9fafb; padding: 16px; border-radius: 8px;
            margin-bottom: 20px; font-size: 14px; color: #374151;
        }
        .detail-box strong { color: #111827; }
        .issue-row { padding: 10px 16px; border-radius: 4px; font-size: 14px; margin-bottom: 6px; }
        .issue-row:last-child { margin-bottom: 0; }
        .issue-malware { background: #fef2f2; border-left: 3px solid #ef4444; color: #991b1b; }
        .issue-vuln { background: #fffbeb; border-left: 3px solid #f59e0b; color: #92400e; }
        .issue-other { background: #f9fafb; border-left: 3px solid #9ca3af; color: #374151; }
        .meta-table { width: 100%; background: #f9fafb; border-radius: 8px; margin-bottom: 24px; border-collapse: collapse; }
        .meta-table td { padding: 12px 16px; font-size: 14px; border-bottom: 1px solid #f3f4f6; }
        .meta-table tr:last-child td { border-bottom: none; }
        .meta-label { color: #6b7280; }
        .meta-value { color: #111827; font-weight: 500; text-align: right; }
        .mono-pill { font-family: ui-monospace, SFMono-Regular, monospace; font-size: 12px; background: #e5e7eb; padding: 2px 8px; border-radius: 4px; }
        .footer {
            padding: 24px; text-align: center; font-size: 13px;
            color: #9ca3af; background-color: #f9fafb; border-top: 1px solid #e5e7eb;
        }
        .footer p { margin-bottom: 4px; font-size: 13px; color: #9ca3af; }
        @media only screen and (max-width: 600px) {
            .wrapper { padding: 0; }
            .card { border-radius: 0; width: 100% !important; border: none; }
            .inner { padding: 28px 20px; }
            .header { padding: 20px; }
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="card">
            <div class="header">
                <?php if ( $sitefort_brand_logo_url !== '' ) : ?>
                    <img class="header-logo" src="<?php echo esc_url( $sitefort_brand_logo_url, [ 'https' ] ); ?>" alt="<?php echo esc_attr( $sitefort_brand_name ); ?>" />
                <?php else : ?>
                    <div class="header-mark" aria-hidden="true"><?php echo esc_html( $sitefort_brand_initial ); ?></div>
                <?php endif; ?>
                <p class="header-brand"><?php echo esc_html( $sitefort_brand_name ); ?></p>
                <p class="header-caption"><?php echo esc_html( $data['label'] ?? 'Security Notification' ); ?></p>
            </div>
            <div class="inner">
                <?php echo wp_kses_post( $content ); ?>
            </div>
            <div class="footer">
                <?php if ( $sitefort_show_company ) : ?>
                    <p>&copy; <?php echo esc_html( $sitefort_year ); ?> <?php echo esc_html( $sitefort_company_name ); ?>. All rights reserved.</p>
                <?php endif; ?>
                <?php if ( ! $sitefort_hide_attribution ) : ?>
                    <p>Sent from <?php echo esc_html( $sitefort_brand_name ); ?> on <?php echo esc_html( $sitefort_site_host ); ?></p>
                <?php endif; ?>
                <?php if ( '' !== $sitefort_support_url ) : ?>
                    <p><a href="<?php echo esc_url( $sitefort_support_url, [ 'https' ] ); ?>">Support</a></p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</body>
</html>
