import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

const currentDir = path.dirname(fileURLToPath(import.meta.url));
const serverRoot = path.resolve(currentDir, "..");
const projectRoot = path.resolve(serverRoot, "..");
const sourceDist = path.join(projectRoot, "client", "dist");
const targetDist = path.join(serverRoot, "client", "dist");
const sourceIndex = path.join(sourceDist, "index.html");

if (!fs.existsSync(sourceIndex)) {
  console.error(`Client build not found: ${sourceIndex}`);
  console.error("Run `npm --workspace client run build` before preparing the cPanel server folder.");
  process.exit(1);
}

fs.rmSync(targetDist, { recursive: true, force: true });
fs.mkdirSync(path.dirname(targetDist), { recursive: true });
fs.cpSync(sourceDist, targetDist, { recursive: true });

console.log(`Client dist copied to ${targetDist}`);
