#!/bin/bash

# omarchy:summary=Shut down after closing application windows
# omarchy:examples=omarchy shutdown | omarchy system shutdown
# omarchy:aliases=omarchy shutdown

if [[ ${1:-} == "--inhibited" ]]; then
  # Close windows in the background so they cannot delay power-off.
  (
    omarchy-osd -i shutdown -m "Shutting down" -d 5000
    omarchy-state clear re*-required
    omarchy-hyprland-window-close-all
  ) &

  sleep 2 # Allow apps like Chrome to shut down correctly
  systemctl poweroff --no-wall
elif [[ ${1:-} == "--inhibit" ]]; then
  # Report failures from inside the service, independently of the launching terminal.
  # Block lid handling explicitly: it ignores high-level sleep inhibitors.
  systemd-inhibit --what=sleep:idle:handle-lid-switch \
    --who=Omarchy --why="Shutdown in progress" --mode=block \
    "$0" --inhibited || {
    status=$?
    omarchy-notification-send -u critical "Shutdown failed" "Could not complete shutdown. Please try again."
    exit "$status"
  }
else
  # Schedule the shutdown in the user manager so closing the terminal's systemd
  # scope cannot terminate it before it runs.
  systemd-run --user --collect --quiet --property=Type=exec --property=RuntimeMaxSec=30s \
    "$OMARCHY_PATH/bin/omarchy-system-shutdown" --inhibit
fi
