#!/usr/bin/env bash

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
gallery="$repo_dir/config/dot/quickshell/prism-gallery.qml"
launcher="$repo_dir/config/dot/quickshell/scripts/panama-prism-gallery"

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

[[ -f "$gallery" ]] || fail 'the real-component gallery is missing'
[[ -x "$launcher" ]] || fail 'the gallery launcher is missing or not executable'

rg -Fq 'import qs.widgets' "$gallery" || fail 'gallery does not use the shared widget module'
for component in Pill Toggle ValueSlider ThemedIcon PrismEdge; do
    rg -q "^[[:space:]]*${component}[[:space:]]*\\{" "$gallery" \
        || fail "gallery does not render the real ${component} component"
done

rg -Fq 'title: "Panama Prism Gallery"' "$gallery" \
    || fail 'gallery does not expose a stable compositor window title'
rg -Fq 'FloatingWindow {' "$gallery" \
    || fail 'gallery must be a normal tiled application window'

if rg -q '#[0-9a-fA-F]{3,8}' "$gallery"; then
    fail 'gallery hardcodes a color instead of using the Theme vocabulary'
fi
if rg -q 'loops:[[:space:]]*(Animation\.Infinite|-1)|running:[[:space:]]*true' "$gallery"; then
    fail 'gallery contains an always-running animation'
fi

rg -Fq 'qs -p "$gallery" --daemonize' "$launcher" \
    || fail 'launcher does not start the isolated Quickshell gallery config'
rg -Fq 'qs -p "$gallery" kill' "$launcher" \
    || fail 'launcher cannot close an already-open gallery instance'

printf 'Prism gallery contract: PASS\n'
