#!/usr/bin/env bash
set -euo pipefail
# POS: entertainment status — Show enabled plugins and scheduler state

source "$(dirname "$0")/../lib/common.sh" 2>/dev/null || source "$(dirname "$0")/common.sh"
source "$(dirname "$0")/../lib/entertainment-lib.sh" 2>/dev/null || source "$(dirname "$0")/entertainment-lib.sh"

usage() {
    cat <<EOF
Usage: pos entertainment status

Show the enabled plugins (from ENABLED in $CONFIG_FILE),
the detected scheduler, and the schedule for each plugin.

Enable plugins with: pos entertainment enable <plugin> [interval]
EOF
    exit 0
}

case "${1:-}" in
    -h|--help) usage ;;
esac

raw="$(config_value ENABLED)"
echo "Config: $CONFIG_FILE"

parse_enabled "$raw"
echo "Enabled plugins:"
if [ ${#ENABLED_ENTRIES[@]} -eq 0 ]; then
    echo "  (none — enable one with: pos entertainment enable <plugin> [interval])"
fi
for entry in "${ENABLED_ENTRIES[@]}"; do
    plugin="${entry%%,*}"; interval="${entry##*,}"
    [ "$interval" = "$plugin" ] && interval="$DEFAULT_INTERVAL"
    printf '  %-12s %-16s last run: %s\n' "$plugin" "$(ut_interval_label "$interval")" "$(last_run_str "$plugin")"
done

not_enabled=()
for name in $(list_plugins "$(ent_plugin_dir)"); do
    found=0
    for entry in "${ENABLED_ENTRIES[@]}"; do
        [ "${entry%%,*}" = "$name" ] && found=1 && break
    done
    [ "$found" -eq 0 ] && not_enabled+=("$name")
done
if [ ${#not_enabled[@]} -gt 0 ]; then
    echo
    echo "Installed but not enabled:"
    for name in "${not_enabled[@]}"; do
        printf '  %s\n' "$name"
    done
fi

echo
if systemctl --user show-environment >/dev/null 2>&1; then
    echo "Scheduler: systemd user timers"
    echo "Timers (systemd --user):"
    systemctl --user list-timers --no-pager 'pos-entertainment-*.timer' 2>/dev/null || \
        echo "  (no timers)"
else
    echo "Scheduler: none available (no systemd user manager)"
fi
