#!/usr/bin/env bash

# Toggles the nightlight (hyprsunset) between enabled (4000) and disabled (6500)
# Declarative + Hybrid (instant IPC) for Nomarchy NixOS.

STATE_FILE="$HOME/.config/home-manager/hyprsunset-state.json"
mkdir -p "$(dirname "$STATE_FILE")"

if [ ! -f "$STATE_FILE" ]; then
    echo '{"enabled": false, "temperature": 4000}' > "$STATE_FILE"
fi

enabled=$(jq -r '.enabled' "$STATE_FILE")

if [[ $enabled == "true" ]]; then
    NEW_ENABLED="false"
    TEMP=6500
    notify-send -u low "  Nightlight DISABLED"
else
    NEW_ENABLED="true"
    TEMP=4000
    notify-send -u low "  Nightlight ENABLED"
fi

# Instant feedback via IPC
if pgrep -x hyprsunset >/dev/null; then
    hyprctl hyprsunset temperature $TEMP
else
    # Should be started by systemd, but just in case
    setsid hyprsunset -t $TEMP &
fi

echo "{\"enabled\": $NEW_ENABLED, \"temperature\": 4000}" > "$STATE_FILE"
echo "Nightlight set to $NEW_ENABLED ($TEMP) declaratively."
# No need to run env-update if we don't want the delay, 
# as next HM switch will pick it up and apply it.
