const argumentsList = process.argv.slice(2);
if (argumentsList[0] === "--") argumentsList.shift();
const [expectedVersion] = argumentsList;
if (!expectedVersion) throw new Error("Usage: node scripts/check-deployment.mjs <expected-version-id>");

let input = "";
for await (const chunk of process.stdin) input += chunk;

let deployment;
try { deployment = JSON.parse(input); }
catch { throw new Error("Wrangler deployment status did not contain valid JSON."); }

const versions = deployment?.versions;
if (!Array.isArray(versions) || versions.length !== 1 ||
    versions[0]?.version_id !== expectedVersion || versions[0]?.percentage !== 100) {
  throw new Error("The candidate is not the sole 100% production version.");
}

console.log(`SmartCut64 licensing deployment is 100% version ${expectedVersion}.`);
