const fs = require("fs");
const path = require("path");

const mode = process.argv[2] === "local" ? "local" : "prod";
const defaultApiBase = mode === "prod" ? "https://dlxlh.dtacloud.cn" : "http://localhost:8082";
const apiBase = String(process.env.TARO_APP_API_BASE || defaultApiBase).replace(/\/+$/, "");
const optional = process.env.SKILL_CONFIG_OPTIONAL === "1";

const target = path.resolve(__dirname, "..", "dist", "skills", "activity-skill", "utils", "config.js");
const targetDir = path.dirname(target);

if (!fs.existsSync(targetDir)) {
  if (optional) {
    process.exit(0);
  }
  console.error(`Activity skill config directory not found: ${targetDir}`);
  process.exit(1);
}

const content = `const BASE_URL = ${JSON.stringify(apiBase)}
const SOURCE = 'WECHAT_AI_SKILL'

module.exports = {
  BASE_URL,
  SOURCE
}
`;

fs.writeFileSync(target, content, "utf8");
console.log(`Activity skill API base written: ${apiBase}`);
