import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { execFileSync } from 'node:child_process';

const [directory, ...options] = process.argv.slice(2);
if (!directory || options.some((value, index) => value !== '--index' && value !== '--source-ref' && options[index - 1] !== '--source-ref'))
  throw new Error('Usage: node scripts/verify-adoption-evidence.mjs <sealed-directory> [--index] [--source-ref <commit|:>]');
const root = path.resolve(directory), gitRoot = process.cwd(), useIndex = options.includes('--index');
const sourceRef = options.includes('--source-ref') ? options[options.indexOf('--source-ref') + 1] : null;
if (sourceRef) assert.ok(sourceRef === ':' || /^[a-f0-9]{7,40}$/.test(sourceRef));
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
const git = object => execFileSync('git', ['show', object], { cwd: gitRoot, windowsHide: true, maxBuffer: 16 * 1024 * 1024 });
const contained = rel => {
  assert.ok(typeof rel === 'string' && rel && !rel.includes('\\') && !path.isAbsolute(rel) && !rel.split('/').some(part => !part || part === '..'));
  const file = path.resolve(root, rel); assert.ok(file.startsWith(root + path.sep)); assert.ok(fs.lstatSync(file).isFile()); return file;
};
const manifestBytes = fs.readFileSync(path.join(root, 'manifest.json')), manifest = JSON.parse(manifestBytes);
assert.equal(manifest.kind, 'adoption-stage-evidence'); assert.equal(manifest.realModelRequests, 0); assert.equal(manifest.officialServiceChanged, false);
const names = new Set(); let bytes = 0;
for (const row of manifest.files) {
  assert.ok(!names.has(row.path)); names.add(row.path); const data = fs.readFileSync(contained(row.path));
  assert.equal(data.length, row.bytes, row.path); assert.equal(sha(data), row.sha256, row.path); bytes += data.length;
  if (row.path.endsWith('.png')) assert.equal(data.subarray(0, 8).toString('hex'), '89504e470d0a1a0a');
  if (useIndex) assert.equal(sha(git(':' + path.relative(gitRoot, path.join(root, row.path)).replaceAll('\\', '/'))), row.sha256);
}
assert.equal(bytes, manifest.bytes);
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(row => {
  const file = path.join(dir, row.name); assert.equal(fs.lstatSync(file).isSymbolicLink(), false);
  return row.isDirectory() ? walk(file) : [path.relative(root, file).replaceAll('\\', '/')];
});
assert.deepEqual(walk(root).filter(name => name !== 'manifest.json').sort(), [...names].sort());
if (useIndex) assert.equal(sha(git(':' + path.relative(gitRoot, path.join(root, 'manifest.json')).replaceAll('\\', '/'))), sha(manifestBytes));
for (const check of manifest.testRuns ?? []) {
  const log = fs.readFileSync(contained(check.path), 'utf8');
  for (const [label, count] of Object.entries({ tests: check.tests, pass: check.passed, fail: check.failed }))
    assert.match(log, new RegExp('(?:ℹ |# )' + label + ' ' + count + '(?:\\r?\\n|$)'));
}
for (const check of manifest.uiRuns ?? []) {
  const result = JSON.parse(fs.readFileSync(contained(check.path), 'utf8'));
  assert.equal(result.success, check.success); assert.equal(result.passed.length, check.passed); assert.equal(result.realModelRequests, 0);
}
if (sourceRef) for (const row of manifest.sourceFiles) {
  assert.ok(names.has(row.snapshot)); assert.ok(row.path && !row.path.includes('..') && !path.isAbsolute(row.path));
  const original = fs.readFileSync(contained(row.snapshot)); const recorded = git(sourceRef === ':' ? ':' + row.path : sourceRef + ':' + row.path);
  // Source snapshots preserve original checkout bytes. The repository's text
  // attributes store LF; accept only that declared text representation change.
  const gitText = Buffer.from(original.toString('utf8').replaceAll('\r\n', '\n'));
  assert.equal(sha(recorded), sha(original) === sha(recorded) ? sha(original) : sha(gitText), 'Source ref differs: ' + row.path);
}
console.log(JSON.stringify({ ok: true, stage: manifest.stage, files: names.size, bytes, testRuns: manifest.testRuns?.length ?? 0,
  uiRuns: manifest.uiRuns?.length ?? 0, sourceFiles: manifest.sourceFiles.length, indexChecked: useIndex, sourceRef, realModelRequests: 0 }));
