#!/usr/bin/env bash

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
shelf="$repo_dir/config/dot/quickshell/modules/overview/ScratchpadShelf.qml"
body="$repo_dir/config/dot/quickshell/modules/overview/OverviewBody.qml"
keys="$repo_dir/config/dot/hypr/keybinds.lua"

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

[[ -f "$shelf" ]] || fail 'global scratchpad shelf is missing'
rg -q 'special:scratch' "$body" || fail 'overview does not discover the global scratch workspace'
rg -q 'ScratchpadShelf \{' "$body" || fail 'overview does not instantiate the shelf'
rg -q 'DropArea \{' "$shelf" || fail 'shelf is not a drag destination'
rg -q 'onRestoreRequested' "$body" || fail 'shelf cards cannot restore to the selected workspace'
rg -q 'SUPER.*X|mod .*\+ X' "$keys" || fail 'Super+X scratchpad toggle disappeared'
rg -q 'SHIFT \+ X' "$keys" || fail 'Super+Shift+X send-to-scratchpad binding disappeared'

qs ipc call overview open >/dev/null
for _ in $(seq 1 20); do
    if hyprctl layers | rg -q 'namespace: qs-overview'; then
        qs ipc call overview close >/dev/null
        printf 'scratchpad-shelf contract: PASS\n'
        exit 0
    fi
    sleep 0.1
done

fail 'overview did not map with the shelf present'
