#!/usr/bin/env bash
set -u

VERSION="0.8.17"
WALLPAPER="file:///usr/share/backgrounds/brz-healthos/brz-healthos-aug-2026.png"
MARKER_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/brz-healthos"
MARKER="$MARKER_DIR/desktop-${VERSION}-bottom-dock-applied"

# Apply release defaults once. Later choices made in GNOME Appearance belong to
# the user and must survive logout/restart.
if [ -f "$MARKER" ]; then
  exit 0
fi

has_schema() {
  gsettings list-schemas 2>/dev/null | grep -Fxq "$1"
}

has_key() {
  local schema="$1" key="$2"
  has_schema "$schema" && gsettings list-keys "$schema" 2>/dev/null | grep -Fxq "$key"
}

set_key() {
  local schema="$1" key="$2" value="$3"
  if has_key "$schema" "$key"; then
    gsettings set "$schema" "$key" "$value" >/dev/null 2>&1 || true
  fi
}

reset_key() {
  local schema="$1" key="$2"
  if has_key "$schema" "$key"; then
    gsettings reset "$schema" "$key" >/dev/null 2>&1 || true
  fi
}

mkdir -p "$MARKER_DIR"

# Apply the packaged BRZ theme. Ubuntu/Yaru remains installed and can be
# restored with brz-healthos-theme default or the shell recovery command.
set_key org.gnome.desktop.interface gtk-theme "'BRZ-HealthOS-Light'"
set_key org.gnome.desktop.interface icon-theme "'BRZ-HealthOS'"
reset_key org.gnome.desktop.interface cursor-theme
set_key org.gnome.desktop.interface color-scheme "'prefer-light'"
set_key org.gnome.desktop.background picture-uri "'$WALLPAPER'"
set_key org.gnome.desktop.background picture-uri-dark "'$WALLPAPER'"
set_key org.gnome.desktop.background picture-options "'zoom'"
set_key org.gnome.desktop.screensaver picture-uri "'$WALLPAPER'"
set_key org.gnome.desktop.interface enable-animations true
set_key org.gnome.desktop.interface clock-format "'24h'"
set_key org.gnome.desktop.interface clock-show-date true
set_key org.gnome.desktop.interface clock-show-weekday true
set_key org.gnome.desktop.interface enable-hot-corners true

# Keep conservative Ubuntu window-management behavior.
reset_key org.gnome.desktop.wm.preferences button-layout
reset_key org.gnome.desktop.wm.preferences center-new-windows
reset_key org.gnome.mutter dynamic-workspaces

# BRZ shell extension only adds BRZ identity and replaces the Show Applications
# logo. It deliberately does NOT load a custom GNOME Shell stylesheet.
set_key org.gnome.shell disable-user-extensions false
if command -v gnome-extensions >/dev/null 2>&1; then
  gnome-extensions enable brz-healthos-shell@brztermin.com >/dev/null 2>&1 || true
  gnome-extensions enable ubuntu-dock@ubuntu.com >/dev/null 2>&1 || true
fi

# Start from clean Ubuntu Dock values, then apply the BRZ bottom-dock layout.
for key in dock-position extend-height always-center-icons dock-fixed autohide \
  intellihide intellihide-mode autohide-in-fullscreen require-pressure-to-show \
  show-delay hide-delay animation-time dash-max-icon-size click-action show-trash \
  show-mounts show-show-apps-button show-apps-at-top show-apps-always-in-the-edge \
  custom-theme-shrink transparency-mode background-opacity custom-background-color \
  background-color custom-theme-running-dots-color \
  custom-theme-running-dots-border-color custom-theme-running-dots-border-width \
  apply-custom-theme; do
  reset_key org.gnome.shell.extensions.dash-to-dock "$key"
done

# Keep the application toolbar along the bottom edge. Intelligent auto-hide
# gives maximized applications (especially the browser) the entire display,
# while the dock remains available by moving the pointer to the bottom edge.
set_key org.gnome.shell.extensions.dash-to-dock dock-position "'BOTTOM'"
set_key org.gnome.shell.extensions.dash-to-dock extend-height false
set_key org.gnome.shell.extensions.dash-to-dock always-center-icons true
set_key org.gnome.shell.extensions.dash-to-dock dock-fixed false
set_key org.gnome.shell.extensions.dash-to-dock autohide true
set_key org.gnome.shell.extensions.dash-to-dock intellihide true
set_key org.gnome.shell.extensions.dash-to-dock autohide-in-fullscreen true
set_key org.gnome.shell.extensions.dash-to-dock show-show-apps-button true
set_key org.gnome.shell.extensions.dash-to-dock show-apps-at-top true

# Quiet desktop for a managed healthcare workstation.
set_key org.gnome.shell.extensions.ding show-home false
set_key org.gnome.shell.extensions.ding show-trash false
set_key org.gnome.shell.extensions.ding start-corner "'top-right'"

printf '%s\n' "$VERSION" > "$MARKER"
printf '%s\n' "BRZ HealthOS Stable Branding $VERSION applied."
printf '%s\n' "Original Ubuntu/Yaru themes restored; BRZ identity/logo layer enabled."
printf '%s\n' "Log out and back in to reload GNOME Shell."

# Ensure the primary pharmacy platform is immediately available on the desktop
# for the current user. Administrators can add more HTTPS shortcuts with
# "Add Web Shortcut" or the brz-web-shortcut command.
if command -v brz-web-shortcut >/dev/null 2>&1; then
  brz-web-shortcut add "PlatformaOTC" "https://app.platformaotc.com" "brz-healthos" >/dev/null 2>&1 || true
fi
