import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);
import "./chunk-HYQNUVGO.js";

// app/lib/adminuser-self-heal.ts
var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
async function selfHealAdminUser(args) {
  const { driver, userId, accountId } = args;
  if (!UUID_REGEX.test(accountId)) {
    throw new Error(
      `[adminuser-self-heal] refusing to run: accountId is not a UUID (got: '${accountId.slice(0, 12)}\u2026')`
    );
  }
  if (!UUID_REGEX.test(userId)) {
    throw new Error(
      `[adminuser-self-heal] refusing to run: userId is not a UUID (got: '${userId.slice(0, 12)}\u2026')`
    );
  }
  const session = driver.session();
  try {
    const result = await session.run(
      `MATCH (au:AdminUser {userId: $userId})
       WHERE au.accountId IS NULL OR au.role IS NULL
       SET au.accountId = COALESCE(au.accountId, $accountId),
           au.role      = COALESCE(au.role, 'owner'),
           au.updatedAt = $now
       RETURN count(au) AS healed`,
      { userId, accountId, now: (/* @__PURE__ */ new Date()).toISOString() }
    );
    const raw = result.records[0]?.get("healed");
    let healed = 0;
    if (typeof raw === "number") {
      healed = raw;
    } else if (raw && typeof raw.toNumber === "function") {
      healed = raw.toNumber();
    }
    console.error(
      `[adminuser-self-heal] healed=${healed} userId=${userId.slice(0, 8)} accountId=${accountId.slice(0, 8)}`
    );
    return { healed };
  } finally {
    await session.close();
  }
}
export {
  selfHealAdminUser
};
