<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Webhook Receiver - Anonre</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500;600&family=Inter:wght@300;400;500;600;700;800&family=Orbitron:wght@400;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-primary: #0A0A0A;
            --bg-secondary: #1A1A1A;
            --text-primary: #E0E0E0;
            --text-secondary: #888888;
            --accent: #00FFCC;
            --accent-gradient: linear-gradient(45deg, #00FFCC, #FF00FF);
            --success: #00A388;
            --error: #FF4444;
            --border: #333333;
            --card-shadow: 0 6px 15px rgba(0, 255, 204, 0.2);
            --hover-shadow: 0 10px 25px rgba(0, 255, 204, 0.3);
            --input-border-focus: #00FFCC;
        }

        [data-theme="dark"] {
            --bg-primary: #0A0A0A;
            --bg-secondary: #1A1A1A;
            --text-primary: #E0E0E0;
            --text-secondary: #888888;
            --accent: #00FFCC;
            --accent-gradient: linear-gradient(45deg, #00FFCC, #FF00FF);
            --success: #00A388;
            --error: #FF4444;
            --border: #333333;
            --card-shadow: 0 6px 15px rgba(0, 255, 204, 0.2);
            --hover-shadow: 0 10px 25px rgba(0, 255, 204, 0.3);
            --input-border-focus: #00FFCC;
        }

        [data-theme="light"] {
            --bg-primary: #F5F5F5;
            --bg-secondary: #FFFFFF;
            --text-primary: #111111;
            --text-secondary: #666666;
            --accent: #000000;
            --accent-gradient: linear-gradient(45deg, #000000, #444444);
            --success: #2d2d2d;
            --error: #ff4a4a;
            --border: #e0e0e0;
            --card-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            --hover-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
            --input-border-focus: #000000;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            transition: all 0.3s ease;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            line-height: 1.6;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow-x: hidden;
        }

        #matrix-bg {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            background: var(--bg-primary);
        }

        .container {
            max-width: 1500px;
            margin: 0 auto;
            padding: 2.5rem;
            flex-grow: 1;
            position: relative;
            z-index: 1;
        }

        header {
            margin-bottom: 3.5rem;
            padding: 2.5rem 0;
            border-bottom: 2px solid transparent;
            background-clip: padding-box;
            animation: glowBorder 2s infinite alternate;
        }

        @keyframes glowBorder {
            0% { border-color: rgba(0, 255, 204, 0.3); }
            100% { border-color: rgba(255, 0, 255, 0.3); }
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        h1 {
            font-family: 'Orbitron', sans-serif;
            font-size: 3rem;
            font-weight: 700;
            letter-spacing: -1px;
            margin-bottom: 0.8rem;
            background: var(--accent-gradient);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            text-shadow: 0 0 8px rgba(0, 255, 204, 0.5);
        }

        .header-subtitle {
            color: var(--text-secondary);
            font-size: 1.2rem;
            max-width: 600px;
            animation: fadeIn 1s ease forwards;
        }

        .header-subtitle a {
            color: var(--accent);
            text-decoration: none;
            font-weight: 600;
        }

        .header-subtitle a:hover {
            text-decoration: underline;
            color: var(--text-primary);
        }

        .header-actions {
            display: flex;
            align-items: center;
            gap: 1.2rem;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 0.6rem;
            padding: 0.6rem 1.2rem;
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            border-radius: 25px;
            font-size: 1rem;
            color: var(--text-secondary);
            box-shadow: var(--card-shadow);
        }

        .user-info i {
            color: var(--accent);
            font-size: 1.2rem;
        }

        .theme-toggle {
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            color: var(--text-primary);
            width: 50px;
            height: 50px;
            border-radius: 50%;
            cursor: pointer;
            font-size: 1.3rem;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: transform 0.4s ease, border-color 0.3s ease;
        }

        .theme-toggle:hover {
            transform: rotate(360deg);
            border-color: var(--accent);
            box-shadow: 0 0 12px rgba(0, 255, 204, 0.6);
        }

        .logout-btn {
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            color: var(--text-primary);
            padding: 0.6rem 1.2rem;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 0.6rem;
            text-decoration: none;
            box-shadow: var(--card-shadow);
        }

        .logout-btn:hover {
            background: var(--accent-gradient);
            color: var(--bg-primary);
            border-color: transparent;
            box-shadow: var(--hover-shadow);
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2.5rem;
            margin-bottom: 3.5rem;
        }

        .stat-card {
            background: var(--bg-secondary);
            padding: 2.5rem;
            border-radius: 16px;
            text-align: center;
            position: relative;
            overflow: hidden;
            border: 2px solid transparent;
            box-shadow: var(--card-shadow);
            animation: fadeIn 0.5s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: var(--hover-shadow);
            border-color: rgba(0, 255, 204, 0.3);
        }

        .stat-value {
            font-family: 'Orbitron', sans-serif;
            font-size: 3rem;
            font-weight: 700;
            margin: 1.2rem 0;
            color: var(--accent);
            text-shadow: 0 0 5px rgba(0, 255, 204, 0.3);
        }

        .stat-label {
            color: var(--text-secondary);
            font-size: 1rem;
            text-transform: uppercase;
            letter-spacing: 2px;
            font-weight: 600;
        }

        .stat-card i {
            color: var(--text-secondary);
            font-size: 2rem;
            margin-top: 1.2rem;
        }

        .controls {
            display: flex;
            gap: 1.2rem;
            margin-bottom: 2.5rem;
            flex-wrap: wrap;
        }

        .webhook-url-container {
            flex: 1;
            position: relative;
            min-width: 320px;
        }

        .webhook-url {
            width: 100%;
            padding: 1.2rem 120px 1.2rem 1.2rem;
            border: 1px solid var(--border);
            border-radius: 10px;
            font-family: 'Fira Code', monospace;
            background: var(--bg-primary);
            color: var(--accent);
            font-size: 1.1rem;
            box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.4);
        }

        .webhook-url:focus {
            outline: none;
            border-color: transparent;
            background: var(--accent-gradient);
            box-shadow: 0 0 10px rgba(0, 255, 204, 0.7);
            color: var(--bg-primary);
        }

        .copy-btn {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background: var(--accent-gradient);
            color: var(--bg-primary);
            border: none;
            padding: 0.7rem 1.4rem;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            box-shadow: 0 4px 10px rgba(0, 255, 204, 0.3);
        }

        .copy-btn:hover {
            background: var(--success);
            transform: translateY(-50%) scale(1.05);
            box-shadow: var(--hover-shadow);
        }

        .btn {
            padding: 1.2rem 2.2rem;
            border: none;
            border-radius: 10px;
            font-family: 'Orbitron', sans-serif;
            font-weight: 600;
            cursor: pointer;
            font-size: 1rem;
            position: relative;
            overflow: hidden;
        }

        .btn-primary {
            background: var(--accent-gradient);
            color: var(--bg-primary);
            box-shadow: 0 4px 15px rgba(0, 255, 204, 0.3);
        }

        .btn-outline {
            background: transparent;
            border: 1px solid var(--border);
            color: var(--text-primary);
        }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
            transition: left 0.5s ease;
        }

        .btn:hover::before {
            left: 100%;
        }

        .btn:hover {
            transform: translateY(-4px) scale(1.02);
            box-shadow: var(--hover-shadow);
        }

        .search-container {
            margin-bottom: 2.5rem;
            position: relative;
        }

        .search-input {
            width: 100%;
            padding: 1.2rem 1.2rem 1.2rem 3.8rem;
            border: 1px solid var(--border);
            border-radius: 10px;
            background: var(--bg-primary);
            color: var(--text-primary);
            font-size: 1.1rem;
            box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.4);
        }

        .search-input:focus {
            outline: none;
            border-color: transparent;
            background: var(--accent-gradient);
            box-shadow: 0 0 10px rgba(0, 255, 204, 0.7);
            color: var(--bg-primary);
        }

        .search-icon {
            position: absolute;
            left: 1.2rem;
            top: 50%;
            transform: translateY(-50%);
            color: var(--text-secondary);
            font-size: 1.3rem;
        }

        .webhooks-container {
            display: grid;
            gap: 2rem;
        }

        .webhook-card {
            background: var(--bg-secondary);
            border-radius: 16px;
            padding: 2rem;
            position: relative;
            overflow: hidden;
            border: 2px solid transparent;
            box-shadow: var(--card-shadow);
            animation: fadeIn 0.5s ease;
        }

        .webhook-card:hover {
            transform: translateY(-5px);
            box-shadow: var(--hover-shadow);
            border-color: rgba(0, 255, 204, 0.3);
        }

        .webhook-header {
            display: flex;
            align-items: center;
            gap: 1.2rem;
            margin-bottom: 1.8rem;
            border-bottom: 1px dashed var(--border);
            padding-bottom: 1.2rem;
        }

        .method-badge {
            padding: 0.5rem 1.2rem;
            border-radius: 8px;
            font-family: 'Orbitron', sans-serif;
            font-weight: 600;
            font-size: 1rem;
            letter-spacing: 1px;
            text-transform: uppercase;
            box-shadow: 0 3px 6px rgba(0,0,0,0.3);
        }

        .method-POST { background: var(--success); color: var(--bg-primary); }
        .method-GET { background: var(--accent); color: var(--bg-primary); }
        .method-PUT { background: #800080; color: var(--bg-primary); }
        .method-DELETE { background: var(--error); color: var(--bg-primary); }
        .method-PATCH { background: #FFA500; color: var(--bg-primary); }
        .method-HEAD { background: #1E90FF; color: var(--bg-primary); }

        .webhook-path {
            font-family: 'Fira Code', monospace;
            color: var(--text-primary);
            font-size: 1.1rem;
            background: rgba(0,0,0,0.3);
            padding: 0.3rem 0.6rem;
            border-radius: 6px;
        }

        .webhook-timestamp {
            margin-left: auto;
            color: var(--text-secondary);
            font-size: 1rem;
            font-family: 'Fira Code', monospace;
        }

        .webhook-timestamp i {
            color: var(--accent);
            margin-right: 0.4rem;
        }

        .webhook-data {
            background: var(--bg-primary);
            padding: 1.8rem;
            border-radius: 10px;
            overflow-x: auto;
            border: 1px solid var(--border);
            box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
        }

        .webhook-data pre {
            margin: 0;
            font-family: 'Fira Code', monospace;
            font-size: 1rem;
            color: var(--text-primary);
            white-space: pre-wrap;
            word-break: break-all;
        }

        .headers-collapse {
            margin-top: 1.2rem;
            background: var(--bg-primary);
            border-radius: 10px;
            padding: 1rem 1.8rem;
            border: 1px solid var(--border);
            box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.4);
        }

        .headers-collapse summary {
            cursor: pointer;
            color: var(--accent);
            font-weight: 600;
            font-size: 1.1rem;
            display: flex;
            align-items: center;
            gap: 0.6rem;
        }

        .headers-collapse summary::-webkit-details-marker {
            display: none;
        }

        .headers-collapse summary i {
            transition: transform 0.3s ease;
        }

        .headers-collapse[open] summary i {
            transform: rotate(90deg);
        }

        .headers-collapse summary:hover {
            color: var(--text-primary);
        }

        .empty-state {
            text-align: center;
            padding: 7rem 2.5rem;
            color: var(--text-secondary);
            border: 1px dashed var(--border);
            border-radius: 16px;
            background: var(--bg-secondary);
            box-shadow: var(--card-shadow);
            animation: fadeIn 0.5s ease;
        }

        .empty-state i {
            font-size: 4.5rem;
            margin-bottom: 1.8rem;
            color: var(--text-secondary);
            text-shadow: 0 0 6px rgba(0,0,0,0.3);
        }

        .empty-state h3 {
            font-family: 'Orbitron', sans-serif;
            font-size: 2rem;
            color: var(--text-primary);
            margin-bottom: 1rem;
        }

        .empty-state p {
            font-size: 1.2rem;
            color: var(--text-secondary);
        }

        .notification {
            position: fixed;
            bottom: 2.5rem;
            right: 2.5rem;
            padding: 1.2rem 2.2rem;
            border-radius: 10px;
            background: var(--accent-gradient);
            color: var(--bg-primary);
            font-weight: 600;
            box-shadow: 0 0 15px rgba(0, 255, 204, 0.7);
            display: flex;
            align-items: center;
            gap: 0.6rem;
            z-index: 1000;
            animation: slideIn 0.4s ease forwards;
        }

        @keyframes slideIn {
            from { transform: translateX(100%); opacity: 0; }
            to { transform: translateX(0); opacity: 1; }
        }

        .loading-indicator {
            display: inline-flex;
            align-items: center;
            gap: 0.6rem;
            padding: 0.6rem 1.2rem;
            background: var(--accent-gradient);
            color: var(--bg-primary);
            border-radius: 25px;
            font-size: 1rem;
            position: fixed;
            bottom: 2.5rem;
            left: 2.5rem;
            box-shadow: 0 0 12px rgba(0, 255, 204, 0.6);
        }

        .loading-indicator::before {
            content: '';
            width: 10px;
            height: 10px;
            background: var(--bg-primary);
            border-radius: 50%;
            animation: pulse 1.2s infinite;
        }

        @keyframes pulse {
            0% { transform: scale(0.7); opacity: 0.4; }
            50% { transform: scale(1.3); opacity: 1; }
            100% { transform: scale(0.7); opacity: 0.4; }
        }

        @keyframes fadeIn {
            to { transform: translateY(0); opacity: 1; }
        }

        @media (max-width: 768px) {
            .container { padding: 1.5rem; }
            .header-content { flex-direction: column; text-align: center; gap: 2rem; }
            .header-actions { justify-content: center; flex-wrap: wrap; }
            .controls { flex-direction: column; }
            .webhook-url-container { min-width: 100%; }
            .stat-card { padding: 2rem; }
            .webhook-header { flex-direction: column; align-items: flex-start; gap: 1rem; }
            .webhook-timestamp { margin-left: 0; margin-top: 0.8rem; }
            h1 { font-size: 2.5rem; }
            .header-subtitle { font-size: 1.1rem; }
        }
    </style>
</head>
<body>
    <canvas id="matrix-bg"></canvas>
    <div class="container">
        <header>
            <div class="header-content">
                <div>
                    <h1>Welcome, {{ session.username }}</h1>
                    <p class="header-subtitle">Real-time webhook monitoring by <a href="https://github.com/joelindra" target="_blank" rel="noopener noreferrer">Joel Indra</a></p>
                </div>
                <div class="header-actions">
                    <div class="user-info">
                        <i class="fas fa-user-secret"></i>
                        <span>{{ session.username }}</span>
                    </div>
                    <button class="theme-toggle" onclick="toggleTheme()" aria-label="Toggle theme">
                        <i class="fas fa-moon"></i>
                    </button>
                    <a href="/logout" class="logout-btn">
                        <i class="fas fa-sign-out-alt"></i> Logout
                    </a>
                </div>
            </div>
        </header>

        <div class="stats-grid">
            <div class="stat-card">
                <span class="stat-label">Total Webhooks</span>
                <div class="stat-value" id="total-webhooks">0</div>
                <i class="fas fa-network-wired"></i>
            </div>
            <div class="stat-card">
                <span class="stat-label">Success Rate</span>
                <div class="stat-value" id="success-rate">0%</div>
                <i class="fas fa-shield-alt"></i>
            </div>
            <div class="stat-card">
                <span class="stat-label">Last Received</span>
                <div class="stat-value" id="last-received" style="font-size: 1.8rem;">-</div>
                <i class="fas fa-clock"></i>
            </div>
        </div>

        <div class="controls">
            <div class="webhook-url-container">
                <input type="text" id="webhook-url" class="webhook-url" readonly placeholder="Your webhook URL">
                <button id="copy-btn" class="copy-btn">
                    <i class="fas fa-copy"></i> Copy
                </button>
            </div>
            <button onclick="clearHistory()" class="btn btn-outline">
                <i class="fas fa-trash-alt"></i> Clear History
            </button>
            <button onclick="toggleAutoRefresh()" class="btn btn-primary" id="refresh-button">
                <i class="fas fa-sync"></i> Enable Auto Refresh
            </button>
        </div>

        <div class="search-container">
            <i class="fas fa-search search-icon"></i>
            <input type="text" id="search-input" class="search-input" placeholder="Search webhooks by content, headers, or method...">
        </div>

        <div id="webhooks-container" class="webhooks-container"></div>
    </div>

    <script>
        let autoRefreshInterval;
        let isAutoRefreshEnabled = false;
        let isDarkMode = true;
        let webhooksData = [];

        document.addEventListener('DOMContentLoaded', () => {
            const savedTheme = localStorage.getItem('theme') || 'dark';
            document.documentElement.setAttribute('data-theme', savedTheme);
            document.querySelector('.theme-toggle i').className = savedTheme === 'dark' ? 'fas fa-sun' : 'fas fa-moon';
            isDarkMode = savedTheme === 'dark';
            updateWebhookUrl();
            document.getElementById('copy-btn').addEventListener('click', copyWebhookUrl);
            refreshWebhooks();
            initMatrixBackground();
        });

        function initMatrixBackground() {
            const canvas = document.getElementById('matrix-bg');
            const ctx = canvas.getContext('2d');
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

            const chars = '01';
            const fontSize = 14;
            const columns = canvas.width / fontSize;
            const drops = Array(Math.floor(columns)).fill(1);

            function draw() {
                ctx.fillStyle = 'rgba(10, 10, 10, 0.05)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                ctx.fillStyle = '#00FFCC';
                ctx.font = `${fontSize}px 'Fira Code'`;

                for (let i = 0; i < drops.length; i++) {
                    const text = chars.charAt(Math.floor(Math.random() * chars.length));
                    ctx.fillText(text, i * fontSize, drops[i] * fontSize);
                    if (drops[i] * fontSize > canvas.height && Math.random() > 0.975) {
                        drops[i] = 0;
                    }
                    drops[i]++;
                }
            }

            setInterval(draw, 33);
            window.addEventListener('resize', () => {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            });
        }

        function toggleTheme() {
            isDarkMode = !isDarkMode;
            document.documentElement.setAttribute('data-theme', isDarkMode ? 'dark' : 'light');
            document.querySelector('.theme-toggle i').className = isDarkMode ? 'fas fa-sun' : 'fas fa-moon';
            localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
        }

        function showNotification(message, type = 'success') {
            const existing = document.querySelector('.notification');
            if (existing) existing.remove();

            const notification = document.createElement('div');
            notification.className = 'notification';
            notification.innerHTML = `
                <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-triangle'}"></i> ${message}
            `;
            document.body.appendChild(notification);
            setTimeout(() => {
                notification.style.opacity = '0';
                notification.style.transform = 'translateX(100%)';
                setTimeout(() => notification.remove(), 300);
            }, 3000);
        }

        function updateWebhookUrl() {
            const protocol = window.location.protocol;
            const hostname = window.location.hostname;
            const port = window.location.port ? `:${window.location.port}` : '';
            const webhookPath = '/webhook';
            const fullWebhookUrl = `${protocol}//${hostname}${port}${webhookPath}`;
            document.getElementById('webhook-url').value = fullWebhookUrl;
        }

        function copyWebhookUrl() {
            const webhookUrl = document.getElementById('webhook-url').value;
            if (navigator.clipboard) {
                navigator.clipboard.writeText(webhookUrl)
                    .then(() => showNotification('URL copied to clipboard', 'success'))
                    .catch(() => fallbackCopyTextToClipboard(webhookUrl));
            } else {
                fallbackCopyTextToClipboard(webhookUrl);
            }
        }

        function fallbackCopyTextToClipboard(text) {
            const textArea = document.createElement('textarea');
            textArea.value = text;
            textArea.style.position = 'fixed';
            textArea.style.top = '0';
            textArea.style.left = '0';
            textArea.style.width = '2em';
            textArea.style.height = '2em';
            textArea.style.padding = '0';
            textArea.style.border = 'none';
            textArea.style.outline = 'none';
            textArea.style.boxShadow = 'none';
            textArea.style.background = 'transparent';
            document.body.appendChild(textArea);
            textArea.focus();
            textArea.select();
            try {
                const successful = document.execCommand('copy');
                showNotification(successful ? 'URL copied to clipboard' : 'Failed to copy URL', successful ? 'success' : 'error');
            } catch (err) {
                showNotification('Failed to copy URL', 'error');
            }
            document.body.removeChild(textArea);
        }

        function updateStats() {
            const total = webhooksData.length;
            const successCount = webhooksData.filter(w =>
                w.data?.status === 'success' ||
                (w.headers && w.headers['X-Status'] === 'success')
            ).length;
            const successRate = total ? ((successCount / total) * 100).toFixed(1) : 0;
            const lastReceived = webhooksData[0]?.timestamp || '-';
            document.getElementById('total-webhooks').textContent = total;
            document.getElementById('success-rate').textContent = `${successRate}%`;
            document.getElementById('last-received').textContent = lastReceived;
        }

        function clearHistory() {
            fetch('/clear', { method: 'POST' })
                .then(response => response.json())
                .then(data => {
                    if (data.status === 'success') {
                        webhooksData = [];
                        updateWebhooksDisplay();
                        showNotification('History cleared', 'success');
                        updateStats();
                    } else {
                        showNotification('Failed to clear history', 'error');
                    }
                })
                .catch(error => showNotification('Error clearing history', 'error'));
        }

        function toggleAutoRefresh() {
            const button = document.getElementById('refresh-button');
            if (isAutoRefreshEnabled) {
                clearInterval(autoRefreshInterval);
                button.innerHTML = '<i class="fas fa-sync"></i> Enable Auto Refresh';
                button.classList.remove('btn-outline');
                button.classList.add('btn-primary');
                isAutoRefreshEnabled = false;
                showNotification('Auto refresh disabled', 'error');
                const indicator = document.querySelector('.loading-indicator');
                if (indicator) indicator.remove();
            } else {
                autoRefreshInterval = setInterval(refreshWebhooks, 2000);
                button.innerHTML = '<i class="fas fa-sync fa-spin"></i> Disable Auto Refresh';
                button.classList.remove('btn-primary');
                button.classList.add('btn-outline');
                isAutoRefreshEnabled = true;
                showNotification('Auto refresh enabled', 'success');
                const indicator = document.createElement('div');
                indicator.className = 'loading-indicator';
                indicator.textContent = 'Live';
                document.body.appendChild(indicator);
            }
        }

        function formatJSON(obj) {
            if (typeof obj !== 'object' || obj === null) {
                return String(obj);
            }
            return JSON.stringify(obj, null, 2)
                .replace(/&/g, '&amp;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;');
        }

        function updateWebhooksDisplay(searchTerm = '') {
            let filteredWebhooks = webhooksData;
            if (searchTerm) {
                const lowercaseSearch = searchTerm.toLowerCase();
                filteredWebhooks = webhooksData.filter(webhook => {
                    const methodMatch = webhook.method.toLowerCase().includes(lowercaseSearch);
                    const pathMatch = webhook.path.toLowerCase().includes(lowercaseSearch);
                    const dataMatch = JSON.stringify(webhook.data).toLowerCase().includes(lowercaseSearch);
                    const headersMatch = JSON.stringify(webhook.headers).toLowerCase().includes(lowercaseSearch);
                    return methodMatch || pathMatch || dataMatch || headersMatch;
                });
            }

            const container = document.getElementById('webhooks-container');
            if (filteredWebhooks.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <i class="fas fa-inbox"></i>
                        <h3>No Webhooks Yet</h3>
                        <p>Send a request to the webhook URL to see data here</p>
                    </div>
                `;
                return;
            }

            container.innerHTML = filteredWebhooks.map((webhook, index) => `
                <div class="webhook-card">
                    <div class="webhook-header">
                        <span class="method-badge method-${webhook.method}">${webhook.method}</span>
                        <span class="webhook-path">${webhook.path}</span>
                        <span class="webhook-timestamp">
                            <i class="far fa-clock"></i> ${webhook.timestamp}
                        </span>
                    </div>
                    <div class="webhook-data">
                        <pre><code>${formatJSON(webhook.data)}</code></pre>
                    </div>
                    <details class="headers-collapse" data-webhook-id="${webhook.id}">
                        <summary>
                            <i class="fas fa-chevron-right"></i> Headers
                        </summary>
                        <div class="webhook-data" style="margin-top: 1.2rem;">
                            <pre><code>${formatJSON(webhook.headers)}</code></pre>
                        </div>
                    </details>
                </div>
            `).join('');

            attachDetailsToggleListeners();
        }

        const openDetails = new Set();

        function attachDetailsToggleListeners() {
            document.querySelectorAll('.headers-collapse').forEach(detailsElement => {
                detailsElement.removeEventListener('toggle', handleDetailsToggle);
                detailsElement.addEventListener('toggle', handleDetailsToggle);
                const webhookId = detailsElement.dataset.webhookId;
                if (openDetails.has(webhookId)) {
                    detailsElement.open = true;
                }
            });
        }

        function handleDetailsToggle(event) {
            const detailsElement = event.target;
            const webhookId = detailsElement.dataset.webhookId;
            if (detailsElement.open) {
                event.preventDefault();
                detailsElement.open = true;
                openDetails.add(webhookId);
            } else {
                openDetails.delete(webhookId);
            }
        }

        async function refreshWebhooks() {
            try {
                const response = await fetch('/get-webhooks');
                webhooksData = await response.json();
                webhooksData.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
                updateWebhooksDisplay(document.getElementById('search-input').value);
                updateStats();
            } catch (error) {
                console.error('Error refreshing webhooks:', error);
            }
        }

        document.getElementById('search-input').addEventListener('input', (e) => {
            updateWebhooksDisplay(e.target.value);
        });
    </script>
</body>
</html>