#pragma once
#include "gfx/dag_types.h"
#include <string>
#include <vector>

struct sqlite3; // fwd decl

namespace fn::gfx {

// Persistent record for a user-saved generator (Code → DAG Gen).
struct GeneratorRecord {
    std::string id;                       // snake_case unique
    std::string label;                    // visible name in palette
    std::string description;              // free text
    std::string source_glsl;              // original Code (with fragColor, uniforms)
    std::string body_glsl;                // translated DAG Gen body (with return)
    int         param_count = 0;          // number of floats in u_params block
    std::vector<float>       param_defaults;
    std::vector<std::string> param_names;
    std::vector<DagControl>  controls;
    std::string tags;                     // CSV
    std::string created_at;               // ISO-8601 (set by save if empty)
    std::string updated_at;               // ISO-8601 (set by save)
};

// Open the database at `path` (created if missing). ":memory:" is supported for tests.
// Returns true on success. Idempotent: calling open() again with the same path is a no-op.
bool shaderlab_db_open(const std::string& path);

// Close the active connection (no-op if not open).
void shaderlab_db_close();

// Insert or replace a generator. Sets gen.created_at if empty and updated_at unconditionally
// (caller's struct is updated in place too). Returns false on SQL error.
bool shaderlab_db_save_generator(GeneratorRecord& gen, std::string* err = nullptr);

// Load all generators ordered by label.
std::vector<GeneratorRecord> shaderlab_db_list_generators();

// Load a single generator by id. Returns false if not found.
bool shaderlab_db_get_generator(const std::string& id, GeneratorRecord& out);

// Remove a generator by id. Returns true if a row was deleted.
bool shaderlab_db_delete_generator(const std::string& id);

// Returns the underlying sqlite3* handle (or nullptr if not open).
// For composing additional tables (e.g. ui_layouts) on the same connection.
sqlite3* shaderlab_db_handle();

} // namespace fn::gfx
