#pragma once

#include "commit_request.hpp"
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

/**
 * @brief Reference implementation of CommitRequest parser using nlohmann/json.
 *
 * This parser serves as a reference implementation for testing and validation.
 * It implements the same parsing logic as JsonCommitRequestParser but uses
 * the well-tested nlohmann/json library for simplicity and correctness.
 */
class NlohmannReferenceParser {
public:
  enum class ParseResult { Success, ParseError, ValidationError };

  /**
   * @brief Parse a JSON string into a CommitRequest.
   * @param request The request object to populate
   * @param json_str The JSON string to parse
   * @return ParseResult indicating success or failure type
   */
  ParseResult parse(CommitRequest &request, const std::string &json_str);

  /**
   * @brief Get the last error message if parsing failed.
   * @return Error message string, empty if no error
   */
  const std::string &get_error() const { return error_message_; }

private:
  std::string error_message_;

  bool parse_preconditions(CommitRequest &request,
                           const nlohmann::json &preconditions_array);
  bool parse_operations(CommitRequest &request,
                        const nlohmann::json &operations_array);
  std::string decode_base64(const std::string &base64_str);
  Precondition::Type parse_precondition_type(const std::string &type_str);
  Operation::Type parse_operation_type(const std::string &type_str);
};