#!/usr/bin/env node

/**
 * inject-rewriteMedia.js
 *
 * deny image_generate 工具，使智能体优先使用 yiran-skill-media skill。
 */

const { readConfig, writeConfig } = require('../find-config');

function run() {
  let configPath;
  try {
    ({ configPath } = readConfig());
  } catch (err) {
    console.error('❌ ' + err.message);
    process.exit(1);
  }

  console.log('📍 找到配置: ' + configPath);
  console.log('');

  const { config } = readConfig();

  // ── deny image_generate 工具 ──
  if (!config.tools) config.tools = {};
  if (!config.tools.deny) config.tools.deny = [];
  for (const tool of ['image_generate', 'music_generate']) {
    if (!config.tools.deny.includes(tool)) {
      config.tools.deny.push(tool);
      console.log('  ✓ 已添加 tools.deny["' + tool + '"]');
    } else {
      console.log('  ○ tools.deny 已包含 ' + tool + '，跳过');
    }
  }

  writeConfig(config, configPath);

  // ── 验证 ──
  const verify = readConfig();
  const deniedTools = verify.config.tools?.deny || [];

  console.log('');
  console.log('✅ rewriteMedia 注入完成');
  console.log('  [验证] tools.deny image_generate: ' + deniedTools.includes('image_generate'));
  console.log('  [验证] tools.deny music_generate: ' + deniedTools.includes('music_generate'));
  console.log('');
}

module.exports = { run };
