import fs from 'node:fs';
import { CondensedGraph } from '../src/core/CondensedGraph.js';
import { ShardedSnapshotBuilder } from '../src/core/shards/ShardedSnapshotBuilder.js';

function parseArgs(argv) {
  const args = new Map();
  for (let i = 2; i < argv.length; i++) {
    const value = argv[i];
    if (!value.startsWith('--')) continue;
    const [key, inline] = value.slice(2).split('=');
    if (inline !== undefined) {
      args.set(key, inline);
      continue;
    }
    const next = argv[i + 1];
    if (next && !next.startsWith('--')) {
      args.set(key, next);
      i++;
    } else {
      args.set(key, true);
    }
  }
  return args;
}

const args = parseArgs(process.argv);
const input = args.get('input');
const output = args.get('output') || 'tmp/shards';
const bucketSize = Number(args.get('bucket') || 4096);
const directions = args.get('directions')
  ? String(args.get('directions')).split(',')
  : ['out', 'in'];

if (!input) {
  console.error('Usage: node new-eval/sharded-snapshot-build.js --input <condensed.bin> --output <dir> [--bucket 4096] [--directions out,in]');
  process.exit(1);
}

const buffer = fs.readFileSync(input);
const graph = CondensedGraph.fromBinary(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
const builder = new ShardedSnapshotBuilder({ bucketSize, includeDirections: directions });
const manifest = builder.build(graph, output);
console.log(`Shards written to ${output}`);
console.log(`Manifest shards: ${manifest.shards.length}`);
