import { createHmac } from "node:crypto";

type TestAdminTokenClaims = {
  aud: string;
  email: string;
  exp: number;
  role: "viewer" | "operator" | "superadmin";
  sub: string;
  type: "admin_access" | "admin_session";
};

function encodeSegment(value: unknown) {
  return Buffer.from(JSON.stringify(value)).toString("base64url");
}

export function createSignedAdminToken(claims: TestAdminTokenClaims, secret: string) {
  const header = encodeSegment({ alg: "HS256", typ: "JWT" });
  const payload = encodeSegment(claims);
  const signature = createHmac("sha256", secret).update(`${header}.${payload}`).digest("base64url");

  return `${header}.${payload}.${signature}`;
}
