import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { z } from "zod";

const execFileAsync = promisify(execFile);

export const systemPowerActionSchema = z.enum(["reboot", "poweroff"]);
export type SystemPowerAction = z.infer<typeof systemPowerActionSchema>;

export async function triggerSystemPower(action: SystemPowerAction): Promise<void> {
  await execFileAsync("/usr/bin/sudo", ["-n", "/usr/local/sbin/pi-car-companion-power", action], {
    timeout: 10_000,
    maxBuffer: 16 * 1024
  });
}
