// src/binding.cpp
#include <napi.h>
#include <string>

#include "common/debug_log.h"
#include "common/shutdown.h"
#if defined(_WIN32)
#include "windows/fs_meta.h"
#include "windows/hidden.h"
#elif defined(__APPLE__)
#include "darwin/fs_meta.h"
#include "darwin/get_mount_point.h"
#include "darwin/hidden.h"
#elif defined(__linux__)
#include "common/volume_metadata.h"
#endif

namespace {

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

  if (info.Length() < 1 || !info[0].IsBoolean()) {
    throw Napi::TypeError::New(env, "Boolean argument expected");
  }

  FSMeta::Debug::enableDebugLogging = info[0].As<Napi::Boolean>();
  return env.Undefined();
}

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

  if (info.Length() < 1 || !info[0].IsString()) {
    throw Napi::TypeError::New(env, "String argument expected");
  }

  // Set the debug prefix directly
  FSMeta::Debug::SetDebugPrefix(info[0].As<Napi::String>().Utf8Value());
  return env.Undefined();
}

#if defined(_WIN32) || defined(__APPLE__)
// Fix: Remove extra parameter and use correct signature
Napi::Value GetVolumeMountPoints(const Napi::CallbackInfo &info) {
  return FSMeta::GetVolumeMountPoints(info);
}
#endif

Napi::Value GetVolumeMetadata(const Napi::CallbackInfo &info) {
  return FSMeta::GetVolumeMetadata(info);
}

#if defined(__APPLE__)
Napi::Value GetMountPointForPath(const Napi::CallbackInfo &info) {
  return FSMeta::GetMountPoint(info);
}
#endif

#if defined(_WIN32) || defined(__APPLE__)
Napi::Value GetHiddenAttribute(const Napi::CallbackInfo &info) {
  return FSMeta::GetHiddenAttribute(info);
}

Napi::Value SetHiddenAttribute(const Napi::CallbackInfo &info) {
  return FSMeta::SetHiddenAttribute(info);
}
#endif

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  // Register a cleanup hook so in-flight native workers can short-circuit
  // during env teardown instead of racing FreeEnvironment.
  FSMeta::EnsureShutdownHook(env);

  exports.Set("setDebugLogging", Napi::Function::New(env, SetDebugLogging));
  exports.Set("setDebugPrefix", Napi::Function::New(env, SetDebugPrefix));

#if defined(_WIN32) || defined(__APPLE__)
  exports.Set("getVolumeMountPoints",
              Napi::Function::New(env, GetVolumeMountPoints));
#endif

  exports.Set("getVolumeMetadata", Napi::Function::New(env, GetVolumeMetadata));

#if defined(__APPLE__)
  exports.Set("getMountPoint", Napi::Function::New(env, GetMountPointForPath));
#endif

#if defined(_WIN32) || defined(__APPLE__)
  exports.Set("isHidden", Napi::Function::New(env, GetHiddenAttribute));
  exports.Set("setHidden", Napi::Function::New(env, SetHiddenAttribute));
#endif

  return exports;
}

NODE_API_MODULE(fs_metadata, Init)

} // namespace