#!/usr/bin/env bash

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
harness="$repo_dir/config/dot/quickshell/settings-system-harness.qml"

fail() {
    printf 'settings system contract: %s\n' "$1" >&2
    exit 1
}

qs_for_harness() {
    qs -p "$harness" "$@"
}

cleanup() {
    qs_for_harness kill >/dev/null 2>&1 || true
}
trap cleanup EXIT

original_auto_hdr="$(hyprctl -j getoption render:cm_auto_hdr | jq -r .int)"
original_vrr="$(hyprctl -j getoption misc:vrr | jq -r .int)"
original_direct="$(hyprctl -j getoption render:direct_scanout | jq -r .int)"

qs_for_harness --daemonize >/dev/null
for _ in $(seq 1 40); do
    if qs_for_harness ipc show 2>/dev/null | rg -q '^target settings-system-test$'; then
        break
    fi
    sleep 0.1
done
qs_for_harness ipc show 2>/dev/null | rg -q '^target settings-system-test$' || fail 'test IPC target did not start'

qs_for_harness ipc call settings-system-test refresh >/dev/null
status='{}'
for _ in $(seq 1 40); do
    status="$(qs_for_harness ipc call settings-system-test status | jq -c .)"
    if jq -e '.monitorName != "" and .width > 0 and .height > 0 and .busy == false' <<<"$status" >/dev/null; then
        break
    fi
    sleep 0.1
done
# Against the live compositor, not a named machine: this once asserted DP-2 at
# 4500x3000 in XRGB2101010, which pinned the desktop it was written on and
# could never pass on a laptop's eDP-1. The property is that SystemSettings
# mirrors whatever monitor is actually primary, normalized -- so ask Hyprland
# what that is.
live_monitor="$(hyprctl -j monitors | jq -c '.[0]')"
jq -e --argjson live "$live_monitor" '
    .monitorName == $live.name
    and .width == $live.width
    and .height == $live.height
    and ((.scale - $live.scale) | fabs) < 0.001
    and (.format | length) > 0' <<<"$status" >/dev/null \
    || fail "live monitor data was not normalized: $status (compositor: $live_monitor)"

[[ "$(qs_for_harness ipc call settings-system-test panelAllowed '__definitely_not_a_panel__' | jq -r .)" == "false" ]] \
    || fail 'unsupported GNOME panel was accepted'
[[ "$(qs_for_harness ipc call settings-system-test panelAllowed sound | jq -r .)" == "true" ]] \
    || fail 'supported GNOME sound panel was rejected'

# Apply the exact current values: this exercises the real allow-listed command
# without changing the daily-driver compositor state.
qs_for_harness ipc call settings-system-test apply "$([[ "$original_auto_hdr" == 1 ]] && printf true || printf false)" "$original_vrr" "$original_direct" >/dev/null
sleep 0.3

[[ "$(hyprctl -j getoption render:cm_auto_hdr | jq -r .int)" == "$original_auto_hdr" ]] || fail 'Auto HDR changed unexpectedly'
[[ "$(hyprctl -j getoption misc:vrr | jq -r .int)" == "$original_vrr" ]] || fail 'VRR changed unexpectedly'
[[ "$(hyprctl -j getoption render:direct_scanout | jq -r .int)" == "$original_direct" ]] || fail 'direct scanout changed unexpectedly'

trap - EXIT
cleanup
printf 'settings system contract: PASS\n'
