#!/usr/bin/env node

/**
 * inject-controlui.js
 *
 * 将 gateway.controlUi 配置设置为：
 *   allowInsecureAuth: true
 *   allowedOrigins: ["*"]
 *   dangerouslyDisableDeviceAuth: true
 *
 * 入口: myclaw inject-controlui
 */

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

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

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

  if (!config.gateway) config.gateway = {};

  config.gateway.controlUi = {
    allowInsecureAuth: true,
    allowedOrigins: ['*'],
    dangerouslyDisableDeviceAuth: true,
  };

  writeConfig(config, configPath);

  console.log('✅ gateway.controlUi 已设置:');
  console.log('   allowInsecureAuth:          true');
  console.log('   allowedOrigins:             ["*"]');
  console.log('   dangerouslyDisableDeviceAuth: true');
  console.log('');
}

module.exports = { run };
