import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fail, pass, repositoryRoot } from './lib.mjs';

const failures = [];
const example = await readFile(path.join(repositoryRoot, '.env.example'), 'utf8');
const implementation = await readFile(
  path.join(repositoryRoot, 'src/lib/integrations/environment.ts'),
  'utf8',
);
const keys = [
  'NEXT_PUBLIC_DEMO_ENABLED',
  'DEMO_SUBMISSION_MODE',
  'DEMO_REQUEST_TIMEOUT_MS',
  'DEMO_IDEMPOTENCY_TTL_SECONDS',
  'DEMO_TEST_SCENARIOS_ENABLED',
  'DEMO_LOCAL_TEST_MODE',
  'NEXT_PUBLIC_ANALYTICS_MODE',
];
for (const key of keys) {
  if (!example.includes(`${key}=`)) failures.push(`.env.example omits ${key}.`);
  if (!implementation.includes(key)) failures.push(`Environment validator omits ${key}.`);
}
if (!example.includes('DEMO_SUBMISSION_MODE=blocked')) {
  failures.push('Safe production example does not default demo submission to blocked.');
}
if (!example.includes('NEXT_PUBLIC_ANALYTICS_MODE=disabled')) {
  failures.push('Safe production example does not default analytics to disabled.');
}

if (failures.length) fail(failures);
else
  pass(
    'Phase 14 environment inventory is explicit and defaults production integrations to blocked.',
  );
