// compute_pipeline — Pure TQL pipeline execution.
// Chains compute_stage calls: output of stage N becomes input of stage N+1.
// Promoted from cpp/apps/primitives_gallery/playground/tables/ (issue 0081-B).
#include "core/compute_pipeline.h"
#include <vector>
#include <string>

namespace data_table {

StageOutput compute_pipeline(const char* const* in_cells, int in_rows, int in_cols,
                             const std::vector<std::string>& in_headers,
                             const std::vector<ColumnType>&  in_types,
                             const std::vector<Stage>&       stages)
{
    if (stages.empty()) {
        // Passthrough: wrap raw input.
        Stage empty_stage;
        return compute_stage(in_cells, in_rows, in_cols, in_headers, in_types, empty_stage);
    }

    // Run first stage against raw input.
    StageOutput prev = compute_stage(in_cells, in_rows, in_cols, in_headers, in_types, stages[0]);

    // Chain remaining stages: each consumes the previous StageOutput.
    for (size_t i = 1; i < stages.size(); ++i) {
        StageOutput next = compute_stage(
            prev.cells.empty() ? nullptr : prev.cells.data(),
            prev.rows,
            prev.cols,
            prev.headers,
            prev.types,
            stages[i]);
        prev = std::move(next);
    }

    return prev;
}

} // namespace data_table
