#!/usr/bin/env bash

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

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

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

[[ -f "$repo_dir/config/dot/quickshell/modules/bar/ActivityIndicator.qml" ]] || fail 'bar indicator is missing'
[[ -f "$repo_dir/config/dot/quickshell/modules/bar/ActivityPanel.qml" ]] || fail 'activity detail panel is missing'
rg -q 'ActivityIndicator \{' "$repo_dir/config/dot/quickshell/modules/bar/Bar.qml" || fail 'bar does not instantiate the activity indicator'
! rg -q '^[[:space:]]*RecordingIndicator \{\}' "$repo_dir/config/dot/quickshell/shell.qml" || fail 'legacy recording popup is still instantiated'

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

fail 'active privacy state did not map its detail panel'
