#!/usr/bin/env node
/**
 * Wraps `drizzle-kit push --force` so it can run under a closed-stdin
 * post-merge / system-update environment.
 *
 * Drizzle's column/table rename-detection picker prints lines like:
 *   "Is <name> column in <table> table created or renamed from another column?"
 *   "Is <name> table created or renamed from another table?"
 * The highlighted default ("❯") is always the safe "+ create …" option,
 * which is what we want — picking a rename would silently drop data.
 *
 * For each unique question we see, we send a single CR to accept the
 * highlighted default. We key by the question line itself, not by the
 * presence of "❯", so re-renders of the same prompt don't double-send,
 * and a brand-new prompt right after a previous one isn't dropped by a
 * time-based throttle.
 */
import { spawn } from "node:child_process";

const child = spawn("npx", ["drizzle-kit", "push", "--force"], {
  stdio: ["pipe", "pipe", "inherit"],
  env: { ...process.env, FORCE_COLOR: "0" },
});

// Each known interactive drizzle-kit prompt is matched here. We answer
// every unique question exactly once by sending CR, which selects the
// highlighted default — "+ create" for rename detection, and the safe
// "No, …" option for the truncate confirmation. Add new patterns here
// as drizzle-kit introduces new question types.
const PROMPT_PATTERNS = [
  /Is\s+\S+\s+(?:column|table).*?created or renamed from another (?:column|table)\?/g,
  /Do you want to truncate\s+\S+\s+table\?/g,
];

let buf = "";
const seenPrompts = new Set();

child.stdout.on("data", (chunk) => {
  const s = chunk.toString("utf8");
  process.stdout.write(s);
  buf += s;

  for (const re of PROMPT_PATTERNS) {
    re.lastIndex = 0;
    let m;
    while ((m = re.exec(buf)) !== null) {
      const key = m[0];
      if (!seenPrompts.has(key)) {
        seenPrompts.add(key);
        child.stdin.write("\r");
      }
    }
  }

  if (buf.length > 8192) buf = buf.slice(-2048);
});

child.on("exit", (code) => process.exit(code ?? 1));
child.on("error", (err) => {
  console.error("drizzle-kit spawn failed:", err);
  process.exit(1);
});
