#include <iostream>
#include <vector>
#include <string>
#include <chrono>
 
// CSV utilities
std::vector<std::vector<std::string>> branchlessTokenize(const std::string &content);

// JSON utilities
std::string constructJSON(const std::vector<std::vector<std::string>> &data);

std::string csvToJson(const std::string &csvContent)
{
  // 1. Tokenize CSV content
  auto data = branchlessTokenize(csvContent);

  // 2. Construct JSON string
  return constructJSON(data);
}

int main()
{
  const int ITERATIONS = 1000;                                                                               // Number of times to run the conversion for accuracy
  std::string csvContent = "name,age,city\nAlice,30,New York\nBob,25,San Francisco\nCharlie,35,Los Angeles"; // Some sample CSV data
  std::string csvContent = "name,age,city\nAlice,30,New York\nBob,25,San Francisco\nCharlie,35,Los Angeles"; // Some sample CSV data

  // Expected JSON result
  std::string expectedJson = R"([
  {
    "name": "Alice",
    "age": "30",
    "city": "New York"
  },
  {
    "name": "Bob",
    "age": "25",
    "city": "San Francisco"
  },
  {
    "name": "Charlie",
    "age": "35",
    "city": "Los Angeles"
  }
])";

  // Warm-up run (optional but can help with JIT, caching, etc.)
  std::string jsonResult = csvToJson(csvContent);

  // Start timer
  auto start_time = std::chrono::high_resolution_clock::now();

  for (int i = 0; i < ITERATIONS; ++i)
  {
    jsonResult = csvToJson(csvContent);
  }

  // Stop timer and calculate average elapsed time
  auto end_time = std::chrono::high_resolution_clock::now();
  auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
  // Assert the produced JSON is correct
  assert(jsonResult == expectedJson);

  // Output results
  std::cout << "Average conversion took: " << static_cast<double>(elapsed) / ITERATIONS << "ms\n";
  std::cout << "Resulting JSON:\n"
            << jsonResult << "\n";

  return 0;
}
