{% extends "modern/base.html" %}

{% block title %}Graph Explorer - BLOOM LIMS{% endblock %}

{% block extra_css %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.28.1/cytoscape.min.js"></script>
<script src="https://unpkg.com/dagre@0.8.5/dist/dagre.min.js"></script>
<script src="https://unpkg.com/cytoscape-dagre@2.5.0/cytoscape-dagre.js"></script>
<style>
    #graph-layout {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        width: min(98vw, 1500px);
        margin: 0 auto;
    }
    #cy-container {
        display: flex;
        gap: 1rem;
        width: 100%;
        height: 82vh;
        min-height: 600px;
    }
    #cy {
        flex: 1 1 80%;
        width: 80%;
        height: 100%;
        background: var(--color-gray-900);
        border-radius: 8px;
        border: 1px solid var(--color-gray-600);
    }
    #sidebar {
        flex: 1 1 20%;
        width: 20%;
        min-width: 310px;
        max-width: 390px;
        display: flex;
        flex-direction: column;
        gap: 1rem;
        overflow-y: auto;
    }
    .control-panel {
        background: var(--color-gray-800);
        padding: 1rem;
        border-radius: 8px;
        border: 1px solid var(--color-gray-600);
    }
    .control-panel h3 {
        margin-bottom: 0.75rem;
        color: var(--color-gray-100);
        font-size: 1rem;
    }
    .control-group {
        margin-bottom: 0.75rem;
    }
    .control-group label {
        display: block;
        margin-bottom: 0.25rem;
        font-size: 0.85rem;
        color: var(--color-gray-400);
    }
    .control-group input,
    .control-group select {
        width: 100%;
        padding: 0.5rem;
        border-radius: 4px;
        border: 1px solid var(--color-gray-600);
        background: var(--color-gray-700);
        color: var(--color-gray-100);
    }
    .btn-row {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
    }
    .btn-row .btn {
        flex: 1 1 auto;
    }
    .range-row {
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 0.5rem;
        align-items: center;
    }
    .range-row span {
        color: var(--color-gray-300);
        font-size: 0.85rem;
        min-width: 2rem;
        text-align: right;
    }
    .type-filter-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.35rem 0.5rem;
        max-height: 160px;
        overflow-y: auto;
    }
    .type-filter-item {
        font-size: 0.78rem;
        color: var(--color-gray-300);
        display: flex;
        gap: 0.35rem;
        align-items: center;
        white-space: nowrap;
    }
    .subtype-buttons {
        display: flex;
        flex-wrap: wrap;
        gap: 0.35rem;
        max-height: 180px;
        overflow-y: auto;
    }
    .subtype-chip {
        border: 1px solid var(--color-gray-600);
        border-radius: 999px;
        background: var(--color-gray-700);
        color: var(--color-gray-300);
        font-size: 0.72rem;
        padding: 0.2rem 0.55rem;
        cursor: pointer;
    }
    .subtype-chip.active {
        background: var(--color-highlight);
        color: var(--color-white);
        border-color: var(--color-highlight);
    }
    #node-info {
        background: var(--color-gray-800);
        padding: 1rem;
        border-radius: 8px;
        border: 1px solid var(--color-gray-600);
        min-height: 220px;
        max-height: 36vh;
        overflow-y: auto;
    }
    #node-info h3 {
        margin-bottom: 0.75rem;
        color: var(--color-gray-100);
    }
    #node-info-content {
        font-size: 0.9rem;
    }
    #node-info-content p {
        margin-bottom: 0.5rem;
    }
    #node-info-content strong {
        color: var(--color-gray-400);
    }
    .details-grid {
        display: grid;
        grid-template-columns: minmax(130px, 220px) 1fr;
        gap: 0.35rem 0.75rem;
        margin-bottom: 0.8rem;
    }
    .detail-key {
        color: var(--color-gray-400);
        font-weight: 600;
        word-break: break-word;
    }
    .detail-value {
        color: var(--color-gray-100);
        word-break: break-word;
    }
    .details-section-title {
        font-size: 0.82rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--color-gray-300);
        margin: 0.8rem 0 0.35rem 0;
    }
    .json-block {
        margin: 0;
        padding: 0.65rem;
        border-radius: 6px;
        border: 1px solid var(--color-gray-600);
        background: var(--color-gray-900);
        font-size: 0.78rem;
        line-height: 1.35;
        overflow-x: auto;
        white-space: pre-wrap;
        word-break: break-word;
    }
    #details-cogs-output {
        margin-top: 0.55rem;
        font-size: 0.8rem;
        color: var(--color-gray-300);
        min-height: 1.2rem;
    }
    #graph-mode-status {
        margin-top: 0.6rem;
        padding: 0.5rem 0.6rem;
        border: 1px solid var(--color-gray-600);
        border-radius: 6px;
        font-size: 0.82rem;
        color: var(--color-gray-300);
        min-height: 2.1rem;
    }
    #graph-mode-status.ok {
        color: #c9ffd9;
        border-color: #2f7a4a;
    }
    #graph-mode-status.warn {
        color: #ffd7a8;
        border-color: #8d6b2f;
    }
    #graph-mode-status.error {
        color: #ffd0d0;
        border-color: #8b3d3d;
    }
    #relationship-dialog,
    #node-action-dialog,
    #help-dialog {
        border: 1px solid var(--color-gray-600);
        border-radius: 8px;
        background: var(--color-gray-800);
        color: var(--color-gray-100);
        width: min(92vw, 500px);
    }
    #relationship-dialog::backdrop,
    #node-action-dialog::backdrop,
    #help-dialog::backdrop {
        background: rgba(0, 0, 0, 0.45);
    }
    .dialog-title {
        margin: 0 0 0.65rem 0;
        color: var(--color-gray-100);
        font-size: 1rem;
    }
    .dialog-actions {
        display: flex;
        justify-content: flex-end;
        gap: 0.5rem;
        margin-top: 0.8rem;
    }
    .legacy-dialog-line {
        margin-bottom: 0.4rem;
        color: var(--color-gray-300);
        font-size: 0.85rem;
    }
    .legacy-dialog-link-row {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
        margin-top: 0.6rem;
    }
    .legacy-cogs-output {
        margin-top: 0.55rem;
        font-size: 0.82rem;
        color: var(--color-gray-300);
        min-height: 1.4rem;
    }
    .help-list {
        margin: 0;
        padding-left: 1.15rem;
        color: var(--color-gray-300);
        font-size: 0.84rem;
        line-height: 1.45;
    }
    .help-list li {
        margin-bottom: 0.35rem;
    }
    .legend {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-top: 0.5rem;
    }
    .legend-item {
        display: flex;
        align-items: center;
        gap: 0.25rem;
        font-size: 0.75rem;
    }
    .legend-color {
        width: 12px;
        height: 12px;
        border-radius: 2px;
    }
    .loading {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: var(--color-gray-400);
    }
    @media (max-width: 980px) {
        #cy-container {
            flex-direction: column;
            height: auto;
            min-height: 0;
        }
        #sidebar {
            width: 100%;
            min-width: 0;
            max-width: none;
            max-height: 42vh;
        }
        #cy {
            width: 100%;
            height: 60vh;
            min-height: 420px;
        }
        #node-info {
            max-height: 45vh;
        }
        .details-grid {
            grid-template-columns: 1fr;
        }
        .type-filter-grid {
            grid-template-columns: 1fr;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="page-header">
    <div>
        <h1 class="page-title"><i class="fas fa-project-diagram"></i> Graph Explorer</h1>
        <p class="page-subtitle">TapDB gesture-compatible Cytoscape graph view with BLOOM legacy tooling restored.</p>
    </div>
</div>

<div id="graph-layout">
    <div id="cy-container">
        <div id="cy"><div class="loading">Loading graph data...</div></div>
        <div id="sidebar">
            <div class="control-panel">
                <h3>Navigation</h3>
                <div class="control-group">
                    <label for="start-euid">Start EUID</label>
                    <input type="text" id="start-euid" value="{{ start_euid }}" placeholder="e.g., AY-1C">
                </div>
                <div class="control-group">
                    <label for="depth">Depth</label>
                    <input type="number" id="depth" value="{{ depth }}" min="1" max="10">
                </div>
                <div class="btn-row">
                    <button class="btn btn-primary btn-sm" onclick="loadGraph()">Load Graph</button>
                    <button class="btn btn-outline btn-sm" onclick="cy && cy.fit()">Fit</button>
                </div>
            </div>

            <div class="control-panel">
                <h3>Search & Find</h3>
                <div class="control-group">
                    <label for="search-query">Fuzzy Search (id/name/type/subtype)</label>
                    <input type="text" id="search-query" placeholder="Type to highlight matches">
                </div>
                <div class="control-group">
                    <label for="find-euid">Find EUID</label>
                    <input type="text" id="find-euid" placeholder="Exact EUID">
                </div>
                <div class="btn-row">
                    <button class="btn btn-outline btn-sm" onclick="applySearch(true)">Apply Search</button>
                    <button class="btn btn-outline btn-sm" onclick="findAndCenterByEuid()">Find</button>
                </div>
            </div>

            <div class="control-panel">
                <h3>Filters</h3>
                <div class="control-group">
                    <label for="transparency-slider">Filter by Connected Edge Count ≤</label>
                    <div class="range-row">
                        <input type="range" id="transparency-slider" min="0" max="20" value="1">
                        <span id="transparency-display">1</span>
                    </div>
                </div>
                <div class="control-group">
                    <label for="distance-slider">Relative Distance (0 = all)</label>
                    <div class="range-row">
                        <input type="range" id="distance-slider" min="0" max="15" value="0">
                        <span id="distance-display">0</span>
                    </div>
                </div>
                <div class="control-group">
                    <label>Type Visibility</label>
                    <div id="type-checkboxes" class="type-filter-grid">
                        <span style="color: var(--color-gray-400); font-size: 0.78rem;">Load graph to populate.</span>
                    </div>
                </div>
                <div class="control-group">
                    <label>Subtype Muting</label>
                    <div id="subtype-buttons" class="subtype-buttons">
                        <span style="color: var(--color-gray-400); font-size: 0.78rem;">Load graph to populate.</span>
                    </div>
                </div>
            </div>

            <div class="control-panel">
                <h3>Layout</h3>
                <div class="control-group">
                    <label for="layout-select">Layout Type</label>
                    <select id="layout-select" onchange="applyLayout()">
                        <option value="dagre">Dagre (Hierarchical)</option>
                        <option value="cose">CoSE (Force-directed)</option>
                        <option value="breadthfirst">Breadth First</option>
                        <option value="circle">Circle</option>
                        <option value="grid">Grid</option>
                    </select>
                </div>
            </div>

            <div class="control-panel">
                <h3>Graph Gestures</h3>
                <div style="font-size: 0.74rem; color: var(--color-gray-400); line-height: 1.42;">
                    {% if is_admin %}
                    <div><strong>D + right click</strong>: delete node or edge</div>
                    <div><strong>L + left click node</strong>: pick child, then click parent to create edge</div>
                    {% else %}
                    <div>Graph mutation shortcuts are admin-only.</div>
                    {% endif %}
                    <div><strong>3x left click node</strong>: child-wave glow (pink)</div>
                    <div><strong>3x right click node</strong>: parent-wave glow (aqua)</div>
                    <div><strong>P + left click node</strong>: COGS to produce (parents)</div>
                    <div><strong>C + left click node</strong>: COGS of children</div>
                    <div><strong>N + left click node</strong>: neighborhood expansion</div>
                    <div><strong>I + left click node</strong>: legacy node action popup</div>
                </div>
                <div class="btn-row" style="margin-top: 0.65rem;">
                    <button class="btn btn-outline btn-sm" onclick="openHelpDialog()">Help</button>
                </div>
                <div id="graph-mode-status">Ready.</div>
            </div>

            <div class="control-panel">
                <h3>Legend</h3>
                <div class="legend" id="legend-items">
                    <span style="color: var(--color-gray-400); font-size: 0.85rem;">Loading...</span>
                </div>
            </div>
        </div>
    </div>

    <div id="node-info">
        <h3>Details</h3>
        <div id="node-info-content">
            <p style="color: var(--color-gray-400);">Click a node or edge to see details</p>
        </div>
        <div id="details-cogs-output"></div>
    </div>
</div>

<dialog id="relationship-dialog">
    <h3 class="dialog-title">Create Lineage Edge</h3>
    <p id="relationship-dialog-context"></p>
    <label for="relationship-type-select" style="font-size: 0.85rem; color: var(--color-gray-400);">Relationship Type</label>
    <select id="relationship-type-select">
        <option value="contains">contains</option>
        <option value="derived_from">derived_from</option>
        <option value="references">references</option>
        <option value="depends_on">depends_on</option>
        <option value="produces">produces</option>
        <option value="consumes">consumes</option>
        <option value="generic" selected>generic</option>
        <option value="related">related</option>
    </select>
    <div class="dialog-actions">
        <button id="relationship-dialog-cancel" type="button" class="btn btn-outline btn-sm">Cancel</button>
        <button id="relationship-dialog-create" type="button" class="btn btn-primary btn-sm">Create Edge</button>
    </div>
</dialog>

<dialog id="node-action-dialog">
    <h3 class="dialog-title">Node Actions</h3>
    <div class="legacy-dialog-line"><strong>EUID:</strong> <span id="node-action-euid">-</span></div>
    <div class="legacy-dialog-line"><strong>Name:</strong> <span id="node-action-name">-</span></div>
    <div class="legacy-dialog-link-row">
        <button type="button" class="btn btn-outline btn-sm" onclick="runLegacyDialogCogs('parents')">COGS To Produce</button>
        <button type="button" class="btn btn-outline btn-sm" onclick="runLegacyDialogCogs('children')">COGS Of Children</button>
        <button type="button" class="btn btn-outline btn-sm" onclick="openLegacyDialogObject()">Open EUID Details</button>
        <button type="button" class="btn btn-outline btn-sm" onclick="centerLegacyDialogNode()">Center Node</button>
    </div>
    <div class="legacy-cogs-output" id="node-action-cogs-output"></div>
    <div class="dialog-actions">
        <button type="button" class="btn btn-outline btn-sm" onclick="closeNodeActionDialog()">Close</button>
    </div>
</dialog>

<dialog id="help-dialog">
    <h3 class="dialog-title">Graph Help</h3>
    <ul class="help-list">
        <li><strong>D + right click</strong>: delete node/edge (admin only).</li>
        <li><strong>L + left click node</strong>: select child, then click parent to create edge (admin only).</li>
        <li><strong>3x left click node</strong>: child-wave glow traversal.</li>
        <li><strong>3x right click node</strong>: parent-wave glow traversal.</li>
        <li><strong>P + left click node</strong>: calculate COGS to produce.</li>
        <li><strong>C + left click node</strong>: calculate COGS of children.</li>
        <li><strong>N + left click node</strong>: neighborhood highlight; repeated clicks expand depth.</li>
        <li><strong>I + left click node</strong>: open legacy-style node action popup.</li>
        <li><strong>Esc</strong>: clear pending lineage selection.</li>
    </ul>
    <div class="dialog-actions">
        <button type="button" class="btn btn-outline btn-sm" onclick="closeHelpDialog()">Close</button>
    </div>
</dialog>
{% endblock %}

{% block extra_js %}
<script>
window.BloomGraphConfig = {
    canEdit: {{ "true" if is_admin else "false" }},
    graphPath: "/dindex2",
    defaultMergeRef: {{ merge_ref | tojson }}
};
</script>
<script src="/static/js/graph.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
    initGraphPage();
});
</script>
{% endblock %}
