/**
 * lib/awr-consent.js — resolve, per connection, whether a health check may read
 * AWR (Diagnostics Pack) data.
 *
 * AWR reads (DBA_HIST_*) are Diagnostics Pack usage. This is the single place the
 * server decides, fail-closed, from the customer's explicit per-connection
 * consent (db/option-consent.js) — never from a queryable view or a default
 * parameter. Extracted from server.js so the decision is unit-testable; server.js
 * imports it and both health-check runners (direct + proxy) call it.
 *
 * Policy 3a (consent-only, default OFF): no connection id, unset/denied consent,
 * or ANY error → false. A false result must never block the health check — the
 * collector simply runs on real-time V$ views only.
 */

'use strict';

const optionConsent = require('../db/option-consent');

/**
 * @param {number|null|undefined} connectionId
 * @returns {Promise<boolean>} true ONLY when diagnostics_pack consent is granted.
 */
async function resolveAwrConsent(connectionId) {
  if (!connectionId) return false;
  try {
    return await optionConsent.isGranted(connectionId, 'diagnostics_pack');
  } catch (err) {
    console.error('[awr-consent] isGranted error for conn %s:', connectionId, err.message);
    return false; // fail closed — a consent-store hiccup must not open AWR reads
  }
}

module.exports = { resolveAwrConsent };
