package infra

import (
	"fmt"
	"os/exec"
)

// NotifyDesktop sends a desktop notification on Linux via the `notify-send`
// binary (libnotify). It is impure: it shells out to an external program.
//
// Degradation is intentional and silent: if `notify-send` is not on the PATH,
// the function returns nil without error. A machine without a notification
// server is not a failure condition for the caller — the notification is simply
// skipped. Only a real execution failure of an existing `notify-send` is
// returned (wrapped with context).
//
// When `notify-send` is present it runs:
//
//	notify-send --app-name=fleetview --urgency=normal -- <title> <body>
//
// The `--` separator guarantees that a title or body starting with "-" is
// treated as positional text, not as a flag. An empty title falls back to a
// sensible default; an empty body is accepted by notify-send as-is.
func NotifyDesktop(title, body string) error {
	bin, err := exec.LookPath("notify-send")
	if err != nil {
		// No notification server / binary on this machine: skip silently.
		return nil
	}

	if title == "" {
		title = "Notificación"
	}

	cmd := exec.Command(bin, "--app-name=fleetview", "--urgency=normal", "--", title, body)
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("notify-send failed: %w", err)
	}
	return nil
}
