import crypto from "node:crypto";
const GW = "http://172.28.144.1:8088";
const SECRET = "p9Xv7Lm2Qq8Nz4TfKc1Hs6YwRe3Ud0BafwefWEFw324234QEWF";
const PID = "90571220-e6b2-44f3-b5a2-ed65999bf525";
const b64 = (b) => Buffer.from(b).toString("base64url");
const now = Math.floor(Date.now() / 1000);
const h = b64(JSON.stringify({ alg: "HS256", typ: "JWT" }));
const p = b64(JSON.stringify({ sub: "00000000-0000-0000-0000-0000000000aa", tenant_id: "00000000-0000-0000-0000-0000000000bb", tenant_slug: "flatrender", is_admin: "true", role: "Admin", iss: "flatrender-identity", aud: "flatrender", exp: now + 3600, iat: now }));
const sig = crypto.createHmac("sha256", SECRET).update(`${h}.${p}`).digest("base64url");
const H = { "Content-Type": "application/json", Authorization: `Bearer ${h}.${p}.${sig}` };

const scan = { source: "ae-jsx", scenes: [{ key: "c1", title: "Scene 1", scene_type: "Normal", elements: [{ key: "frl_c1t1", title: "frl_c1t1", type: "Text", default_value: "hello" }], colors: [] }], shared_colors: [] };

const res = await fetch(`${GW}/v1/projects/${PID}/scan/apply`, { method: "POST", headers: H, body: JSON.stringify({ scan, options: { overwrite_existing: true } }) });
console.log("apply status:", res.status);
console.log("body:", (await res.text()).slice(0, 400));
