import { promises as fs } from "fs";
import { join } from "path";
import { homedir } from "os";
import { exec } from "child_process";
import { promisify } from "util";
import log from "electron-log/main";

const execPromise = promisify(exec);

// Define the interval as a variable (in seconds)
const KEEP_ALIVE_INTERVAL_SECONDS = 15 * 60; // 15 minutes

export async function setupKeepAliveAgent(): Promise<void> {
  const plistContent = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.convenientbrands.bodyshop-desktop.keepalive</string>
  <key>ProgramArguments</key>
  <array>
    <string>Shop Partner Keep Alive</string>
    <string>imexmedia://keep-alive</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>StartInterval</key>
  <integer>${KEEP_ALIVE_INTERVAL_SECONDS}</integer>
</dict>
</plist>`;

  const plistPath = join(
    homedir(),
    "/Library/LaunchAgents/com.convenientbrands.bodyshop-desktop.keepalive.plist",
  );

  try {
    await fs.writeFile(plistPath, plistContent);
    const { stdout, stderr } = await execPromise(`launchctl load ${plistPath}`);
    log.info(`Launch agent created and loaded: ${stdout}`);
    if (stderr) log.warn(`Launch agent stderr: ${stderr}`);
  } catch (error) {
    log.error(
      `Error setting up launch agent: ${error instanceof Error ? error.message : String(error)}`,
    );
    throw error; // Rethrow to allow caller to handle
  }
}

export async function isKeepAliveAgentInstalled(): Promise<boolean> {
  const plistPath = join(
    homedir(),
    "/Library/LaunchAgents/com.convenientbrands.bodyshop-desktop.keepalive.plist",
  );
  const maxRetries = 3;
  const retryDelay = 500; // 500ms delay between retries

  for (let attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      await fs.access(plistPath, fs.constants.F_OK);
      const { stdout } = await execPromise(
        `launchctl list | grep com.convenientbrands.bodyshop-desktop.keepalive`,
      );
      return !!stdout; // Return true if plist exists and agent is loaded
    } catch (error) {
      log.debug(
        `Launch agent not found (attempt ${attempt}/${maxRetries}): ${error instanceof Error ? error.message : String(error)}`,
      );
      if (attempt === maxRetries) {
        return false; // Return false after all retries fail
      }
      // Wait before retrying
      await new Promise((resolve) => setTimeout(resolve, retryDelay));
    }
  }
  return false; // Fallback return
}
