const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

function setConvexEnv(name, value) {
  // 说明：使用 stdin 传值，避免以 `-----BEGIN ...` 开头的内容被 CLI 误当作参数/选项。
  execSync(`npx convex env set ${name}`, {
    input: String(value ?? ""),
    stdio: ['pipe', 'inherit', 'inherit'],
    cwd: process.cwd(),
  });
}

const privateKey = `-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCPcrWDwjh74nu1
gfYo9ywVIRMedfPwbNiDjHnovzHAIeToc1+Lw3ju/io9OME3KLWzIibUHX39LilK
sSQKigJ3+i7J13IquJD++ozY9C8Dp/P34txVY+ECSQRdIBbETLJxYBWXOx6Ysw/d
/CUuDOdEw2IhNvN97sTh5jUZe6f/H6mfCpJ3X5SZrqrIUTTL6r4Lj9ZS3PUX5ivU
6sd2rXzpDsUQgrNgmvGN3w9nAHudxAUv/zOGa4802Z+z14LCBIX6v6PIK79YQ4s9
/5hH73MesA6vgzWShJcyVy3guyde+duhpECo1q3d6dIZeqrazQwqAW3vcpmEFJdX
jkpociDfAgMBAAECggEAAYtRE+mH1SGThlkvTrKWeWXBQG8xoJFzZTsiZtSEExbq
UWxIh4cjqqL2znDpd5ALILIJ6/ejTxHrpN+yTSC+NQ9u6IJWusoA2ZXV5VH/nZD1
yeHZ0FuCZRVnJB9/zz4qH5lSsi2TPz6SOagIuG2wIafeyw+94EmtOedSBAO2Q8NN
3jHoINBCyRu6hU3ml0h7daoIhUw9ONI7MZUlYvuV7Ti3yf+czzpqwYx3gZA39sa8
qUbJPFk6ts+CVjqSAdYVSfU3TC1Us1usOM3+04mACp3vc6ZUKxnB4Dsk/sSHj71n
EcZ3fOR7EgjmXf6wBiq24T2+0UzoHW2yDaiAowr6iQKBgQDDs2eJTopFejt47EMm
zY//e8fyUDKx07CWwUZKP78IMTZcp8BwHmKTSY+VbQLvVYnNxADkckYapokjU3a7
GMOXxvKVLqfgU/oUBIkRFDRo+n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fs9eCwgSF-r-jyCu_WEOLPf-YR-9zHrAOr4M1koSXMlct4LsnXvnboaRAqNat3enSGXqq2s0MKgFt73KZhBSXV45KaHIg3w","e":"AQAB"}]}';

// 使用临时目录（跨平台兼容）
const tmpDir = process.env.TMPDIR || process.env.TEMP || '/tmp';
const keyFile = path.join(tmpDir, 'convex_jwt_key.txt');
const jwksFile = path.join(tmpDir, 'convex_jwks.txt');

// 写入临时文件
fs.writeFileSync(keyFile, privateKey);
fs.writeFileSync(jwksFile, jwks);

console.log('Setting JWT_PRIVATE_KEY from file...');
const keyContent = fs.readFileSync(keyFile, 'utf8');
try {
  setConvexEnv("JWT_PRIVATE_KEY", keyContent);
  console.log('✓ JWT_PRIVATE_KEY set');
} catch (e) {
  console.error('✗ Failed to set JWT_PRIVATE_KEY:', e.message);
}

console.log('Setting JWKS...');
try {
  setConvexEnv("JWKS", jwks);
  console.log('✓ JWKS set');
} catch (e) {
  console.error('✗ Failed to set JWKS:', e.message);
}

// 清理临时文件
try {
  fs.unlinkSync(keyFile);
  fs.unlinkSync(jwksFile);
} catch (e) {
  // 忽略清理错误
}

console.log('Done! You can now test registration.');
