// Block direct .env file edits (not .env.example, .env.production, .env.test)
//
// Hook exit-code contract (Claude Code PreToolUse):
//   exit 0 = allow (stdout JSON is only parsed on exit 0)
//   exit 2 = BLOCK — the reason must be written to STDERR
//   any other exit code = non-blocking error (the tool call proceeds)
// So to actually block, we must print to stderr and exit 2.
let data = "";
process.stdin.on("data", (chunk) => (data += chunk));
process.stdin.on("end", () => {
  try {
    const input = JSON.parse(data);
    const filePath = input.tool_input?.file_path || "";
    const basename = filePath.split("/").pop().split("\\").pop();
    if (basename === ".env") {
      console.error("Direct .env edits are blocked. Use .env.example instead.");
      process.exit(2);
    }
  } catch {
    // Deliberate fail-open: on unparseable input exit 0 (allow). Exiting 2
    // here would block EVERY Edit/Write if the hook payload format changed.
  }
});
