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

const configPath = path.join(__dirname, "app.config.js");
let configFile = fs.readFileSync(configPath, "utf8");

// --- Update versionCode ---
const codeRegex = /versionCode:\s*(\d+)/;
const codeMatch = configFile.match(codeRegex);

if (!codeMatch) {
  console.error("❌ Tidak menemukan versionCode di app.config.js");
  process.exit(1);
}

const currentCode = parseInt(codeMatch[1], 10);
const newCode = currentCode + 1;
configFile = configFile.replace(codeRegex, `versionCode: ${newCode}`);

// --- Update versionName ---
const nameRegex = /version:\s*"(.*?)"/;
const nameMatch = configFile.match(nameRegex);

if (!nameMatch) {
  console.error("❌ Tidak menemukan version di app.config.js");
  process.exit(1);
}

let [major, minor, patch] = nameMatch[1].split(".").map(Number);
patch += 1; // bump patch version
const newName = `${major}.${minor}.${patch}`;
configFile = configFile.replace(nameRegex, `version: "${newName}"`);

// --- Simpan file ---
fs.writeFileSync(configPath, configFile, "utf8");

console.log(`✅ versionCode: ${currentCode} → ${newCode}`);
console.log(`✅ versionName: ${nameMatch[1]} → ${newName}`);
