#!/usr/bin/env bash

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
shell_file="$repo_dir/config/dot/quickshell/shell.qml"
rules_file="$repo_dir/config/dot/hypr/rules.lua"

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

cleanup() {
    qs ipc call status-events reset >/dev/null 2>&1 || true
    qs ipc call focus end >/dev/null 2>&1 || true
}
trap cleanup EXIT

[[ -f "$repo_dir/config/dot/quickshell/modules/signals/SignalGlass.qml" ]] || fail 'shared host is missing'
[[ -f "$repo_dir/config/dot/quickshell/modules/signals/EventCard.qml" ]] || fail 'event presentation is missing'
[[ -f "$repo_dir/config/dot/quickshell/modules/signals/FocusCard.qml" ]] || fail 'focus presentation is missing'
[[ "$(rg -c 'SignalGlass \{\}' "$shell_file")" == "1" ]] || fail 'shell does not instantiate exactly one shared host delegate'
! rg -q 'FocusCapsule \{\}' "$shell_file" || fail 'legacy focus-only host is still instantiated'
rg -q 'namespace = "\^qs-signal-glass\$"' "$rules_file" || fail 'Hyprland layer rules do not match the shared namespace'

qs ipc call status-events reset >/dev/null
qs ipc call status-events fixture critical >/dev/null
for _ in $(seq 1 20); do
    if hyprctl layers | rg -q 'namespace: qs-signal-glass'; then
        printf 'signal-glass contract: PASS\n'
        exit 0
    fi
    sleep 0.1
done

fail 'event did not map the shared Signal Glass layer'
