import CryptoJS from "crypto-js";

// Secret key (keep in .env)
const SECRET = process.env.NEXT_PUBLIC_LINK_SECRET || "mysecret";

export function encodeLink(fileUrl: string, leadId: string) {
  const payload = JSON.stringify({ fileUrl, leadId });
  const encrypted = CryptoJS.AES.encrypt(payload, SECRET).toString();
  // make it URL-safe
  return encodeURIComponent(encrypted);
}

export function decodeLink(token: string) {
  const bytes = CryptoJS.AES.decrypt(decodeURIComponent(token), SECRET);
  const decrypted = bytes.toString(CryptoJS.enc.Utf8);
  return JSON.parse(decrypted);
}
