#!/usr/bin/env bash
set -euo pipefail
# POS: entertainment config — Show or edit the entertainment config (ENABLED auto-trigger list, weather location)

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"

keys_section() {
    # Renders the Keys reference: ENABLED + every key declared by installed
    # plugins (via their # POS_KEYS: headers), aligned by key name.
    local d="$1" maxw=8 line key plugin desc req
    while IFS= read -r line; do
        IFS='|' read -r key _desc _req <<<"$line"
        [ ${#key} -gt "$maxw" ] && maxw=${#key}
    done <<< "$(config_keys "$d")"

    printf '  %-*s %s\n' "$maxw" "ENABLED" "Auto-trigger list: comma-separated 'plugin, interval' pairs,"
    printf '  %-*s %s\n' "$maxw" "" "e.g. ENABLED=\"weather, 5m gold, 1h\""
    while IFS= read -r line; do
        [ -n "$line" ] || continue
        IFS='|' read -r plugin key desc req <<<"$line"
        printf '  %-*s %s (%s)   [%s]\n' "$maxw" "$key" "$desc" "$req" "$plugin"
    done <<< "$(config_keys "$d")"
    printf '  %-*s %s\n' "$maxw" "<any>" "UPPER_SNAKE key settable freely; a warning is shown if no installed plugin declares it"
}

usage() {
    cat <<EOF
Usage: pos entertainment config [set KEY=VALUE ...]

Show the entertainment config file, or set one or more keys.
After 'set', auto-trigger timers are re-synced from ENABLED.

Config file: $CONFIG_FILE

Keys (declared by the installed plugins' # POS_KEYS: headers):
$(keys_section "$(plugin_dir)")

Intervals: 5m 10m 15m 30m 45m hourly 2h 6h 12h daily weekly, or OnCalendar=...

Examples:
  pos entertainment config
  pos entertainment config set ENABLED="weather, 5m gold, 1h"
  pos entertainment config set WEATHER_LAT=36.51 WEATHER_LON=40.75
  pos entertainment config set ENABLED=""
EOF
    exit 0
}

key_known() {
    config_key_known "$(plugin_dir)" "$1"
}

case "${1:-}" in
    -h|--help) usage ;;
    set)
        shift
        [ $# -ge 1 ] || usage
        for pair in "$@"; do
            key="${pair%%=*}"
            val="${pair#*=}"
            [[ "$key" =~ ^[A-Z][A-Z0-9_]*$ ]] || \
                err "Invalid key '$key' (expected UPPER_SNAKE, e.g. WEATHER_LAT)"
            [ -z "$val" ] && [ "$key" != "ENABLED" ] && err "No value given (expected $key=...)"
            key_known "$key" || warn "Key '$key' not referenced by any installed plugin — saving anyway"
            write_config_key "$key" "$val"
            ok "$key saved to $CONFIG_FILE"
        done
        sync_timers
        ;;
    *)
        echo "Config: $CONFIG_FILE"
        if [ -f "$CONFIG_FILE" ]; then
            cat "$CONFIG_FILE"
        else
            echo "(not created yet — run 'pos entertainment enable <plugin> [interval]' or 'pos entertainment config set ...')"
        fi
        echo
        echo "ENABLED: $(config_value ENABLED)"
        echo "Intervals: 5m 10m 15m 30m 45m hourly 2h 6h 12h daily weekly, or OnCalendar=..."
        echo
        echo "Plugin keys (declared in each plugin's # POS_KEYS: header):"
        keys_section "$(plugin_dir)"
        ;;
esac
