/**
 * Offline check: prints the VERIFY response Smartcar expects (HMAC-SHA256 hex).
 * Compare output to the Dashboard "Expected response" when debugging verification.
 *
 * Usage:
 *   Set APPLICATION_MANAGEMENT_TOKEN (or SMARTCAR_APPLICATION_MANAGEMENT_TOKEN), then:
 *   npx ts-node src/scripts/smartcarWebhookVerifyCli.ts challenge_yourChallengeStringHere
 */

import "../loadEnv";
import { hashSmartcarWebhookChallenge } from "../utils/smartcarWebhookChallenge";

function main() {
  const challenge = process.argv[2]?.trim();
  const amt =
    process.env.APPLICATION_MANAGEMENT_TOKEN?.trim() ||
    process.env.SMARTCAR_APPLICATION_MANAGEMENT_TOKEN?.trim();

  if (!challenge || challenge.startsWith("-")) {
    console.error(
      "Usage: npx ts-node src/scripts/smartcarWebhookVerifyCli.ts <challenge_string>"
    );
    process.exit(1);
  }

  if (!amt) {
    console.error(
      "Set APPLICATION_MANAGEMENT_TOKEN or SMARTCAR_APPLICATION_MANAGEMENT_TOKEN in .env first."
    );
    process.exit(1);
  }

  const hmacHex = hashSmartcarWebhookChallenge(amt, challenge);
  console.log(JSON.stringify({ challenge: hmacHex }, null, 0));
}

main();
