'use strict';

function _truthy(value) {
  return /^(1|true|yes|on)$/i.test(String(value || '').trim());
}

function assertNoForeignSqliteOwner({
  blockedEnv,
  overrideEnv,
  databaseLabel,
  ownerLabel,
} = {}) {
  const blockedRole = blockedEnv ? process.env[blockedEnv] : '';
  if (!blockedRole || (overrideEnv && _truthy(process.env[overrideEnv]))) return;

  const err = new Error(
    `${databaseLabel || 'SQLite database'} must be accessed through its ${ownerLabel || 'owner'} API; ` +
    `refusing direct access inside process role ${blockedEnv}=${blockedRole}.` +
    (overrideEnv ? ` Set ${overrideEnv}=1 only for explicit standalone repair.` : '')
  );
  err.code = 'SQLITE_OWNER_VIOLATION';
  err.blockedEnv = blockedEnv;
  err.blockedRole = blockedRole;
  err.overrideEnv = overrideEnv || '';
  throw err;
}

module.exports = {
  assertNoForeignSqliteOwner,
};
