#pragma once
#include "imgui.h"

struct GraphData;
struct GraphRenderer;
struct SpatialHash;

// Persistent state for graph_viewport widget. Create one per viewport and keep
// alive across frames.
struct GraphViewportState {
    // Camera
    float cam_x = 0.0f, cam_y = 0.0f;
    float zoom     = 1.0f;
    float zoom_min = 0.01f, zoom_max = 50.0f;

    // Interaction result (read after calling graph_viewport each frame)
    int  hovered_node  = -1;   // node index under cursor, -1 if none
    int  selected_node = -1;   // last clicked node index, -1 if none
    bool is_dragging   = false;

    // Layout
    bool  layout_running = true;  // animate force layout each frame
    float layout_energy  = 0.0f; // kinetic energy from last step

    // Internal — managed by graph_viewport / graph_viewport_destroy
    GraphRenderer* renderer  = nullptr;
    SpatialHash*   spatial   = nullptr;
    bool           initialized = false;

    // Widget pixel dimensions tracked for resize detection
    int render_w = 0, render_h = 0;

    // Node being dragged (-1 = none)
    int drag_node = -1;
};

// Main viewport widget. Call every ImGui frame.
// id:    unique ImGui widget identifier
// graph: mutable graph data (node positions updated on drag)
// state: persistent state (camera, selection, GPU renderer); must outlive frames
// size:  widget size in pixels — ImVec2(0,0) uses all available space
// Returns true if any user interaction occurred (hover, click, drag, zoom).
bool graph_viewport(const char* id, GraphData& graph, GraphViewportState& state,
                    ImVec2 size = ImVec2(0.0f, 0.0f));

// Release GPU resources. Call once when done with the viewport.
void graph_viewport_destroy(GraphViewportState& state);

// Fit camera to current graph bounds with 10% padding.
void graph_viewport_fit(GraphData& graph, GraphViewportState& state);
