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

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const buildEnv = process.env.ELECTROBUN_BUILD_ENV || "dev";

const requiredFiles = [
  "src/bun/index.js",
  "src/app/index.html",
  "src/app/main.jsx",
  "electrobun.config.ts",
  "package.json",
  "config.json",
];

for (const relativePath of requiredFiles) {
  const filePath = path.join(rootDir, relativePath);
  if (!existsSync(filePath)) {
    console.error(`pre-build: required file not found: ${relativePath}`);
    process.exit(1);
  }
}

if (buildEnv === "stable" && process.env.ELECTROBUN_OS === "macos") {
  const codesignVars = [
    "ELECTROBUN_DEVELOPER_ID",
    "ELECTROBUN_APPLEID",
    "ELECTROBUN_APPLEIDPASS",
    "ELECTROBUN_TEAMID",
  ];
  const missing = codesignVars.filter((name) => !process.env[name]);

  if (missing.length > 0) {
    console.warn(
      `pre-build: stable macOS build without code signing credentials (${missing.join(", ")}); Electrobun will skip signing.`,
    );
  }
}

const prepareIcons = spawnSync(
  "bun",
  [
    path.join(rootDir, "scripts/prepare-app-icons.mjs"),
    "assets/farmcontrolhosticon.png",
  ],
  { cwd: rootDir, stdio: "inherit", env: process.env },
);

if (prepareIcons.status !== 0) {
  process.exit(prepareIcons.status ?? 1);
}

const writeBuildInfo = spawnSync(
  "bun",
  [path.join(rootDir, "scripts/write-build-info.mjs")],
  { cwd: rootDir, stdio: "inherit", env: process.env },
);

if (writeBuildInfo.status !== 0) {
  process.exit(writeBuildInfo.status ?? 1);
}

if (buildEnv === "dev") {
  console.log(
    "pre-build: dev environment — skipping production renderer build (use dev:renderer for Vite)",
  );
  console.log(`pre-build: validation passed (${buildEnv})`);
  process.exit(0);
}

const buildRenderer = spawnSync(
  "bun",
  [path.join(rootDir, "scripts/build-renderer.mjs")],
  {
    cwd: rootDir,
    stdio: "inherit",
    env: {
      ...process.env,
      NODE_ENV: "production",
    },
  },
);

if (buildRenderer.status !== 0) {
  process.exit(buildRenderer.status ?? 1);
}

const rendererIndex = path.join(rootDir, "dist/mainview/index.html");
if (!existsSync(rendererIndex)) {
  console.error(`pre-build: renderer output not found: dist/mainview/index.html`);
  process.exit(1);
}

console.log(`pre-build: validation and renderer build passed (${buildEnv})`);
