#pragma once
// data_table_grid — render del grid de datos: ImGui::BeginTable, headers con sort,
// freeze cols, drag-drop de columnas, renderers declarativos (Badge/Progress/
// Duration/Icon/Button/Dots/CategoricalChip/ColorScale), color rules per cell,
// selection (Ctrl+C TSV), stats overlay, cell popup.
//
// Sub-funcion extraida de modules/data_table/data_table.cpp (issue 0107c).
// Rangos del fuente original:
//   - draw_cell_custom (declarative renderers): lineas 376-655
//   - Grid setup + header row + cell loop (stage 0): lineas 3436-3765
//   - Grid setup + cell loop (stage > 0): lineas 4040-4311
//   - Ctrl+C TSV copy: lineas 3725-3766
//   - Stats overlay en encabezados: lineas 4160-4226
//
// Dependencias: data_table_types.h, data_table_color_rules.h, imgui.h.

#include "core/data_table_types.h"
#include "imgui.h"
#include <vector>
#include <string>

namespace data_table {

// ---------------------------------------------------------------------------
// draw_cell_custom — renderer declarativo por ColumnSpec.renderer.
// Soporta: Text (fallback), Badge, Progress, Duration, Icon, Button, Dots,
//          CategoricalChip, ColorScale.
// Emite TableEvent (ButtonClick, RowDoubleClick) en events_out si no-null.
// Llamar dentro del contexto de un ImGui::BeginTable activo.
// ---------------------------------------------------------------------------
void draw_cell_custom(const ColumnSpec& spec, const char* value,
                      int row, int col,
                      std::vector<TableEvent>* events_out);

// ---------------------------------------------------------------------------
// render_grid_stage0 — renderiza el grid para el path stage==0 (datos crudos
// + derivados via Lua). Incluye: BeginTable, headers (sort click, drag-drop,
// header menu via draw_header_menu), cell loop con draw_cell_custom,
// apply_color_rules_for_cell, selection rect, Ctrl+C TSV copy.
//
// Parametros:
//   id           — ID ImGui unico para el BeginTable.
//   st           — State mutable (sort, selection, color_rules, col_order, ...).
//   cells        — cells originales row-major [rows * orig_cols].
//   row_count    — numero de filas originales.
//   orig_cols    — numero de columnas originales (sin derived).
//   eff_cols     — numero de columnas efectivas (orig + derived).
//   eff_headers  — punteros a los nombres de columna efectivos (size eff_cols).
//   eff_types    — tipos de columna efectivos (size eff_cols).
//   src_for_eff  — mapeo eff col -> src col en orig (size eff_cols).
//   visible_rows — filas visibles tras filtrado (indices en [0, row_count)).
//   events_out   — si no null, recibe TableEvent de esta frame.
// ---------------------------------------------------------------------------
void render_grid_stage0(const char* id,
                        State& st,
                        const char* const* cells,
                        int row_count, int orig_cols, int eff_cols,
                        const char* const* eff_headers,
                        const ColumnType* eff_types,
                        const int* src_for_eff,
                        const std::vector<int>& visible_rows,
                        const TableInput& main_t,
                        std::vector<TableEvent>* events_out);

// ---------------------------------------------------------------------------
// render_grid_stage_n — renderiza el grid para el path stage > 0 (datos
// materializados del compute_stage chain). Similar a stage0 pero opera sobre
// cur_cells/cur_rows/cur_cols_n ya materializados.
//
// input_headers_active — headers del INPUT del stage activo (para drill popup).
// n_breakouts          — numero de breakout cols en el stage activo.
// ---------------------------------------------------------------------------
void render_grid_stage_n(const char* id,
                         State& st,
                         const char* const* cur_cells,
                         int cur_rows, int cur_cols_n,
                         const std::vector<std::string>& cur_headers,
                         const std::vector<ColumnType>& cur_types,
                         const std::vector<std::string>& input_headers_active,
                         int n_breakouts,
                         const TableInput& main_t,
                         std::vector<TableEvent>* events_out);

} // namespace data_table
