#!/usr/bin/env bash

set -euo pipefail

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

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

qs ipc show | rg -q '^target calendar-agenda$' \
    || fail 'calendar IPC target is missing'

qs ipc call calendar-agenda fixture daybook >/dev/null
status="$(qs ipc call calendar-agenda status)"
jq -e '
    .fixture == true and
    .sourceCount == 3 and
    .eventCount == 5 and
    .selectedDate == "2026-08-17"
' <<<"$status" >/dev/null || fail 'daybook fixture state is malformed'

qs ipc call calendar-agenda select 2026-08-18 >/dev/null
jq -e '.selectedDate == "2026-08-18" and .selectedEventCount == 1' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'date selection did not update events'

qs ipc call calendar-agenda open >/dev/null
jq -e '.open == true and .page == "agenda"' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'calendar open did not route to Agenda'

qs ipc call notifications open >/dev/null
jq -e '.open == true and .page == "notifications"' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'notification open did not route to Notifications'

qs ipc call activity fixture microphone-on >/dev/null
qs ipc call calendar-agenda page ongoing >/dev/null
jq -e '.open == true and .page == "ongoing" and .ongoingCount >= 1' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'persistent activity did not route to Ongoing'

qs ipc call activity fixture all-on >/dev/null
qs ipc call focus start >/dev/null
jq -e '.ongoingCount == 5 and .ongoingFits == true' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'crowded Ongoing activity does not fit the Daybook'

hyprctl layers | rg -q 'namespace: qs-popover-datemenu' \
    || fail 'Daybook layer did not map'
rg -q 'implicitWidth: 760' config/dot/quickshell/modules/datemenu/DateMenu.qml \
    || fail 'Daybook is not the approved width'
for component in AgendaPane AgendaEvent NotificationSummary; do
    rg -q "${component} 1.0 ${component}.qml" config/dot/quickshell/modules/datemenu/qmldir \
        || fail "$component is not registered"
done
rg -q 'gnome-calendar.*--date.*toISOString' config/dot/quickshell/services/CalendarAgenda.qml \
    || fail 'GNOME Calendar date handoff is not a complete ISO-8601 timestamp'

qs ipc call calendar-agenda fixture upcoming >/dev/null
jq -e '.capsuleVisible == true and .capsuleText == "12m"' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'upcoming capsule window is wrong'

qs ipc call calendar-agenda fixture outside-window >/dev/null
jq -e '.capsuleVisible == false' \
    <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'capsule appears outside the quiet window'

qs ipc call calendar-agenda reset >/dev/null
jq -e '.fixture == false' <<<"$(qs ipc call calendar-agenda status)" >/dev/null \
    || fail 'fixture reset did not restore live mode'

trap - EXIT
cleanup
printf 'calendar agenda contract: PASS\n'
