<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Application Health Status</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            color: #333;
            text-align: center;
            padding: 50px;
        }
        h1 {
            font-size: 3em;
            color: #3498db;
        }
        h2 {
            font-size: 2em;
            color: #555;
        }
        table {
            margin: 20px auto;
            border-collapse: collapse;
            width: 90%;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .status-ok {
            color: #27ae60;
            font-weight: bold;
        }
        .status-warning {
            color: #856404;
            font-weight: bold;
        }
        .status-error {
            color: #e74c3c;
            font-weight: bold;
        }
        .exception {
            color: #e74c3c;
            font-weight: bold;
            cursor: pointer;
            text-decoration: underline;
        }

        /* Modal Styling */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            overflow: auto;
        }
        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 1000px;
            text-align: left;
            border-radius: 10px;
            position: relative;
        }
        .close-btn {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
        }
        .close-btn:hover {
            color: black;
        }
        pre {
            background: #f4f4f4;
            padding: 10px;
            border-radius: 5px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>Application Health Status</h1>
    <h2>System Components</h2>

    {% if statuses %}
        <table>
            <thead>
                <tr>
                    <th>Component Name</th>
                    <th>Last Checked</th>
                    <th>Status</th>
                    <th>Exceptions</th>
                </tr>
            </thead>
            <tbody>
                {% for record in statuses %}
                <tr>
                    <td>{{ record.name }}</td>
                    <td>{{ record.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                    <td class="status-{{ record.status.name.lower() }}">
                        {{ record.status.name }}
                    </td>
                    <td>
                        {% if record.exceptions %}
                            <ul>
                                {% for event_name, event_content, exception, traceback in record.exceptions %}
                                    <li class="exception" onclick="showExceptionDetails('{{ event_name }}', `{{ event_content }}`, `{{ exception }}`, `{{ traceback | e }}`)">
                                        <strong>{{ event_name }}</strong>: {{ exception }}
                                    </li>
                                {% endfor %}
                            </ul>
                        {% else %}
                            <ul><li>No exceptions</li></ul>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>No health records available at this time.</p>
    {% endif %}

    <!-- Modal for displaying exception details -->
    <div id="exceptionModal" class="modal">
        <div class="modal-content">
            <span class="close-btn" onclick="closeModal()">&times;</span>
            <h2 id="modal-title"></h2>
            <strong>Event Content:</strong>
            <pre id="modal-event-content"></pre>
            <strong>Exception:</strong>
            <pre id="modal-exception"></pre>
            <strong>Traceback:</strong>
            <pre id="modal-traceback"></pre>
        </div>
    </div>

    <script>
        function showExceptionDetails(eventName, eventContent, exception, traceback) {
            document.getElementById('modal-title').innerText = 'Exception Details - ' + eventName;
            document.getElementById('modal-event-content').innerText = eventContent;
            document.getElementById('modal-exception').innerText = exception;
            document.getElementById('modal-traceback').innerText = traceback;

            document.getElementById('exceptionModal').style.display = "block";
        }

        function closeModal() {
            document.getElementById('exceptionModal').style.display = "none";
        }

        // Close modal when clicking outside content
        window.onclick = function(event) {
            let modal = document.getElementById('exceptionModal');
            if (event.target === modal) {
                modal.style.display = "none";
            }
        };
    </script>
</body>
</html>
