import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash, randomUUID } from 'node:crypto';
import { performance } from 'node:perf_hooks';
import { pathToFileURL } from 'node:url';
import { execFileSync } from 'node:child_process';

// Own deterministic inputs; no model connection, user project or tool execution.
const phase = process.argv[2] ?? 'baseline';
assert.ok(['baseline', 'comparison'].includes(phase));
const output = path.resolve('.qa', 'project-cache-' + phase + '-' + randomUUID());
fs.mkdirSync(output, { recursive: true });
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
let baselineIdentity = null, modulePath = path.resolve('src/core/context/project-index.mjs');
if (phase === 'baseline') {
  const commit = '606b5ec72edb2f86864b6cb2dfa1fab3f9e1af1a', gitPath = 'src/core/context/project-index.mjs';
  const original = execFileSync('git', ['show', commit + ':' + gitPath], { windowsHide: true, encoding: 'utf8' });
  const inspector = pathToFileURL(path.resolve('src/core/project-inspector.mjs')).href, files = pathToFileURL(path.resolve('src/server/files.mjs')).href;
  const adjusted = original.replace("'../project-inspector.mjs'", JSON.stringify(inspector)).replace("'../../server/files.mjs'", JSON.stringify(files));
  assert.notEqual(adjusted, original); modulePath = path.join(output, 'original-index.mjs'); fs.writeFileSync(modulePath, adjusted);
  baselineIdentity = { commit, gitPath, originalSha256: hash(original), adjustedSha256: hash(adjusted), changes: 'Only two import paths relocated to current unchanged default inspection/file interfaces. Original parsing/cache algorithm preserved.' };
}
const { ProjectIndex } = await import(pathToFileURL(modulePath).href);
const rows = [];
const originalRead = fs.readFileSync;
const originalOpen = fs.openSync, originalClose = fs.closeSync, handles = new Map();
fs.openSync = function (filename, ...args) { const handle = originalOpen.call(this, filename, ...args); handles.set(handle, filename); return handle; };
fs.closeSync = function (handle) { handles.delete(handle); return originalClose.call(this, handle); };
let currentRoot = null, reads = 0, bytes = 0;
fs.readFileSync = function (filename, ...args) {
  const result = originalRead.call(this, filename, ...args);
  const source = typeof filename === 'number' ? handles.get(filename) : filename;
  if (typeof source === 'string' && currentRoot && path.resolve(source).startsWith(currentRoot + path.sep)) {
    reads++; bytes += Buffer.byteLength(result);
  }
  return result;
};
try {
  for (const language of ['node', 'react', 'python']) {
    const root = path.join(output, language); fs.mkdirSync(root);
    const inputs = [];
    const write = (name, content) => { fs.writeFileSync(path.join(root, name), content); inputs.push({ path: name, sha256: hash(content), bytes: Buffer.byteLength(content) }); };
    if (language !== 'python') write('package.json', JSON.stringify({ name: 'cache-measure-' + language, type: 'module' }) + '\n');
    else write('pyproject.toml', '[project]\nname = "cache-measure-python"\nversion = "0.1.0"\n');
    const extension = language === 'node' ? 'mjs' : language === 'react' ? 'tsx' : 'py';
    for (let file = 0; file < 72; file++) {
      const declarations = Array.from({ length: 36 }, (_, item) => language === 'python'
        ? `def calculate_${file}_${item}(value):\n    return value + ${item}\n`
        : language === 'react' ? `export const Component${file}_${item} = ({value}: {value: number}) => <button aria-label="查看 ${file}-${item}">{value + ${item}}</button>;\n`
          : `export function calculate${file}_${item}(value) { const result = value + ${item}; return result; }\n`);
      write('module-' + file + '.' + extension, declarations.join(''));
    }
    const project = { id: 'measure-' + language, root: fs.realpathSync.native(root), name: '固定' + language + '解析样本' };
    const cacheDirectory = path.join(output, 'derived-' + language);
    const create = () => new ProjectIndex(phase === 'comparison' ? { cacheDirectory } : {});
    let indexer = create(), first;
    const measure = async (name, indexer) => {
      reads = 0; bytes = 0; currentRoot = project.root;
      const started = performance.now(), index = await indexer.refresh(project), durationMs = performance.now() - started;
      currentRoot = null;
      const result = { language, case: name, durationMs, sourceReads: reads, sourceBytesRead: bytes, refresh: index.refresh,
        cache: index.cache ?? null, sourceFingerprint: index.sourceFingerprint, indexFingerprint: index.indexFingerprint,
        coverage: index.coverage };
      rows.push(result);
      assert.equal(index.coverage.snapshotComplete, true); assert.equal(index.coverage.unindexedFiles, 0);
      console.log(language + '/' + name + ': ' + durationMs.toFixed(1) + 'ms, reads=' + reads + ', updated=' + index.refresh.updatedFiles + ', reused=' + index.refresh.reusedFiles);
      return index;
    };
    first = await measure('cold', indexer);
    for (let i = 0; i < 3; i++) {
      const warm = await measure('warm-' + (i + 1), indexer);
      assert.equal(warm.indexFingerprint, first.indexFingerprint);
    }
    const restarted = await measure('new-indexer', create());
    assert.equal(restarted.indexFingerprint, first.indexFingerprint);
    const target = path.join(root, 'module-0.' + extension), old = fs.statSync(target);
    const content = originalRead(target, 'utf8'), changed = content.replace('value + 0', 'value + 9');
    assert.notEqual(content, changed); assert.equal(Buffer.byteLength(content), Buffer.byteLength(changed));
    fs.writeFileSync(target, changed); fs.utimesSync(target, old.atime, old.mtime);
    const modified = await measure('same-size-mtime-change', indexer);
    assert.notEqual(modified.sourceFingerprint, first.sourceFingerprint); assert.equal(modified.refresh.updatedFiles, 1);
    fs.writeFileSync(path.join(output, language + '-inputs.json'), JSON.stringify({ project, inputs,
      mutation: { path: path.basename(target), before: hash(content), after: hash(changed), preservedSize: true, restoredMtime: true } }, null, 2) + '\n');
  }
} finally { fs.readFileSync = originalRead; fs.openSync = originalOpen; fs.closeSync = originalClose; }
const report = { success: true, phase, at: new Date().toISOString(), node: process.version,
  baselineIdentity,
  interpretation: 'Local derived parsing only. Source reads include declarations and all current hashes. No model calls or token/cost inference.', rows };
fs.writeFileSync(path.join(output, 'result.json'), JSON.stringify(report, null, 2) + '\n');
console.log('EVIDENCE ' + output);
