<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DNSCrypt Proxy Monitoring</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            margin: 0;
            padding: 0;
            color: #333;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        header {
            background-color: #2c3e50;
            color: white;
            padding: 1rem;
            margin-bottom: 20px;
            position: relative;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
        }
        h1 {
            margin: 0;
            font-size: 1.5rem;
        }
        .header-meta {
            display: flex;
            gap: 20px;
            align-items: center;
            font-size: 0.9rem;
        }
        .header-meta a {
            color: white;
            text-decoration: underline;
        }
        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        .card {
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            padding: 20px;
        }
        .card h2 {
            margin-top: 0;
            font-size: 1.2rem;
            color: #2c3e50;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .stat {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .stat-label {
            font-weight: bold;
        }
        .chart-container {
            height: 200px;
            margin-top: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table th, table td {
            text-align: left;
            padding: 8px;
            border-bottom: 1px solid #eee;
        }
        table th {
            background-color: #f9f9f9;
        }
        .status-table td:nth-child(2) {
            font-weight: bold;
        }
        .section-nav {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 20px;
        }
        .section-nav a {
            background-color: #e0e6ed;
            color: #2c3e50;
            padding: 6px 12px;
            border-radius: 4px;
            text-decoration: none;
            font-size: 0.9rem;
            transition: background-color 0.2s ease;
        }
        .section-nav a:hover,
        .section-nav a:focus {
            background-color: #cbd5e1;
        }
        @media (max-width: 600px) {
            .section-nav {
                gap: 6px;
            }
            .section-nav a {
                font-size: 0.8rem;
                padding: 6px 10px;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>DNSCrypt Proxy Monitoring Dashboard</h1>
        <div class="header-meta">
            <span>Last Updated: <strong id="last-updated">-</strong></span>
            <a href="/api/metrics" target="_blank">Raw Data</a>
        </div>
    </header>
    <div class="container">
        <nav class="section-nav">
            <a href="#overview">Overview</a>
            <a href="#cache-performance">Cache</a>
            <a href="#query-types">Query Types</a>
            <a href="#resolver-health">Resolvers</a>
            <a href="#top-domains">Top Domains</a>
            <a href="#source-refresh">Sources</a>
            <a href="#recent-queries">Recent Queries</a>
        </nav>

        <!-- Loading indicator -->
        <div id="loading-indicator" style="text-align: center; padding: 40px; margin: 20px 0; background-color: #f8f9fa; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1);">
            <h2>Loading DNSCrypt Proxy Monitoring...</h2>
            <p>Please wait while we connect to the monitoring service.</p>
            <p>If this message persists, please check that the DNSCrypt Proxy is running with monitoring enabled.</p>
            <div style="margin: 20px 0; height: 4px; background-color: #eee; border-radius: 2px; overflow: hidden;">
                <div id="loading-bar" style="height: 100%; width: 0%; background-color: #2c3e50; animation: loading 2s infinite linear;"></div>
            </div>
            <style>
                @keyframes loading {
                    0% { width: 0%; }
                    50% { width: 100%; }
                    100% { width: 0%; }
                }
            </style>
        </div>

        <div class="dashboard">
            <div class="card">
                <h2 id="overview">Overview</h2>
                <div class="stat">
                    <span class="stat-label">Total Queries:</span>
                    <span id="total-queries">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Blocked Queries:</span>
                    <span id="blocked-queries">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Queries Per Second:</span>
                    <span id="qps">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Uptime:</span>
                    <span id="uptime">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Avg Response Time:</span>
                    <span id="avg-response-time">-</span>
                </div>
            </div>
            <div class="card">
                <h2 id="cache-performance">Cache Performance</h2>
                <div class="stat">
                    <span class="stat-label">Enabled:</span>
                    <span id="cache-enabled">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Cache Hit Ratio:</span>
                    <span id="cache-hit-ratio">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Cache Hits:</span>
                    <span id="cache-hits">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Cache Misses:</span>
                    <span id="cache-misses">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Configured Size:</span>
                    <span id="cache-configured-size">-</span>
                </div>
                <div class="stat">
                    <span class="stat-label">Entries / Capacity:</span>
                    <span><span id="cache-entries">-</span> / <span id="cache-capacity">-</span></span>
                </div>
                <div class="stat">
                    <span class="stat-label">TTL Ranges:</span>
                    <span id="cache-ttl-range">-</span>
                </div>
            </div>
            <div class="card">
                <h2 id="query-types">Query Types</h2>
                <div id="query-types-container">
                    <table id="query-types-table">
                        <thead>
                            <tr>
                                <th>Type</th>
                                <th>Count</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="card">
            <h2 id="resolver-health">Resolver Health</h2>
            <table id="resolver-table" class="status-table">
                <thead>
                    <tr>
                        <th>Resolver</th>
                        <th>Status</th>
                        <th>Success</th>
                        <th>Total</th>
                        <th>Failures</th>
                        <th>Avg Response</th>
                        <th>Last Seen</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>

        <div class="card">
            <h2 id="top-domains">Top Domains</h2>
            <table id="domains-table">
                <thead>
                    <tr>
                        <th>Domain</th>
                        <th>Count</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>

        <div class="card">
            <h2 id="source-refresh">Source Refresh Status</h2>
            <table id="sources-table">
                <thead>
                    <tr>
                        <th>Source</th>
                        <th>Last Refresh</th>
                        <th>Next Refresh</th>
                        <th>Status</th>
                        <th>Age</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>

        <div class="card">
            <h2 id="recent-queries">Recent Queries</h2>
            <table id="queries-table">
                <thead>
                    <tr>
                        <th>Time</th>
                        <th>Domain</th>
                        <th>Type</th>
                        <th>Client</th>
                        <th>Server</th>
                        <th>Response</th>
                        <th>Time (ms)</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <script src="/static/monitoring.js"></script>
</body>
</html>
