import { execFileSync } from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { writeRunScript } from "./writeRunScript.js";
import { cronToOnCalendar } from "../cron.js";
import renderService from "../../../templates/cli/schedule/service.js";
import renderTimer from "../../../templates/cli/schedule/timer.js";
const UNIT_DIR = path.join(os.homedir(), ".config", "systemd", "user");
function unitName(name) {
    return `agency-schedule-${name}`;
}
export class SystemdBackend {
    install(entry) {
        const runScriptPath = writeRunScript(entry);
        const unit = unitName(entry.name);
        const service = renderService({
            name: entry.name,
            agentDir: path.dirname(entry.agentFile),
            runScriptPath,
        });
        const timer = renderTimer({
            name: entry.name,
            onCalendar: cronToOnCalendar(entry.cron),
        });
        fs.mkdirSync(UNIT_DIR, { recursive: true });
        fs.writeFileSync(path.join(UNIT_DIR, `${unit}.service`), service);
        fs.writeFileSync(path.join(UNIT_DIR, `${unit}.timer`), timer);
        execFileSync("systemctl", ["--user", "daemon-reload"]);
        execFileSync("systemctl", ["--user", "enable", "--now", `${unit}.timer`]);
    }
    uninstall(name) {
        const unit = unitName(name);
        try {
            execFileSync("systemctl", ["--user", "disable", "--now", `${unit}.timer`]);
        }
        catch { }
        const servicePath = path.join(UNIT_DIR, `${unit}.service`);
        const timerPath = path.join(UNIT_DIR, `${unit}.timer`);
        if (fs.existsSync(servicePath))
            fs.unlinkSync(servicePath);
        if (fs.existsSync(timerPath))
            fs.unlinkSync(timerPath);
        try {
            execFileSync("systemctl", ["--user", "daemon-reload"]);
        }
        catch { }
    }
}
