<?php
/**
 * Rate Limit 429 Block Page
 *
 * Displays when a request is blocked due to rate limiting.
 *
 * @package VMPFence
 * @since 2.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Ensure required variables are available
if (!isset($violation) || !is_array($violation)) {
    $violation = array(
        'type' => 'rate_limit',
        'action' => 'block',
        'limit' => 'N/A',
        'count' => 'N/A'
    );
}

$retry_after = VMPFence_Config::get('rate_block_duration') * 60; // Convert minutes to seconds
$custom_text = VMPFence_Config::get('custom_block_text');
$block_message = !empty($custom_text) ? $custom_text : 'Too many requests. Please try again later.';
$retry_minutes = VMPFence_Config::get('rate_block_duration');

// Send 403 Forbidden header
if (!headers_sent()) {
    http_response_code(403);
    header('Retry-After: ' . $retry_after);
    header('Content-Type: text/html; charset=UTF-8');
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Rate Limited - Access Denied</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body { 
            font-family: Arial, sans-serif; 
            margin: 40px; 
            background-color: #f1f1f1;
        }
        .container { 
            max-width: 600px; 
            margin: 0 auto; 
            background: white; 
            padding: 40px; 
            border-radius: 5px; 
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 { 
            color: #dc3545; 
            border-bottom: 2px solid #dc3545; 
            padding-bottom: 10px;
        }
        .details {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
        }
        .error-code {
            font-size: 48px;
            font-weight: bold;
            color: #dc3545;
            text-align: center;
            margin: 20px 0;
        }
        .footer {
            font-size: 12px;
            color: #6c757d;
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #e9ecef;
        }
        ul li {
            margin-bottom: 8px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="error-code">403</div>
        <h1>Rate Limit Exceeded</h1>
        <p><?php echo esc_html($block_message); ?></p>
        
        <div class="details">
            <h3>Rate Limit Details:</h3>
            <ul>
                <li><strong>Limit Type:</strong> <?php echo esc_html(ucfirst($violation['type'])); ?> Rate Limit</li>
                <li><strong>Requests Made:</strong> <?php echo esc_html($violation['count']); ?></li>
                <li><strong>Limit Allowed:</strong> <?php echo esc_html($violation['limit']); ?> per minute</li>
                <li><strong>Action:</strong> <?php echo esc_html(ucfirst($violation['action'])); ?></li>
                <li><strong>Block Duration:</strong> <?php echo esc_html($retry_minutes); ?> minutes</li>
            </ul>
        </div>

        <p><strong>What happened?</strong></p>
        <p>Your IP address has exceeded the allowed number of requests per minute. This security measure helps protect our server from excessive traffic.</p>

        <p><strong>What can you do?</strong></p>
        <ul>
            <li>Wait <?php echo esc_html($retry_minutes); ?> minutes before trying again</li>
            <li>Reduce the frequency of your requests</li>
            <li>Contact the website administrator if you believe this is an error</li>
        </ul>

        <div class="footer">
            <strong>Request ID:</strong> <?php echo esc_html(uniqid('rate_', true)); ?><br>
            <strong>Time:</strong> <?php echo esc_html(date('Y-m-d H:i:s T')); ?><br>
            <strong>Your IP:</strong> <?php echo esc_html($_SERVER['REMOTE_ADDR'] ?? 'Unknown'); ?>
        </div>
    </div>
</body>
</html>