#include "add.h"
#include "../tensor.h"

namespace TensorOps {

Napi::Value Add(Tensor* self, const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  if (info.Length() < 1) {
    Napi::TypeError::New(env, "Expected at least one argument").ThrowAsJavaScriptException();
    return env.Null();
  }

  torch::Tensor result;

  // Check if argument is a number (scalar) or Tensor
  if (info[0].IsNumber()) {
    double scalar = info[0].As<Napi::Number>().DoubleValue();
    result = self->tensor + scalar;
  } else if (info[0].IsObject()) {
    Tensor* other = Napi::ObjectWrap<Tensor>::Unwrap(info[0].As<Napi::Object>());
    result = self->tensor + other->tensor;
  } else {
    Napi::TypeError::New(env, "Expected Tensor or number").ThrowAsJavaScriptException();
    return env.Null();
  }

  return Tensor::NewInstance(env, result);
}

}

// Tensor class method calls the operation
Napi::Value Tensor::Add(const Napi::CallbackInfo& info) {
  return TensorOps::Add(this, info);
}
