#!/usr/bin/env bash
# Hyprland Workspace State Manager -- Work
# Path: ~/.config/local-ai/tools/subsec/hyprstate/work

set -euo pipefail

manage_app() {
    local target_ws="$1"
    local exact_class="$2"
    local launch_cmd="$3"

    # Check if a window with this class is already open in Hyprland
    if hyprctl clients -j 2>/dev/null | jq -e ".[] | select(.class == \"${exact_class}\")" >/dev/null 2>&1; then
        # Already open: Just move the existing window to the designated workspace
        hyprctl dispatch movetoworkspacesilent "${target_ws},class:^(${exact_class})$" >/dev/null 2>&1 || true
    else
        # Not open: Launch silently in background
        nohup ${launch_cmd} >/dev/null 2>&1 &
    fi
}

# --- 1. Brave Origin -> Workspace 1 ---
manage_app 1 "brave-origin" "brave-origin"

# --- 2. Nautilus Files -> Workspace 2 ---
manage_app 2 "org.gnome.Nautilus" "nautilus"

# --- 3. Gnome Text Editor -> Workspace 2 ---
manage_app 2 "org.gnome.TextEditor" "gnome-text-editor"

echo "Workspace reset complete: State --Work"
