/**
 * @highflame/overwatch-win32-arm64
 * Platform-specific binaries for Windows ARM64
 */

const path = require("path");
const fs = require("fs");

const PLATFORM = "win32";
const ARCH = "arm64";
const RUST_TARGET = "aarch64-pc-windows-msvc";

/**
 * Get the path to the ramparts binary
 * @returns {string|null} Path to binary or null if not found
 */
function getRampartsBinaryPath() {
  const binaryName = `ramparts-${RUST_TARGET}.exe`;
  const binaryPath = path.join(__dirname, "bin", binaryName);

  if (fs.existsSync(binaryPath)) {
    return binaryPath;
  }

  // Also check for generic name
  const genericPath = path.join(__dirname, "bin", "ramparts.exe");
  if (fs.existsSync(genericPath)) {
    return genericPath;
  }

  return null;
}

/**
 * Get the path to the native Cedar module
 * @returns {string|null} Path to native module or null if not found
 */
function getNativeModulePath() {
  const moduleName = `highflame-policy-node.${PLATFORM}-${ARCH}.node`;
  const modulePath = path.join(__dirname, "native", moduleName);

  if (fs.existsSync(modulePath)) {
    return modulePath;
  }

  return null;
}

/**
 * Load the Cedar policy engine module
 * @returns {object|null} The loaded module or null if not found
 */
function loadCedarModule() {
  const modulePath = getNativeModulePath();
  if (!modulePath) {
    return null;
  }

  try {
    return require(modulePath);
  } catch (error) {
    console.error(`Failed to load Cedar module: ${error.message}`);
    return null;
  }
}

/**
 * Check if this platform package matches the current runtime
 * @returns {boolean}
 */
function isCurrentPlatform() {
  return process.platform === PLATFORM && process.arch === ARCH;
}

module.exports = {
  platform: PLATFORM,
  arch: ARCH,
  rustTarget: RUST_TARGET,
  getRampartsBinaryPath,
  getNativeModulePath,
  loadCedarModule,
  isCurrentPlatform,
};
