#!/usr/bin/env bash

set -euo pipefail

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

project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
helper="$project_root/config/dot/quickshell/scripts/panama-kdeconnect"

[[ -x "$helper" ]] || fail 'helper is missing or not executable'

status="$($helper status)"

# The shape is checked whenever a device is present, but a device being present
# is not itself required. kdeconnectd drops its device objects for a phone it
# has not seen recently -- the pairing survives in its config, the D-Bus object
# does not -- so demanding one makes this fail whenever the phone is off, out of
# range, or simply has not opened the app today. That is the machine's state,
# not a defect in the helper.
jq -e '.available == true and (.devices | type == "array")' <<<"$status" >/dev/null \
    || fail 'live status is not a readable device list'

if [[ "$(jq -r '.devices | length' <<<"$status")" == "0" ]]; then
    printf 'KDE Connect helper contract: PASS (no device visible to the daemon right now; shape checks skipped)\n'
    exit 0
fi

# battery and signal are always present and are null far more often than not:
# the plugin objects exist only while a device is paired, reachable and has the
# plugin loaded. Null is the answer, so the check is on the shape when a value
# is there, never on a value being there.
jq -e '
    ([.devices[] | (keys | sort) == (["actions", "battery", "id", "name", "paired", "reachable", "signal", "type"] | sort)] | all) and
    ([.devices[] | (.id | test("^[A-Fa-f0-9]{32,64}$"))] | all) and
    ([.devices[].actions[] | . == "clipboard" or . == "ping" or . == "ring" or . == "share"] | all) and
    ([.devices[] | select(.battery != null) | (.battery | keys | sort) == ["charge", "charging"]] | all) and
    ([.devices[] | select(.battery != null) | .battery.charge >= 0 and .battery.charge <= 100 and (.battery.charging | type == "boolean")] | all) and
    ([.devices[] | select(.signal != null) | (.signal | keys | sort) == ["networkType", "strength"]] | all) and
    ([.devices[] | select(.signal != null) | .signal.strength >= 0 and (.signal.networkType | type == "string")] | all) and
    ([.devices[] | select(.paired and .reachable | not) | .battery == null and .signal == null] | all)
' <<<"$status" >/dev/null || fail 'live status shape is invalid'

paired_count="$(jq '[.devices[] | select(.paired)] | length' <<<"$status")"
reachable_count="$(jq '[.devices[] | select(.reachable)] | length' <<<"$status")"
printf 'KDE Connect helper contract: PASS (%s paired, %s reachable; identities redacted)\n' \
    "$paired_count" \
    "$reachable_count"
