import { readFileSync, writeFileSync } from "fs";
import { execSync } from "child_process";
import { join, dirname } from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, "..");
const versionFile = join(rootDir, "src", "version.txt");

const version = readFileSync(versionFile, "utf-8").trim();
const parts = version.split(".");
const major = parseInt(parts[0], 10);
const minor = parseInt(parts[1], 10) + 1;
const newVersion = `${major.toString().padStart(2, "0")}.${minor
  .toString()
  .padStart(2, "0")}`;

writeFileSync(versionFile, newVersion + "\n", "utf-8");

try {
  execSync(`git add "${versionFile}"`, { cwd: rootDir, stdio: "inherit" });
  execSync(`git commit -m "version changed to ${newVersion}"`, {
    cwd: rootDir,
    stdio: "inherit",
  });
  execSync(`git push`, {
    cwd: rootDir,
    stdio: "inherit",
  });
  execSync(`git pull`, {
    cwd: rootDir,
    stdio: "inherit",
  });
  console.log(`Version bumped to ${newVersion} and committed`);
} catch (error) {
  console.error("Failed to commit:", error.message);
}
