<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Route {{ route.route_number }} Details</title>
    <!-- Link to the external stylesheet -->
    <link rel="stylesheet" href="../css/style.css">
    <!-- Cytoscape.js library, loaded from a shared scripts directory -->
    <script src="../scripts/cytoscape.min.js"></script>
</head>
<body>

<header>
    <div class="container">
        <h1>Route {{ route.route_number }}</h1>
        <p>Job ID: {{ job_id }}</p>
    </div>
</header>

<div class="container">
    <nav class="navigation">
        <a href="./index.html" class="back-link">&larr; Back to Job Overview</a>
        <div class="nav-links">
            <a href="./route_{{ route.route_number - 1 }}.html" class="nav-button {% if route.route_number == 1 %}disabled{% endif %}">&larr; Previous</a>
            <span>Route {{ route.route_number }} of {{ total_routes }}</span>
            <a href="./route_{{ route.route_number + 1 }}.html" class="nav-button {% if route.route_number == total_routes %}disabled{% endif %}">Next &rarr;</a>
        </div>
    </nav>

    <main>
        <section>
            <h2>Synthesis Tree</h2>
            <div id="cy"></div>
        </section>
    </main>

    <footer>
        <p>Generated by Pending AI CLI</p>
    </footer>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var elements = {{ cytoscape_elements | tojson | safe }};

        elements.forEach(function(el) {
            if (el.group === 'nodes' && el.data.svg) {
                el.data.svg = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(el.data.svg);
            }
        });

        var cy = cytoscape({
            container: document.getElementById('cy'),
            elements: elements,
            style: [
                {
                    selector: 'node',
                    style: {
                        'shape': 'rectangle',
                        'background-image': 'data(svg)',
                        'background-fit': 'none',
                        'background-color': 'white',
                        'width': 'data(width)',
                        'height': 'data(height)',
                        'border-width': 2,
                        'border-color': '#e5e7eb',
                    }
                },
                {
                    selector: 'edge',
                    style: {
                        'width': 2,
                        'line-color': '#d1d5db',
                        'source-arrow-color': '#d1d5db',
                        'source-arrow-shape': 'triangle',
                        'curve-style': 'bezier'
                    }
                }
            ],
        });

        cy.ready(function() {
            // Define the layout object.
            var layout = cy.layout({
                name: 'breadthfirst',
                directed: true,
                fit: false,
                padding: 15,
                spacingFactor: 0.85,
                grid: true
            });

            layout.promiseOn('layoutstop').then(function() {
                const bounds = cy.elements().boundingBox();
                const containerHeight = cy.height();
                const containerWidth = cy.width();

                // Check if the graph's dimensions exceed the container's dimensions.
                if (bounds.h > containerHeight || bounds.w > containerWidth) {
                    // If it overflows, fit the entire graph into the view with padding.
                    cy.fit(null, 30); // Use null to fit all elements.
                } else {
                    // --- CHANGE 2: If the graph fits, center it in the viewport. ---
                    cy.center();
                }
            });

            // Explicitly run the layout.
            layout.run();
        });
    });
</script>

</body>
</html>
