<?php

defined( 'ABSPATH' ) || exit;

class ZACLS_Email_Templates {

	public static function get_template( $template_name, $vars = array() ) {
		$defaults = array(
			'site_name'    => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
			'site_url'     => home_url(),
			'logo_url'     => '',
			'current_year' => gmdate( 'Y' ),
			'admin_email'  => get_option( 'admin_email' ),
		);

		$vars = wp_parse_args( $vars, $defaults );

		$header = self::get_header( $vars );
		$footer = self::get_footer( $vars );
		$body   = '';

		switch ( $template_name ) {
			case 'login-notification':
				$body = self::login_notification_body( $vars );
				break;
			case 'ip-blocked':
				$body = self::ip_blocked_body( $vars );
				break;
			case 'security-report':
				$body = self::security_report_body( $vars );
				break;
			case 'verify-device':
				$body = self::verify_device_body( $vars );
				break;
			case 'otp':
				$body = self::otp_body( $vars );
				break;
			default:
				$body = self::default_body( $vars );
		}

		return $header . $body . $footer;
	}

	private static function get_header( $vars ) {
		return '
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
body,table,td,p,a,li,blockquote{-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}
body{margin:0;padding:0;width:100%!important;height:100%!important;background-color:#f4f4f5;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;font-size:15px;line-height:1.6;color:#3c434a}
table{border-collapse:collapse}
td{vertical-align:top}
.email-wrapper{max-width:600px;margin:0 auto;padding:20px 10px}
.email-container{background:#ffffff;border-radius:8px;overflow:hidden;box-shadow:0 1px 4px rgba(0,0,0,0.08)}
.email-header{background:#2271b1;padding:32px 40px;text-align:center}
.email-header h1{color:#ffffff;font-size:22px;margin:0;font-weight:600}
.email-body{padding:32px 40px;color:#3c434a}
.email-body h2{font-size:20px;color:#1d2327;margin:0 0 16px;font-weight:600}
.email-body p{margin:0 0 16px}
.email-body .code-block{background:#f0f0f1;border-radius:4px;padding:16px;text-align:center;font-size:28px;font-weight:700;letter-spacing:4px;color:#2271b1;margin:16px 0}
.email-body .button{display:inline-block;padding:12px 28px;background:#2271b1;color:#ffffff!important;text-decoration:none;border-radius:4px;font-weight:600;font-size:15px}
.email-footer{background:#f6f7f7;padding:24px 40px;text-align:center;font-size:12px;color:#787c82}
.email-footer a{color:#2271b1;text-decoration:none}
.email-stats-table{width:100%;border-collapse:collapse;margin:16px 0}
.email-stats-table td{padding:10px 12px;border:1px solid #e5e7ea;font-size:14px}
.email-stats-table td.label{font-weight:600;background:#f6f7f7;width:50%}
@media screen and (max-width:480px){
.email-header,.email-body,.email-footer{padding-left:20px!important;padding-right:20px!important}
}
</style></head>
<body>
<div class="email-wrapper">
<div class="email-container">
<div class="email-header">
<h1>' . esc_html( $vars['site_name'] ) . '</h1>
</div>
<div class="email-body">';
	}

	private static function get_footer( $vars ) {
		return '</div>
<div class="email-footer">
<p style="margin:0 0 8px"><strong>' . esc_html( $vars['site_name'] ) . '</strong><br>
<a href="' . esc_url( $vars['site_url'] ) . '">' . esc_html( $vars['site_url'] ) . '</a></p>
<p style="margin:0">' . sprintf(
esc_html__( 'This email was sent by the ZA Creative Login Shield plugin. &copy; %s', 'za-creative-login-shield' ),
esc_html( $vars['current_year'] )
) . '</p>
</div>
</div>
</div>
</body>
</html>';
	}

	private static function login_notification_body( $vars ) {
		return '<h2>' . esc_html__( 'New Login Detected', 'za-creative-login-shield' ) . '</h2>
<p>' . sprintf(
esc_html__( 'User %s logged in from a new IP address.', 'za-creative-login-shield' ),
esc_html( $vars['display_name'] ?? '' )
) . '</p>
<table class="email-stats-table">
<tr><td class="label">' . esc_html__( 'User', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['display_name'] ?? '' ) . '</td></tr>
<tr><td class="label">' . esc_html__( 'IP Address', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['ip'] ?? '' ) . '</td></tr>
<tr><td class="label">' . esc_html__( 'Time', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['time'] ?? '' ) . '</td></tr>
<tr><td class="label">' . esc_html__( 'User Agent', 'za-creative-login-shield' ) . '</td><td style="font-size:11px">' . esc_html( $vars['user_agent'] ?? '' ) . '</td></tr>
</table>
<p>' . esc_html__( 'If this was not the user, please investigate immediately.', 'za-creative-login-shield' ) . '</p>
<p><a href="' . esc_url( admin_url( 'admin.php?page=zacls&tab=logs' ) ) . '" class="button">' . esc_html__( 'View Login Logs', 'za-creative-login-shield' ) . '</a></p>';
	}

	private static function ip_blocked_body( $vars ) {
		return '<h2>' . esc_html__( 'IP Address Blocked', 'za-creative-login-shield' ) . '</h2>
<p>' . sprintf(
esc_html__( 'IP address %s has been automatically blocked due to suspicious activity.', 'za-creative-login-shield' ),
esc_html( $vars['ip'] ?? '' )
) . '</p>
<table class="email-stats-table">
<tr><td class="label">' . esc_html__( 'IP Address', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['ip'] ?? '' ) . '</td></tr>
<tr><td class="label">' . esc_html__( 'Time', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['time'] ?? '' ) . '</td></tr>
</table>
<p><a href="' . esc_url( admin_url( 'admin.php?page=zacls&tab=blocked-ips' ) ) . '" class="button">' . esc_html__( 'Manage Blocked IPs', 'za-creative-login-shield' ) . '</a></p>';
	}

	private static function security_report_body( $vars ) {
		$rows = '';
		$stats = $vars['stats'] ?? array();
		$items = array(
			__( 'Total Login Attempts', 'za-creative-login-shield' ) => $stats['total'] ?? 0,
			__( 'Failed Attempts', 'za-creative-login-shield' ) => $stats['failed'] ?? 0,
			__( 'Successful Logins', 'za-creative-login-shield' ) => $stats['success'] ?? 0,
			__( 'Blocked IPs', 'za-creative-login-shield' ) => $stats['blocked'] ?? 0,
			__( 'Unique IPs', 'za-creative-login-shield' ) => $stats['unique_ips'] ?? 0,
			__( 'Attack Rate', 'za-creative-login-shield' ) => ( $stats['attack_rate'] ?? 0 ) . '%',
		);

		foreach ( $items as $label => $value ) {
			$rows .= '<tr><td class="label">' . esc_html( $label ) . '</td><td>' . esc_html( $value ) . '</td></tr>';
		}

		return '<h2>' . sprintf(
			esc_html__( '%s Security Report', 'za-creative-login-shield' ),
			esc_html( $vars['interval_label'] ?? '' )
		) . '</h2>
<p>' . sprintf(
esc_html__( 'Security summary for %s', 'za-creative-login-shield' ),
esc_html( $vars['site_name'] )
) . '</p>
<table class="email-stats-table">' . $rows . '</table>
<p><a href="' . esc_url( admin_url( 'admin.php?page=zacls' ) ) . '" class="button">' . esc_html__( 'View Full Dashboard', 'za-creative-login-shield' ) . '</a></p>';
	}

	private static function verify_device_body( $vars ) {
		return '<h2>' . esc_html__( 'Verify New Device', 'za-creative-login-shield' ) . '</h2>
<p>' . esc_html__( 'A login attempt was made from a new device. Please verify this is you.', 'za-creative-login-shield' ) . '</p>
<table class="email-stats-table">
<tr><td class="label">' . esc_html__( 'IP Address', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['ip'] ?? '' ) . '</td></tr>
<tr><td class="label">' . esc_html__( 'Time', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $vars['time'] ?? '' ) . '</td></tr>
</table>
<p>' . esc_html__( 'Your verification code is:', 'za-creative-login-shield' ) . '</p>
<div class="code-block">' . esc_html( $vars['code'] ?? '' ) . '</div>
<p>' . esc_html__( 'This code expires in 10 minutes.', 'za-creative-login-shield' ) . '</p>
<p>' . esc_html__( "If you didn't attempt to log in, please change your password immediately.", 'za-creative-login-shield' ) . '</p>';
	}

	private static function otp_body( $vars ) {
		return '<h2>' . esc_html__( 'Your Login Verification Code', 'za-creative-login-shield' ) . '</h2>
<p>' . sprintf(
esc_html__( 'Hi %s,', 'za-creative-login-shield' ),
esc_html( $vars['display_name'] ?? '' )
) . '</p>
<p>' . esc_html__( 'Enter the following code to complete your login:', 'za-creative-login-shield' ) . '</p>
<div class="code-block">' . esc_html( $vars['code'] ?? '' ) . '</div>
<p>' . esc_html__( 'This code expires in 10 minutes.', 'za-creative-login-shield' ) . '</p>
<p>' . esc_html__( "If you didn't request this, please ignore this email.", 'za-creative-login-shield' ) . '</p>';
	}

	private static function default_body( $vars ) {
		return '<h2>' . esc_html__( 'Security Notification', 'za-creative-login-shield' ) . '</h2>
<p>' . esc_html( $vars['message'] ?? '' ) . '</p>';
	}
}
