const fs = require('fs'); const path = require('path'); const { outputsRoot } = require('./output-paths'); function webhookDir() { return path.join(outputsRoot(), 'webhook'); } function ensureWebhookDir() { fs.mkdirSync(webhookDir(), { recursive: true }); } function configPath() { ensureWebhookDir(); return path.join(webhookDir(), 'webhook-config.json'); } function readConfig() { const filePath = configPath(); if (!fs.existsSync(filePath)) return { callbackUrl: null, secret: null, autoCreateGroup: true }; try { return { callbackUrl: null, secret: null, autoCreateGroup: true, ...JSON.parse(fs.readFileSync(filePath, 'utf8')) }; } catch { return { callbackUrl: null, secret: null, autoCreateGroup: true }; } } function writeConfig(config) { ensureWebhookDir(); fs.writeFileSync(configPath(), JSON.stringify({ ...readConfig(), ...config, updatedAt: new Date().toISOString() }, null, 2), 'utf8'); } function relayConfigPath() { ensureWebhookDir(); return path.join(webhookDir(), 'relay-config.json'); } function readRelayConfig() { const filePath = relayConfigPath(); if (!fs.existsSync(filePath)) return {}; try { return JSON.parse(fs.readFileSync(filePath, 'utf8')); } catch { return {}; } } function writeRelayConfig(config) { ensureWebhookDir(); fs.writeFileSync(relayConfigPath(), JSON.stringify({ ...readRelayConfig(), ...config, updatedAt: new Date().toISOString() }, null, 2), 'utf8'); } module.exports = { readConfig, writeConfig, readRelayConfig, writeRelayConfig };