<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LlamaBot Log Viewer</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://unpkg.com/htmx.org@1.9.6"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-dark.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <link rel="stylesheet" href="{{ url_for('static', path='styles.css') }}">
</head>
<body>
    <div class="container mt-4">
        <h1 class="mb-4">LlamaBot Log Viewer</h1>

        <!-- Tabs -->
        <ul class="nav nav-tabs mb-4" id="myTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="logs-tab" data-bs-toggle="tab" data-bs-target="#logs" type="button" role="tab" aria-controls="logs" aria-selected="true">Logs</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="prompts-tab" data-bs-toggle="tab" data-bs-target="#prompts" type="button" role="tab" aria-controls="prompts" aria-selected="false">Compare Prompts</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="experiments-tab" data-bs-toggle="tab" data-bs-target="#experiments" type="button" role="tab" aria-controls="experiments" aria-selected="false">Experiments</button>
            </li>
        </ul>

        <!-- Tab Content -->
        <div class="tab-content" id="myTabContent">
            <!-- Logs Tab -->
            <div class="tab-pane fade show active" id="logs" role="tabpanel" aria-labelledby="logs-tab">
                <!-- Filter Controls First -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <input type="text"
                               class="form-control"
                               name="text_filter"
                               placeholder="Filter logs..."
                               hx-get="/logs/filtered_logs"
                               hx-trigger="keyup changed delay:500ms"
                               hx-target="#logs-tbody"
                               hx-include="[name='function_name']">
                    </div>
                    <div class="col-md-6">
                        <select class="form-select"
                                name="function_name"
                                hx-get="/logs/filtered_logs"
                                hx-trigger="change"
                                hx-target="#logs-tbody"
                                hx-include="[name='text_filter']">
                            <option value="">Select a prompt function</option>
                            {% for prompt in prompts %}
                                <option value="{{ prompt.function_name }}">
                                    {{ prompt.function_name }} ({{ prompt.count }} versions)
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <!-- Add this right after the filter controls div and before the Logs Table and Details -->
                <div class="row mb-3">
                    <div class="col-12">
                        <div class="d-flex align-items-center gap-3">
                            <label class="mb-0">Export Format:</label>
                            <select id="export-format" class="form-select form-select-sm" style="width: auto;">
                                <option value="openai">OpenAI Format</option>
                            </select>
                            <div class="form-check mb-0">
                                <input class="form-check-input" type="checkbox" id="positive-only">
                                <label class="form-check-label" for="positive-only">
                                    Export only helpful responses
                                </label>
                            </div>
                            <button
                                class="btn btn-sm btn-primary"
                                onclick="exportLogs()">
                                Export
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Logs Table and Details -->
                <div class="row">
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-hover mb-0">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>Object Name</th>
                                                <th>Timestamp</th>
                                                <th>Model Name</th>
                                                <th>Temperature</th>
                                                <th>Prompts Used</th>
                                            </tr>
                                        </thead>
                                        <tbody id="logs-tbody"
                                               hx-get="/logs/filtered_logs"
                                               hx-trigger="load">
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div id="log-details">
                            <p class="text-muted">Select a log to view details</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Compare Prompts Tab -->
            <div class="tab-pane fade" id="prompts" role="tabpanel" aria-labelledby="prompts-tab">
                <h2 class="mb-3">Compare Prompts</h2>
                <div class="mb-3">
                    <select id="function-name-select"
                            class="form-select"
                            name="function_name"
                            hx-get="/prompts/history"
                            hx-trigger="change"
                            hx-target="#prompt-history-container">
                        <option value="">Select a function name</option>
                        {% for prompt in prompts %}
                            <option value="{{ prompt.function_name }}">
                                {{ prompt.function_name }} ({{ prompt.count }} versions)
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div id="prompt-history-container"></div>
            </div>

            <!-- Experiments Tab -->
            <div class="tab-pane fade" id="experiments" role="tabpanel" aria-labelledby="experiments-tab">
                <h2 class="mb-3">Experiments</h2>
                <div class="mb-3">
                    <select id="experiment-name-select"
                            class="form-select"
                            name="experiment_name"
                            hx-get="/experiments/details"
                            hx-trigger="change"
                            hx-target="#experiment-details-container">
                        <option value="">Select an experiment</option>
                        {% for experiment in experiments %}
                            <option value="{{ experiment.name }}">
                                {{ experiment.name }} ({{ experiment.count }} runs)
                            </option>
                        {% endfor %}
                    </select>
                </div>
                <div id="experiment-details-container"></div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Custom JS -->
    <script src="{{ url_for('static', path='script.js') }}"></script>

    <!-- Add this script section at the end of the body -->
    <script>
    function exportLogs() {
        const format = document.getElementById('export-format').value;
        const positiveOnly = document.getElementById('positive-only').checked;
        const textFilter = document.querySelector('[name="text_filter"]').value;
        const functionName = document.querySelector('[name="function_name"]').value;

        const url = `/logs/export/${format}?text_filter=${encodeURIComponent(textFilter)}&function_name=${encodeURIComponent(functionName)}&positive_only=${positiveOnly}`;
        window.location.href = url;
    }
    </script>
</body>
</html>
