#include "sqlite_impl.h"

#include <algorithm>
#include <cctype>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <limits>

#include "aggregate_function.h"
#include "pending_exception.h"
#include "shims/sqlite_errors.h"

// Database-aware error handling functions to avoid forward declaration issues
namespace {
using photostructure::sqlite::CanRunJavaScript;
using photostructure::sqlite::ClearPendingException;

// SharedArrayBuffer is neither an ArrayBufferView nor a Napi ArrayBuffer, and
// node-addon-api's IsSharedArrayBuffer() is gated behind the experimental
// NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER, so we detect it ourselves.
//
// This reads Symbol.toStringTag rather than doing an `instanceof` against the
// global SharedArrayBuffer: `instanceof` is realm-sensitive, and callers can
// legitimately live in another realm (Jest runs each test file in its own vm
// context, worker threads have their own globals), where the constructor
// identity differs and the check silently fails.
bool IsSharedArrayBufferValue(Napi::Env env, Napi::Value value) {
  if (!value.IsObject() || value.IsArrayBuffer()) {
    return false;
  }
  // NAPI_CPP_EXCEPTIONS is enabled, so MaybeOrValue<Symbol> is a plain Symbol.
  Napi::Symbol tag_key = Napi::Symbol::WellKnown(env, "toStringTag");
  Napi::Value tag = value.As<Napi::Object>().Get(tag_key);
  return tag.IsString() &&
         tag.As<Napi::String>().Utf8Value() == "SharedArrayBuffer";
}

// True when a callback's JavaScript exception takes the place of the SQLite
// error, which the callback marks with SetIgnoreNextSQLiteError(): a deferred
// authorizer exception is rethrown here, and any other is already pending. The
// caller must then not throw.
bool ExceptionReplacesSqliteError(Napi::Env env,
                                  photostructure::sqlite::DatabaseSync *db) {
  if (db == nullptr || !db->ShouldIgnoreSQLiteError()) {
    return false;
  }
  db->SetIgnoreNextSQLiteError(false);
  if (db->HasDeferredAuthorizerException()) {
    db->RethrowDeferredAuthorizerException();
    return true;
  }
  // Suppression that swallows no pending exception would also swallow the
  // SQLite error, reporting a failed statement as a success.
  return env.IsExceptionPending();
}

inline void ThrowErrSqliteErrorWithDb(Napi::Env env,
                                      photostructure::sqlite::DatabaseSync *db,
                                      const char *message = nullptr) {
  if (ExceptionReplacesSqliteError(env, db)) {
    return;
  }

  const char *msg = (message != nullptr) ? message : "SQLite error";
  Napi::Error error = node::NewErrorFromSqliteText(env, msg);
  if (!error.IsEmpty()) {
    error.ThrowAsJavaScriptException();
  }
}

inline void ThrowEnhancedSqliteErrorWithDB(
    Napi::Env env, photostructure::sqlite::DatabaseSync *db_sync, sqlite3 *db,
    int /*sqlite_code*/, const std::string &message) {
  if (ExceptionReplacesSqliteError(env, db_sync)) {
    return;
  }

  // A pending exception here is not the statement's own error, which sets the
  // ignore flag checked above. It is what a virtual table iterator's return()
  // threw while SQLite closed the cursors of the failing statement. node:sqlite
  // reports the SQLite error in its place; creating our error object with it
  // still pending would fail and surface the cleanup error instead.
  ClearPendingException(env);

  // Use extended error code from db handle (e.g., 1555 for
  // SQLITE_CONSTRAINT_PRIMARYKEY) instead of basic code (e.g., 19 for
  // SQLITE_CONSTRAINT) to match Node.js behavior
  int extended_code = db ? sqlite3_extended_errcode(db) : SQLITE_ERROR;
  node::ThrowEnhancedSqliteError(env, db, extended_code, message);
}
} // namespace
#include "sqlite_exception.h"
#include "user_function.h"
#include "virtual_table.h"

namespace photostructure::sqlite {

// JavaScript safe integer limits (2^53 - 1)
constexpr int64_t JS_MAX_SAFE_INTEGER = 9007199254740991LL;
constexpr int64_t JS_MIN_SAFE_INTEGER = -9007199254740991LL;

Napi::Value IntegerToValue(Napi::Env env, sqlite3_int64 value,
                           bool use_big_ints) {
  if (use_big_ints) {
    return Napi::BigInt::New(env, static_cast<int64_t>(value));
  }

  if (value > JS_MAX_SAFE_INTEGER || value < JS_MIN_SAFE_INTEGER) {
    char error_msg[128];
    snprintf(error_msg, sizeof(error_msg),
             "Value is too large to be represented as a JavaScript "
             "number: %" PRId64,
             static_cast<int64_t>(value));
    node::THROW_ERR_OUT_OF_RANGE(env, error_msg);
    return env.Undefined();
  }

  return Napi::Number::New(env, static_cast<double>(value));
}

// Path validation function implementation
std::optional<std::string> ValidateDatabasePath(Napi::Env env, Napi::Value path,
                                                const std::string &field_name) {
  auto has_null_bytes = [](const std::string &str) {
    return str.find('\0') != std::string::npos;
  };

  if (path.IsString()) {
    std::string location = path.As<Napi::String>().Utf8Value();
    if (!has_null_bytes(location)) {
      return location;
    }
  } else if (path.IsBuffer()) {
    const auto buffer = path.As<Napi::Buffer<uint8_t>>();
    const size_t length = buffer.Length();
    const uint8_t *data = buffer.Data();

    // Check for null bytes in buffer
    if (std::find(data, data + length, 0) == data + length) {
      return std::string(reinterpret_cast<const char *>(data), length);
    }
  } else if (path.IsObject()) {
    Napi::Object url = path.As<Napi::Object>();

    // Check if it's a URL object with href property
    if (url.Has("href")) {
      Napi::Value href = url.Get("href");
      if (href.IsString()) {
        std::string location = href.As<Napi::String>().Utf8Value();
        if (!has_null_bytes(location)) {
          // node:sqlite rejects an href that does not parse as a URL before
          // checking its scheme (ada::can_parse). URL.canParse() runs the
          // same WHATWG URL parser.
          Napi::Object url_class = env.Global().Get("URL").As<Napi::Object>();
          if (!url_class.Get("canParse")
                   .As<Napi::Function>()
                   .Call(url_class, {href})
                   .ToBoolean()
                   .Value()) {
            node::THROW_ERR_INVALID_URL(env, "Invalid URL");
            return std::nullopt;
          }

          // Check if it's a file:// URL
          if (location.compare(0, 7, "file://") == 0) {
            // Check if URL has query parameters - if so, return full URI
            // for SQLite URI mode (e.g., file:///path/to/db?mode=ro)
            size_t query_pos = location.find('?');
            if (query_pos != std::string::npos) {
              // Return full URI for SQLite to parse with SQLITE_OPEN_URI
              return location;
            }

            // Convert file:// URL to file path with proper validation
            std::string file_path = location.substr(7);

            // Enhanced URL decoding with security checks
            std::string decoded_path;
            decoded_path.reserve(file_path.length());

            // Maximum path length check (platform-specific, but 4096 is
            // reasonable)
            const size_t MAX_PATH_LENGTH = 4096;
            if (file_path.length() > MAX_PATH_LENGTH) {
              node::THROW_ERR_INVALID_ARG_TYPE(
                  env,
                  ("The \"" + field_name + "\" path is too long.").c_str());
              return std::nullopt;
            }

            // URL decode with multiple passes to prevent double-encoding bypass
            int decode_passes = 0;
            const int MAX_DECODE_PASSES = 5; // Prevent infinite decoding loops
            std::string current_path = file_path;
            std::string next_path;

            while (decode_passes < MAX_DECODE_PASSES) {
              bool found_encoding = false;
              next_path.clear();
              next_path.reserve(current_path.length());

              for (size_t i = 0; i < current_path.length(); ++i) {
                if (current_path[i] == '%' && i + 2 < current_path.length()) {
                  // Validate hex characters
                  if (std::isxdigit(current_path[i + 1]) &&
                      std::isxdigit(current_path[i + 2])) {
                    char hex_str[3] = {current_path[i + 1], current_path[i + 2],
                                       '\0'};
                    long val = std::strtol(hex_str, nullptr, 16);

                    // Special handling for control characters and dangerous
                    // sequences
                    if (val == 0) {
                      node::THROW_ERR_INVALID_ARG_TYPE(
                          env, ("The \"" + field_name +
                                "\" contains encoded null bytes.")
                                   .c_str());
                      return std::nullopt;
                    }

                    next_path += static_cast<char>(val);
                    i += 2;
                    found_encoding = true;
                  } else {
                    // Invalid hex sequence, reject
                    node::THROW_ERR_INVALID_ARG_TYPE(
                        env, ("The \"" + field_name +
                              "\" contains invalid percent encoding.")
                                 .c_str());
                    return std::nullopt;
                  }
                } else {
                  next_path += current_path[i];
                }
              }

              if (!found_encoding) {
                decoded_path = current_path;
                break;
              }

              current_path = next_path;
              decode_passes++;
            }

            if (decode_passes >= MAX_DECODE_PASSES) {
              node::THROW_ERR_INVALID_ARG_TYPE(
                  env, ("The \"" + field_name +
                        "\" contains too many levels of percent encoding.")
                           .c_str());
              return std::nullopt;
            }

            // Security: Check for null bytes after all decoding
            if (has_null_bytes(decoded_path)) {
              node::THROW_ERR_INVALID_ARG_TYPE(
                  env, ("The \"" + field_name +
                        "\" argument contains null bytes after URL decoding.")
                           .c_str());
              return std::nullopt;
            }

            // Security: Normalize path components to detect traversal attempts
            // This includes various representations of ".."
            std::vector<std::string> dangerous_patterns = {
                "..", "/..", "../", "\\..", "..\\",
                // Windows alternate stream syntax (but allow single colon for
                // drive letters)
                "::", "::$",
                // Zero-width characters that might hide dangerous sequences
                "\u200B", "\u200C", "\u200D", "\uFEFF"};

            // Check each component after splitting by directory separators
            std::string normalized_path = decoded_path;
            std::replace(normalized_path.begin(), normalized_path.end(), '\\',
                         '/');

            // Split path and check each component
            size_t start = 0;
            size_t end = normalized_path.find('/');

            while (end != std::string::npos) {
              std::string component =
                  normalized_path.substr(start, end - start);

              // Check for dangerous patterns in component
              if (component == "..") {
                // Always reject ..
                node::THROW_ERR_INVALID_ARG_TYPE(
                    env, ("The \"" + field_name +
                          "\" argument contains path traversal sequences.")
                             .c_str());
                return std::nullopt;
              }

              // Check for other dangerous patterns
              for (const auto &pattern : dangerous_patterns) {
                if (component.find(pattern) != std::string::npos) {
                  node::THROW_ERR_INVALID_ARG_TYPE(
                      env, ("The \"" + field_name +
                            "\" argument contains dangerous sequences.")
                               .c_str());
                  return std::nullopt;
                }
              }

              start = end + 1;
              end = normalized_path.find('/', start);
            }

            // Check last component
            if (start < normalized_path.length()) {
              std::string component = normalized_path.substr(start);
              if (component == "..") {
                // Always reject ..
                node::THROW_ERR_INVALID_ARG_TYPE(
                    env, ("The \"" + field_name +
                          "\" argument contains path traversal sequences.")
                             .c_str());
                return std::nullopt;
              }

              // Check for other dangerous patterns
              for (const auto &pattern : dangerous_patterns) {
                if (component.find(pattern) != std::string::npos) {
                  node::THROW_ERR_INVALID_ARG_TYPE(
                      env, ("The \"" + field_name +
                            "\" argument contains dangerous sequences.")
                               .c_str());
                  return std::nullopt;
                }
              }
            }

            return decoded_path;
          } else {
            node::THROW_ERR_INVALID_URL_SCHEME(env);
            return std::nullopt;
          }
        }
      }
    }
  }

  node::THROW_ERR_INVALID_ARG_TYPE(env,
                                   ("The \"" + field_name +
                                    "\" argument must be a string, "
                                    "Uint8Array, or URL without null bytes.")
                                       .c_str());
  return std::nullopt;
}

// Note: Static constructors removed to fix worker thread issues
// Constructors are now stored in per-instance AddonData

// Forward declarations for addon data access
extern AddonData *GetAddonData(napi_env env);

// Helper to create an object with null prototype (matches Node.js behavior)
// Node.js uses Object::New(isolate, Null(isolate), ...) but N-API doesn't have
// this capability, so we use cached Object.create(null) instead.
Napi::Object CreateObjectWithNullPrototype(Napi::Env env) {
  AddonData *addon_data = GetAddonData(env);
  if (addon_data && !addon_data->objectCreateFn.IsEmpty()) {
    // Call Object.create(null) to create object with null prototype
    return addon_data->objectCreateFn.Value()
        .Call({env.Null()})
        .As<Napi::Object>();
  }
  // Fallback to regular object if Object.create not available
  return Napi::Object::New(env);
}

bool SqliteTextToValue(Napi::Env env, const char *text, size_t length,
                       napi_value *out) {
  // V8 refuses a string longer than String::kMaxLength by returning an empty
  // handle without throwing, which Node-API reports as napi_generic_failure
  // with no exception pending. For a non-null pointer and SQLite's int-sized
  // length that is the only way this call fails, so its status identifies the
  // oversized value without hard-coding V8's limit.
  if (napi_create_string_utf8(env, text, length, out) == napi_ok) {
    return true;
  }
  node::THROW_ERR_STRING_TOO_LONG(env);
  return false;
}

// Converts a nullable C string from SQLite to a JavaScript value. Returns false
// with ERR_STRING_TOO_LONG pending when the string exceeds V8's limit.
static bool NullableSQLiteStringToValue(Napi::Env env, const char *str,
                                        napi_value *out) {
  if (str == nullptr) {
    *out = env.Null();
    return true;
  }
  return SqliteTextToValue(env, str, strlen(str), out);
}

// DatabaseSync Implementation
Napi::Object DatabaseSync::Init(Napi::Env env, Napi::Object exports) {
  Napi::Function func = DefineClass(
      env, "Database",
      {InstanceMethod("open", &DatabaseSync::Open),
       InstanceMethod("close", &DatabaseSync::Close),
       InstanceMethod("dispose", &DatabaseSync::Dispose),
       InstanceMethod("prepare", &DatabaseSync::Prepare),
       InstanceMethod("exec", &DatabaseSync::Exec),
       InstanceMethod("function", &DatabaseSync::CustomFunction),
       InstanceMethod("aggregate", &DatabaseSync::AggregateFunction),
       InstanceMethod("createModule", &DatabaseSync::CreateModule),
       InstanceMethod("enableLoadExtension",
                      &DatabaseSync::EnableLoadExtension),
       InstanceMethod("loadExtension", &DatabaseSync::LoadExtension),
       InstanceMethod("enableDefensive", &DatabaseSync::EnableDefensive),
       InstanceMethod("createSession", &DatabaseSync::CreateSession),
       InstanceMethod("applyChangeset", &DatabaseSync::ApplyChangeset),
       InstanceMethod("setAuthorizer", &DatabaseSync::SetAuthorizer),
       InstanceMethod("getLimit", &DatabaseSync::GetLimit),
       InstanceMethod("setLimit", &DatabaseSync::SetLimit),
       InstanceMethod("backup", &DatabaseSync::Backup),
       InstanceMethod("serialize", &DatabaseSync::Serialize),
       InstanceMethod("deserialize", &DatabaseSync::Deserialize),
       InstanceMethod("location", &DatabaseSync::LocationMethod),
       InstanceAccessor("isOpen", &DatabaseSync::IsOpenGetter, nullptr),
       InstanceAccessor("isTransaction", &DatabaseSync::IsTransactionGetter,
                        nullptr),
       // Symbol-keyed so it stays out of the node:sqlite API surface; the
       // TypeScript SQLTagStore reads it to apply the same authorizer guard
       // the native methods use.
       InstanceAccessor(
           Napi::Symbol::For(env, "photostructure.sqlite.inAuthorizerCallback"),
           &DatabaseSync::IsInAuthorizerCallbackGetter, nullptr)});

  // Store constructor in per-instance addon data instead of static variable
  AddonData *addon_data = GetAddonData(env);
  if (addon_data) {
    addon_data->databaseSyncConstructor =
        Napi::Reference<Napi::Function>::New(func);
  }

  // Add Symbol.dispose to the prototype (Node.js v25+ compatibility)
  Napi::Value symbolDispose =
      env.Global().Get("Symbol").As<Napi::Object>().Get("dispose");
  if (!symbolDispose.IsUndefined()) {
    func.Get("prototype")
        .As<Napi::Object>()
        .Set(symbolDispose,
             Napi::Function::New(
                 env, [](const Napi::CallbackInfo &info) -> Napi::Value {
                   DatabaseSync *db =
                       DatabaseSync::Unwrap(info.This().As<Napi::Object>());
                   return db->Dispose(info);
                 }));
  }

  // Add Symbol.for('sqlite-type') property for type identification
  // See: https://github.com/nodejs/node/pull/59405
  Napi::Object symbolConstructor =
      env.Global().Get("Symbol").As<Napi::Object>();
  Napi::Function symbolFor = symbolConstructor.Get("for").As<Napi::Function>();
  Napi::Value sqliteTypeSymbol = symbolFor.Call(
      symbolConstructor, {Napi::String::New(env, "sqlite-type")});
  func.Get("prototype")
      .As<Napi::Object>()
      .Set(sqliteTypeSymbol, Napi::String::New(env, "node:sqlite"));

  // Node.js PR #65988 renamed DatabaseSync to Database and kept the old name
  // as an alias (DEP0210).
  exports.Set("Database", func);
  exports.Set("DatabaseSync", func);
  return exports;
}

DatabaseSync::DatabaseSync(const Napi::CallbackInfo &info)
    : Napi::ObjectWrap<DatabaseSync>(info),
      creation_thread_(std::this_thread::get_id()), env_(info.Env()),
      config_("") {
  // Register this instance for cleanup tracking
  RegisterDatabaseInstance(info.Env(), this);

  // Register cleanup hook to clean up references before environment teardown
  napi_add_env_cleanup_hook(env_, CleanupHook, this);

  // Node.js requires a path argument - throw if missing
  if (info.Length() == 0 || info[0].IsUndefined()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        info.Env(),
        "The \"path\" argument must be a string, Uint8Array, or URL without "
        "null bytes.");
    return;
  }

  // Validate and extract the database path
  std::optional<std::string> location =
      ValidateDatabasePath(info.Env(), info[0], "path");
  if (!location.has_value()) {
    return; // Error already thrown by ValidateDatabasePath
  }

  try {
    std::string loc = std::move(*location);
    // Check if this is a file:// URI (for SQLite URI mode)
    bool is_uri = (loc.compare(0, 7, "file://") == 0);
    DatabaseOpenConfiguration config(std::move(loc));
    if (is_uri) {
      config.set_open_uri(true);
    }

    // Track whether to open immediately (default: true)
    bool should_open = true;

    // Handle options object if provided as second argument
    if (info.Length() > 1) {
      if (!info[1].IsObject()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            info.Env(), "The \"options\" argument must be an object.");
        return;
      }

      Napi::Object options = info[1].As<Napi::Object>();

      // Validate and parse 'open' option
      Napi::Value open_val = options.Get("open");
      if (!open_val.IsUndefined()) {
        if (!open_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(), "The \"options.open\" argument must be a boolean.");
          return;
        }
        should_open = open_val.As<Napi::Boolean>().Value();
      }

      // Validate and parse 'readOnly' option
      Napi::Value read_only_val = options.Get("readOnly");
      if (!read_only_val.IsUndefined()) {
        if (!read_only_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.readOnly\" argument must be a boolean.");
          return;
        }
        config.set_read_only(read_only_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'enableForeignKeyConstraints' option
      Napi::Value enable_fk_val = options.Get("enableForeignKeyConstraints");
      if (!enable_fk_val.IsUndefined()) {
        if (!enable_fk_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.enableForeignKeyConstraints\" argument must be a "
              "boolean.");
          return;
        }
        config.set_enable_foreign_keys(
            enable_fk_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'enableDoubleQuotedStringLiterals' option
      Napi::Value enable_dqs_val =
          options.Get("enableDoubleQuotedStringLiterals");
      if (!enable_dqs_val.IsUndefined()) {
        if (!enable_dqs_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.enableDoubleQuotedStringLiterals\" argument must "
              "be a boolean.");
          return;
        }
        config.set_enable_dqs(enable_dqs_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'allowExtension' option
      Napi::Value allow_ext_val = options.Get("allowExtension");
      if (!allow_ext_val.IsUndefined()) {
        if (!allow_ext_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.allowExtension\" argument must be a boolean.");
          return;
        }
        allow_load_extension_ = allow_ext_val.As<Napi::Boolean>().Value();
      }

      // Validate and parse 'timeout' option
      Napi::Value timeout_val = options.Get("timeout");
      if (!timeout_val.IsUndefined()) {
        if (!timeout_val.IsNumber()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.timeout\" argument must be an integer.");
          return;
        }
        double timeout_double = timeout_val.As<Napi::Number>().DoubleValue();
        if (timeout_double != std::trunc(timeout_double)) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.timeout\" argument must be an integer.");
          return;
        }
        config.set_timeout(timeout_val.As<Napi::Number>().Int32Value());
      }

      // Validate and parse 'readBigInts' option
      Napi::Value read_bigints_val = options.Get("readBigInts");
      if (!read_bigints_val.IsUndefined()) {
        if (!read_bigints_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.readBigInts\" argument must be a boolean.");
          return;
        }
        config.set_read_big_ints(read_bigints_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'returnArrays' option
      Napi::Value return_arrays_val = options.Get("returnArrays");
      if (!return_arrays_val.IsUndefined()) {
        if (!return_arrays_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.returnArrays\" argument must be a boolean.");
          return;
        }
        config.set_return_arrays(return_arrays_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'allowBareNamedParameters' option
      Napi::Value allow_bare_val = options.Get("allowBareNamedParameters");
      if (!allow_bare_val.IsUndefined()) {
        if (!allow_bare_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.allowBareNamedParameters\" argument must be a "
              "boolean.");
          return;
        }
        config.set_allow_bare_named_params(
            allow_bare_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'allowUnknownNamedParameters' option
      Napi::Value allow_unknown_val =
          options.Get("allowUnknownNamedParameters");
      if (!allow_unknown_val.IsUndefined()) {
        if (!allow_unknown_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.allowUnknownNamedParameters\" argument must be a "
              "boolean.");
          return;
        }
        config.set_allow_unknown_named_params(
            allow_unknown_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'defensive' option
      Napi::Value defensive_val = options.Get("defensive");
      if (!defensive_val.IsUndefined()) {
        if (!defensive_val.IsBoolean()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(),
              "The \"options.defensive\" argument must be a boolean.");
          return;
        }
        config.set_enable_defensive(defensive_val.As<Napi::Boolean>().Value());
      }

      // Validate and parse 'limits' option
      Napi::Value limits_val = options.Get("limits");
      if (!limits_val.IsUndefined()) {
        if (!limits_val.IsObject() || limits_val.IsNull()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              info.Env(), "The \"options.limits\" argument must be an object.");
          return;
        }

        Napi::Object limits_obj = limits_val.As<Napi::Object>();

        // Limit name to SQLite limit ID mapping (matches upstream
        // kLimitMapping)
        static const struct {
          const char *name;
          int id;
        } kLimitNames[] = {
            {"length", SQLITE_LIMIT_LENGTH},
            {"sqlLength", SQLITE_LIMIT_SQL_LENGTH},
            {"column", SQLITE_LIMIT_COLUMN},
            {"exprDepth", SQLITE_LIMIT_EXPR_DEPTH},
            {"compoundSelect", SQLITE_LIMIT_COMPOUND_SELECT},
            {"vdbeOp", SQLITE_LIMIT_VDBE_OP},
            {"functionArg", SQLITE_LIMIT_FUNCTION_ARG},
            {"attach", SQLITE_LIMIT_ATTACHED},
            {"likePatternLength", SQLITE_LIMIT_LIKE_PATTERN_LENGTH},
            {"variableNumber", SQLITE_LIMIT_VARIABLE_NUMBER},
            {"triggerDepth", SQLITE_LIMIT_TRIGGER_DEPTH},
        };

        for (const auto &limit : kLimitNames) {
          Napi::Value val = limits_obj.Get(limit.name);
          if (!val.IsUndefined()) {
            if (!val.IsNumber()) {
              std::string msg = std::string("The \"options.limits.") +
                                limit.name + "\" argument must be an integer.";
              node::THROW_ERR_INVALID_ARG_TYPE(info.Env(), msg.c_str());
              return;
            }

            double dval = val.As<Napi::Number>().DoubleValue();
            if (dval != std::trunc(dval) || std::isinf(dval) ||
                std::isnan(dval)) {
              std::string msg = std::string("The \"options.limits.") +
                                limit.name + "\" argument must be an integer.";
              node::THROW_ERR_INVALID_ARG_TYPE(info.Env(), msg.c_str());
              return;
            }

            int limit_val = static_cast<int>(dval);
            if (limit_val < 0) {
              std::string msg = std::string("The \"options.limits.") +
                                limit.name +
                                "\" argument must be non-negative.";
              node::THROW_ERR_OUT_OF_RANGE(info.Env(), msg.c_str());
              return;
            }

            config.set_limit(limit.id, limit_val);
          }
        }
      }
    }

    // Store configuration for later use
    config_ = std::move(config);

    // Only open if should_open is true
    if (should_open) {
      InternalOpen(config_);
    }
  } catch (const SqliteException &e) {
    node::ThrowFromSqliteException(info.Env(), e);
  } catch (const Napi::Error &e) {
    // A JavaScript exception, such as one a getter on the options threw, is
    // rethrown as is rather than as a SQLite error.
    e.ThrowAsJavaScriptException();
  } catch (const std::exception &e) {
    node::THROW_ERR_SQLITE_ERROR(info.Env(), e.what());
  }
}

DatabaseSync::~DatabaseSync() {
  // Closing the connection here must not reach back into JavaScript.
  DestructorScope destructor_scope(this);

  // Remove cleanup hook if still registered
  napi_remove_env_cleanup_hook(env_, CleanupHook, this);

  // Unregister this instance
  UnregisterDatabaseInstance(env_, this);

  if (connection_) {
    InternalClose();
  }

  // A session keeps this object's JS wrapper alive, but when both become
  // unreachable in the same collection N-API may finalize this one first.
  // Clear their back-pointers so ~Session can't dereference this instance.
  DetachAllSessions();
}

void DatabaseSync::CleanupHook(void *arg) {
  // Called before environment teardown - safe to Reset() references here
  auto *self = static_cast<DatabaseSync *>(arg);

  // Clean up authorizer callback if it exists
  if (self->authorizer_callback_) {
    self->authorizer_callback_->Reset();
  }
  self->ClearDeferredAuthorizerException();
}

// SQLite forbids modifying a connection from inside its own authorizer
// callback and explicitly counts prepare/step as modification. The result of
// violating that contract is unspecified, so reject SQLite-backed operations
// on the invoking connection with a clear ERR_INVALID_STATE instead.
//
// node:sqlite now carries the same guard (THROW_AND_RETURN_IF_IN_AUTHORIZER),
// so the message matches it verbatim. We additionally guard SQLite-backed
// read-only getters (columns/expandedSQL/location/getLimit/isTransaction) for
// one consistent callback contract.
bool DatabaseSync::ThrowIfInAuthorizerCallback(Napi::Env env) const {
  if (!IsInAuthorizerCallback()) {
    return false;
  }

  node::THROW_ERR_INVALID_STATE(
      env, "database cannot be accessed from an authorizer callback");
  return true;
}

DatabaseSync::CallbackGuard::CallbackGuard(DatabaseSync *database)
    : database_(database) {
  std::lock_guard<std::mutex> lock(database_->sessions_mutex_);
  pinned_sessions_.reserve(database_->sessions_.size());
  for (Session *session : database_->sessions_) {
    // This implementation retains closed wrappers so their methods can still
    // distinguish "session is not open" from "database is not open". Node's
    // active-session set does not contain those wrappers, and SQLite cannot be
    // walking them, so they must not participate in callback pinning.
    if (session->GetSession() == nullptr) {
      continue;
    }
    session->Ref();
    pinned_sessions_.push_back(session);
  }
  ++database_->callback_depth_;
}

DatabaseSync::CallbackGuard::~CallbackGuard() noexcept {
  --database_->callback_depth_;
  // Do not hold sessions_mutex_ while unpinning. If a future Node-API version
  // can finalize synchronously here, ~Session() must be free to remove itself.
  for (auto it = pinned_sessions_.rbegin(); it != pinned_sessions_.rend();
       ++it) {
    try {
      (*it)->Unref();
    } catch (...) {
      // Destructors must not propagate a Node-API teardown error across the
      // SQLite C callback boundary. A failed unref leaves the wrapper pinned,
      // which is safer than finalizing native state that SQLite may still own.
    }
  }
}

void DatabaseSync::RethrowDeferredAuthorizerException() {
  Napi::Error error = GetDeferredAuthorizerException();
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);
  error.ThrowAsJavaScriptException();
}

Napi::Value DatabaseSync::Open(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is already open");
    return env.Undefined();
  }

  try {
    InternalOpen(config_);
  } catch (const SqliteException &e) {
    node::ThrowFromSqliteException(env, e);
  } catch (const std::exception &e) {
    node::THROW_ERR_SQLITE_ERROR(env, e.what());
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::Close(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // SQLite forbids closing a connection while a statement is executing on it.
  // The only way close() is reachable in that state is from inside a user
  // callback invoked by sqlite3_step()/sqlite3_exec(), or from an authorizer;
  // reject it rather than finalize a statement whose VM is still on the stack
  // (undefined behavior). node:sqlite guards close() on IsInCallback() alone,
  // so an authorizer reports this reason rather than the generic one.
  if (IsInCallback()) {
    node::THROW_ERR_INVALID_STATE(
        env, "database cannot be closed while in a callback");
    return env.Undefined();
  }

  try {
    InternalClose();
  } catch (const std::exception &e) {
    node::THROW_ERR_SQLITE_ERROR(env, e.what());
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::Dispose(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  // Symbol.dispose is a no-op inside any callback SQLite invoked on this
  // connection: closing there would finalize a statement whose sqlite3_step is
  // still on the stack (undefined behavior). Tested without throwing, because
  // node:sqlite runs Dispose's close under a TryCatch and swallows the error.
  if (IsInCallback()) {
    return env.Undefined();
  }

  // Try to close, but ignore errors during disposal (matches Node.js v25
  // behavior)
  try {
    if (IsOpen()) {
      InternalClose();
    }
  } catch (...) {
    // Ignore errors during disposal
  }

  // Closing finalizes statements, which runs the return() of any suspended
  // virtual table iterator, and an exception it threw is still pending.
  // node:sqlite drops that too.
  ClearPendingException(env);

  return env.Undefined();
}

Napi::Value DatabaseSync::Prepare(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"sql\" argument must be a string.");
    return env.Undefined();
  }

  std::string sql = info[0].As<Napi::String>().Utf8Value();

  // Parse optional second argument (options object)
  // Node.js v25+ supports per-statement options that override database defaults
  std::optional<bool> opt_read_big_ints;
  std::optional<bool> opt_return_arrays;
  std::optional<bool> opt_allow_bare_named_params;
  std::optional<bool> opt_allow_unknown_named_params;
  bool persistent = false;

  if (info.Length() > 1 && !info[1].IsUndefined()) {
    if (!info[1].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }

    Napi::Object options = info[1].As<Napi::Object>();

    // Parse readBigInts option
    Napi::Value read_big_ints_val = options.Get("readBigInts");
    if (!read_big_ints_val.IsUndefined()) {
      if (!read_big_ints_val.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.readBigInts\" argument must be a boolean.");
        return env.Undefined();
      }
      opt_read_big_ints = read_big_ints_val.As<Napi::Boolean>().Value();
    }

    // Parse returnArrays option
    Napi::Value return_arrays_val = options.Get("returnArrays");
    if (!return_arrays_val.IsUndefined()) {
      if (!return_arrays_val.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.returnArrays\" argument must be a boolean.");
        return env.Undefined();
      }
      opt_return_arrays = return_arrays_val.As<Napi::Boolean>().Value();
    }

    // Parse allowBareNamedParameters option
    Napi::Value allow_bare_val = options.Get("allowBareNamedParameters");
    if (!allow_bare_val.IsUndefined()) {
      if (!allow_bare_val.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.allowBareNamedParameters\" argument must be a "
                 "boolean.");
        return env.Undefined();
      }
      opt_allow_bare_named_params = allow_bare_val.As<Napi::Boolean>().Value();
    }

    // Parse allowUnknownNamedParameters option
    Napi::Value allow_unknown_val = options.Get("allowUnknownNamedParameters");
    if (!allow_unknown_val.IsUndefined()) {
      if (!allow_unknown_val.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.allowUnknownNamedParameters\" argument must be "
                 "a boolean.");
        return env.Undefined();
      }
      opt_allow_unknown_named_params =
          allow_unknown_val.As<Napi::Boolean>().Value();
    }

    // Parse persistent option (SQLITE_PREPARE_PERSISTENT)
    Napi::Value persistent_val = options.Get("persistent");
    if (!persistent_val.IsUndefined()) {
      if (!persistent_val.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.persistent\" argument must be a boolean.");
        return env.Undefined();
      }
      persistent = persistent_val.As<Napi::Boolean>().Value();
    }
  }

  // Reading the options bag above can run user JavaScript through a property
  // getter, which may have closed the database since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Clear any stale deferred exception from a previous operation
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);

  try {
    // Create new StatementSync instance using addon data constructor
    AddonData *addon_data = GetAddonData(env);
    if (!addon_data || addon_data->statementSyncConstructor.IsEmpty()) {
      node::THROW_ERR_INVALID_STATE(
          env, "StatementSync constructor not initialized");
      return env.Undefined();
    }
    Napi::Object stmt_obj =
        addon_data->statementSyncConstructor.New({}).As<Napi::Object>();

    // Initialize the statement (applies database-level defaults)
    StatementSync *stmt = StatementSync::Unwrap(stmt_obj);
    stmt->InitStatement(this, sql, persistent);

    // sqlite3_prepare_v3() reports success without producing a statement when
    // the input holds no SQL, such as a comment or an empty string. Such a
    // statement can never be stepped, so reject it here rather than handing
    // back an object whose every method fails. Untrack it first: leaving it in
    // statements_ would dangle once the unreachable wrapper is collected.
    if (stmt->statement_ == nullptr) {
      UntrackStatement(stmt);
      stmt->database_ = nullptr;
      node::THROW_ERR_INVALID_ARG_VALUE(
          env, "The SQL query contains no statements.");
      return env.Undefined();
    }

    // Keep the database's JS object alive for as long as the statement is, as
    // node:sqlite's BaseObjectPtr<DatabaseSync> does; otherwise a caller that
    // keeps only the statement or its iterator loses the database to GC, whose
    // finalizer closes the connection and finalizes the statement mid-use. A
    // Napi::Reference member would be released during GC finalization, which
    // crashes on Alpine/musl (0691ae5).
    stmt_obj.DefineProperty(Napi::PropertyDescriptor::Value(
        addon_data->statementSyncDatabaseKey.Value().As<Napi::Symbol>(),
        Value(), napi_default));

    // Apply per-statement option overrides (if explicitly provided)
    if (opt_read_big_ints.has_value()) {
      stmt->use_big_ints_ = *opt_read_big_ints;
    }
    if (opt_return_arrays.has_value()) {
      stmt->return_arrays_ = *opt_return_arrays;
    }
    if (opt_allow_bare_named_params.has_value()) {
      stmt->allow_bare_named_params_ = *opt_allow_bare_named_params;
    }
    if (opt_allow_unknown_named_params.has_value()) {
      stmt->allow_unknown_named_params_ = *opt_allow_unknown_named_params;
    }

    return stmt_obj;
  } catch (const SqliteException &e) {
    // SqliteException stores message in std::string, avoiding Windows ARM ABI
    // issues where std::exception::what() can return corrupted strings
    if (HasDeferredAuthorizerException()) {
      RethrowDeferredAuthorizerException();
      return env.Undefined();
    }
    node::ThrowFromSqliteException(env, e);
    return env.Undefined();
  } catch (const std::exception &e) {
    // Handle deferred authorizer exceptions:
    //
    // When an authorizer callback throws a JavaScript exception, we use a
    // "marker" exception pattern to safely propagate the error:
    //
    // 1. On Windows (MSVC), std::exception::what() can sometimes return an
    //    empty string, causing message loss.
    //
    // 2. DatabaseSync holds Napi::Error's persistent reference until this
    //    SQLite call unwinds, preserving the exact thrown JavaScript value.
    //
    // See also: StatementSync::InitStatement for the other half of this
    // pattern.
    if (HasDeferredAuthorizerException()) {
      RethrowDeferredAuthorizerException();
      return env.Undefined();
    }
    node::THROW_ERR_SQLITE_ERROR(env, e.what());
    return env.Undefined();
  }
}

Napi::Value DatabaseSync::Exec(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"sql\" argument must be a string.");
    return env.Undefined();
  }

  std::string sql = info[0].As<Napi::String>().Utf8Value();

  // Clear any stale deferred exception from a previous operation
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);

  char *error_msg = nullptr;
  // Raise the user-callback depth for the duration of sqlite3_exec so that a
  // close()/deserialize() re-entered from a user function it invokes throws
  // ERR_INVALID_STATE. sqlite3_exec is a C call, so no C++ exception may
  // unwind through it: the callbacks it runs must report errors as pending or
  // deferred JavaScript exceptions (see AGENTS.md). That makes manual
  // balancing here safe; an escaping exception would skip LeaveStatementStep()
  // and leave close() throwing ERR_INVALID_STATE for good.
  EnterStatementStep();
  int result =
      sqlite3_exec(connection(), sql.c_str(), nullptr, nullptr, &error_msg);
  LeaveStatementStep();

  if (result != SQLITE_OK) {
    // Check for deferred authorizer exception first
    if (HasDeferredAuthorizerException()) {
      if (error_msg)
        sqlite3_free(error_msg);
      RethrowDeferredAuthorizerException();
      return env.Undefined();
    }
    std::string error = error_msg ? error_msg : "Unknown SQLite error";
    if (error_msg)
      sqlite3_free(error_msg);
    // The database-aware helper leaves a callback's pending JavaScript
    // exception in place of the SQLite error, and reports the SQLite error
    // over one thrown by a virtual table iterator's cleanup.
    ThrowEnhancedSqliteErrorWithDB(env, this, connection(), result, error);
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::LocationMethod(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  // Default to "main" if no dbName provided
  std::string db_name = "main";
  if (info.Length() > 0 && !info[0].IsUndefined()) {
    if (!info[0].IsString()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"dbName\" argument must be a string.");
      return env.Undefined();
    }
    db_name = info[0].As<Napi::String>().Utf8Value();
  }

  // Use sqlite3_db_filename() to get the database file path
  const char *filename = sqlite3_db_filename(connection(), db_name.c_str());

  // Return null for in-memory databases, non-existent databases, or if database
  // not found
  if (filename == nullptr || strlen(filename) == 0) {
    return env.Null();
  }

  return Napi::String::New(env, filename);
}

Napi::Value DatabaseSync::Serialize(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  std::string db_name = "main";
  if (info.Length() > 0 && !info[0].IsUndefined()) {
    if (!info[0].IsString()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"dbName\" argument must be a string.");
      return env.Undefined();
    }
    db_name = info[0].As<Napi::String>().Utf8Value();
  }

  // Clear any stale deferred exception from a previous operation.
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);

  // Ordinary in-memory and file-backed databases use an internal
  // PRAGMA page_count path that can invoke the authorizer. The callback's
  // AuthorizerGuard rejects same-connection reentry while it is on the stack.
  sqlite3_int64 size = 0;
  unsigned char *data =
      sqlite3_serialize(connection_, db_name.c_str(), &size, 0);

  // sqlite3_serialize ignores errors from its empty-database
  // "BEGIN IMMEDIATE; COMMIT;" and can return either a zero-sized result or a
  // non-null buffer after the authorizer throws. Deferred callback state must
  // therefore take precedence over every data/size outcome.
  if (HasDeferredAuthorizerException()) {
    if (data != nullptr) {
      sqlite3_free(data);
    }
    RethrowDeferredAuthorizerException();
    return env.Undefined();
  }

  if (data == nullptr) {
    if (size == 0) {
      auto ab = Napi::ArrayBuffer::New(env, 0);
      return Napi::Uint8Array::New(env, 0, ab, 0);
    }
    node::THROW_ERR_SQLITE_ERROR(env, sqlite3_errmsg(connection_));
    return env.Undefined();
  }

  // ArrayBuffer takes ownership of the sqlite3_malloc'd buffer; the finalizer
  // calls sqlite3_free when V8 GCs the backing store.
  auto ab = Napi::ArrayBuffer::New(
      env, data, static_cast<size_t>(size),
      [](Napi::Env /*env*/, void *ptr) { sqlite3_free(ptr); });

  return Napi::Uint8Array::New(env, static_cast<size_t>(size), ab, 0);
}

Napi::Value DatabaseSync::Deserialize(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // deserialize() finalizes every open statement and replaces the database
  // contents; doing so while a statement is executing (i.e. from inside a
  // user-defined function callback) would pull the rug from under the running
  // VM. Reject it against the same IsInCallback() contract as close().
  if (IsInCallback()) {
    node::THROW_ERR_INVALID_STATE(
        env, "database cannot be deserialized while in a callback");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsTypedArray() ||
      info[0].As<Napi::TypedArray>().TypedArrayType() != napi_uint8_array) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"buffer\" argument must be a Uint8Array.");
    return env.Undefined();
  }

  Napi::Uint8Array input = info[0].As<Napi::Uint8Array>();
  size_t byte_length = input.ByteLength();

  if (byte_length == 0) {
    node::THROW_ERR_INVALID_ARG_VALUE(
        env, "The \"buffer\" argument must not be empty.");
    return env.Undefined();
  }

  std::string db_name = "main";
  if (info.Length() > 1 && !info[1].IsUndefined()) {
    if (!info[1].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }
    Napi::Object options = info[1].As<Napi::Object>();
    Napi::Value db_name_value = options.Get("dbName");
    if (!db_name_value.IsUndefined()) {
      if (!db_name_value.IsString()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.dbName\" argument must be a string.");
        return env.Undefined();
      }
      db_name = db_name_value.As<Napi::String>().Utf8Value();
    }
  }

  // Reading the options bag above can run user JavaScript through a property
  // getter, which may have closed the database since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // The same user JavaScript may also have shrunk or detached the backing
  // store, in which case byte_length is stale and the copy below would read
  // past the end of it. Handing that to SQLite would disclose it through
  // serialize(). Re-read the view: a length-tracking view of a resized
  // ArrayBuffer reports its new length, and a detached one reports 0.
  input = info[0].As<Napi::Uint8Array>();
  if (input.ByteLength() < byte_length) {
    node::THROW_ERR_INVALID_STATE(
        env, "The \"buffer\" argument was resized while reading \"options\"");
    return env.Undefined();
  }

  // SQLITE_DESERIALIZE_FREEONCLOSE transfers buffer ownership to SQLite, which
  // calls sqlite3_free() on close (or on this call's failure). The buffer must
  // therefore come from sqlite3_malloc, not new/malloc.
  unsigned char *buf =
      static_cast<unsigned char *>(sqlite3_malloc64(byte_length));
  if (buf == nullptr) {
    Napi::Error::New(env, "Memory allocation failed")
        .ThrowAsJavaScriptException();
    return env.Undefined();
  }
  memcpy(buf, input.Data(), byte_length);

  // Finalize all open prepared statements; sqlite3_deserialize requires no
  // active statements on the connection. After this call, any user-held
  // StatementSync instances will throw on use.
  FinalizeStatements();

  // Finalizing ran the return() of any suspended virtual table iterator. An
  // exception it threw stays pending, as in node:sqlite.

  // Clear any stale deferred exception from a previous operation.
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);

  // sqlite3_deserialize prepares an internal ATTACH statement that can invoke
  // the authorizer. The callback's AuthorizerGuard rejects same-connection
  // reentry while it is on the stack.
  int r = sqlite3_deserialize(
      connection_, db_name.c_str(), buf, byte_length, byte_length,
      SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE);

  if (HasDeferredAuthorizerException()) {
    RethrowDeferredAuthorizerException();
    return env.Undefined();
  }

  if (r != SQLITE_OK) {
    node::THROW_ERR_SQLITE_ERROR(env, sqlite3_errmsg(connection_));
    return env.Undefined();
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::IsOpenGetter(const Napi::CallbackInfo &info) {
  return Napi::Boolean::New(info.Env(), IsOpen());
}

Napi::Value
DatabaseSync::IsInAuthorizerCallbackGetter(const Napi::CallbackInfo &info) {
  return Napi::Boolean::New(info.Env(), IsInAuthorizerCallback());
}

Napi::Value DatabaseSync::IsTransactionGetter(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  // Check if we're in a transaction
  bool in_transaction = !sqlite3_get_autocommit(connection());
  return Napi::Boolean::New(env, in_transaction);
}

void DatabaseSync::InternalOpen(DatabaseOpenConfiguration config) {
  // Store configuration for later use by statements
  config_ = std::move(config);
  location_ = config_.location();
  read_only_ = config_.get_read_only();

  int flags = SQLITE_OPEN_CREATE;
  if (read_only_) {
    flags = SQLITE_OPEN_READONLY;
  } else {
    flags |= SQLITE_OPEN_READWRITE;
  }

  // Add URI flag when location contains URI parameters
  if (config_.get_open_uri()) {
    flags |= SQLITE_OPEN_URI;
  }

  int result = sqlite3_open_v2(location_.c_str(), &connection_, flags, nullptr);

  if (result != SQLITE_OK) {
    std::string error = sqlite3_errmsg(connection_);
    // Capture error info before closing
    SqliteException ex(connection_, result,
                       "Failed to open database: " + error);
    if (connection_) {
      sqlite3_close(connection_);
      connection_ = nullptr;
    }
    throw ex;
  }

  // Configure foreign keys using sqlite3_db_config (matches Node.js behavior)
  // This properly handles both enabling and disabling FK constraints
  int fk_enabled;
  result =
      sqlite3_db_config(connection(), SQLITE_DBCONFIG_ENABLE_FKEY,
                        config_.get_enable_foreign_keys() ? 1 : 0, &fk_enabled);
  if (result != SQLITE_OK) {
    std::string error = sqlite3_errmsg(connection());
    SqliteException ex(connection_, result,
                       "Failed to configure foreign keys: " + error);
    sqlite3_close(connection_);
    connection_ = nullptr;
    throw ex;
  }

  if (config_.get_timeout() > 0) {
    sqlite3_busy_timeout(connection(), config_.get_timeout());
  }

  // Configure double-quoted string literals
  if (config_.get_enable_dqs()) {
    int dqs_enable = 1;
    result = sqlite3_db_config(connection(), SQLITE_DBCONFIG_DQS_DML,
                               dqs_enable, nullptr);
    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(connection());
      SqliteException ex(connection_, result,
                         "Failed to configure DQS_DML: " + error);
      sqlite3_close(connection_);
      connection_ = nullptr;
      throw ex;
    }

    result = sqlite3_db_config(connection(), SQLITE_DBCONFIG_DQS_DDL,
                               dqs_enable, nullptr);
    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(connection());
      SqliteException ex(connection_, result,
                         "Failed to configure DQS_DDL: " + error);
      sqlite3_close(connection_);
      connection_ = nullptr;
      throw ex;
    }
  }

  // Configure defensive mode
  if (config_.get_enable_defensive()) {
    int defensive_enabled;
    result = sqlite3_db_config(connection(), SQLITE_DBCONFIG_DEFENSIVE, 1,
                               &defensive_enabled);
    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(connection());
      SqliteException ex(connection_, result,
                         "Failed to configure DEFENSIVE: " + error);
      sqlite3_close(connection_);
      connection_ = nullptr;
      throw ex;
    }
  }

  // Apply limits from constructor options and later db.limits writes (see
  // SetLimit()), so a reopened connection keeps the runtime values.
  //
  // Bind the array once rather than calling the accessor three times per
  // iteration: it makes the has_value()/deref pair obviously operate on the
  // same optional, which is also what lets clang-tidy's dataflow analysis see
  // the access is checked (bugprone-unchecked-optional-access).
  const auto &limits = config_.limits();
  for (size_t i = 0; i < limits.size(); i++) {
    const std::optional<int> &limit = limits[i];
    if (limit.has_value()) {
      sqlite3_limit(connection_, static_cast<int>(i), *limit);
    }
  }

  // close() keeps the enableLoadExtension() setting, so re-apply it to the
  // new connection the same way EnableLoadExtension() does.
  if (enable_load_extension_) {
    result = sqlite3_db_config(
        connection(), SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, nullptr);
    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(connection());
      SqliteException ex(connection_, result,
                         "Failed to configure extension loading: " + error);
      sqlite3_close(connection_);
      connection_ = nullptr;
      throw ex;
    }
  }

  // The callback is always registered once the public package entrypoint has
  // supplied its Channel. It checks hasSubscribers before doing expansion or
  // allocating the payload, so subscription changes take effect immediately.
  AddonData *addon_data = GetAddonData(env_);
  if (addon_data != nullptr && !addon_data->queryDiagnosticsChannel.IsEmpty()) {
    sqlite3_trace_v2(connection_, SQLITE_TRACE_PROFILE, QueryTraceCallback,
                     this);
  }

  // The authorizer outlives the connection, so reopening must reinstall it.
  // setAuthorizer(null) resets authorizer_callback_, so a cleared authorizer
  // stays cleared.
  if (authorizer_callback_) {
    result = sqlite3_set_authorizer(connection_, AuthorizerCallback, this);
    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(connection());
      SqliteException ex(connection_, result,
                         "Failed to set authorizer: " + error);
      sqlite3_close(connection_);
      connection_ = nullptr;
      throw ex;
    }
  }
}

int DatabaseSync::QueryTraceCallback(unsigned int type, void *user_data,
                                     void *statement, void *duration) {
  if (type != SQLITE_TRACE_PROFILE || user_data == nullptr ||
      statement == nullptr || duration == nullptr) {
    return 0;
  }

  auto *database = static_cast<DatabaseSync *>(user_data);
  if (database->AreTraceEventsSuppressed()) {
    return 0;
  }

  // Result materialization may already have raised a JavaScript exception
  // before sqlite3_reset() delivers the profile event. Any N-API call made
  // with that exception pending can replace its structured error object, so
  // leave the original failure untouched and skip diagnostics publication.
  bool exception_pending = false;
  if (napi_is_exception_pending(database->env_, &exception_pending) !=
          napi_ok ||
      exception_pending) {
    return 0;
  }

  Napi::Env env(database->env_);
  AddonData *addon_data = GetAddonData(env);
  if (addon_data == nullptr || addon_data->queryDiagnosticsChannel.IsEmpty()) {
    return 0;
  }

  // SQLite calls this from inside sqlite3_step() and sqlite3_reset(), so no
  // C++ exception may leave it. A failure skips the event: subscribing to
  // diagnostics must not change whether the traced statement succeeds.
  try {
    auto callback_guard = database->EnterCallback();
    Napi::HandleScope scope(env);
    Napi::Object channel = addon_data->queryDiagnosticsChannel.Value();
    Napi::Value has_subscribers = channel.Get("hasSubscribers");
    if (!has_subscribers.IsBoolean() ||
        !has_subscribers.As<Napi::Boolean>().Value()) {
      return 0;
    }

    auto *sqlite_statement = static_cast<sqlite3_stmt *>(statement);
    char *expanded = sqlite3_expanded_sql(sqlite_statement);
    napi_value sql;
    if (expanded != nullptr) {
      // A bound blob over about 256 MB expands to hex longer than V8's
      // maximum string length, which fails this call without throwing. Pass
      // the length: given NAPI_AUTO_LENGTH, V8 aborts the process instead.
      napi_status status =
          napi_create_string_utf8(env, expanded, strlen(expanded), &sql);
      sqlite3_free(expanded);
      if (status != napi_ok) {
        return 0;
      }
    } else {
      const char *source = sqlite3_sql(sqlite_statement);
      if (source == nullptr) {
        return 0;
      }
      sql = Napi::String::New(env, source);
    }

    Napi::Object payload = CreateObjectWithNullPrototype(env);
    payload.Set("sql", sql);
    payload.Set("database", database->Value());
    payload.Set(
        "duration",
        Napi::Number::New(
            env, static_cast<double>(*static_cast<sqlite3_int64 *>(duration))));

    Napi::Value publish_value = channel.Get("publish");
    if (!publish_value.IsFunction()) {
      return 0;
    }

    // Raw Node-API (see pending_exception.h). A subscriber's exception is
    // discarded like the other failures here.
    napi_value argv[] = {payload};
    napi_value result;
    if (napi_call_function(env, channel, publish_value, 1, argv, &result) !=
        napi_ok) {
      ClearPendingException(env);
    }
  } catch (const Napi::Error &) {
    return 0;
  } catch (const std::exception &) {
    return 0;
  } catch (...) {
    return 0;
  }
  return 0;
}

void DatabaseSync::InternalClose() {
  if (connection_) {
    // Clear the callback before teardown. Unlike Node's internal diagnostics
    // binding, our stable Node-API bridge cannot unregister tracing as soon as
    // the final subscriber leaves, so a no-subscriber callback may still be
    // installed when GC closes the wrapper. Teardown must not call into N-API.
    sqlite3_trace_v2(connection_, 0, nullptr, nullptr);

    // Finalize any active backup jobs first
    // This prevents use-after-free if backup is running on worker thread
    FinalizeBackups();

    // Finalize every live StatementSync's sqlite3_stmt now. This is safe
    // because close() and Dispose() reject when IsExecutingStatement() is
    // true, so InternalClose() can never run while a sqlite3_step is on the
    // stack. Finalizing here (rather than deferring to ~StatementSync on GC)
    // releases the SHARED/read locks any suspended statement holds, so the
    // following sqlite3_close() succeeds immediately instead of leaving a
    // zombie connection whose locks outlive the close() call.
    FinalizeStatements();

    // Delete all sessions before closing the database
    // This is required by SQLite to avoid undefined behavior
    DeleteAllSessions();

    // Close the database connection
    int result = sqlite3_close(connection_);
    if (result != SQLITE_OK) {
      // Force close even if there are outstanding statements
      sqlite3_close_v2(connection_);
    }
    connection_ = nullptr;
  }
  location_.clear();
}

// V8's Value::IsInt32(): a Number holding an integer in int32 range, and not
// -0. A function's `length` is configurable, so node:sqlite validates it with
// this check instead of casting it blindly.
static bool IsInt32Number(const Napi::Value &value) {
  if (!value.IsNumber()) {
    return false;
  }
  const double d = value.As<Napi::Number>().DoubleValue();
  return d >= std::numeric_limits<int32_t>::min() &&
         d <= std::numeric_limits<int32_t>::max() && d == std::trunc(d) &&
         !(d == 0 && std::signbit(d));
}

Napi::Value DatabaseSync::CustomFunction(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"name\" argument must be a string.");
    return env.Undefined();
  }

  // Parse arguments: function(name, [options], callback)
  int fn_index = info.Length() < 3 ? 1 : 2;
  bool use_bigint_args = false;
  bool varargs = false;
  bool deterministic = false;
  bool direct_only = false;

  // Parse options object if provided
  if (fn_index > 1) {
    if (!info[1].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }

    Napi::Object options = info[1].As<Napi::Object>();

    Napi::Value use_bigint_args_v = options.Get("useBigIntArguments");
    if (!use_bigint_args_v.IsUndefined()) {
      if (!use_bigint_args_v.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env,
            "The \"options.useBigIntArguments\" argument must be a boolean.");
        return env.Undefined();
      }
      use_bigint_args = use_bigint_args_v.As<Napi::Boolean>().Value();
    }

    Napi::Value varargs_v = options.Get("varargs");
    if (!varargs_v.IsUndefined()) {
      if (!varargs_v.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.varargs\" argument must be a boolean.");
        return env.Undefined();
      }
      varargs = varargs_v.As<Napi::Boolean>().Value();
    }

    Napi::Value deterministic_v = options.Get("deterministic");
    if (!deterministic_v.IsUndefined()) {
      if (!deterministic_v.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.deterministic\" argument must be a boolean.");
        return env.Undefined();
      }
      deterministic = deterministic_v.As<Napi::Boolean>().Value();
    }

    Napi::Value direct_only_v = options.Get("directOnly");
    if (!direct_only_v.IsUndefined()) {
      if (!direct_only_v.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.directOnly\" argument must be a boolean.");
        return env.Undefined();
      }
      direct_only = direct_only_v.As<Napi::Boolean>().Value();
    }
  }

  if (!info[fn_index].IsFunction()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"function\" argument must be a function.");
    return env.Undefined();
  }

  std::string name = info[0].As<Napi::String>().Utf8Value();
  Napi::Function fn = info[fn_index].As<Napi::Function>();

  // Determine argument count
  int argc = -1; // Default to varargs
  if (!varargs) {
    Napi::Value length_prop = fn.Get("length");
    if (!IsInt32Number(length_prop)) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"function.length\" property must be an integer.");
      return env.Undefined();
    }
    argc = length_prop.As<Napi::Number>().Int32Value();
  }

  // Reading the options bag and "function.length" above can run user
  // JavaScript through a property getter, which may have closed the database
  // since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Create UserDefinedFunction wrapper
  UserDefinedFunction *user_data =
      new UserDefinedFunction(env, fn, this, use_bigint_args);

  // Set SQLite flags
  int flags = SQLITE_UTF8;
  if (deterministic) {
    flags |= SQLITE_DETERMINISTIC;
  }
  if (direct_only) {
    flags |= SQLITE_DIRECTONLY;
  }

  // Register with SQLite
  int result =
      sqlite3_create_function_v2(connection(), name.c_str(), argc, flags,
                                 user_data, UserDefinedFunction::xFunc,
                                 nullptr, // No aggregate step
                                 nullptr, // No aggregate final
                                 UserDefinedFunction::xDestroy);

  if (result != SQLITE_OK) {
    // SQLite has already invoked xDestroy on user_data: createFunctionApi()
    // destroys the user data of a registration it did not keep. Deleting it
    // here again was a double free.
    std::string error = "Failed to create function: ";
    error += sqlite3_errmsg(connection());
    ThrowErrSqliteErrorWithDb(env, this, error.c_str());
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::AggregateFunction(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"name\" argument must be a string.");
    return env.Undefined();
  }

  if (!info[1].IsObject()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options\" argument must be an object.");
    return env.Undefined();
  }

  std::string name = info[0].As<Napi::String>().Utf8Value();
  Napi::Object options = info[1].As<Napi::Object>();

  // Parse start value
  Napi::Value start = options.Get("start");
  if (start.IsUndefined()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options.start\" argument must be a function or a primitive "
             "value.");
    return env.Undefined();
  }
  // CustomAggregate keeps a BigInt start value as int64, as it does the
  // aggregate state (see CustomAggregate::StoreValue).
  if (start.IsBigInt()) {
    bool lossless;
    start.As<Napi::BigInt>().Int64Value(&lossless);
    if (!lossless) {
      node::THROW_ERR_OUT_OF_RANGE(
          env,
          "BigInt value is too large to be represented as a SQLite integer");
      return env.Undefined();
    }
  }

  // Parse step function
  Napi::Value step_v = options.Get("step");
  if (!step_v.IsFunction()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options.step\" argument must be a function.");
    return env.Undefined();
  }
  Napi::Function step_fn = step_v.As<Napi::Function>();

  // Parse result function (optional)
  Napi::Function result_fn;
  Napi::Value result_v = options.Get("result");
  if (!result_v.IsUndefined()) {
    result_fn = result_v.As<Napi::Function>();
  }

  // Parse boolean options with validation
  bool use_bigint_args = false;
  Napi::Value use_bigint_args_v = options.Get("useBigIntArguments");
  if (!use_bigint_args_v.IsUndefined()) {
    if (!use_bigint_args_v.IsBoolean()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env,
          "The \"options.useBigIntArguments\" argument must be a boolean.");
      return env.Undefined();
    }
    use_bigint_args = use_bigint_args_v.As<Napi::Boolean>().Value();
  }

  bool varargs = false;
  Napi::Value varargs_v = options.Get("varargs");
  if (!varargs_v.IsUndefined()) {
    if (!varargs_v.IsBoolean()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options.varargs\" argument must be a boolean.");
      return env.Undefined();
    }
    varargs = varargs_v.As<Napi::Boolean>().Value();
  }

  bool deterministic = false;
  // Note: deterministic is handled via sqlite flags but Node.js
  // doesn't seem to validate it separately for aggregates

  bool direct_only = false;
  Napi::Value direct_only_v = options.Get("directOnly");
  if (!direct_only_v.IsUndefined()) {
    if (!direct_only_v.IsBoolean()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options.directOnly\" argument must be a boolean.");
      return env.Undefined();
    }
    direct_only = direct_only_v.As<Napi::Boolean>().Value();
  }

  // Parse inverse function (optional)
  Napi::Function inverse_fn;
  Napi::Value inverse_v = options.Get("inverse");
  if (!inverse_v.IsUndefined()) {
    if (!inverse_v.IsFunction()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options.inverse\" argument must be a function.");
      return env.Undefined();
    }
    inverse_fn = inverse_v.As<Napi::Function>();
  }

  // Determine argument count
  int argc = -1; // Default to varargs
  if (!varargs) {
    Napi::Value length_prop = step_fn.Get("length");
    if (!IsInt32Number(length_prop)) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options.step.length\" property must be an integer.");
      return env.Undefined();
    }
    // Subtract 1 because the first argument is the aggregate value
    argc = length_prop.As<Napi::Number>().Int32Value() - 1;

    // Also check inverse function length if provided
    if (!inverse_fn.IsEmpty()) {
      Napi::Value inverse_length = inverse_fn.Get("length");
      if (!IsInt32Number(inverse_length)) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.inverse.length\" property must be an integer.");
        return env.Undefined();
      }
      int inverse_argc = inverse_length.As<Napi::Number>().Int32Value() - 1;
      argc = std::max({argc, inverse_argc, 0});
    }

    // Ensure argc is non-negative
    argc = std::max(argc, 0);
  }

  // Reading the options bag and the step/inverse "length" properties above can
  // run user JavaScript through a property getter, which may have closed the
  // database since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Set SQLite flags
  int flags = SQLITE_UTF8;
  if (deterministic) {
    flags |= SQLITE_DETERMINISTIC;
  }
  if (direct_only) {
    flags |= SQLITE_DIRECTONLY;
  }

  // Create CustomAggregate wrapper
  CustomAggregate *user_data;
  try {
    user_data = new CustomAggregate(env, this, use_bigint_args, start, step_fn,
                                    inverse_fn, result_fn);
  } catch (const std::exception &e) {
    std::string error = "Failed to create CustomAggregate: ";
    error += e.what();
    node::THROW_ERR_INVALID_ARG_VALUE(env, error.c_str());
    return env.Undefined();
  }

  // Register with SQLite - Node.js always uses sqlite3_create_window_function
  // for aggregates
  auto xInverse = !inverse_fn.IsEmpty() ? CustomAggregate::xInverse : nullptr;
  auto xValue = xInverse ? CustomAggregate::xValue : nullptr;
  int result = sqlite3_create_window_function(
      connection(), name.c_str(), argc, flags, user_data,
      CustomAggregate::xStep, CustomAggregate::xFinal, xValue, xInverse,
      CustomAggregate::xDestroy);

  if (result != SQLITE_OK) {
    // As in CustomFunction(): SQLite has already invoked xDestroy on
    // user_data, so it must not be deleted here.
    std::string error = "Failed to create aggregate function '";
    error += name;
    error += "': ";
    error += sqlite3_errmsg(connection());
    error += " (SQLite error code: ";
    error += std::to_string(result);
    error += ")";
    node::THROW_ERR_SQLITE_ERROR(env, error.c_str());
  }

  return env.Undefined();
}

// Port of node:sqlite's Database::CreateModule. The validation order and
// messages follow src/upstream/node_sqlite.cc.
Napi::Value DatabaseSync::CreateModule(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"name\" argument must be a string.");
    return env.Undefined();
  }

  if (!info[1].IsObject()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options\" argument must be an object.");
    return env.Undefined();
  }

  std::string name = info[0].As<Napi::String>().Utf8Value();
  Napi::Object options = info[1].As<Napi::Object>();

  Napi::Value columns_v = options.Get("columns");
  if (!columns_v.IsArray()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options.columns\" argument must be an array.");
    return env.Undefined();
  }

  Napi::Array columns = columns_v.As<Napi::Array>();
  uint32_t num_columns = columns.Length();

  if (num_columns == 0) {
    node::THROW_ERR_INVALID_ARG_VALUE(
        env, "The \"options.columns\" array must not be empty.");
    return env.Undefined();
  }

  Napi::Value rows_v = options.Get("rows");
  if (!rows_v.IsFunction()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"options.rows\" argument must be a function.");
    return env.Undefined();
  }
  Napi::Function rows_fn = rows_v.As<Napi::Function>();

  bool direct_only = false;
  Napi::Value direct_only_v = options.Get("directOnly");
  if (!direct_only_v.IsUndefined()) {
    if (!direct_only_v.IsBoolean()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options.directOnly\" argument must be a boolean.");
      return env.Undefined();
    }
    direct_only = direct_only_v.As<Napi::Boolean>().Value();
  }

  bool use_bigint_args = false;
  Napi::Value use_bigint_args_v = options.Get("useBigIntArguments");
  if (!use_bigint_args_v.IsUndefined()) {
    if (!use_bigint_args_v.IsBoolean()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env,
          "The \"options.useBigIntArguments\" argument must be a boolean.");
      return env.Undefined();
    }
    use_bigint_args = use_bigint_args_v.As<Napi::Boolean>().Value();
  }

  // Build CREATE TABLE schema SQL from columns.
  std::string schema_sql = "CREATE TABLE x(";
  std::vector<int> hidden_col_indices;

  for (uint32_t i = 0; i < num_columns; i++) {
    Napi::Value col_v = columns.Get(i);
    if (!col_v.IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "Each column in \"options.columns\" must be an object.");
      return env.Undefined();
    }
    Napi::Object col = col_v.As<Napi::Object>();

    Napi::Value col_name_v = col.Get("name");
    if (!col_name_v.IsString()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The column \"name\" property must be a string.");
      return env.Undefined();
    }
    std::string col_name = col_name_v.As<Napi::String>().Utf8Value();

    Napi::Value col_type_v = col.Get("type");
    if (!col_type_v.IsString()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The column \"type\" property must be a string.");
      return env.Undefined();
    }
    std::string col_type = col_type_v.As<Napi::String>().Utf8Value();

    bool hidden = false;
    Napi::Value hidden_v = col.Get("hidden");
    if (!hidden_v.IsUndefined()) {
      if (!hidden_v.IsBoolean()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The column \"hidden\" property must be a boolean.");
        return env.Undefined();
      }
      hidden = hidden_v.As<Napi::Boolean>().Value();
    }

    if (hidden) {
      hidden_col_indices.push_back(static_cast<int>(i));
    }

    if (i > 0) {
      schema_sql += ", ";
    }

    // Validate column type against allowed SQLite type names.
    if (col_type != "INTEGER" && col_type != "TEXT" && col_type != "REAL" &&
        col_type != "BLOB" && col_type != "ANY") {
      node::THROW_ERR_INVALID_ARG_VALUE(
          env, "The column \"type\" property must be one of "
               "'INTEGER', 'TEXT', 'REAL', 'BLOB', or 'ANY'.");
      return env.Undefined();
    }

    // Quote column name to prevent SQL injection.
    schema_sql += "\"";
    for (char c : col_name) {
      if (c == '"') {
        schema_sql += "\"\"";
      } else {
        schema_sql += c;
      }
    }
    schema_sql += "\" ";
    schema_sql += col_type;

    if (hidden) {
      schema_sql += " HIDDEN";
    }
  }

  schema_sql += ")";

  // Reading the options bag and the column definitions above can run user
  // JavaScript through a property getter, which may have closed the database
  // since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  auto *module = new VirtualTableModule(
      env, this, rows_fn, std::move(schema_sql), static_cast<int>(num_columns),
      std::move(hidden_col_indices), use_bigint_args, direct_only);

  // On failure SQLite has already called xDestroyModule on `module`, so it
  // must not be deleted here.
  int r =
      sqlite3_create_module_v2(connection(), name.c_str(), module->module_def(),
                               module, VirtualTableModule::xDestroyModule);
  if (r != SQLITE_OK) {
    ThrowEnhancedSqliteErrorWithDB(env, this, connection(), r,
                                   sqlite3_errmsg(connection()));
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::EnableLoadExtension(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"allow\" argument must be a boolean.");
    return env.Undefined();
  }

  bool enable = info[0].As<Napi::Boolean>().Value();

  // Check if extension loading was disallowed at database creation
  if (!allow_load_extension_ && enable) {
    node::THROW_ERR_INVALID_STATE(env,
                                  "Cannot enable extension loading because it "
                                  "was disabled at database creation.");
    return env.Undefined();
  }

  enable_load_extension_ = enable;

  // Configure SQLite to enable/disable extension loading
  int result =
      sqlite3_db_config(connection(), SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
                        enable ? 1 : 0, nullptr);

  if (result != SQLITE_OK) {
    std::string error = "Failed to configure extension loading: ";
    error += sqlite3_errmsg(connection());
    node::THROW_ERR_SQLITE_ERROR(env, error.c_str());
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::LoadExtension(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!allow_load_extension_) {
    node::THROW_ERR_INVALID_STATE(env, "Extension loading is not allowed");
    return env.Undefined();
  }

  if (!enable_load_extension_) {
    node::THROW_ERR_INVALID_STATE(env, "Extension loading is not enabled");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The \"path\" argument must be a string.");
    return env.Undefined();
  }

  std::string path = info[0].As<Napi::String>().Utf8Value();

  // Optional entry point parameter
  const char *entry_point = nullptr;
  std::string entry_point_str;
  if (info.Length() > 1 && info[1].IsString()) {
    entry_point_str = info[1].As<Napi::String>().Utf8Value();
    entry_point = entry_point_str.c_str();
  }

  // Load the extension
  // Extension entry points receive this connection and may execute SQL that
  // invokes its authorizer. Start a fresh deferred-error scope and consume it
  // even when the extension ignores the SQL error and reports successful init.
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);
  char *errmsg = nullptr;
  int result =
      sqlite3_load_extension(connection(), path.c_str(), entry_point, &errmsg);

  if (HasDeferredAuthorizerException()) {
    if (errmsg != nullptr) {
      sqlite3_free(errmsg);
    }
    RethrowDeferredAuthorizerException();
    return env.Undefined();
  }

  if (result != SQLITE_OK) {
    std::string error = "Failed to load extension '";
    error += path;
    error += "': ";
    if (errmsg) {
      error += errmsg;
      sqlite3_free(errmsg);
    } else {
      error += sqlite3_errmsg(connection());
    }
    node::THROW_ERR_SQLITE_ERROR(env, error.c_str());
    return env.Undefined();
  }

  return env.Undefined();
}

Napi::Value DatabaseSync::EnableDefensive(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"active\" argument must be a boolean.");
    return env.Undefined();
  }

  int enable = info[0].As<Napi::Boolean>().Value() ? 1 : 0;
  int defensive_enabled;
  int result = sqlite3_db_config(connection(), SQLITE_DBCONFIG_DEFENSIVE,
                                 enable, &defensive_enabled);
  if (result != SQLITE_OK) {
    node::ThrowEnhancedSqliteError(env, connection(), result,
                                   "Failed to set defensive mode");
    return env.Undefined();
  }
  // Open() applies config_, so a reopened connection keeps this setting.
  config_.set_enable_defensive(enable != 0);

  return env.Undefined();
}

Napi::Value DatabaseSync::CreateSession(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  std::string table;
  std::string db_name = "main";

  // Parse options if provided
  if (info.Length() > 0) {
    if (!info[0].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }

    Napi::Object options = info[0].As<Napi::Object>();

    // Get table option
    if (options.Has("table")) {
      Napi::Value table_value = options.Get("table");
      if (table_value.IsString()) {
        table = table_value.As<Napi::String>().Utf8Value();
      } else {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.table\" argument must be a string.");
        return env.Undefined();
      }
    }

    // Get db option
    if (options.Has("db")) {
      Napi::Value db_value = options.Get("db");
      if (db_value.IsString()) {
        db_name = db_value.As<Napi::String>().Utf8Value();
      } else {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.db\" argument must be a string.");
        return env.Undefined();
      }
    }
  }

  // Reading the options bag above can run user JavaScript through a property
  // getter, which may have closed the database since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Create the session
  sqlite3_session *pSession;
  int r = sqlite3session_create(connection(), db_name.c_str(), &pSession);

  if (r != SQLITE_OK) {
    std::string error = "Failed to create session: ";
    error += sqlite3_errmsg(connection());
    node::THROW_ERR_SQLITE_ERROR(env, error.c_str());
    return env.Undefined();
  }

  // Attach table if specified
  r = sqlite3session_attach(pSession, table.empty() ? nullptr : table.c_str());

  if (r != SQLITE_OK) {
    sqlite3session_delete(pSession);
    std::string error = "Failed to attach table to session: ";
    error += sqlite3_errmsg(connection());
    node::THROW_ERR_SQLITE_ERROR(env, error.c_str());
    return env.Undefined();
  }

  // Create and return the Session object
  return Session::Create(env, this, pSession);
}

void DatabaseSync::AddSession(Session *session) {
  std::lock_guard<std::mutex> lock(sessions_mutex_);
  sessions_.insert(session);
}

void DatabaseSync::RemoveSession(Session *session) {
  std::lock_guard<std::mutex> lock(sessions_mutex_);
  sessions_.erase(session);
}

void DatabaseSync::DeleteAllSessions() {
  std::lock_guard<std::mutex> lock(sessions_mutex_);
  // Retain the set: these Session wrappers may outlive us, and ~DatabaseSync
  // needs the list to clear their back-pointers via DetachAllSessions().
  // Iteration is safe because nothing below re-enters Add/RemoveSession.
  for (auto *session : sessions_) {
    // Direct SQLite cleanup since we're in database destruction
    if (session->GetSession()) {
      sqlite3session_delete(session->GetSession());
      // Clear the session pointer but KEEP database_ so we can detect
      // "database closed" vs "session closed" in Session methods. This object
      // is still alive here, so the back-pointer stays valid.
      session->session_ = nullptr;
    }
  }
}

void DatabaseSync::DetachAllSessions() {
  std::lock_guard<std::mutex> lock(sessions_mutex_);
  for (auto *session : sessions_) {
    // Session::Delete() checks database_ before calling RemoveSession(), so
    // nulling it here also keeps a later ~Session from touching this object.
    session->database_ = nullptr;
  }
  sessions_.clear();
}

void DatabaseSync::AddBackup(BackupJob *backup) {
  std::lock_guard<std::mutex> lock(backups_mutex_);
  backups_.insert(backup);
}

void DatabaseSync::RemoveBackup(BackupJob *backup) {
  std::lock_guard<std::mutex> lock(backups_mutex_);
  backups_.erase(backup);
}

void DatabaseSync::FinalizeBackups() {
  // Copy the set while holding the lock, then release before cleanup
  // This prevents deadlock if destructor calls RemoveBackup
  std::set<BackupJob *> backups_copy;
  {
    std::lock_guard<std::mutex> lock(backups_mutex_);
    backups_copy = backups_;
    backups_.clear(); // Clear now to prevent destructor issues
  }

  // Clean up each active backup without holding the lock
  // We clear source_ to prevent the destructor from trying to
  // RemoveBackup (the set is already empty anyway)
  for (auto *backup : backups_copy) {
    backup->ClearSource(); // Prevent RemoveBackup call in destructor
    backup->Abandon();
  }
}

void DatabaseSync::TrackStatement(StatementSync *stmt) {
  std::lock_guard<std::mutex> lock(statements_mutex_);
  statements_.insert(stmt);
}

void DatabaseSync::UntrackStatement(StatementSync *stmt) {
  std::lock_guard<std::mutex> lock(statements_mutex_);
  statements_.erase(stmt);
}

void DatabaseSync::FinalizeStatements() {
  // Take the set under lock then drain it; FinalizeFromDatabase nulls each
  // statement's database_ so its destructor will not call UntrackStatement.
  // Finalizing can run JavaScript, since a virtual table cursor's xClose calls
  // its iterator's return(), and that can prepare another statement. Repeat
  // until none is left, so the caller never closes or replaces the connection
  // under a statement that still points at this database.
  while (true) {
    std::set<StatementSync *> statements_copy;
    {
      std::lock_guard<std::mutex> lock(statements_mutex_);
      if (statements_.empty()) {
        return;
      }
      statements_copy.swap(statements_);
    }

    for (auto *stmt : statements_copy) {
      stmt->FinalizeFromDatabase();
    }
  }
}

// Context structure for changeset callbacks to avoid global state
struct ChangesetCallbacks {
  std::function<int(int)> conflictCallback;
  std::function<bool(std::string)> filterCallback;
  Napi::Env env;
  // Store pending exception for re-throwing after SQLite call
  std::string pendingExceptionMessage;
  bool hasPendingException = false;
};

// Helper to extract error message from Napi::Error
// Handles both Error objects (use .message property) and primitive throws.
// For GetAndClearPendingException(), err.Value() returns the ORIGINAL thrown
// value - if JS throws "string", Value() IS the string, not a wrapper.
// See: node-addon-api/test/error.cc line 306-310 (CatchError function)
static std::string GetErrorMessage(const Napi::Error &err,
                                   const char *fallback) {
  // Try 1: Message() works for Error objects with .message property
  try {
    std::string msg = err.Message();
    if (!msg.empty()) {
      return msg;
    }
  } catch (...) {
    // Message() failed, continue trying
  }

  // Try 2: For primitives or when Message() is empty, err.Value() returns
  // the original thrown value. Just convert it to string directly.
  // This works for: throw "string", throw 42, throw new Error("msg")
  try {
    Napi::Value val = err.Value();
    if (!val.IsEmpty() && !val.IsUndefined() && !val.IsNull()) {
      return val.ToString().Utf8Value();
    }
  } catch (...) {
    // Value() or ToString() failed, continue
  }

  // Try 3: For C++ catch blocks, check ERROR_WRAP_VALUE property
  // (only relevant when catching Napi::Error as C++ exception)
  try {
    Napi::Value val = err.Value();
    if (val.IsObject()) {
      Napi::Object errObj = val.As<Napi::Object>();
      static const char *ERROR_WRAP_VALUE =
          "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
      Napi::Value wrapped = errObj.Get(ERROR_WRAP_VALUE);
      if (!wrapped.IsUndefined()) {
        return wrapped.ToString().Utf8Value();
      }
    }
  } catch (...) {
    // Property access failed, continue
  }

  return fallback;
}

// Calls a filter or onConflict callback with the value `make_arg` returns, and
// returns `convert` applied to its result. SQLite calls these from C, so no C++
// exception may leave: a failure is recorded in `callbacks` for ApplyChangeset
// to throw, and `failed` is returned instead, as it is for every call after
// one failed. `name` is "onConflict" or "filter", and `threw` is the message
// for an exception that has none.
template <typename T, typename MakeArg, typename Convert>
static T CallChangesetCallback(ChangesetCallbacks &callbacks, Napi::Env env,
                               const Napi::Function &fn, const char *name,
                               const char *threw, T failed, MakeArg make_arg,
                               Convert convert) {
  if (callbacks.hasPendingException) {
    return failed;
  }
  auto fail = [&](std::string message) {
    callbacks.pendingExceptionMessage = std::move(message);
    callbacks.hasPendingException = true;
    return failed;
  };
  try {
    Napi::HandleScope scope(env);
    // Raw Node-API (see pending_exception.h).
    napi_value argv[] = {make_arg()};
    napi_value result;
    if (napi_call_function(env, env.Undefined(), fn, 1, argv, &result) !=
        napi_ok) {
      return fail(GetErrorMessage(env.GetAndClearPendingException(), threw));
    }
    return convert(Napi::Value(env, result));
  } catch (const Napi::Error &e) {
    // Catch Napi::Error specifically (inherits from std::exception)
    return fail(GetErrorMessage(e, threw));
  } catch (const std::exception &e) {
    // Catch non-Napi C++ exceptions (e.g., SqliteException)
    return fail(std::string("C++ exception in ") + name + ": " + e.what());
  } catch (...) {
    if (env.IsExceptionPending()) {
      std::string message = std::string("Exception in ") + name + " callback";
      return fail(
          GetErrorMessage(env.GetAndClearPendingException(), message.c_str()));
    }
    return fail(std::string("Unknown exception in ") + name + " callback");
  }
}

static int xConflict(void *pCtx, int eConflict, sqlite3_changeset_iter *pIter) {
  if (!pCtx)
    return SQLITE_CHANGESET_ABORT;
  ChangesetCallbacks *callbacks = static_cast<ChangesetCallbacks *>(pCtx);
  if (!callbacks->conflictCallback)
    return SQLITE_CHANGESET_ABORT;
  return callbacks->conflictCallback(eConflict);
}

static int xFilter(void *pCtx, const char *zTab) {
  if (!pCtx)
    return 1;
  ChangesetCallbacks *callbacks = static_cast<ChangesetCallbacks *>(pCtx);
  // Skip filter callback if we already have a pending exception
  if (callbacks->hasPendingException)
    return 0;
  if (!callbacks->filterCallback)
    return 1;
  return callbacks->filterCallback(zTab) ? 1 : 0;
}

Napi::Value DatabaseSync::ApplyChangeset(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsTypedArray()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"changeset\" argument must be a Uint8Array.");
    return env.Undefined();
  }

  // Create callback context to avoid global state
  ChangesetCallbacks callbacks{nullptr, nullptr, env, "", false};

  // Parse options if provided
  if (info.Length() > 1 && !info[1].IsUndefined()) {
    if (!info[1].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }

    Napi::Object options = info[1].As<Napi::Object>();

    // Handle onConflict callback
    if (options.Has("onConflict")) {
      Napi::Value conflictValue = options.Get("onConflict");
      if (!conflictValue.IsUndefined()) {
        if (!conflictValue.IsFunction()) {
          node::THROW_ERR_INVALID_ARG_TYPE(
              env, "The \"options.onConflict\" argument must be a function.");
          return env.Undefined();
        }

        Napi::Function conflictFunc = conflictValue.As<Napi::Function>();
        callbacks.conflictCallback = [&callbacks, env,
                                      conflictFunc](int conflictType) -> int {
          return CallChangesetCallback(
              callbacks, env, conflictFunc, "onConflict",
              "onConflict callback threw an exception", SQLITE_CHANGESET_ABORT,
              [&] { return Napi::Number::New(env, conflictType); },
              [](Napi::Value result) {
                // Return -1 (invalid value) for non-integer results
                // This makes SQLite return SQLITE_MISUSE
                return result.IsNumber()
                           ? result.As<Napi::Number>().Int32Value()
                           : -1;
              });
        };
      }
    }

    // Handle filter callback
    if (options.Has("filter")) {
      Napi::Value filterValue = options.Get("filter");
      if (!filterValue.IsFunction()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.filter\" argument must be a function.");
        return env.Undefined();
      }

      Napi::Function filterFunc = filterValue.As<Napi::Function>();
      callbacks.filterCallback = [&callbacks, env,
                                  filterFunc](std::string tableName) -> bool {
        return CallChangesetCallback(
            callbacks, env, filterFunc, "filter",
            "Filter callback threw an exception", false,
            [&] { return Napi::String::New(env, tableName); },
            [](Napi::Value result) { return result.ToBoolean().Value(); });
      };
    }
  }

  // Reading the options bag above can run user JavaScript through a property
  // getter, which may have closed the database since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Get the changeset data from TypedArray (Uint8Array or Buffer)
  Napi::TypedArray typed_array = info[0].As<Napi::TypedArray>();
  Napi::ArrayBuffer array_buffer = typed_array.ArrayBuffer();
  size_t byte_offset = typed_array.ByteOffset();
  size_t byte_length = typed_array.ByteLength();
  uint8_t *data = static_cast<uint8_t *>(array_buffer.Data()) + byte_offset;

  if (byte_length > static_cast<size_t>(std::numeric_limits<int>::max())) {
    node::THROW_ERR_OUT_OF_RANGE(env, "The changeset is too large.");
    return env.Undefined();
  }

  // SQLite can invoke JavaScript through a filter or conflict callback, and
  // also through a user-defined SQL function reached from a CHECK constraint
  // or trigger, so JavaScript can run even with no callbacks supplied. Give
  // SQLite a private copy it cannot detach or overwrite mid-apply. Ports
  // https://github.com/nodejs/node/pull/65870.
  std::vector<uint8_t> changeset_copy;
  if (byte_length > 0) {
    changeset_copy.assign(data, data + byte_length);
    data = changeset_copy.data();
  }

  // sqlite3changeset_apply runs internal SQL that can invoke the authorizer.
  // Start a fresh deferred-error scope for this outer SQLite call.
  ClearDeferredAuthorizerException();
  SetIgnoreNextSQLiteError(false);

  // Apply the changeset with context instead of global state
  int r;
  {
    auto callback_guard = EnterCallback();
    r = sqlite3changeset_apply(connection(), static_cast<int>(byte_length),
                               data, xFilter, xConflict, &callbacks);
  }

  // SQLite cleanup SQL runs after filter/conflict callbacks. Match Node's
  // last-exception-wins behavior by surfacing a later authorizer exception
  // before an earlier callback exception.
  if (HasDeferredAuthorizerException()) {
    RethrowDeferredAuthorizerException();
    return env.Undefined();
  }

  // Check for pending exception from callbacks - re-throw it
  if (callbacks.hasPendingException) {
    SetIgnoreNextSQLiteError(false);
    Napi::Error::New(env, callbacks.pendingExceptionMessage)
        .ThrowAsJavaScriptException();
    return env.Undefined();
  }

  if (r == SQLITE_OK) {
    return Napi::Boolean::New(env, true);
  }

  if (r == SQLITE_ABORT) {
    // Not an error, just means the operation was aborted
    return Napi::Boolean::New(env, false);
  }

  // Other errors - use enhanced error with errcode property
  const char *errMsg = sqlite3_errmsg(connection());
  node::ThrowEnhancedSqliteError(env, connection(), r, errMsg);
  return env.Undefined();
}

// StatementSync Implementation
Napi::Object StatementSync::Init(Napi::Env env, Napi::Object exports) {
  Napi::Function func = DefineClass(
      env, "Statement",
      {InstanceMethod("run", &StatementSync::Run),
       InstanceMethod("get", &StatementSync::Get),
       InstanceMethod("all", &StatementSync::All),
       InstanceMethod("iterate", &StatementSync::Iterate),
       InstanceMethod("setReadBigInts", &StatementSync::SetReadBigInts),
       InstanceMethod("setReturnArrays", &StatementSync::SetReturnArrays),
       InstanceMethod("setAllowBareNamedParameters",
                      &StatementSync::SetAllowBareNamedParameters),
       InstanceMethod("setAllowUnknownNamedParameters",
                      &StatementSync::SetAllowUnknownNamedParameters),
       InstanceMethod("columns", &StatementSync::Columns),
       InstanceMethod("stat", &StatementSync::Stat),
       InstanceMethod("resetStats", &StatementSync::ResetStats),
       InstanceMethod("close", &StatementSync::Close),
       InstanceAccessor("sourceSQL", &StatementSync::SourceSQLGetter, nullptr),
       // Symbol-keyed so they stay out of the node:sqlite API surface; the
       // TypeScript SQLTagStore reads what node:sqlite's native tag store
       // reads directly off the statement.
       InstanceAccessor(
           Napi::Symbol::For(env, "photostructure.sqlite.parameterCount"),
           &StatementSync::ParameterCountGetter, nullptr),
       InstanceAccessor(
           Napi::Symbol::For(env, "photostructure.sqlite.finalized"),
           &StatementSync::IsFinalizedGetter, nullptr),
       InstanceAccessor("expandedSQL", &StatementSync::ExpandedSQLGetter,
                        nullptr)});

  // Store constructor in per-instance addon data instead of static variable
  AddonData *addon_data = GetAddonData(env);
  if (addon_data) {
    addon_data->statementSyncConstructor =
        Napi::Reference<Napi::Function>::New(func);
    addon_data->statementSyncDatabaseKey = Napi::Reference<Napi::Value>::New(
        Napi::Symbol::New(env, "StatementSync database"), 1);
  }

  // Add Symbol.dispose to the prototype so `using stmt = db.prepare(...)`
  // deterministically finalizes the statement.
  Napi::Value symbolDispose =
      env.Global().Get("Symbol").As<Napi::Object>().Get("dispose");
  if (!symbolDispose.IsUndefined()) {
    func.Get("prototype")
        .As<Napi::Object>()
        .Set(symbolDispose,
             Napi::Function::New(
                 env, [](const Napi::CallbackInfo &info) -> Napi::Value {
                   StatementSync *stmt =
                       StatementSync::Unwrap(info.This().As<Napi::Object>());
                   return stmt->Dispose(info);
                 }));
  }

  // Node.js PR #65988 renamed StatementSync to Statement and kept the old
  // name as an alias (DEP0211).
  exports.Set("Statement", func);
  exports.Set("StatementSync", func);
  return exports;
}

StatementSync::StatementSync(const Napi::CallbackInfo &info)
    : Napi::ObjectWrap<StatementSync>(info),
      creation_thread_(std::this_thread::get_id()) {
  // Constructor - initialization happens in InitStatement
}

void StatementSync::InitStatement(DatabaseSync *database,
                                  const std::string &sql, bool persistent) {
  if (!database || !database->IsOpen()) {
    throw std::runtime_error("database is not open");
  }

  database_ = database;
  database->TrackStatement(this);
  source_sql_ = sql;

  // Apply database-level defaults
  use_big_ints_ = database->config_.get_read_big_ints();
  return_arrays_ = database->config_.get_return_arrays();
  allow_bare_named_params_ = database->config_.get_allow_bare_named_params();
  allow_unknown_named_params_ =
      database->config_.get_allow_unknown_named_params();

  // Prepare the statement
  const char *tail = nullptr;
  int result;
  {
    database->EnterStatementStep();
    result = sqlite3_prepare_v3(database->connection(), sql.c_str(), -1,
                                persistent ? SQLITE_PREPARE_PERSISTENT : 0,
                                &statement_, &tail);
    database->LeaveStatementStep();
  }

  if (result != SQLITE_OK) {
    // Handle deferred authorizer exceptions:
    //
    // When an authorizer callback throws a JavaScript exception, we use a
    // "marker" exception pattern to safely propagate the error:
    //
    // 1. On Windows (MSVC), std::exception::what() can sometimes return an
    //    empty string, causing message loss.
    //
    // 2. DatabaseSync holds Napi::Error's persistent reference until this
    //    SQLite call unwinds, preserving the exact thrown JavaScript value.
    //
    // 3. This matches Node.js's behavior where JavaScript exceptions from
    //    authorizer callbacks propagate correctly to the caller.
    if (database->HasDeferredAuthorizerException()) {
      // Throw a marker exception; the exact JavaScript error is retained by
      // DatabaseSync and rethrown by Prepare().
      throw std::runtime_error("");
    }
    // Use sqlite3_errmsg directly without prefix - matches Node.js error format
    std::string error = sqlite3_errmsg(database->connection());
    // Use SqliteException to capture error info - avoids Windows ARM ABI issues
    // with std::runtime_error::what() returning corrupted strings
    throw SqliteException(database->connection(), result, error);
  }
}

StatementSync::~StatementSync() {
  // If database_ is non-null, the database is still alive and tracking us;
  // remove ourselves from its set. When the database is torn down it nulls
  // database_ via FinalizeFromDatabase() before destruction, so we never
  // call back into a freed DatabaseSync. This pattern avoids N-API Reset()
  // calls during GC, which caused JIT corruption on Alpine/musl.
  // See: commit 4da0638, nodejs/node-addon-api#660
  DatabaseSync *database = database_;
  if (database) {
    database->UntrackStatement(this);
  }
  if (statement_ && !finalized_) {
    // This runs from a finalizer, so finalizing must not reach back into
    // JavaScript, as a virtual table cursor's xClose would.
    std::optional<DatabaseSync::DestructorScope> destructor_scope;
    if (database) {
      destructor_scope.emplace(database);
      database->EnterTraceSuppression();
    }
    sqlite3_finalize(statement_);
    if (database) {
      database->LeaveTraceSuppression();
    }
  }
}

void StatementSync::FinalizeFromDatabase() {
  sqlite3_stmt *statement = finalized_ ? nullptr : statement_;
  DatabaseSync *database = database_;
  // Mark this statement finalized before SQLite finalizes it, as node:sqlite's
  // unique_ptr::reset() does: finalizing closes virtual table cursors, whose
  // iterator's return() can reach this statement again. Also detach from the
  // database so ~StatementSync will not call UntrackStatement back into a
  // database that is being torn down or has already cleared its tracking set.
  statement_ = nullptr;
  finalized_ = true;
  database_ = nullptr;
  if (statement) {
    database->EnterTraceSuppression();
    sqlite3_finalize(statement);
    database->LeaveTraceSuppression();
  }
}

inline int StatementSync::ResetStatement() {
  reset_generation_++;
  return sqlite3_reset(statement_);
}

Napi::Value StatementSync::Run(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(env, "Statement is not properly initialized");
    return env.Undefined();
  }

  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  try {
    // Held across reset, binding, and stepping: binding a named parameter runs
    // JavaScript getters, and re-entering this statement there would reset the
    // virtual machine a second time behind the outer call's back.
    StepGuard guard(this);
    if (!Reset()) {
      return env.Undefined();
    }
    BindParameters(info);

    // Check if BindParameters set a pending exception
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    // Execute the statement
    sqlite3_step(statement_);
    // Reset immediately after step to ensure sqlite3_changes() returns
    // correct value. This fixes an issue where RETURNING queries would
    // report changes: 0 on the first call.
    // See: https://github.com/nodejs/node/issues/57344
    int result = ResetStatement();

    if (result != SQLITE_OK) {
      std::string error = sqlite3_errmsg(database_->connection());
      ThrowEnhancedSqliteErrorWithDB(env, database_, database_->connection(),
                                     result, error);
      return env.Undefined();
    }

    // Create result object
    Napi::Object result_obj = Napi::Object::New(env);

    // Get changes and lastInsertRowid
    sqlite3_int64 changes = sqlite3_changes64(database_->connection());
    sqlite3_int64 last_rowid =
        sqlite3_last_insert_rowid(database_->connection());

    Napi::Value last_rowid_value =
        IntegerToValue(env, last_rowid, use_big_ints_);
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }
    Napi::Value changes_value = IntegerToValue(env, changes, use_big_ints_);
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    result_obj.Set("changes", changes_value);
    result_obj.Set("lastInsertRowid", last_rowid_value);

    return result_obj;
  } catch (const Napi::Error &e) {
    // A JavaScript exception, such as one a named parameter's getter threw, is
    // rethrown as is. An error built from e.what() would replace it, and a
    // thrown primitive has no message.
    e.ThrowAsJavaScriptException();
    return env.Undefined();
  } catch (const std::exception &e) {
    ThrowErrSqliteErrorWithDb(env, database_, e.what());
    return env.Undefined();
  }
}

Napi::Value StatementSync::Get(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(env, "Statement is not properly initialized");
    return env.Undefined();
  }

  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  // Held across reset, binding, and stepping: binding a named parameter runs
  // JavaScript getters, and re-entering this statement there would reset the
  // virtual machine a second time behind the outer call's back. Declared
  // outside the try so the catch block's reset is covered too: resetting can
  // run a virtual table iterator's return().
  StepGuard guard(this);
  try {
    if (!Reset()) {
      return env.Undefined();
    }
    BindParameters(info);

    // Check if BindParameters set a pending exception
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    int result = sqlite3_step(statement_);

    if (result == SQLITE_ROW) {
      Napi::Value value = CreateResult();
      // Reset statement after fetching result to release locks (like Node.js
      // OnScopeLeave)
      ResetStatement();
      return value;
    } else if (result == SQLITE_DONE) {
      // Reset statement to release locks even when no rows returned
      ResetStatement();
      return env.Undefined();
    } else {
      // Reset statement before throwing to release locks
      ResetStatement();
      std::string error = sqlite3_errmsg(database_->connection());
      ThrowEnhancedSqliteErrorWithDB(env, database_, database_->connection(),
                                     result, error);
      return env.Undefined();
    }
  } catch (const Napi::Error &e) {
    // A JavaScript exception is rethrown as is (see Run).
    ResetStatement();
    e.ThrowAsJavaScriptException();
    return env.Undefined();
  } catch (const std::exception &e) {
    // Reset statement on exception to release locks
    ResetStatement();
    ThrowErrSqliteErrorWithDb(env, database_, e.what());
    return env.Undefined();
  }
}

Napi::Value StatementSync::All(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(env, "Statement is not properly initialized");
    return env.Undefined();
  }

  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  // Held across reset, binding, and the whole row loop: binding a named
  // parameter runs JavaScript getters, and a user function invoked by any
  // step must not be able to re-enter this statement until the loop unwinds.
  // Declared outside the try so the catch block's reset is covered too:
  // resetting can run a virtual table iterator's return().
  StepGuard guard(this);
  try {
    if (!Reset()) {
      return env.Undefined();
    }
    BindParameters(info);

    // Check if BindParameters set a pending exception
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    Napi::Array results = Napi::Array::New(env);
    uint32_t index = 0;

    // Column metadata (count + object-mode keys) is resolved once, lazily, on
    // the first row. Doing it after the first successful step means the values
    // reflect any auto-reprepare, and the key strings are built once and reused
    // for every row rather than recreated per column per row.
    int column_count = 0;
    std::vector<napi_value> keys;
    bool row_meta_ready = false;

    while (true) {
      int result = sqlite3_step(statement_);

      if (result == SQLITE_ROW) {
        if (!row_meta_ready) {
          column_count = sqlite3_column_count(statement_);
          if (!return_arrays_ && !BuildColumnKeys(env, column_count, &keys)) {
            ResetStatement();
            return env.Undefined();
          }
          row_meta_ready = true;
        }
        Napi::Value row = BuildRow(env, column_count, &keys);
        // BuildRow can set a pending exception (e.g. an integer outside the
        // safe range); stop stepping and let it propagate.
        if (env.IsExceptionPending()) {
          ResetStatement();
          return env.Undefined();
        }
        results.Set(index++, row);
      } else if (result == SQLITE_DONE) {
        // Reset statement to release locks (like Node.js OnScopeLeave)
        ResetStatement();
        break;
      } else {
        // Reset statement before throwing to release locks
        ResetStatement();
        std::string error = sqlite3_errmsg(database_->connection());
        ThrowEnhancedSqliteErrorWithDB(env, database_, database_->connection(),
                                       result, error);
        return env.Undefined();
      }
    }

    return results;
  } catch (const Napi::Error &e) {
    // A JavaScript exception is rethrown as is (see Run).
    ResetStatement();
    e.ThrowAsJavaScriptException();
    return env.Undefined();
  } catch (const std::exception &e) {
    // Reset statement on exception to release locks
    ResetStatement();
    ThrowErrSqliteErrorWithDb(env, database_, e.what());
    return env.Undefined();
  }
}

Napi::Value StatementSync::Iterate(const Napi::CallbackInfo &info) {
  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(info.Env(), "statement has been finalized");
    return info.Env().Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(info.Env(), "Database connection is closed");
    return info.Env().Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(info.Env())) {
    return info.Env().Undefined();
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(info.Env(),
                                  "Statement is not properly initialized");
    return info.Env().Undefined();
  }

  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(info.Env(),
                                  "statement is already being executed");
    return info.Env().Undefined();
  }

  {
    // Held across reset and binding: binding a named parameter runs JavaScript
    // getters, and re-entering this statement there would reset the virtual
    // machine again and hand out a second iterator over the same VM. The guard
    // is released before the iterator is returned; stepping is then guarded
    // per-call by the iterator itself.
    StepGuard guard(this);

    // Reset the statement first
    int r = ResetStatement();
    if (r != SQLITE_OK) {
      node::THROW_ERR_SQLITE_ERROR(info.Env(),
                                   sqlite3_errmsg(database_->connection()));
      return info.Env().Undefined();
    }
    // Resetting closes a cursor an unfinished iterate() left open, and its
    // iterator's return() may have thrown: leave that for the caller.
    if (info.Env().IsExceptionPending()) {
      return info.Env().Undefined();
    }

    // Bind parameters if provided
    BindParameters(info, 0);

    // Check if BindParameters set a pending exception
    if (info.Env().IsExceptionPending()) {
      return info.Env().Undefined();
    }
  }

  // Create and return iterator
  return StatementSyncIterator::Create(info.Env(), this);
}

void StatementSync::CloseStatement() {
  if (finalized_) {
    return;
  }
  // Stop the database from tracking us first, so a later close()/deserialize()
  // does not walk a statement we have already finalized.
  DatabaseSync *database = database_;
  if (database) {
    database->UntrackStatement(this);
    // Drop the back-pointer for the same reason FinalizeFromDatabase() does.
    // Once we are untracked, FinalizeStatements() will never visit us again,
    // so nothing else would ever clear it -- and ~StatementSync must not call
    // UntrackStatement() on a database that has already been finalized. N-API
    // gives no ordering guarantee between the two wrappers.
    database_ = nullptr;
  }
  // Mark this statement finalized before SQLite finalizes it, as node:sqlite's
  // unique_ptr::reset() does: finalizing closes virtual table cursors, whose
  // iterator's return() can reach this statement again.
  sqlite3_stmt *statement = statement_;
  statement_ = nullptr;
  finalized_ = true;
  if (statement) {
    // Safe to finalize even if the database is already closed; SQLite handles
    // that gracefully.
    if (database) {
      database->EnterTraceSuppression();
    }
    sqlite3_finalize(statement);
    if (database) {
      database->LeaveTraceSuppression();
    }
  }
}

Napi::Value StatementSync::Close(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  // Finalizing a statement whose sqlite3_step() is still on the stack is
  // undefined behavior. That is reachable by calling close() on the executing
  // statement from inside a user-defined function callback.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  // Finalizing a *busy* statement from an authorizer can release its locks and
  // change the outer statement's outcome. An idle statement holds no such
  // state, so finalizing it from the callback is allowed.
  if (ThrowIfBusyInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  CloseStatement();
  return env.Undefined();
}

// True (with ERR_INVALID_STATE pending) when finalizing this statement from
// inside an authorizer callback would disturb SQLite state the outer statement
// still depends on. Mirrors node:sqlite's
// THROW_AND_RETURN_IF_BUSY_IN_AUTHORIZER.
bool StatementSync::ThrowIfBusyInAuthorizerCallback(Napi::Env env) const {
  if (!database_ || !database_->IsInAuthorizerCallback() || !statement_ ||
      !sqlite3_stmt_busy(statement_)) {
    return false;
  }
  node::THROW_ERR_INVALID_STATE(
      env, "database cannot be accessed from an authorizer callback");
  return true;
}

Napi::Value StatementSync::Dispose(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // Disposal is idempotent, so an already-finalized statement is a no-op even
  // inside a callback.
  if (finalized_) {
    return env.Undefined();
  }

  // Everything past this point would free or disturb live SQLite state, so
  // dispose() reports it rather than silently skipping the finalize.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (ThrowIfBusyInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  CloseStatement();
  return env.Undefined();
}

Napi::Value StatementSync::SourceSQLGetter(const Napi::CallbackInfo &info) {
  if (finalized_ || !statement_) {
    node::THROW_ERR_INVALID_STATE(info.Env(), "statement has been finalized");
    return info.Env().Undefined();
  }
  return Napi::String::New(info.Env(), source_sql_);
}

Napi::Value StatementSync::ExpandedSQLGetter(const Napi::CallbackInfo &info) {
  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(info.Env(), "statement has been finalized");
    return info.Env().Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(info.Env(), "Database connection is closed");
    return info.Env().Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(info.Env())) {
    return info.Env().Undefined();
  }

  if (statement_) {
    std::unique_ptr<char, decltype(&sqlite3_free)> expanded(
        sqlite3_expanded_sql(statement_), sqlite3_free);
    if (expanded) {
      // A bound blob over about 256 MB expands past V8's maximum string
      // length, for which this raises ERR_STRING_TOO_LONG.
      napi_value result;
      if (!SqliteTextToValue(info.Env(), expanded.get(), strlen(expanded.get()),
                             &result)) {
        return info.Env().Undefined(); // ERR_STRING_TOO_LONG is pending
      }
      return Napi::Value(info.Env(), result);
    }
  }
  return info.Env().Undefined();
}

Napi::Value StatementSync::SetReadBigInts(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // Mutating result-shaping config while the statement is mid-step would
  // desync the in-flight all()/toArray() loop from its cached column keys
  // (an object-mode BuildRow would index a key vector that was never built),
  // so reject re-entrant mutation exactly like the stepping operations do.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"readBigInts\" argument must be a boolean.");
    return env.Undefined();
  }

  use_big_ints_ = info[0].As<Napi::Boolean>().Value();
  return env.Undefined();
}

Napi::Value StatementSync::SetReturnArrays(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // A returnArrays flip mid-step is the memory-safety case: All()/ToArray()
  // build the object-mode column-key vector once, lazily, and only when
  // return_arrays_ was false at that point. Flipping to object mode afterward
  // makes BuildRow index an empty vector (OOB read). Reject it like the other
  // re-entrant statement mutations.
  //
  // NOTE: this is an INTENTIONAL divergence from node:sqlite, whose setters
  // (setReturnArrays/setReadBigInts/setAllow*) permit mutation from callbacks
  // invoked while the statement is stepping. Upstream snapshots result-shaping
  // modes for all()/get()/run(), so a mutation there affects later operations;
  // iterator rows read the live modes after each step. We freeze all config
  // setters instead, both to prevent the cached-key OOB above and to provide
  // one consistent contract. The remaining setters guard for consistency, not
  // memory safety.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"returnArrays\" argument must be a boolean.");
    return env.Undefined();
  }

  return_arrays_ = info[0].As<Napi::Boolean>().Value();
  return env.Undefined();
}

Napi::Value
StatementSync::SetAllowBareNamedParameters(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // No statement mutation is allowed while a step is on the stack (see the
  // matching guard in SetReturnArrays); keep all config setters consistent.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"allowBareNamedParameters\" argument must be a boolean.");
    return env.Undefined();
  }

  allow_bare_named_params_ = info[0].As<Napi::Boolean>().Value();
  return env.Undefined();
}

Napi::Value
StatementSync::SetAllowUnknownNamedParameters(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // No statement mutation is allowed while a step is on the stack (see the
  // matching guard in SetReturnArrays); keep all config setters consistent.
  if (stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"enabled\" argument must be a boolean.");
    return env.Undefined();
  }

  allow_unknown_named_params_ = info[0].As<Napi::Boolean>().Value();
  return env.Undefined();
}

Napi::Value StatementSync::Columns(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // When database is closed, statement is implicitly finalized by SQLite
  if (finalized_ || !database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(env, "Statement is not properly initialized");
    return env.Undefined();
  }

  int column_count = sqlite3_column_count(statement_);
  Napi::Array columns = Napi::Array::New(env, column_count);

  for (int i = 0; i < column_count; i++) {
    Napi::Object column_info = CreateObjectWithNullPrototype(env);

    // column: The original column name (sqlite3_column_origin_name)
    napi_value origin_name;
    if (!NullableSQLiteStringToValue(
            env, sqlite3_column_origin_name(statement_, i), &origin_name)) {
      return env.Undefined(); // ERR_STRING_TOO_LONG is pending
    }
    column_info.Set("column", origin_name);

    // database: The database name (sqlite3_column_database_name)
    napi_value database_name;
    if (!NullableSQLiteStringToValue(
            env, sqlite3_column_database_name(statement_, i), &database_name)) {
      return env.Undefined(); // ERR_STRING_TOO_LONG is pending
    }
    column_info.Set("database", database_name);

    // name: The column name/alias (sqlite3_column_name)
    const char *column_name = sqlite3_column_name(statement_, i);
    if (column_name) {
      column_info.Set("name", Napi::String::New(env, column_name));
    } else {
      column_info.Set("name", env.Null());
    }

    // table: The table name (sqlite3_column_table_name)
    napi_value table_name;
    if (!NullableSQLiteStringToValue(
            env, sqlite3_column_table_name(statement_, i), &table_name)) {
      return env.Undefined(); // ERR_STRING_TOO_LONG is pending
    }
    column_info.Set("table", table_name);

    // type: The declared type (sqlite3_column_decltype)
    napi_value decl_type;
    if (!NullableSQLiteStringToValue(
            env, sqlite3_column_decltype(statement_, i), &decl_type)) {
      return env.Undefined(); // ERR_STRING_TOO_LONG is pending
    }
    column_info.Set("type", decl_type);

    columns.Set(i, column_info);
  }

  return columns;
}

namespace {
// Maps the JavaScript counter names accepted by StatementSync.prototype.stat()
// to their SQLITE_STMTSTATUS_* constants, mirroring kStatusMapping in
// node_sqlite.h. The bundled SQLite is far newer than 3.38.0, so the filter
// counters are always present.
struct StatusInfo {
  const char *js_name;
  int sqlite_status_id;
};

constexpr StatusInfo kStatusMapping[] = {
    {"fullscanStep", SQLITE_STMTSTATUS_FULLSCAN_STEP},
    {"sort", SQLITE_STMTSTATUS_SORT},
    {"autoindex", SQLITE_STMTSTATUS_AUTOINDEX},
    {"vmStep", SQLITE_STMTSTATUS_VM_STEP},
    {"reprepare", SQLITE_STMTSTATUS_REPREPARE},
    {"run", SQLITE_STMTSTATUS_RUN},
    {"filterMiss", SQLITE_STMTSTATUS_FILTER_MISS},
    {"filterHit", SQLITE_STMTSTATUS_FILTER_HIT},
    {"memused", SQLITE_STMTSTATUS_MEMUSED},
};
} // namespace

Napi::Value
StatementSync::ParameterCountGetter(const Napi::CallbackInfo &info) {
  if (finalized_ || !statement_) {
    return Napi::Number::New(info.Env(), 0);
  }
  return Napi::Number::New(info.Env(),
                           sqlite3_bind_parameter_count(statement_));
}

Napi::Value StatementSync::IsFinalizedGetter(const Napi::CallbackInfo &info) {
  return Napi::Boolean::New(info.Env(), finalized_ || !statement_ ||
                                            !database_ || !database_->IsOpen());
}

Napi::Value StatementSync::Stat(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  // A closed database implicitly finalizes its statements.
  if (finalized_ || !database_ || !database_->IsOpen() || !statement_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsString()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"counter\" argument must be a string.");
    return env.Undefined();
  }

  std::string counter = info[0].As<Napi::String>().Utf8Value();
  for (const auto &status : kStatusMapping) {
    if (counter == status.js_name) {
      // The reset flag is always false; the counter is read without clearing.
      int value =
          sqlite3_stmt_status(statement_, status.sqlite_status_id, false);
      return Napi::Number::New(env, value);
    }
  }

  node::THROW_ERR_INVALID_ARG_VALUE(
      env, "The \"counter\" argument is not a valid statistic name.");
  return env.Undefined();
}

Napi::Value StatementSync::ResetStats(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!ValidateThread(env)) {
    return env.Undefined();
  }

  if (finalized_ || !database_ || !database_->IsOpen() || !statement_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  // sqlite3_stmt_status() resets a single counter per call, so every exposed
  // counter is visited. The returned value is the pre-reset one and is unused.
  // SQLITE_STMTSTATUS_MEMUSED is skipped: it reports current memory usage
  // rather than an accumulated counter, and SQLite ignores the reset flag for
  // it.
  for (const auto &status : kStatusMapping) {
    if (status.sqlite_status_id == SQLITE_STMTSTATUS_MEMUSED) {
      continue;
    }
    sqlite3_stmt_status(statement_, status.sqlite_status_id, true);
  }

  return env.Undefined();
}

void StatementSync::BindParameters(const Napi::CallbackInfo &info,
                                   size_t start_index) {
  Napi::Env env = info.Env();

  // Safety checks
  if (finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return;
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return;
  }

  if (!statement_) {
    node::THROW_ERR_INVALID_STATE(env, "Statement is not properly initialized");
    return;
  }

  // Track where positional parameters start
  size_t positional_start = start_index;

  // Check if first argument is an object for named parameters. Binary values
  // are positional even though they are objects: IsBuffer() covers every
  // ArrayBufferView, and ArrayBuffer/SharedArrayBuffer must be excluded
  // explicitly or they bind as an empty named-parameter set (leaving the real
  // parameter unbound, i.e. silently NULL).
  if (info.Length() > start_index && info[start_index].IsObject() &&
      !info[start_index].IsBuffer() && !info[start_index].IsArray() &&
      !info[start_index].IsTypedArray() && !info[start_index].IsArrayBuffer() &&
      !IsSharedArrayBufferValue(env, info[start_index])) {
    // Named parameters binding from the object
    Napi::Object obj = info[start_index].As<Napi::Object>();
    positional_start =
        start_index + 1; // Positional args start after the object

    // Build bare named params map if needed
    if (allow_bare_named_params_ && !bare_named_params_.has_value()) {
      bare_named_params_.emplace();
      int param_count = sqlite3_bind_parameter_count(statement_);

      // Parameter indexing starts at one
      for (int i = 1; i <= param_count; ++i) {
        const char *name = sqlite3_bind_parameter_name(statement_, i);
        if (name == nullptr) {
          continue;
        }

        std::string bare_name = std::string(name + 1); // Skip the : or $ prefix
        std::string full_name = std::string(name);
        auto insertion = bare_named_params_->insert({bare_name, full_name});

        if (!insertion.second) {
          // Check if the existing mapping is the same
          auto existing_full_name = insertion.first->second;
          if (full_name != existing_full_name) {
            std::string error_msg =
                "Cannot create bare named parameter '" + bare_name +
                "' because of conflicting names '" + existing_full_name +
                "' and '" + full_name + "'.";
            node::THROW_ERR_INVALID_STATE(env, error_msg.c_str());
            return;
          }
        }
      }
    }

    // Bind named parameters
    Napi::Array keys = obj.GetPropertyNames();
    for (uint32_t j = 0; j < keys.Length(); j++) {
      Napi::Value key = keys[j];
      std::string key_str = key.As<Napi::String>().Utf8Value();

      int param_index =
          sqlite3_bind_parameter_index(statement_, key_str.c_str());
      if (param_index == 0 && allow_bare_named_params_ &&
          bare_named_params_.has_value()) {
        // Try to find bare named parameter
        auto lookup = bare_named_params_->find(key_str);
        if (lookup != bare_named_params_->end()) {
          param_index =
              sqlite3_bind_parameter_index(statement_, lookup->second.c_str());
        }
      }

      if (param_index > 0) {
        Napi::Value value = obj.Get(key_str);
        BindSingleParameter(param_index, value);
        // Check for pending exceptions set by THROW_ERR_* macros
        // (they use ThrowAsJavaScriptException which doesn't throw C++
        // exceptions)
        if (env.IsExceptionPending()) {
          return;
        }
      } else {
        // Unknown named parameter
        if (allow_unknown_named_params_) {
          // Skip unknown parameters when allowed (matches Node.js v25 behavior)
          continue;
        } else {
          // Throw error when not allowed (default behavior)
          node::THROW_ERR_INVALID_STATE(env, "Unknown named parameter '" +
                                                 key_str + "'");
          return;
        }
      }
    }
  }

  // Bind remaining positional parameters to anonymous placeholders (?)
  // This handles both: (a) all args are positional, (b) first arg was named
  // params object and remaining are positional
  if (positional_start < info.Length()) {
    int anon_idx = 1;
    int param_count = sqlite3_bind_parameter_count(statement_);

    for (size_t i = positional_start; i < info.Length(); i++) {
      // Skip to the next anonymous placeholder (unnamed or ?NNN)
      while (anon_idx <= param_count) {
        const char *param_name =
            sqlite3_bind_parameter_name(statement_, anon_idx);
        // Anonymous placeholders have nullptr name or start with '?'
        if (param_name == nullptr || param_name[0] == '?') {
          break;
        }
        anon_idx++;
      }

      // When anon_idx > param_count, SQLite will return SQLITE_RANGE
      // and we'll throw an appropriate ERR_SQLITE_ERROR

      BindSingleParameter(anon_idx, info[i]);
      // Check for pending exceptions set by THROW_ERR_* macros
      if (env.IsExceptionPending()) {
        return;
      }
      anon_idx++;
    }
  }
}

void StatementSync::BindSingleParameter(int param_index, Napi::Value param) {
  // Safety check - statement_ should be valid if we got here
  if (!statement_ || finalized_) {
    return; // Silent return since error was already thrown by caller
  }

  int rc = SQLITE_OK;

  try {
    if (param.IsNull() || param.IsUndefined()) {
      // undefined binds to NULL so that passing it explicitly matches omitting
      // the parameter altogether. Ports
      // https://github.com/nodejs/node/pull/65709.
      rc = sqlite3_bind_null(statement_, param_index);
    } else if (param.IsBigInt()) {
      // Handle BigInt before IsNumber since BigInt values should bind as int64
      bool lossless;
      int64_t bigint_val = param.As<Napi::BigInt>().Int64Value(&lossless);
      if (lossless) {
        rc = sqlite3_bind_int64(statement_, param_index,
                                static_cast<sqlite3_int64>(bigint_val));
      } else {
        // BigInt too large for SQLite int64 - throw error (matches Node.js)
        node::THROW_ERR_INVALID_ARG_VALUE(Env(),
                                          "BigInt value is too large to bind.");
        return;
      }
    } else if (param.IsNumber()) {
      // JavaScript has one Number type. Match node:sqlite by binding every
      // Number as a double; callers who need SQLite INTEGER semantics use a
      // BigInt, which is handled above.
      rc = sqlite3_bind_double(statement_, param_index,
                               param.As<Napi::Number>().DoubleValue());
    } else if (param.IsString()) {
      std::string str = param.As<Napi::String>().Utf8Value();
      // Pass the byte length, as node:sqlite does: with -1, SQLite stops at
      // the first NUL byte and binds only the text before it.
      rc = sqlite3_bind_text64(statement_, param_index, str.data(), str.size(),
                               SQLITE_TRANSIENT, SQLITE_UTF8);
    } else if (param.IsBoolean()) {
      rc = sqlite3_bind_int(statement_, param_index,
                            param.As<Napi::Boolean>().Value() ? 1 : 0);
    } else if (param.IsDataView()) {
      // IMPORTANT: Check DataView BEFORE IsBuffer() because N-API's IsBuffer()
      // returns true for ALL ArrayBufferViews (including DataView), but
      // Buffer::As() doesn't work correctly for DataView (returns length=0).
      Napi::DataView dataView = param.As<Napi::DataView>();
      Napi::ArrayBuffer arrayBuffer = dataView.ArrayBuffer();
      size_t byteOffset = dataView.ByteOffset();
      size_t byteLength = dataView.ByteLength();
      // Use non-NULL pointer for zero-length blobs to preserve BLOB vs NULL
      // distinction. See: https://sqlite.org/c3ref/bind_blob.html
      const void *data = nullptr;
      if (arrayBuffer.Data() != nullptr && byteLength > 0) {
        data = static_cast<const uint8_t *>(arrayBuffer.Data()) + byteOffset;
      }
      rc = sqlite3_bind_blob(statement_, param_index, data ? data : "",
                             SafeCastToInt(byteLength), SQLITE_TRANSIENT);
    } else if (param.IsBuffer()) {
      // Handles Buffer and TypedArray (both are ArrayBufferViews that work
      // correctly with Buffer cast - Buffer::Data() handles byte offsets
      // internally)
      Napi::Buffer<uint8_t> buffer = param.As<Napi::Buffer<uint8_t>>();
      // Use non-NULL pointer for zero-length blobs to preserve BLOB vs NULL
      // distinction. See: https://sqlite.org/c3ref/bind_blob.html
      const void *data = buffer.Data();
      rc = sqlite3_bind_blob(statement_, param_index, data ? data : "",
                             SafeCastToInt(buffer.Length()), SQLITE_TRANSIENT);
    } else if (param.IsFunction()) {
      // Functions cannot be bound to SQLite parameters - throw error
      node::THROW_ERR_INVALID_ARG_TYPE(
          Env(), ("Provided value cannot be bound to SQLite parameter " +
                  std::to_string(param_index) + ".")
                     .c_str());
      return;
    } else if (param.IsArrayBuffer()) {
      // Handle ArrayBuffer as binary data
      Napi::ArrayBuffer arrayBuffer = param.As<Napi::ArrayBuffer>();
      // Use non-NULL pointer for zero-length blobs to preserve BLOB vs NULL
      // distinction. See: https://sqlite.org/c3ref/bind_blob.html
      const void *data = arrayBuffer.Data();
      rc = sqlite3_bind_blob(statement_, param_index, data ? data : "",
                             SafeCastToInt(arrayBuffer.ByteLength()),
                             SQLITE_TRANSIENT);
    } else if (IsSharedArrayBufferValue(Env(), param)) {
      // A SharedArrayBuffer cannot be cast to Napi::ArrayBuffer, so read its
      // bytes through a Uint8Array view. SQLITE_TRANSIENT copies immediately,
      // so the view does not need to outlive this call.
      Napi::Object view = Env()
                              .Global()
                              .Get("Uint8Array")
                              .As<Napi::Function>()
                              .New({param})
                              .As<Napi::Object>();
      Napi::Buffer<uint8_t> bytes = view.As<Napi::Buffer<uint8_t>>();
      const void *data = bytes.Data();
      rc = sqlite3_bind_blob(statement_, param_index, data ? data : "",
                             SafeCastToInt(bytes.Length()), SQLITE_TRANSIENT);
    } else if (param.IsObject()) {
      // Objects and arrays cannot be bound to SQLite parameters (same as
      // Node.js behavior). Note: DataView, Buffer, TypedArray, and ArrayBuffer
      // are handled above and don't reach this branch.
      node::THROW_ERR_INVALID_ARG_TYPE(
          Env(), ("Provided value cannot be bound to SQLite parameter " +
                  std::to_string(param_index) + ".")
                     .c_str());
      return;
    } else {
      // For any other type (Symbol, etc.), throw error like Node.js does
      node::THROW_ERR_INVALID_ARG_TYPE(
          Env(), ("Provided value cannot be bound to SQLite parameter " +
                  std::to_string(param_index) + ".")
                     .c_str());
      return;
    }

    // Check the result of sqlite3_bind_*
    if (rc != SQLITE_OK) {
      sqlite3 *db_handle = database_ ? database_->connection() : nullptr;
      // Get the error message from SQLite
      const char *err_msg = sqlite3_errstr(rc);
      node::ThrowEnhancedSqliteError(Env(), db_handle, rc,
                                     err_msg ? err_msg : "SQLite error");
      return;
    }
  } catch (const Napi::Error &e) {
    // Re-throw Napi errors
    throw;
  } catch (const std::exception &e) {
    // Convert standard exceptions to Napi errors
    throw Napi::Error::New(Env(), e.what());
  }
}

// Converts one column of the current row into a JS value written to *out.
// Shared by both the array and object result paths. Returns false with a
// pending exception ONLY for an integer outside the safe range
// (ERR_OUT_OF_RANGE) or TEXT longer than V8's maximum string length
// (ERR_STRING_TOO_LONG); every other branch writes *out and returns true.
// Returning a bool lets BuildRow drop a per-column env.IsExceptionPending()
// N-API call.
bool StatementSync::GetColumnValue(Napi::Env env, int i, int column_type,
                                   napi_value *out) {
  switch (column_type) {
  case SQLITE_NULL:
    *out = env.Null();
    return true;
  case SQLITE_INTEGER: {
    sqlite3_int64 int_val = sqlite3_column_int64(statement_, i);
    if (use_big_ints_) {
      // Always return BigInt when readBigInts is true
      *out = Napi::BigInt::New(env, static_cast<int64_t>(int_val));
      return true;
    }
    if (int_val > JS_MAX_SAFE_INTEGER || int_val < JS_MIN_SAFE_INTEGER) {
      // Throw ERR_OUT_OF_RANGE for values outside safe integer range
      // (matches Node.js behavior).
      char error_msg[128];
      snprintf(error_msg, sizeof(error_msg),
               "Value is too large to be represented as a JavaScript "
               "number: %" PRId64,
               static_cast<int64_t>(int_val));
      node::THROW_ERR_OUT_OF_RANGE(env, error_msg);
      return false;
    }
    *out = Napi::Number::New(env, static_cast<double>(int_val));
    return true;
  }
  case SQLITE_FLOAT:
    *out = Napi::Number::New(env, sqlite3_column_double(statement_, i));
    return true;
  case SQLITE_TEXT: {
    const unsigned char *text = sqlite3_column_text(statement_, i);
    // sqlite3_column_text() can return NULL on OOM or encoding errors
    if (!text) {
      *out = Napi::String::New(env, "");
      return true;
    }
    // Pass the byte length (valid to read after column_text) so N-API skips a
    // strlen over the value.
    int byte_len = sqlite3_column_bytes(statement_, i);
    return SqliteTextToValue(env, reinterpret_cast<const char *>(text),
                             static_cast<size_t>(byte_len), out);
  }
  case SQLITE_BLOB: {
    const void *blob_data = sqlite3_column_blob(statement_, i);
    int blob_size = sqlite3_column_bytes(statement_, i);
    // sqlite3_column_blob() can return NULL for zero-length BLOBs or on OOM.
    // Return Uint8Array to match Node.js node:sqlite behavior.
    if (!blob_data || blob_size == 0) {
      auto array_buffer = Napi::ArrayBuffer::New(env, 0);
      *out = Napi::Uint8Array::New(env, 0, array_buffer, 0);
      return true;
    }
    auto array_buffer = Napi::ArrayBuffer::New(env, blob_size);
    memcpy(array_buffer.Data(), blob_data, blob_size);
    *out = Napi::Uint8Array::New(env, blob_size, array_buffer, 0);
    return true;
  }
  default:
    *out = env.Null();
    return true;
  }
}

// Builds the object-mode property-key strings for the current column set into
// `out_keys`. Callers invoke this once per operation (after the first
// successful step, so the names reflect any auto-reprepare) and reuse the
// resulting handles for every row, avoiding a fresh V8 string per column per
// row. The handles are plain locals scoped to the current call; we
// intentionally do not persist them in a Napi::Reference member (see the
// header note about napi_delete_reference / Alpine-musl JIT corruption).
bool StatementSync::BuildColumnKeys(Napi::Env env, int column_count,
                                    std::vector<napi_value> *out_keys) {
  out_keys->resize(column_count);
  for (int i = 0; i < column_count; i++) {
    const char *column_name = sqlite3_column_name(statement_, i);
    if (column_name == nullptr) {
      node::THROW_ERR_INVALID_STATE(env, "Cannot get name of column");
      return false;
    }
    (*out_keys)[i] = Napi::String::New(env, column_name);
  }
  return true;
}

// Builds one JS row from the current step. `keys` may be null: when provided
// (the multi-row all()/toArray() paths) object-mode properties are set with the
// caller's reused key handles, avoiding a fresh V8 string per column per row;
// when null (the single-row get()/next() paths) each property is set straight
// from sqlite3_column_name, which is leaner when there is nothing to amortize.
// `keys` is ignored in array mode.
Napi::Value StatementSync::BuildRow(Napi::Env env, int column_count,
                                    const std::vector<napi_value> *keys) {
  if (return_arrays_) {
    Napi::Array result = Napi::Array::New(env, column_count);
    for (int i = 0; i < column_count; i++) {
      int column_type = sqlite3_column_type(statement_, i);
      napi_value value;
      // GetColumnValue returns false only after setting a pending exception;
      // stop immediately and let the caller (which checks IsExceptionPending)
      // propagate it. Issuing another N-API call here would throw C++.
      if (!GetColumnValue(env, i, column_type, &value)) {
        return env.Undefined();
      }
      result.Set(static_cast<uint32_t>(i), value);
    }
    return result;
  }

  // Return result as object with null prototype (matches Node.js behavior).
  Napi::Object result = CreateObjectWithNullPrototype(env);
  for (int i = 0; i < column_count; i++) {
    int column_type = sqlite3_column_type(statement_, i);
    napi_value value;
    if (!GetColumnValue(env, i, column_type, &value)) {
      return env.Undefined();
    }
    if (keys != nullptr) {
      result.Set((*keys)[i], value);
    } else {
      result.Set(sqlite3_column_name(statement_, i), value);
    }
  }
  return result;
}

Napi::Value StatementSync::CreateResult() {
  Napi::Env env = Env();

  // Safety checks
  if (!statement_ || finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // Single-row path (get()/iterator next()): build the row directly from column
  // names. There is only one row, so caching key handles would add overhead
  // without any reuse to amortize it.
  return BuildRow(env, sqlite3_column_count(statement_), nullptr);
}

bool StatementSync::Reset() {
  // Safety check
  if (!statement_ || finalized_) {
    return true; // Silent return, error should have been caught earlier
  }

  ResetStatement();
  sqlite3_clear_bindings(statement_);
  return !Env().IsExceptionPending();
}

// ================================
// StatementSyncIterator Implementation
// ================================

Napi::Object StatementSyncIterator::Init(Napi::Env env, Napi::Object exports) {
  Napi::Function func =
      DefineClass(env, "StatementIterator",
                  {InstanceMethod("next", &StatementSyncIterator::Next),
                   InstanceMethod("return", &StatementSyncIterator::Return),
                   InstanceMethod("toArray", &StatementSyncIterator::ToArray)});

  // Set up Symbol.iterator on the prototype to make it properly iterable
  Napi::Object prototype = func.Get("prototype").As<Napi::Object>();
  Napi::Symbol iteratorSymbol = Napi::Symbol::WellKnown(env, "iterator");

  // Add [Symbol.iterator]() { return this; } to make it iterable
  prototype.Set(iteratorSymbol,
                Napi::Function::New(env, [](const Napi::CallbackInfo &info) {
                  return info.This();
                }));

  // Make our iterator inherit from Iterator.prototype so it's an instanceof
  // Iterator and gets Iterator Helper methods (map, filter, etc.)
  Napi::Object global = env.Global();
  Napi::Value iteratorValue = global.Get("Iterator");
  if (iteratorValue.IsFunction()) {
    Napi::Object iteratorProto =
        iteratorValue.As<Napi::Function>().Get("prototype").As<Napi::Object>();
    // Use Object.setPrototypeOf to set Iterator.prototype as prototype
    Napi::Object objectCtor = global.Get("Object").As<Napi::Object>();
    Napi::Function setPrototypeOf =
        objectCtor.Get("setPrototypeOf").As<Napi::Function>();
    setPrototypeOf.Call({prototype, iteratorProto});
  }

  // Store constructor in per-instance addon data instead of static variable
  AddonData *addon_data = GetAddonData(env);
  if (addon_data) {
    addon_data->statementSyncIteratorConstructor =
        Napi::Reference<Napi::Function>::New(func);
    addon_data->statementSyncIteratorStatementKey =
        Napi::Reference<Napi::Value>::New(
            Napi::Symbol::New(env, "StatementSyncIterator statement"), 1);
  }

  exports.Set("StatementSyncIterator", func);
  return exports;
}

Napi::Object StatementSyncIterator::Create(Napi::Env env, StatementSync *stmt) {
  AddonData *addon_data = GetAddonData(env);
  if (!addon_data || addon_data->statementSyncIteratorConstructor.IsEmpty()) {
    Napi::Error::New(env, "StatementSyncIterator constructor not initialized")
        .ThrowAsJavaScriptException();
    return Napi::Object::New(env);
  }
  Napi::Object obj = addon_data->statementSyncIteratorConstructor.New({});
  // Keep the statement's JS object alive for as long as the iterator is, as
  // node:sqlite's BaseObjectPtr<StatementSync> does; otherwise the statement
  // can be collected and stmt_ dangles. A Napi::Reference member would be
  // released during GC finalization, which crashes on Alpine/musl (4da0638).
  obj.DefineProperty(Napi::PropertyDescriptor::Value(
      addon_data->statementSyncIteratorStatementKey.Value().As<Napi::Symbol>(),
      stmt->Value(), napi_default));
  StatementSyncIterator *iter =
      Napi::ObjectWrap<StatementSyncIterator>::Unwrap(obj);
  iter->SetStatement(stmt);
  return obj;
}

StatementSyncIterator::StatementSyncIterator(const Napi::CallbackInfo &info)
    : Napi::ObjectWrap<StatementSyncIterator>(info), stmt_(nullptr),
      done_(false) {}

StatementSyncIterator::~StatementSyncIterator() {}

void StatementSyncIterator::SetStatement(StatementSync *stmt) {
  stmt_ = stmt;
  done_ = false;
  statement_reset_generation_ = stmt->reset_generation_;
}

Napi::Value StatementSyncIterator::Next(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!stmt_ || stmt_->finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!stmt_->database_ || !stmt_->database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // A drained iterator touches no SQLite state, so it stays usable from a
  // callback and is checked before the guards below.
  if (done_) {
    Napi::Object result = CreateObjectWithNullPrototype(env);
    result.Set("done", true);
    result.Set("value", env.Null());
    return result;
  }

  if (stmt_->database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (stmt_->stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  if (statement_reset_generation_ != stmt_->reset_generation_) {
    node::THROW_ERR_INVALID_STATE(env, "iterator was invalidated");
    return env.Undefined();
  }

  int r;
  {
    StatementSync::StepGuard guard(stmt_);
    r = sqlite3_step(stmt_->statement_);
  }

  if (r != SQLITE_ROW) {
    if (r != SQLITE_DONE) {
      std::string error = sqlite3_errmsg(stmt_->database_->connection());
      ThrowEnhancedSqliteErrorWithDB(env, stmt_->database_,
                                     stmt_->database_->connection(), r, error);
      return env.Undefined();
    }

    // End of results
    sqlite3_reset(stmt_->statement_);
    done_ = true;

    // Finishing early, as for LIMIT, closed a virtual table cursor, and its
    // iterator's return() may have thrown: leave that for the caller.
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    Napi::Object result = CreateObjectWithNullPrototype(env);
    result.Set("done", true);
    result.Set("value", env.Null());
    return result;
  }

  // Create row object using existing CreateResult method
  Napi::Value row_value = stmt_->CreateResult();
  // CreateResult can set a pending exception (e.g. an integer outside the safe
  // range); surface it instead of wrapping an undefined value.
  if (env.IsExceptionPending()) {
    return env.Undefined();
  }

  Napi::Object result = CreateObjectWithNullPrototype(env);
  result.Set("done", false);
  result.Set("value", row_value);
  return result;
}

Napi::Value StatementSyncIterator::Return(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!stmt_ || stmt_->finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!stmt_->database_ || !stmt_->database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  // A drained iterator holds no SQLite state, so return() stays available and
  // idempotent from inside a callback. A live one is reset here, which these
  // guards reject: a language-invoked return() cannot be reached mid-step, so
  // they only fire for an explicit call from inside a callback.
  if (!done_) {
    if (stmt_->database_->ThrowIfInAuthorizerCallback(env)) {
      return env.Undefined();
    }

    if (stmt_->stepping_) {
      node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
      return env.Undefined();
    }

    {
      // Resetting closes virtual table cursors, whose iterator's return() can
      // reach this statement; it must find the statement executing.
      StatementSync::StepGuard guard(stmt_);
      sqlite3_reset(stmt_->statement_);
    }
    done_ = true;
    // The return() it ran may have thrown: leave that for the caller.
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }
  }

  Napi::Object result = CreateObjectWithNullPrototype(env);
  result.Set("done", true);
  result.Set("value", env.Null());
  return result;
}

Napi::Value StatementSyncIterator::ToArray(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!stmt_ || stmt_->finalized_) {
    node::THROW_ERR_INVALID_STATE(env, "statement has been finalized");
    return env.Undefined();
  }

  if (!stmt_->database_ || !stmt_->database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "Database connection is closed");
    return env.Undefined();
  }

  if (stmt_->database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (stmt_->stepping_) {
    node::THROW_ERR_INVALID_STATE(env, "statement is already being executed");
    return env.Undefined();
  }

  Napi::Array arr = Napi::Array::New(env);
  uint32_t idx = 0;

  // Column metadata is resolved once, lazily, on the first row so the keys are
  // built a single time and reused for every row (see StatementSync::All).
  int column_count = 0;
  std::vector<napi_value> keys;
  bool row_meta_ready = false;

  StatementSync::StepGuard guard(stmt_);
  while (!done_) {
    int r = sqlite3_step(stmt_->statement_);

    if (r != SQLITE_ROW) {
      if (r != SQLITE_DONE) {
        std::string error = sqlite3_errmsg(stmt_->database_->connection());
        ThrowEnhancedSqliteErrorWithDB(
            env, stmt_->database_, stmt_->database_->connection(), r, error);
        return env.Undefined();
      }

      // End of results
      sqlite3_reset(stmt_->statement_);
      done_ = true;
      break;
    }

    if (!row_meta_ready) {
      column_count = sqlite3_column_count(stmt_->statement_);
      if (!stmt_->return_arrays_ &&
          !stmt_->BuildColumnKeys(env, column_count, &keys)) {
        // Release the read lock before surfacing the (OOM) error, matching
        // StatementSync::All's error paths.
        sqlite3_reset(stmt_->statement_);
        return env.Undefined();
      }
      row_meta_ready = true;
    }

    Napi::Value row_value = stmt_->BuildRow(env, column_count, &keys);

    // Check if BuildRow threw an error
    if (env.IsExceptionPending()) {
      return env.Undefined();
    }

    arr.Set(idx++, row_value);
  }

  return arr;
}

// Session Implementation
Napi::Object Session::Init(Napi::Env env, Napi::Object exports) {
  Napi::Function func =
      DefineClass(env, "Session",
                  {InstanceMethod("changeset", &Session::Changeset),
                   InstanceMethod("patchset", &Session::Patchset),
                   InstanceMethod("close", &Session::Close),
                   InstanceMethod("dispose", &Session::Dispose)});

  // Store constructor in per-instance addon data instead of static variable
  AddonData *addon_data = GetAddonData(env);
  if (addon_data) {
    addon_data->sessionConstructor = Napi::Reference<Napi::Function>::New(func);
    addon_data->sessionDatabaseKey = Napi::Reference<Napi::Value>::New(
        Napi::Symbol::New(env, "Session database"), 1);
  }

  // Add Symbol.dispose to the prototype (Node.js v25+ compatibility)
  Napi::Value symbolDispose =
      env.Global().Get("Symbol").As<Napi::Object>().Get("dispose");
  if (!symbolDispose.IsUndefined()) {
    func.Get("prototype")
        .As<Napi::Object>()
        .Set(symbolDispose,
             Napi::Function::New(
                 env, [](const Napi::CallbackInfo &info) -> Napi::Value {
                   Session *session =
                       Session::Unwrap(info.This().As<Napi::Object>());
                   return session->Dispose(info);
                 }));
  }

  exports.Set("Session", func);
  return exports;
}

Napi::Object Session::Create(Napi::Env env, DatabaseSync *database,
                             sqlite3_session *session) {
  AddonData *addon_data = GetAddonData(env);
  if (!addon_data || addon_data->sessionConstructor.IsEmpty()) {
    Napi::Error::New(env, "Session constructor not initialized")
        .ThrowAsJavaScriptException();
    return Napi::Object::New(env);
  }
  Napi::Object obj = addon_data->sessionConstructor.New({});
  // Keep the database's JS object alive for as long as the session is, as
  // node:sqlite's BaseObjectPtr<DatabaseSync> does; otherwise a caller that
  // keeps only the session loses the database to GC, whose finalizer closes
  // the connection and deletes the session. A Napi::Reference member would be
  // released during GC finalization, which crashes on Alpine/musl (4da0638).
  obj.DefineProperty(Napi::PropertyDescriptor::Value(
      addon_data->sessionDatabaseKey.Value().As<Napi::Symbol>(),
      database->Value(), napi_default));
  Session *sess = Napi::ObjectWrap<Session>::Unwrap(obj);
  sess->SetSession(database, session);
  return obj;
}

Session::Session(const Napi::CallbackInfo &info)
    : Napi::ObjectWrap<Session>(info), session_(nullptr) {}

Session::~Session() {
  Delete();
  // Sessions stay registered for the whole life of the wrapper (see Delete()),
  // so this is the one place that deregisters while the database is still
  // alive. A non-null database_ means ~DatabaseSync has not run -- once it
  // does, DetachAllSessions() nulls this and drops us from the set, and the
  // check below correctly does nothing.
  if (database_ != nullptr) {
    database_->RemoveSession(this);
    database_ = nullptr;
  }
}

void Session::SetSession(DatabaseSync *database, sqlite3_session *session) {
  database_ = database;
  session_ = session;
  if (database_) {
    database_->AddSession(this);
  }
}

void Session::Delete() {
  if (session_ == nullptr)
    return;

  // Store the session pointer and clear our member immediately
  // to prevent double-delete
  sqlite3_session *session_to_delete = session_;
  session_ = nullptr;

  // Deliberately stay registered in the database's session list, and keep
  // database_ non-null, so Session methods can still tell "database closed"
  // from "session closed". Registration is what lets ~DatabaseSync find us and
  // clear database_ (DetachAllSessions); dropping out of the list here while
  // keeping the pointer would leave a closed session holding a back-pointer
  // nobody ever clears, and ~Session would then call into a freed database.
  // Deregistration happens in ~Session (database alive) or DetachAllSessions
  // (database dying), whichever comes first.
  sqlite3session_delete(session_to_delete);
}

template <int (*sqliteChangesetFunc)(sqlite3_session *, int *, void **)>
Napi::Value Session::GenericChangeset(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // Check database first - if db was closed, that's the primary error.
  // database_ is preserved by Delete(), and is nulled by DetachAllSessions()
  // only once the database is destroyed, which the reference set in Create()
  // prevents while JavaScript can still reach this session. The null check is
  // defensive.
  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Then check if session was explicitly closed
  if (session_ == nullptr) {
    node::THROW_ERR_INVALID_STATE(env, "session is not open");
    return env.Undefined();
  }

  if (database_->ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  int nChangeset = 0;
  void *pChangeset = nullptr;

  // Session changeset generation runs internal SAVEPOINT and SELECT SQL that
  // can invoke the authorizer. Start a fresh deferred-error scope for it.
  database_->ClearDeferredAuthorizerException();
  database_->SetIgnoreNextSQLiteError(false);

  int r;
  {
    ChangesetGuard generating(this);
    r = sqliteChangesetFunc(session_, &nChangeset, &pChangeset);
  }

  if (database_->HasDeferredAuthorizerException()) {
    // A RELEASE failure can be ignored by SQLite after it has already produced
    // an allocation, so ownership must be discharged before rethrowing.
    if (pChangeset != nullptr) {
      sqlite3_free(pChangeset);
    }
    database_->RethrowDeferredAuthorizerException();
    return env.Undefined();
  }

  if (r != SQLITE_OK) {
    // Use sqlite3_errstr(r) to get a description of the error code,
    // rather than sqlite3_errmsg() which returns the last error on the
    // connection (which may not be related to this session operation)
    const char *errStr = sqlite3_errstr(r);
    Napi::Error::New(env,
                     std::string("Failed to generate changeset: ") + errStr)
        .ThrowAsJavaScriptException();
    return env.Undefined();
  }

  // Create a Uint8Array from the changeset data (matches node:sqlite API)
  //
  // A session with no recorded changes returns SQLITE_OK with nChangeset == 0
  // and pChangeset == NULL, and Napi::ArrayBuffer::New(env, 0) hands back a
  // NULL Data() pointer. memcpy() declares both of its pointer parameters
  // __attribute__((nonnull)), so passing NULL is undefined behavior *even when
  // the length is zero* -- and the optimizer is entitled to use that nonnull
  // promise to delete subsequent null checks. Skip the copy for the empty case.
  //
  // Caught by UndefinedBehaviorSanitizer (scripts/sanitizers-test.sh):
  //   sqlite_impl.cpp: runtime error: null pointer passed as argument 1,
  //   which is declared to never be null
  Napi::ArrayBuffer arrayBuffer = Napi::ArrayBuffer::New(env, nChangeset);
  if (nChangeset > 0 && pChangeset != nullptr) {
    std::memcpy(arrayBuffer.Data(), pChangeset, nChangeset);
  }

  // Free the changeset allocated by SQLite
  sqlite3_free(pChangeset);

  return Napi::Uint8Array::New(env, nChangeset, arrayBuffer, 0);
}

Napi::Value Session::Changeset(const Napi::CallbackInfo &info) {
  return GenericChangeset<sqlite3session_changeset>(info);
}

Napi::Value Session::Patchset(const Napi::CallbackInfo &info) {
  return GenericChangeset<sqlite3session_patchset>(info);
}

Napi::Value Session::Close(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // Check database first - if db was closed or destroyed, that's the primary
  // error. See GenericChangeset() for why the null check comes first.
  if (!database_ || !database_->IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Then check if session was explicitly closed
  if (session_ == nullptr) {
    node::THROW_ERR_INVALID_STATE(env, "session is not open");
    return env.Undefined();
  }

  // sqlite3session_delete() would free the object SQLite is still reading
  // while a changeset is being generated.
  if (is_generating_changeset_) {
    node::THROW_ERR_INVALID_STATE(env, "session is currently in use");
    return env.Undefined();
  }

  // SQLite's session module reaches back into JavaScript from inside the
  // pre-update hook, while it is still walking the connection's session list
  // and reading the table it found there. Deleting a session frees memory that
  // walk is still using, so no callback may close one. Checked last: changeset
  // generation runs the authorizer, so both conditions hold in that case and
  // the more specific message above has to win.
  if (database_->IsInCallback()) {
    node::THROW_ERR_INVALID_STATE(
        env, "session cannot be closed while in a callback");
    return env.Undefined();
  }

  Delete();
  return env.Undefined();
}

Napi::Value Session::Dispose(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // Explicit disposal is idempotent once the session is closed, including
  // after its database has closed and deleted the native sqlite3_session.
  if (session_ == nullptr) {
    return env.Undefined();
  }

  if (is_generating_changeset_) {
    node::THROW_ERR_INVALID_STATE(env, "session is currently in use");
    return env.Undefined();
  }

  // See Close(). An open session_ implies a live database_: InternalClose()
  // nulls session_ (DeleteAllSessions) before ~DatabaseSync nulls database_
  // (DetachAllSessions).
  if (database_->IsInCallback()) {
    node::THROW_ERR_INVALID_STATE(
        env, "session cannot be closed while in a callback");
    return env.Undefined();
  }

  Delete();
  return env.Undefined();
}

// Static members for tracking active jobs
std::atomic<int> BackupJob::active_jobs_(0);
std::mutex BackupJob::active_jobs_mutex_;
std::set<BackupJob *> BackupJob::active_job_instances_;

// A retry waiting on its timer. The timer callback runs without a V8 context,
// so it cannot create a step; ScheduleRetry() creates the next one up front,
// and the callback only queues it (napi_queue_async_work takes a
// node_api_basic_env). The uv_close() callback deletes this. The async cleanup
// hook makes environment teardown run the loop until that callback has run,
// which a worker needs before it closes its loop.
struct BackupRetry {
  uv_timer_t timer;
  BackupJob *job;
  BackupStep *step; // Not yet queued; null once queued or deleted.
  napi_async_cleanup_hook_handle cleanup_handle = nullptr;

  static void OnTimer(uv_timer_t *timer) {
    auto *retry = static_cast<BackupRetry *>(timer->data);
    BackupJob *job = retry->job;
    job->retry_ = nullptr;
    // BackupStep deletes itself after OnOK().
    NAPI_FATAL_IF_FAILED(napi_queue_async_work(job->env_, *retry->step),
                         "BackupRetry::OnTimer", "napi_queue_async_work");
    retry->step = nullptr;
    retry->Close();
  }

  // Environment teardown: stop the timer. BackupJob::CleanupHook deletes the
  // step and finishes the job.
  static void OnEnvCleanup(napi_async_cleanup_hook_handle, void *arg) {
    auto *retry = static_cast<BackupRetry *>(arg);
    if (!uv_is_closing(reinterpret_cast<uv_handle_t *>(&retry->timer))) {
      retry->Close();
    }
  }

  // Called by BackupJob::CleanupHook instead of letting the timer fire.
  void Cancel() {
    delete step;
    step = nullptr;
    if (!uv_is_closing(reinterpret_cast<uv_handle_t *>(&timer))) {
      Close();
    }
  }

  void Close() {
    uv_close(reinterpret_cast<uv_handle_t *>(&timer), [](uv_handle_t *handle) {
      auto *retry = static_cast<BackupRetry *>(handle->data);
      napi_remove_async_cleanup_hook(retry->cleanup_handle);
      delete retry;
    });
  }
};

void BackupJob::CleanupHook(void *arg) {
  // Called before environment teardown - safe to Reset() references here
  auto *self = static_cast<BackupJob *>(arg);
  // Signal Step() on the worker thread to skip sqlite3_backup_step so we don't
  // drag teardown out, and so OnStepComplete/Finish can short-circuit touching
  // the deferred (which can throw a C++ exception if the env is already torn
  // down).
  self->shutting_down_.store(true, std::memory_order_release);
  if (!self->progress_func_.IsEmpty()) {
    self->progress_func_.Reset();
  }
  // Release the strong reference to the source database now; during teardown
  // the implicit ~ObjectReference in ~BackupJob would be unsafe.
  if (!self->source_ref_.IsEmpty()) {
    self->source_ref_.Reset();
  }
  // No step is queued while a retry waits, so nothing else would finish the
  // job.
  if (self->retry_ != nullptr) {
    self->retry_->Cancel();
    self->retry_ = nullptr;
    self->Finish();
  }
}

BackupStep::BackupStep(Napi::Env env, BackupJob *job)
    : Napi::AsyncWorker(env, "BackupStep"), job_(job) {}

void BackupStep::Execute() { job_->Step(); }

void BackupStep::OnOK() { job_->OnStepComplete(); }

// BackupJob Implementation
BackupJob::BackupJob(Napi::Env env, DatabaseSync *source,
                     Napi::Object source_object, std::string destination_path,
                     std::string source_db, std::string dest_db, int pages,
                     Napi::Function progress_func,
                     Napi::Promise::Deferred deferred)
    : source_(source),
      // Strong reference keeps the source database (and thus its connection,
      // captured below) alive for the whole backup, even if the caller drops
      // its last JS reference and the GC runs mid-backup.
      source_ref_(Napi::Persistent(source_object)),
      // Step() reads this on a worker thread. It stays valid there because
      // closing the source runs Abandon(), which stops further steps first.
      source_connection_(source->connection()),
      destination_path_(std::move(destination_path)),
      source_db_(std::move(source_db)), dest_db_(std::move(dest_db)),
      pages_(pages), deferred_(deferred), env_(env) {
  if (!progress_func.IsEmpty() && !progress_func.IsUndefined()) {
    progress_func_ = Napi::Persistent(progress_func);
  }

  // Register cleanup hook to Reset() reference before environment teardown.
  // This is required for worker thread support per Node-API best practices.
  // See:
  // https://nodejs.github.io/node-addon-examples/special-topics/context-awareness/
  napi_add_env_cleanup_hook(env_, CleanupHook, this);

  active_jobs_++;
  // Register with database for proper cleanup coordination
  source_->AddBackup(this);
}

BackupJob::~BackupJob() {
  // Remove cleanup hook if still registered
  napi_remove_env_cleanup_hook(env_, CleanupHook, this);

  // Don't call progress_func_.Reset() here - CleanupHook already handled it,
  // or the environment is being torn down and Reset() would be unsafe.

  active_jobs_--;
  // Unregister from database
  // Note: source_ may be null if FinalizeBackups was called
  if (source_) {
    source_->RemoveBackup(this);
  }
}

void BackupJob::QueueStep() {
  // BackupStep deletes itself after OnOK().
  (new BackupStep(Napi::Env(env_), this))->Queue();
}

void BackupJob::Step() {
  // This method is executed on a worker thread, not the main thread
  // Note: SQLite backup operations are thread-safe when the source database
  // is only being read. The backup API creates its own read transaction
  // and can safely operate across threads.
  if (shutting_down_.load(std::memory_order_acquire)) {
    // Env is tearing down; skip the step so FreeEnvironment can finish.
    // OnStepComplete sees the flag and doesn't queue another step.
    return;
  }

  // Held for the whole step so Abandon() cannot release backup_ and dest_, or
  // let the source connection close, while this step uses them.
  std::lock_guard<std::mutex> lock(step_mutex_);
  if (abandoned_) {
    return;
  }

  if (first_step_) {
    backup_status_ = sqlite3_open_v2(
        destination_path_.c_str(), &dest_,
        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, nullptr);

    if (backup_status_ != SQLITE_OK) {
      // Let Finish reject after its shutdown guard.
      return;
    }

    // Initialize backup using the connection pointer captured at construction
    backup_ = sqlite3_backup_init(dest_, dest_db_.c_str(), source_connection_,
                                  source_db_.c_str());

    if (!backup_) {
      // Let Finish reject after its shutdown guard. sqlite3_backup_init errors
      // are stored on dest_ and will be read before Cleanup().
      return;
    }
  }

  backup_status_ = sqlite3_backup_step(backup_, pages_);
  first_step_ = false;

  // Read after every step, as node:sqlite does: SQLite knows the page count
  // only after a step that read the source, and a step that returned
  // SQLITE_BUSY may not have.
  total_pages_ = sqlite3_backup_pagecount(backup_);
}

// Node's FreeEnvironment() disallows JavaScript, then runs pending async work
// to completion, and only then runs env cleanup hooks. A step that completes
// in that window runs before CleanupHook has set shutting_down_, and queuing
// another step there creates a new async resource, whose async_hooks init
// callbacks now fail as fatal exceptions. CanRunJavaScript() detects that
// window.
void BackupJob::OnStepComplete() {
  // This runs on the main thread after each Step().
  if (!CanRunJavaScript(env_)) {
    // The env is being torn down: stop as CleanupHook would.
    shutting_down_.store(true, std::memory_order_release);
  }
  // Queue another step unless the backup is done or failed, the env is
  // shutting down, or the source was closed (Abandon() nulls backup_).
  if (backup_ != nullptr && !shutting_down_.load(std::memory_order_acquire)) {
    if (backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED) {
      // Another connection holds a lock: retry later.
      backup_status_ = SQLITE_OK;
      ScheduleRetry();
      return;
    }
    if (backup_status_ == SQLITE_OK) {
      retry_delay_ms_ = 0;
      ReportProgress();
      // The progress callback may have closed the source database.
      if (backup_ != nullptr) {
        QueueStep();
        return;
      }
    }
  }
  Finish();
}

void BackupJob::ScheduleRetry() {
  retry_delay_ms_ =
      retry_delay_ms_ == 0 ? 1 : std::min(retry_delay_ms_ * 2, 100);
  uv_loop_t *loop = nullptr;
  NAPI_FATAL_IF_FAILED(napi_get_uv_event_loop(env_, &loop),
                       "BackupJob::ScheduleRetry", "napi_get_uv_event_loop");
  auto *retry = new BackupRetry{};
  retry->job = this;
  // If close() abandons the backup while the retry waits, this step does
  // nothing and its completion finishes the job.
  retry->step = new BackupStep(Napi::Env(env_), this);
  NAPI_CHECK(uv_timer_init(loop, &retry->timer) == 0,
             "BackupJob::ScheduleRetry", "uv_timer_init");
  retry->timer.data = retry;
  NAPI_FATAL_IF_FAILED(
      napi_add_async_cleanup_hook(env_, BackupRetry::OnEnvCleanup, retry,
                                  &retry->cleanup_handle),
      "BackupJob::ScheduleRetry", "napi_add_async_cleanup_hook");
  NAPI_CHECK(uv_timer_start(&retry->timer, BackupRetry::OnTimer,
                            retry_delay_ms_, 0) == 0,
             "BackupJob::ScheduleRetry", "uv_timer_start");
  retry_ = retry;
}

void BackupJob::ReportProgress() {
  // Node.js only calls progress when there are still pages remaining. Once the
  // callback throws, stop calling it; Finish rejects with its error.
  int remaining_pages = sqlite3_backup_remaining(backup_);
  if (progress_func_.IsEmpty() || progress_error_.has_value() ||
      total_pages_ <= 0 || remaining_pages <= 0) {
    return;
  }

  Napi::Env env(env_);
  Napi::HandleScope scope(env);
  try {
    Napi::Object progress_info = Napi::Object::New(env);
    progress_info.Set("totalPages", Napi::Number::New(env, total_pages_));
    progress_info.Set("remainingPages",
                      Napi::Number::New(env, remaining_pages));
    progress_func_.Value().Call(env.Null(), {progress_info});
  } catch (const Napi::Error &e) {
    // Capture error from progress callback - backup should fail with this.
    // Message() is empty for a thrown primitive.
    progress_error_ = e.Message();
  } catch (...) {
    // Unknown error
    progress_error_ = "Unknown error in progress callback";
  }
}

void BackupJob::Finish() {
  // This runs on the main thread once the backup is done, failed, or was
  // abandoned.
  Napi::Env env(env_);
  Napi::HandleScope scope(env);

  // Save error info BEFORE cleanup nulls the pointers. The shutdown guard below
  // runs before any JS Error construction.
  int saved_status = backup_status_;
  std::string saved_errmsg;
  if (dest_) {
    saved_errmsg = sqlite3_errmsg(dest_);
    int dest_err = sqlite3_errcode(dest_);
    if (dest_err != SQLITE_OK && saved_status == SQLITE_OK) {
      saved_status = dest_err;
    }
  }
  const bool backup_failed = backup_status_ != SQLITE_DONE;
  if (backup_failed &&
      (saved_status == SQLITE_OK || saved_status == SQLITE_DONE)) {
    saved_status = SQLITE_ERROR;
  }

  // Cleanup SQLite resources
  Cleanup();

  if (shutting_down_.load(std::memory_order_acquire)) {
    // Env teardown already started; deferred_ rejection can throw a C++
    // Napi::Error out of this libuv cleanup-hook frame. The JS-side promise
    // is going away with the env anyway.
    delete this;
    return;
  }

  // If progress callback threw an error, reject with that error
  if (progress_error_.has_value()) {
    Napi::Error error = Napi::Error::New(env, *progress_error_);
    try {
      deferred_.Reject(error.Value());
    } catch (...) {
    }
  } else if (backup_failed) {
    std::string err_message;
    if (!saved_errmsg.empty() && saved_errmsg != "not an error") {
      err_message = saved_errmsg;
    } else {
      err_message = sqlite3_errstr(saved_status);
    }

    Napi::Error detailed_error =
        node::NewErrorFromSqliteText(env, err_message.c_str());
    if (detailed_error.IsEmpty()) {
      // ERR_STRING_TOO_LONG is pending. node:sqlite throws it from backup(),
      // but this job finishes after backup() returned, so reject with it.
      try {
        deferred_.Reject(env.GetAndClearPendingException().Value());
      } catch (...) {
      }
      delete this;
      return;
    }
    detailed_error.Set("code", Napi::String::New(env, "ERR_SQLITE_ERROR"));
    detailed_error.Set("errcode", Napi::Number::New(env, saved_status));
    detailed_error.Set("errstr",
                       Napi::String::New(env, sqlite3_errstr(saved_status)));
    try {
      deferred_.Reject(detailed_error.Value());
    } catch (...) {
    }
  } else {
    // Resolve the promise with the total number of pages
    try {
      deferred_.Resolve(Napi::Number::New(env, total_pages_));
    } catch (...) {
    }
  }
  delete this;
}

void BackupJob::Abandon() {
  std::lock_guard<std::mutex> lock(step_mutex_);
  abandoned_ = true;
  Cleanup();
}

void BackupJob::Cleanup() {
  if (backup_) {
    sqlite3_backup_finish(backup_);
    backup_ = nullptr;
  }

  if (dest_) {
    backup_status_ = sqlite3_errcode(dest_);
    sqlite3_close_v2(dest_);
    dest_ = nullptr;
  }
}

// DatabaseSync::Backup implementation
// Note: Validation errors are thrown synchronously (matching Node.js behavior).
// Only the actual backup operation returns a promise.
Napi::Value DatabaseSync::Backup(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  // Validation errors throw synchronously (matching Node.js behavior)
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  // ValidateDatabasePath throws synchronously with ERR_INVALID_ARG_TYPE
  // Use "path" as argument name to match Node.js
  std::optional<std::string> dest_path =
      ValidateDatabasePath(env, info[0], "path");
  if (!dest_path.has_value()) {
    // Exception already thrown by ValidateDatabasePath
    return env.Undefined();
  }

  // Default options matching Node.js API
  int rate = 100;
  std::string source_db = "main";
  std::string target_db = "main";
  Napi::Function progress_func;

  // Parse options if provided
  if (info.Length() > 1) {
    if (!info[1].IsObject()) {
      node::THROW_ERR_INVALID_ARG_TYPE(
          env, "The \"options\" argument must be an object.");
      return env.Undefined();
    }

    Napi::Object options = info[1].As<Napi::Object>();

    // Get rate option (number of pages per step)
    Napi::Value rate_value = options.Get("rate");
    if (!rate_value.IsUndefined()) {
      // Check if it's a number and an integer (not fractional)
      if (!rate_value.IsNumber()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.rate\" argument must be an integer.");
        return env.Undefined();
      }
      double rate_double = rate_value.As<Napi::Number>().DoubleValue();
      if (rate_double != std::trunc(rate_double)) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.rate\" argument must be an integer.");
        return env.Undefined();
      }
      rate = rate_value.As<Napi::Number>().Int32Value();
      if (rate <= 0) {
        node::THROW_ERR_OUT_OF_RANGE(
            env, "The \"options.rate\" argument must be a positive integer.");
        return env.Undefined();
      }
    }

    // Get source database option
    Napi::Value source_value = options.Get("source");
    if (!source_value.IsUndefined()) {
      if (!source_value.IsString()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.source\" argument must be a string.");
        return env.Undefined();
      }
      source_db = source_value.As<Napi::String>().Utf8Value();
    }

    // Get target database option
    Napi::Value target_value = options.Get("target");
    if (!target_value.IsUndefined()) {
      if (!target_value.IsString()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.target\" argument must be a string.");
        return env.Undefined();
      }
      target_db = target_value.As<Napi::String>().Utf8Value();
    }

    // Get progress callback
    Napi::Value progress_value = options.Get("progress");
    if (!progress_value.IsUndefined()) {
      if (!progress_value.IsFunction()) {
        node::THROW_ERR_INVALID_ARG_TYPE(
            env, "The \"options.progress\" argument must be a function.");
        return env.Undefined();
      }
      progress_func = progress_value.As<Napi::Function>();
    }
  }

  // Reading the destination path and the options bag above can run user
  // JavaScript through a property getter, which may have closed the database
  // since it was checked.
  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  // Create promise for async backup operation
  Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);

  // Create and schedule backup job. Pass the database's JS object so the job
  // can hold a strong reference and keep the source alive for the whole
  // backup (see BackupJob::source_ref_).
  BackupJob *job =
      new BackupJob(env, this, info.This().As<Napi::Object>(),
                    std::move(*dest_path), std::move(source_db),
                    std::move(target_db), rate, progress_func, deferred);

  // The job deletes itself once the backup settles the promise
  job->QueueStep();

  return deferred.Promise();
}

// DatabaseSync::SetAuthorizer implementation
Napi::Value DatabaseSync::SetAuthorizer(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  // Handle null to clear the authorizer
  if (info.Length() > 0 && info[0].IsNull()) {
    sqlite3_set_authorizer(connection_, nullptr, nullptr);
    authorizer_callback_.reset();
    return env.Undefined();
  }

  // Validate callback argument
  if (info.Length() < 1 || !info[0].IsFunction()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The \"callback\" argument must be a function or null.");
    return env.Undefined();
  }

  // Store the JavaScript callback
  Napi::Function fn = info[0].As<Napi::Function>();
  authorizer_callback_ =
      std::make_unique<Napi::FunctionReference>(Napi::Persistent(fn));

  // Set the SQLite authorizer with our static callback
  int r = sqlite3_set_authorizer(connection_, AuthorizerCallback, this);

  if (r != SQLITE_OK) {
    authorizer_callback_.reset();
    ThrowEnhancedSqliteErrorWithDB(env, this, connection_, r,
                                   "Failed to set authorizer");
    return env.Undefined();
  }

  return env.Undefined();
}

// Static callback for SQLite authorization
int DatabaseSync::AuthorizerCallback(void *user_data, int action_code,
                                     const char *param1, const char *param2,
                                     const char *param3, const char *param4) {
  DatabaseSync *db = static_cast<DatabaseSync *>(user_data);

  // If no callback is set, allow everything
  if (!db->authorizer_callback_ || db->authorizer_callback_->IsEmpty()) {
    return SQLITE_OK;
  }

  auto callback_guard = db->EnterCallback();
  Napi::Env env(db->env_);
  Napi::HandleScope scope(env);
  auto authorizer_guard = db->EnterAuthorizerCallback();

  try {
    // Convert SQLite authorizer parameters to JavaScript values
    std::vector<napi_value> args;
    args.push_back(Napi::Number::New(env, action_code));
    for (const char *param : {param1, param2, param3, param4}) {
      napi_value arg;
      if (!NullableSQLiteStringToValue(env, param, &arg)) {
        db->SetDeferredAuthorizerException(env.GetAndClearPendingException());
        db->SetIgnoreNextSQLiteError(true);
        return SQLITE_DENY;
      }
      args.push_back(arg);
    }

    // Raw Node-API (see pending_exception.h). The exception must be cleared
    // before returning to SQLite.
    napi_value result_value;
    if (napi_call_function(env, env.Undefined(),
                           db->authorizer_callback_->Value(), args.size(),
                           args.data(), &result_value) != napi_ok) {
      db->SetDeferredAuthorizerException(env.GetAndClearPendingException());
      db->SetIgnoreNextSQLiteError(true);
      return SQLITE_DENY;
    }
    Napi::Value result(env, result_value);

    // Match V8's IsInt32() check in node:sqlite. Node-API's Int32Value()
    // coerces NaN, negative zero, fractions, and wrapping values such as 2^32
    // to zero, which would otherwise turn a malformed result into SQLITE_OK.
    bool is_int32 = false;
    if (result.IsNumber()) {
      const double value = result.As<Napi::Number>().DoubleValue();
      is_int32 = std::isfinite(value) && !(value == 0 && std::signbit(value)) &&
                 std::trunc(value) == value && value >= INT32_MIN &&
                 value <= INT32_MAX;
    }
    if (!is_int32) {
      Napi::TypeError error = Napi::TypeError::New(
          env, "Authorizer callback must return an integer authorization code");
      db->SetDeferredAuthorizerException(error);
      db->SetIgnoreNextSQLiteError(true);
      return SQLITE_DENY;
    }

    int32_t int_result = result.As<Napi::Number>().Int32Value();

    // Validate the return code - don't throw in callback context
    if (int_result != SQLITE_OK && int_result != SQLITE_DENY &&
        int_result != SQLITE_IGNORE) {
      Napi::RangeError error = Napi::RangeError::New(
          env, "Authorizer callback returned a invalid authorization code");
      db->SetDeferredAuthorizerException(error);
      db->SetIgnoreNextSQLiteError(true);
      return SQLITE_DENY;
    }

    return int_result;
  } catch (const Napi::Error &e) {
    // JavaScript exception occurred - clear any pending exception and store
    if (env.IsExceptionPending()) {
      db->SetDeferredAuthorizerException(env.GetAndClearPendingException());
    } else {
      db->SetDeferredAuthorizerException(e);
    }
    db->SetIgnoreNextSQLiteError(true);
    return SQLITE_DENY;
  } catch (const std::exception &e) {
    // C++ exception - clear any pending JS exception and preserve it. If the
    // exception did not originate in JavaScript, create an Error now and defer
    // that exact object until the surrounding SQLite call returns.
    if (env.IsExceptionPending()) {
      db->SetDeferredAuthorizerException(env.GetAndClearPendingException());
    } else {
      Napi::Error error = Napi::Error::New(env, e.what());
      db->SetDeferredAuthorizerException(error);
    }
    db->SetIgnoreNextSQLiteError(true);
    return SQLITE_DENY;
  } catch (...) {
    // Unknown error - clear any pending JS exception and deny
    if (env.IsExceptionPending()) {
      db->SetDeferredAuthorizerException(env.GetAndClearPendingException());
    } else {
      Napi::Error error =
          Napi::Error::New(env, "Unknown error in authorizer callback");
      db->SetDeferredAuthorizerException(error);
    }
    db->SetIgnoreNextSQLiteError(true);
    return SQLITE_DENY;
  }
}

Napi::Value DatabaseSync::GetLimit(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 1 || !info[0].IsNumber()) {
    node::THROW_ERR_INVALID_ARG_TYPE(env,
                                     "The limit ID argument must be a number.");
    return env.Undefined();
  }

  int limit_id = info[0].As<Napi::Number>().Int32Value();
  int current_value = sqlite3_limit(connection_, limit_id, -1);
  return Napi::Number::New(env, current_value);
}

Napi::Value DatabaseSync::SetLimit(const Napi::CallbackInfo &info) {
  Napi::Env env = info.Env();

  if (!IsOpen()) {
    node::THROW_ERR_INVALID_STATE(env, "database is not open");
    return env.Undefined();
  }

  if (ThrowIfInAuthorizerCallback(env)) {
    return env.Undefined();
  }

  if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) {
    node::THROW_ERR_INVALID_ARG_TYPE(
        env, "The limit ID and value arguments must be numbers.");
    return env.Undefined();
  }

  int limit_id = info[0].As<Napi::Number>().Int32Value();
  int new_value = info[1].As<Napi::Number>().Int32Value();
  int old_value = sqlite3_limit(connection_, limit_id, new_value);

  // Record the value so InternalOpen() re-applies it after close() and open(),
  // as node:sqlite does. db.limits validates its arguments, but this method is
  // callable directly: skip a negative value (sqlite3_limit() ignores it) and
  // an id set_limit() doesn't track (it would throw std::out_of_range).
  if (new_value >= 0 && limit_id >= 0 &&
      limit_id < static_cast<int>(DatabaseOpenConfiguration::kNumLimits)) {
    config_.set_limit(limit_id, new_value);
  }

  return Napi::Number::New(env, old_value);
}

// Thread validation implementations
bool DatabaseSync::ValidateThread(Napi::Env env) const {
  if (std::this_thread::get_id() != creation_thread_) {
    node::THROW_ERR_INVALID_STATE(
        env, "Database connection cannot be used from different thread");
    return false;
  }
  return true;
}

bool StatementSync::ValidateThread(Napi::Env env) const {
  if (std::this_thread::get_id() != creation_thread_) {
    node::THROW_ERR_INVALID_STATE(
        env, "Statement cannot be used from different thread");
    return false;
  }
  return true;
}

} // namespace photostructure::sqlite
