#!/usr/bin/env bash
# NexusOS KDE Plasma theme installer — idempotent, safe to re-run.
# Equivalent of assets/themes/install-theme.sh but for KDE.
#
# Usage:
#   assets/themes/KDE/install-plasma.sh [--no-sddm]
#
# --no-sddm  skip the SDDM step (requires sudo) — useful for live testing
set -euo pipefail

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

NO_SDDM=0
for arg in "$@"; do
  [[ "$arg" == "--no-sddm" ]] && NO_SDDM=1
done

echo "NexusOS KDE installer — repo: $REPO"

# ── 1. Create target directories ────────────────────────────────────────
mkdir -p \
  ~/.local/share/aurorae/themes \
  ~/.local/share/plasma/desktoptheme \
  ~/.local/share/color-schemes \
  ~/.local/share/konsole \
  ~/.local/share/kscreenlocker/themes \
  ~/.config/Kvantum

# ── 2. Symlink theme directories (update automatically on git pull) ──────
ln -sfn "$KDE/aurorae/NexusOS"          ~/.local/share/aurorae/themes/NexusOS
ln -sfn "$KDE/plasma/NexusOS"           ~/.local/share/plasma/desktoptheme/NexusOS
ln -sfn "$KDE/kscreenlocker/NexusOS"    ~/.local/share/kscreenlocker/themes/NexusOS
ln -sfn "$KDE/kvantum/NexusOS"          ~/.config/Kvantum/NexusOS

echo "  [ok] theme symlinks"

# ── 3. Copy files that need to be in specific locations (not symlinks) ───
cp -f "$KDE/plasma/NexusOS/NexusOS.colors" ~/.local/share/color-schemes/
cp -f "$KDE/konsole/NexusOS.colorscheme"              ~/.local/share/konsole/
cp -f "$KDE/konsole/NexusOS-Promethean.colorscheme"   ~/.local/share/konsole/
cp -f "$KDE/konsole/Promethean.profile"               ~/.local/share/konsole/

echo "  [ok] color scheme + konsole files"

# ── 4. KDE color scheme ──────────────────────────────────────────────────
if command -v plasma-apply-colorscheme &>/dev/null; then
  plasma-apply-colorscheme NexusOS 2>/dev/null && echo "  [ok] color scheme applied"
else
  echo "  [skip] plasma-apply-colorscheme not found — apply color scheme manually"
fi

# ── 5. Kvantum ───────────────────────────────────────────────────────────
if command -v kvantummanager &>/dev/null; then
  kvantummanager --set NexusOS 2>/dev/null && echo "  [ok] Kvantum set to NexusOS"
else
  echo "  [skip] kvantummanager not found — install qt5-style-kvantum and run: kvantummanager --set NexusOS"
fi

# ── 6. KWin Aurorae decoration ───────────────────────────────────────────
if command -v kwriteconfig5 &>/dev/null; then
  kwriteconfig5 --file kwinrc \
    --group "org.kde.kdecoration2" --key library "org.kde.kwin.aurorae"
  kwriteconfig5 --file kwinrc \
    --group "org.kde.kdecoration2" --key theme "__aurorae__svg__NexusOS"
  # Buttons on the right: minimize, maximize, close (I=minimize, A=maximize, X=close)
  kwriteconfig5 --file kwinrc \
    --group "org.kde.kdecoration2" --key ButtonsOnLeft ""
  kwriteconfig5 --file kwinrc \
    --group "org.kde.kdecoration2" --key ButtonsOnRight "IAX"
  echo "  [ok] KWin decoration: Aurorae NexusOS, buttons right"
else
  echo "  [skip] kwriteconfig5 not found — configure KWin decoration manually"
fi

# ── 7. Plasma shell theme ────────────────────────────────────────────────
if command -v plasma-apply-desktoptheme &>/dev/null; then
  plasma-apply-desktoptheme NexusOS 2>/dev/null && echo "  [ok] Plasma shell theme: NexusOS"
else
  echo "  [skip] plasma-apply-desktoptheme not found — apply desktop theme manually"
fi

# ── 8. kscreenlocker ─────────────────────────────────────────────────────
if command -v kwriteconfig5 &>/dev/null; then
  kwriteconfig5 --file kscreenlockerrc --group Greeter --key Theme NexusOS
  echo "  [ok] kscreenlocker theme: NexusOS"
fi

# ── 9. GTK apps under Plasma ─────────────────────────────────────────────
if command -v kwriteconfig5 &>/dev/null; then
  kwriteconfig5 --file kdeglobals --group KDE    --key widgetStyle "kvantum-dark"
  kwriteconfig5 --file kdeglobals --group Icons  --key Theme NexusOS
  kwriteconfig5 --file kdeglobals --group General \
    --key font "Ubuntu,10,-1,5,50,0,0,0,0,0"
  echo "  [ok] kdeglobals: Kvantum style, NexusOS icons, Ubuntu 10"
fi
if command -v gsettings &>/dev/null; then
  gsettings set org.gnome.desktop.interface gtk-theme  NexusOS 2>/dev/null || true
  gsettings set org.gnome.desktop.interface icon-theme NexusOS 2>/dev/null || true
  echo "  [ok] gsettings: GTK theme + icons = NexusOS"
fi

# ── 10. SDDM login theme ─────────────────────────────────────────────────
if [[ $NO_SDDM -eq 0 ]]; then
  echo "  Deploying SDDM theme + KDE session entry (requires sudo)..."
  sudo cp -r "$KDE/sddm/NexusOS-QML" /usr/share/sddm/themes/
  sudo cp "$REPO/management/sessions/nexusos-kde.desktop" /usr/share/xsessions/
  if command -v kwriteconfig5 &>/dev/null; then
    sudo kwriteconfig5 --file /etc/sddm.conf --group Theme --key Current NexusOS-QML
  else
    # Fallback: write the INI directly
    sudo bash -c 'printf "[Theme]\nCurrent=NexusOS-QML\n" > /etc/sddm.conf.d/nexusos.conf'
  fi
  echo "  [ok] SDDM theme: NexusOS-QML"
  echo "  [ok] KDE session: NexusOS-KDE added to /usr/share/xsessions/"
else
  echo "  [skip] SDDM (--no-sddm passed)"
fi

# ── 11. Promethean Terminal desktop launcher ──────────────────────────────
DESKTOP_SRC="$REPO/bin/promethean/promethean-terminal.desktop"
DESKTOP_DEST="$HOME/.local/share/applications/promethean-terminal.desktop"
if [[ -f "$DESKTOP_SRC" ]]; then
  cp -f "$DESKTOP_SRC" "$DESKTOP_DEST"
  echo "  [ok] Promethean Terminal launcher updated"
fi

# ── 12. Apply live changes (if Plasma is running) ────────────────────────
if qdbus org.kde.KWin /KWin reconfigure 2>/dev/null; then
  echo "  [ok] KWin reconfigured"
  # Restart plasmashell to pick up new shell theme
  if command -v kquitapp5 &>/dev/null && command -v kstart5 &>/dev/null; then
    kquitapp5 plasmashell 2>/dev/null
    sleep 1
    kstart5 plasmashell &
    echo "  [ok] plasmashell restarted"
  fi
fi

echo ""
echo "NexusOS KDE theme installed. On first KDE session:"
echo "  1. System Settings → Appearance → verify all components show NexusOS"
echo "  2. Panel: right-click → Edit Panel to adjust height/position"
echo "  3. Konsole: Settings → Edit Profile → select Promethean (for Promethean Terminal)"
echo "  4. Test lock screen: Meta+L"
