{% extends "base.html" %}

{% block title %}Research Details - Deep Research System{% endblock %}

{% block extra_head %}
<link rel="stylesheet" href="/static/css/research_details.css">
{% endblock %}

{% block content %}
<div class="ldr-page active" id="research-details">
    <div class="ldr-page-header">
        <div class="ldr-results-header">
            <h1>Research Details</h1>
            <div class="ldr-results-actions">
                <button class="btn ldr-btn-outline" id="view-results-btn"><i class="fas fa-file-text"></i> View Results</button>
                <button class="btn ldr-btn-outline" id="view-journals-btn"><i class="fas fa-book"></i> View Journals</button>
                <button class="btn ldr-btn-outline" id="back-to-history"><i class="fas fa-arrow-left"></i> Back to History</button>
            </div>
        </div>
    </div>

    <div id="loading" class="ldr-loading-spinner">
        <i class="fas fa-spinner fa-spin fa-2x"></i>
        <p>Loading research details...</p>
    </div>

    <div id="error" class="ldr-error-message" style="display: none;">
        <i class="fas fa-exclamation-circle fa-2x"></i>
        <p>Error loading research details</p>
    </div>

    <div id="details-content" style="display: none;">
    <div class="ldr-card">
        <div class="ldr-card-content">
            <div class="ldr-research-metadata">
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Query:</span>
                    <span id="research-query" class="ldr-metadata-value"></span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Mode:</span>
                    <span id="research-mode" class="ldr-metadata-value"></span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Date:</span>
                    <span id="research-date" class="ldr-metadata-value"></span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Model:</span>
                    <span id="model-used" class="ldr-metadata-value"></span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Strategy:</span>
                    <span id="research-strategy" class="ldr-metadata-value">-</span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Total Cost:</span>
                    <span id="total-cost" class="ldr-metadata-value">$0.00</span>
                </div>
                <div class="ldr-metadata-item">
                    <span class="ldr-metadata-label">Progress:</span>
                    <div class="ldr-detail-progress-container">
                        <div class="ldr-detail-progress-bar">
                            <div class="ldr-detail-progress-fill" id="detail-progress-fill"></div>
                        </div>
                        <span id="detail-progress-percentage">0%</span>
                    </div>
                </div>
            </div>

            <div class="ldr-detail-actions" id="detail-actions">
                <!-- Conditionally shown based on research status -->
            </div>
        </div>
    </div>

    <!-- Token Usage Over Time (prominent chart at top) -->
    <div class="ldr-card" id="token-usage-top-chart" style="display: none;">
        <div class="ldr-card-content">
            <div id="co-performance-warning" class="ldr-co-performance-warning" style="display: none;">
                <i aria-hidden="true" class="fas fa-exclamation-triangle"></i>
                <div>
                    <strong>Context overflow detected.</strong>
                    This research exceeded its context window, causing truncation. This can <strong>severely degrade</strong> the quality and completeness of results — information is lost and the model operates on incomplete context. Consider using a model with a larger context window or reducing query complexity.
                </div>
            </div>
            <h2><i class="fas fa-chart-line"></i> Token Usage Over Time</h2>
            <div class="ldr-chart-container" style="height: 300px; margin-top: 1rem;">
                <canvas id="co-usage-chart"></canvas>
            </div>
        </div>
    </div>

    <!-- Token Metrics Section -->
    <div class="ldr-card ldr-metrics-section" id="token-metrics-section" style="display: none;">
        <div class="card-header">
            <h2><i class="fas fa-chart-line"></i> Token Usage Timeline</h2>
        </div>
        <div class="ldr-card-content">
            <!-- Metrics Summary -->
            <div class="ldr-metrics-grid" id="metrics-summary">
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Total Tokens</div>
                    <div class="ldr-metric-value" id="total-tokens">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Input Tokens</div>
                    <div class="ldr-metric-value" id="prompt-tokens">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Output Tokens</div>
                    <div class="ldr-metric-value" id="completion-tokens">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">LLM Calls</div>
                    <div class="ldr-metric-value" id="llm-calls">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Avg Response Time</div>
                    <div class="ldr-metric-value" id="avg-response-time">0ms</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Success Rate</div>
                    <div class="ldr-metric-value" id="success-rate">0%</div>
                </div>
            </div>

            <!-- Charts Section -->
            <div class="ldr-charts-grid">
                <!-- Token Consumption Chart -->
                <div>
                    <div style="display: flex; justify-content: space-between; align-items: center;">
                        <h3 style="margin: 0;"><i class="fas fa-clock"></i> Token Consumption Over Time</h3>
                        <div>
                            <button id="chart-view-bars" class="ldr-chart-toggle active">Bars</button>
                            <button id="chart-view-line" class="ldr-chart-toggle">Line</button>
                        </div>
                    </div>
                    <div class="ldr-chart-container" style="height: 250px;">
                        <canvas id="timeline-chart"></canvas>
                    </div>
                </div>

                <!-- Search Results Chart -->
                <div>
                    <h3><i class="fas fa-chart-line"></i> Search Results Over Time</h3>
                    <div class="ldr-chart-container" style="height: 250px;">
                        <canvas id="search-chart"></canvas>
                    </div>
                </div>
            </div>

            <!-- Phase Breakdown -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-layer-group"></i> Breakdown by Research Phase</h3>
                <div class="ldr-phase-stats" id="phase-breakdown">
                    <!-- Populated dynamically -->
                </div>
            </div>

        </div>
    </div>

    <!-- Source Type Distribution Section (separate card for prominence) -->
    <div class="ldr-card ldr-metrics-section" id="source-distribution-section" style="display: none;">
        <div class="card-header">
            <h2><i class="fas fa-chart-pie"></i> Source Type Distribution</h2>
        </div>
        <div class="ldr-card-content">
            <div class="ldr-chart-container" style="height: 350px; max-width: 600px; margin: 0 auto;">
                <canvas id="source-type-chart"></canvas>
            </div>
        </div>
    </div>

    <!-- Search Analytics Section -->
    <div class="ldr-card ldr-metrics-section" id="search-metrics-section" style="display: none;">
        <div class="card-header">
            <h2><i class="fas fa-search"></i> Search Analytics</h2>
        </div>
        <div class="ldr-card-content">
            <!-- Search Summary -->
            <div class="ldr-metrics-grid" id="search-summary">
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Total Searches</div>
                    <div class="ldr-metric-value" id="total-searches">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Total Results</div>
                    <div class="ldr-metric-value" id="total-search-results">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Avg Response Time</div>
                    <div class="ldr-metric-value" id="avg-search-response-time">0ms</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Search Success Rate</div>
                    <div class="ldr-metric-value" id="search-success-rate">0%</div>
                </div>
            </div>

            <!-- Search Engine Performance -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-tachometer-alt"></i> Search Engine Performance</h3>
                <div id="search-engine-performance" style="margin-top: 1rem;">
                    <!-- Populated dynamically -->
                </div>
            </div>

            <!-- Search Engine Breakdown -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-search"></i> Search Calls</h3>
                <div id="search-engine-breakdown" style="margin-top: 1rem;">
                    <!-- Populated dynamically -->
                </div>
            </div>

            <!-- Search Timeline -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-clock"></i> Search Timeline</h3>
                <div id="search-timeline" style="margin-top: 1rem; max-height: 400px; overflow-y: auto;">
                    <!-- Populated dynamically -->
                </div>
            </div>
        </div>
    </div>

    <!-- Context Overflow Section -->
    <div class="ldr-card ldr-metrics-section" id="context-overflow-section" style="display: none; margin-top: 2rem;">
        <div class="card-header">
            <h2><i class="fas fa-layer-group"></i> Context & Token Usage Analysis</h2>
        </div>
        <div class="ldr-card-content">
            <!-- Overview Cards -->
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem;">
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Total Tokens</div>
                    <div class="ldr-metric-value" id="co-total-tokens">-</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Context Limit</div>
                    <div class="ldr-metric-value" id="co-context-limit">-</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Max Tokens Used</div>
                    <div class="ldr-metric-value" id="co-max-tokens">-</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Truncation Status</div>
                    <div class="ldr-metric-value" id="co-truncation-status">-</div>
                </div>
            </div>

            <!-- Phase Breakdown -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-tasks"></i> Token Usage by Phase</h3>
                <div id="co-phase-breakdown" style="margin-top: 1rem;">
                    <!-- Populated dynamically -->
                </div>
            </div>

            <!-- Detailed Requests Table -->
            <div style="margin-top: 2rem;">
                <h3 style="margin-bottom: 1rem;"><i class="fas fa-list"></i> Detailed Request Log</h3>
                <div style="overflow-x: auto; background: var(--card-bg); border-radius: 0.5rem; padding: 0.5rem;">
                    <table class="ldr-data-table">
                        <thead>
                            <tr>
                                <th scope="col">Time</th>
                                <th scope="col">Phase</th>
                                <th scope="col">Function</th>
                                <th scope="col" style="text-align: right;">Prompt</th>
                                <th scope="col" style="text-align: right;">Completion</th>
                                <th scope="col" style="text-align: right;">Total</th>
                                <th scope="col" style="text-align: right;">Context Limit</th>
                                <th scope="col" style="text-align: center;">Truncated</th>
                                <th scope="col">Response Time</th>
                            </tr>
                        </thead>
                        <tbody id="co-requests-table">
                            <!-- Populated dynamically -->
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Link Analytics Section -->
    <div class="ldr-card ldr-metrics-section" id="link-analytics-section" style="display: none;">
        <div class="card-header">
            <h2><i class="fas fa-link"></i> Link & Domain Analytics</h2>
        </div>
        <div class="ldr-card-content">
            <!-- Link Metrics Summary -->
            <div class="ldr-metrics-grid" id="link-metrics-summary">
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Total Links</div>
                    <div class="ldr-metric-value" id="total-links">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Unique Domains</div>
                    <div class="ldr-metric-value" id="unique-domains">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">Academic Sources</div>
                    <div class="ldr-metric-value" id="academic-sources">0</div>
                </div>
                <div class="ldr-metric-card">
                    <div class="ldr-metric-label">News Sources</div>
                    <div class="ldr-metric-value" id="news-sources">0</div>
                </div>
            </div>

            <!-- Domain Distribution -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-globe"></i> Top Domains Used</h3>
                <div id="domain-list" style="max-height: 400px; overflow-y: auto;">
                    <!-- Domain items will be populated here -->
                </div>
            </div>

            <!-- Recent Resources Sample -->
            <div style="margin-top: 2rem;">
                <h3><i class="fas fa-file-text"></i> Sample Resources</h3>
                <div id="resource-sample" style="max-height: 300px; overflow-y: auto;">
                    <!-- Resource samples will be populated here -->
                </div>
            </div>
        </div>
    </div>

    <!-- LLM Call Stack Analysis -->
    <div class="ldr-card ldr-metrics-section" id="call-stack-card" style="display: none; margin-top: 2rem;">
        <div class="card-header">
            <h2><i class="fas fa-code"></i> LLM Call Stack Analysis</h2>
        </div>
        <div class="ldr-card-content">
            <div id="call-stack-traces" style="max-height: 600px; overflow-y: auto;">
                <!-- Populated dynamically -->
            </div>
        </div>
    </div>

    </div> <!-- End details-content -->
</div>
{% endblock %}

{% block page_scripts %}
<script defer src="/static/js/components/context-overflow-shared.js"></script>
<script defer src="/static/js/components/details.js"></script>
{% endblock %}
