<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Evaluation Report</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
    <style>
        body { padding: 20px; }
        .card { margin-bottom: 20px; }
        pre { white-space: pre-wrap; background-color: #f8f9fa; padding: 10px; border-radius: 5px; }
        .metric-box { background-color: #f8f9fa; border-radius: 5px; padding: 10px; margin-bottom: 10px; }
        .metric-label { font-weight: bold; }
        .metric-value { font-size: 1.1em; }
        table { width: 100%; }
        th { position: sticky; top: 0; background-color: #f8f9fa; cursor: pointer; }
        th.sort-asc::after { content: " ▲"; color: #0d6efd; }
        th.sort-desc::after { content: " ▼"; color: #0d6efd; }
        .control-panel { background-color: #f8f9fa; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
        .tab-button { margin-right: 5px; margin-bottom: 5px; }
        .tab-button.active { background-color: #0d6efd; color: white; }
        .examples-container { margin-top: 20px; }
        .hidden { display: none; }
        .generation-text { white-space: pre-wrap; margin: 0; padding: 5px; background: none; }
        table td, table th { vertical-align: middle; }
    </style>
</head>
<body>
    <div class="container-fluid">
        <h1 class="mb-4">Evaluation Report</h1>

        <!-- Charts Section -->
        {% if charts_data %}
        <div class="card mb-4">
            <div class="card-header">
                <h2>Charts</h2>
            </div>
            <div class="card-body">
                <!-- Result Set Comparison Table -->
                {% if charts_data.result_set_comparison and charts_data.result_set_comparison.comparisons %}
                <div class="mb-4">
                    <h4>Result Set Comparison (Win Percentages)</h4>
                    <p class="text-muted">
                        For each example, we find the maximum {{ target_display }} for entries with {{ fluency_display }} between 3 and 9.
                        The table shows the percentage of examples where the row's result set has a higher {{ target_display }} than the column's.
                    </p>
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="thead-light">
                                <tr>
                                    <th scope="col">Result Set (Row) vs. (Column)</th>
                                    {% for col_set in charts_data.result_set_comparison.comparisons %}
                                    <th scope="col">{{ col_set }}</th>
                                    {% endfor %}
                                </tr>
                            </thead>
                            <tbody>
                                {% for row_set in charts_data.result_set_comparison.comparisons %}
                                <tr>
                                    <th scope="row">{{ row_set }}</th>
                                    {% for col_set in charts_data.result_set_comparison.comparisons %}
                                    <td {% if row_set == col_set %}class="table-secondary"{% endif %}>
                                        {% if row_set != col_set and col_set in charts_data.result_set_comparison.comparisons[row_set] %}
                                            {% set comp_data = charts_data.result_set_comparison.comparisons[row_set][col_set] %}
                                            {{ "%.1f"|format(comp_data.win_percentage|default(0)) }}%
                                            <br>
                                            <small class="text-muted">({{ comp_data.win_count|default(0) }}/{{ comp_data.total|default(0) }})</small>
                                        {% else %}
                                            -
                                        {% endif %}
                                    </td>
                                    {% endfor %}
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
                {% endif %}

                <!-- Scatter Plot -->
                {% if charts_data.metric_scatter and charts_data.metric_scatter.data|length > 0 %}
                <div class="mb-4">
                    <h4>{{ fluency_display }} vs {{ target_display }}</h4>
                    <div id="scatter-chart" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const scatterData = {{ charts_data.metric_scatter.data|tojson }};
                            const scatterLayout = {{ charts_data.metric_scatter.layout|tojson }};

                            Plotly.newPlot('scatter-chart', scatterData, scatterLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No data available for {{ fluency_display }} vs {{ target_display }} scatter plot.</p>
                    </div>
                </div>
                {% endif %}

                <!-- Target Metric Violin Plot -->
                {% if charts_data[target ~ '_violin'] and charts_data[target ~ '_violin'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ target_display }} Values (Violin Plot)</h4>
                    <div id="target-violin-chart" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[target ~ '_violin'].data|tojson }};
                            const chartLayout = {{ charts_data[target ~ '_violin'].layout|tojson }};

                            Plotly.newPlot('target-violin-chart', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% endif %}

                <!-- Fluency Metric Violin Plot -->
                {% if charts_data[fluency ~ '_violin'] and charts_data[fluency ~ '_violin'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ fluency_display }} Values (Violin Plot)</h4>
                    <div id="fluency-violin-chart" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[fluency ~ '_violin'].data|tojson }};
                            const chartLayout = {{ charts_data[fluency ~ '_violin'].layout|tojson }};

                            Plotly.newPlot('fluency-violin-chart', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% endif %}

                <!-- All Data Histogram -->
                {% if charts_data[target ~ '_histogram_all'] and charts_data[target ~ '_histogram_all'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ target_display }} Values - All Data</h4>
                    <div id="target-chart-all" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[target ~ '_histogram_all'].data|tojson }};
                            const chartLayout = {{ charts_data[target ~ '_histogram_all'].layout|tojson }};

                            Plotly.newPlot('target-chart-all', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No data available for {{ target_display }} histogram (all data).</p>
                    </div>
                </div>
                {% endif %}

                <!-- Low Cross-Entropy Histogram -->
                {% if charts_data[target ~ '_histogram_low'] and charts_data[target ~ '_histogram_low'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ target_display }} Values - Low {{ fluency_display }} (0-3)</h4>
                    <div id="target-chart-low" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[target ~ '_histogram_low'].data|tojson }};
                            const chartLayout = {{ charts_data[target ~ '_histogram_low'].layout|tojson }};

                            Plotly.newPlot('target-chart-low', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No examples found with {{ fluency_display }} values between 0 and 3.</p>
                    </div>
                </div>
                {% endif %}

                <!-- Medium Cross-Entropy Histogram -->
                {% if charts_data[target ~ '_histogram_medium'] and charts_data[target ~ '_histogram_medium'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ target_display }} Values - Medium {{ fluency_display }} (3-9)</h4>
                    <div id="target-chart-medium" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[target ~ '_histogram_medium'].data|tojson }};
                            const chartLayout = {{ charts_data[target ~ '_histogram_medium'].layout|tojson }};

                            Plotly.newPlot('target-chart-medium', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No examples found with {{ fluency_display }} values between 3 and 9.</p>
                    </div>
                </div>
                {% endif %}

                <!-- High Cross-Entropy Histogram -->
                {% if charts_data[target ~ '_histogram_high'] and charts_data[target ~ '_histogram_high'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ target_display }} Values - High {{ fluency_display }} (9+)</h4>
                    <div id="target-chart-high" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[target ~ '_histogram_high'].data|tojson }};
                            const chartLayout = {{ charts_data[target ~ '_histogram_high'].layout|tojson }};

                            Plotly.newPlot('target-chart-high', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No examples found with {{ fluency_display }} values of 9 or higher.</p>
                    </div>
                </div>
                {% endif %}

                <!-- Fluency Metric Distribution Histogram -->
                {% if charts_data[fluency ~ '_histogram'] and charts_data[fluency ~ '_histogram'].data|length > 0 %}
                <div class="mb-4">
                    <h4>Distribution of {{ fluency_display }} Values</h4>
                    <div id="fluency-chart" style="width: 100%; height: 400px;"></div>
                    <script>
                        document.addEventListener('DOMContentLoaded', function() {
                            const chartData = {{ charts_data[fluency ~ '_histogram'].data|tojson }};
                            const chartLayout = {{ charts_data[fluency ~ '_histogram'].layout|tojson }};

                            Plotly.newPlot('fluency-chart', chartData, chartLayout, {responsive: true});
                        });
                    </script>
                </div>
                {% else %}
                <div class="mb-4">
                    <div class="alert alert-info">
                        <h4 class="alert-heading">No data available</h4>
                        <p>No data available for {{ fluency_display }} histogram.</p>
                    </div>
                </div>
                {% endif %}
            </div>
        </div>
        {% endif %}

        <!-- Sorting Controls -->
        <div class="card mb-4">
            <div class="card-header">
                <h3>Sort Examples</h3>
            </div>
            <div class="card-body">
                <div class="row align-items-center">
                    <div class="col-md-3">
                        <label for="sort-metric" class="form-label">Sort by Metric:</label>
                        <select id="sort-metric" class="form-select">
                            <option value="">Default Order</option>
                            {% for metric in numeric_metrics %}
                            <option value="{{ metric }}">{{ metric|replace('_', ' ')|title }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label for="sort-result-set" class="form-label">Sort Using Result Set:</label>
                        <select id="sort-result-set" class="form-select">
                            <option value="">All Result Sets</option>
                            {% for result_set in available_result_sets %}
                            <option value="{{ result_set }}">{{ result_set }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <div class="form-check mt-3">
                            <input class="form-check-input" type="checkbox" id="sort-ascending" checked>
                            <label class="form-check-label" for="sort-ascending">
                                Ascending Order
                            </label>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <button id="apply-sort" class="btn btn-primary mt-2">Apply Sort</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- Example Selection Dropdown -->
        <div class="mt-4 mb-3">
            <h2>Examples</h2>
            <div class="row">
                <div class="col-md-6">
                    <select id="example-dropdown" class="form-select">
                        {% for example in examples %}
                        <option value="example-{{ example.idx }}">Example {{ example.idx }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>
        </div>

        <!-- Individual Examples -->
        <div class="examples-container">
            {% for example in examples %}
            <div id="example-{{ example.idx }}" class="example-tab-content" data-idx="{{ example.idx }}" style="display: {% if loop.first %}block{% else %}none{% endif %};">
                <div class="card">
                    <div class="card-header">
                        <h3>Example {{ example.idx }}</h3>
                    </div>
                    <div class="card-body">
                        <!-- Information Section (if available) -->
                        {% if example.info_data %}
                        <div class="card mb-3">
                            <div class="card-header bg-primary text-white">
                                <h4 class="mb-0">Information</h4>
                            </div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered">
                                        <tbody>
                                            {% for key, value in example.info_data.items() %}
                                            <tr>
                                                <td><strong>{{ key|replace('_', ' ') }}</strong></td>
                                                <td>
                                                    {% if value is string %}
                                                        <pre>{{ value }}</pre>
                                                    {% elif value is iterable and value is not mapping %}
                                                        {% for item in value %}
                                                            <pre>{{ item }}</pre>
                                                            {% if not loop.last %}<hr>{% endif %}
                                                        {% endfor %}
                                                    {% else %}
                                                        <pre>{{ value }}</pre>
                                                    {% endif %}
                                                </td>
                                            </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        {% endif %}

                        <!-- Generation Table Section -->
                        <div class="card mb-3">
                            <div class="card-header bg-primary text-white">
                                <h4 class="mb-0">Generations</h4>
                            </div>
                            <div class="card-body">
                                <!-- Scatter Plot FIRST -->
                                {% if example.scatter_plot %}
                                <div class="mb-4">
                                    <h5>{{ fluency_display }} vs {{ target_display }} for this Example</h5>
                                    <div id="scatter-plot-example-{{ example.idx }}" style="width: 100%; height: 300px;"></div>
                                    <script>
                                        document.addEventListener('DOMContentLoaded', function() {
                                            const scatterData = {{ example.scatter_plot.data|tojson }};
                                            const scatterLayout = {{ example.scatter_plot.layout|tojson }};

                                            Plotly.newPlot('scatter-plot-example-{{ example.idx }}',
                                                scatterData, scatterLayout, {responsive: true});
                                        });
                                    </script>
                                </div>
                                {% endif %}

                                <!-- Tables for each Result Set -->
                                {% for rs in example.result_sets %}
                                <div class="mb-4">
                                    <h5 class="mb-3">{{ rs.name }}</h5>

                                    <!-- Table of Generations for this Result Set -->
                                    <div class="table-responsive">
                                        <table class="table table-striped table-bordered generation-table">
                                            <thead>
                                                <tr>
                                                    <!-- Dynamically add all columns -->
                                                    {% for column in rs.columns %}
                                                    <th data-sort-type="{{ 'numeric' if column in numeric_metrics else 'text' }}">
                                                        {{ column|replace('_', ' ')|title }}
                                                    </th>
                                                    {% endfor %}
                                                </tr>
                                            </thead>
                                            <tbody>
                                                {% for gen in rs.generations %}
                                                <tr>
                                                    <!-- Dynamically add all values -->
                                                    {% for column in rs.columns %}
                                                        {% if column in numeric_metrics %}
                                                        <td data-value="{{ gen.get(column, 'N/A') }}">
                                                            {% if column in gen and gen[column] is not none %}
                                                                {{ "%.4f"|format(gen[column]) }}
                                                            {% else %}
                                                                N/A
                                                            {% endif %}
                                                        </td>
                                                        {% else %}
                                                        <td>
                                                            {% if column in gen and gen[column] is not none %}
                                                                {% if gen[column] is string and '\n' in gen[column] %}
                                                                    <pre class="generation-text">{{ gen[column] }}</pre>
                                                                {% else %}
                                                                    {{ gen[column] }}
                                                                {% endif %}
                                                            {% endif %}
                                                        </td>
                                                        {% endif %}
                                                    {% endfor %}
                                                </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- Footer -->
    <footer class="mt-5 mb-3 text-center text-muted">
        <p>Evaluation Report - Generated on {{ current_datetime.strftime('%Y-%m-%d %H:%M') }}</p>
    </footer>

    <script>
        // Interactive functionality
        $(document).ready(function() {
            // Example dropdown change handler
            $('#example-dropdown').change(function() {
                const selectedId = $(this).val();
                $('.example-tab-content').hide();
                $('#' + selectedId).show();
            });

            // Apply sorting
            $('#apply-sort').click(function() {
                const metricName = $('#sort-metric').val();
                const ascending = $('#sort-ascending').is(':checked');
                const selectedResultSet = $('#sort-result-set').val();

                if (!metricName) {
                    // If no metric selected, revert to default order
                    resetExampleOrder();
                    return;
                }

                // Filter examples by result set if selected
                let filteredData = exampleData;
                if (selectedResultSet) {
                    filteredData = exampleData.filter(item => item.resultSet === selectedResultSet);
                }

                // Group by example index (to avoid duplicates)
                const exampleMetrics = {};
                filteredData.forEach(item => {
                    const idx = item.idx;
                    const metricValue = item.metrics[metricName];

                    // Only process items that have the selected metric
                    if (metricValue !== undefined) {
                        if (!exampleMetrics[idx] ||
                            (ascending && metricValue < exampleMetrics[idx].value) ||
                            (!ascending && metricValue > exampleMetrics[idx].value)) {
                            exampleMetrics[idx] = {
                                idx: idx,
                                elementId: item.elementId,
                                value: metricValue,
                                resultSet: item.resultSet
                            };
                        }
                    }
                });

                // Convert to array and sort
                const sortedExamples = Object.values(exampleMetrics);
                sortedExamples.sort((a, b) => {
                    return ascending ? a.value - b.value : b.value - a.value;
                });

                // Clear and rebuild dropdown with sorted examples
                const dropdown = $('#example-dropdown');
                dropdown.empty();

                sortedExamples.forEach(ex => {
                    const resultSetInfo = ex.resultSet ? ` (${ex.resultSet})` : '';
                    dropdown.append(`<option value="${ex.elementId}">Example ${ex.idx}${resultSetInfo} - ${metricName}: ${ex.value.toFixed(4)}</option>`);
                });

                // Show the first example in the sorted list
                if (sortedExamples.length > 0) {
                    dropdown.val(sortedExamples[0].elementId).change();
                }
            });

            // Table sorting
            $('.generation-table th').click(function() {
                const table = $(this).closest('table');
                const index = $(this).index();
                const sortType = $(this).data('sort-type');
                const isAsc = $(this).hasClass('sort-asc');

                // Remove sorting indicators from all headers in this table
                $(this).siblings().removeClass('sort-asc sort-desc');

                // Set direction: Toggle or default to ascending
                if (isAsc) {
                    $(this).removeClass('sort-asc').addClass('sort-desc');
                } else {
                    $(this).removeClass('sort-desc').addClass('sort-asc');
                }

                // Get rows and sort
                const rows = table.find('tbody tr').get();
                rows.sort(function(a, b) {
                    let x, y;

                    if (sortType === 'text') {
                        x = $(a).find('td').eq(index).text().toLowerCase();
                        y = $(b).find('td').eq(index).text().toLowerCase();
                        return isAsc ? x.localeCompare(y) : y.localeCompare(x);
                    } else {
                        // Numeric sorting
                        x = parseFloat($(a).find('td').eq(index).data('value')) || 0;
                        y = parseFloat($(b).find('td').eq(index).data('value')) || 0;
                        return isAsc ? x - y : y - x;
                    }
                });

                // Reattach rows in sorted order
                $.each(rows, function(index, row) {
                    table.find('tbody').append(row);
                });
            });
        });
    </script>
</body>
</html>
