{% extends "base.html" %}

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

{% block extra_head %}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
    .metrics-section {
        margin-top: 2rem;
    }

    .metrics-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
        margin-bottom: 2rem;
    }

    .metric-card {
        padding: 1rem;
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 0.5rem;
        text-align: center;
    }

    .metric-value {
        font-size: 1.5rem;
        font-weight: bold;
        color: var(--primary-color);
        margin: 0.5rem 0;
    }

    .metric-label {
        color: var(--text-secondary);
        font-size: 0.875rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }

    .chart-container {
        position: relative;
        height: 300px;
        margin-top: 1rem;
    }

    .phase-stats {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: 1rem;
        margin-top: 1rem;
    }

    .phase-stat-item {
        padding: 0.75rem;
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 0.375rem;
        text-align: center;
    }

    .phase-name {
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 0.25rem;
    }

    .phase-tokens {
        color: var(--primary-color);
        font-weight: 500;
    }

    .phase-calls {
        color: var(--text-secondary);
        font-size: 0.875rem;
    }

    .search-engine-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.75rem;
        margin-bottom: 0.5rem;
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 0.375rem;
        transition: background-color 0.2s;
    }

    .search-engine-item:hover {
        background-color: var(--border-color);
    }

    .search-engine-info {
        display: flex;
        flex-direction: column;
    }

    .search-engine-name {
        font-weight: 600;
        color: var(--text-primary);
    }

    .search-engine-stats {
        font-size: 0.875rem;
        color: var(--text-secondary);
        margin-top: 0.25rem;
    }

    .search-timeline-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.75rem;
        margin-bottom: 0.5rem;
        background: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 0.375rem;
        border-left: 3px solid var(--primary-color);
    }

    .search-timeline-query {
        font-weight: 500;
        color: var(--text-primary);
        margin-bottom: 0.25rem;
    }

    .search-timeline-meta {
        font-size: 0.875rem;
        color: var(--text-secondary);
    }

    .search-timeline-results {
        text-align: right;
        font-size: 0.875rem;
    }

    .search-status-success {
        color: #22c55e;
    }

    .search-status-error {
        color: #ef4444;
    }

    .loading-spinner {
        text-align: center;
        padding: 3rem;
    }

    .error-message {
        text-align: center;
        padding: 2rem;
        color: var(--error-color, #dc3545);
    }

    .charts-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 2rem;
        margin-bottom: 2rem;
    }

    @media (max-width: 768px) {
        .charts-grid {
            grid-template-columns: 1fr;
            gap: 1.5rem;
        }
    }
</style>
{% endblock %}

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

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

    <div id="error" class="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="card">
        <div class="card-content">
            <div class="research-metadata">
                <div class="metadata-item">
                    <span class="metadata-label">Query:</span>
                    <span id="research-query" class="metadata-value"></span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Mode:</span>
                    <span id="research-mode" class="metadata-value"></span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Date:</span>
                    <span id="research-date" class="metadata-value"></span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Model:</span>
                    <span id="model-used" class="metadata-value"></span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Strategy:</span>
                    <span id="research-strategy" class="metadata-value">-</span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Total Cost:</span>
                    <span id="total-cost" class="metadata-value">$0.00</span>
                </div>
                <div class="metadata-item">
                    <span class="metadata-label">Progress:</span>
                    <div class="detail-progress-container">
                        <div class="detail-progress-bar">
                            <div class="detail-progress-fill" id="detail-progress-fill"></div>
                        </div>
                        <span id="detail-progress-percentage">0%</span>
                    </div>
                </div>
            </div>

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

    <!-- Token Metrics Section -->
    <div class="card 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="card-content">
            <!-- Metrics Summary -->
            <div class="metrics-grid" id="metrics-summary">
                <div class="metric-card">
                    <div class="metric-label">Total Tokens</div>
                    <div class="metric-value" id="total-tokens">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Input Tokens</div>
                    <div class="metric-value" id="prompt-tokens">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Output Tokens</div>
                    <div class="metric-value" id="completion-tokens">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">LLM Calls</div>
                    <div class="metric-value" id="llm-calls">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Avg Response Time</div>
                    <div class="metric-value" id="avg-response-time">0ms</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Success Rate</div>
                    <div class="metric-value" id="success-rate">0%</div>
                </div>
            </div>

            <!-- Charts Section -->
            <div class="charts-grid">
                <!-- Token Consumption Chart -->
                <div>
                    <h3><i class="fas fa-clock"></i> Token Consumption Over Time</h3>
                    <div class="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="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="phase-stats" id="phase-breakdown">
                    <!-- Populated dynamically -->
                </div>
            </div>

            <!-- Call Stack Analysis -->
            <div style="margin-top: 2rem;" id="call-stack-section">
                <h3><i class="fas fa-code"></i> LLM Call Stack Analysis</h3>
                <div style="margin-top: 1rem;">
                    <div id="call-stack-traces" style="max-height: 400px; overflow-y: auto;">
                        <!-- Populated dynamically -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Search Analytics Section -->
    <div class="card 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="card-content">
            <!-- Search Summary -->
            <div class="metrics-grid" id="search-summary">
                <div class="metric-card">
                    <div class="metric-label">Total Searches</div>
                    <div class="metric-value" id="total-searches">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Total Results</div>
                    <div class="metric-value" id="total-search-results">0</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Avg Response Time</div>
                    <div class="metric-value" id="avg-search-response-time">0ms</div>
                </div>
                <div class="metric-card">
                    <div class="metric-label">Search Success Rate</div>
                    <div class="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 Engine Performance</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>
    </div> <!-- End details-content -->
</div>
{% endblock %}

{% block templates %}
{% endblock %}

{% block page_scripts %}
<script src="{{ url_for('research.serve_static', path='js/components/details.js') }}"></script>
{% endblock %}
