# Tempo shell hook for Fish shell
# Add this to your Fish config: ~/.config/fish/config.fish

# Configuration
set -gx TEMPO_BIN (which tempo)
set -q TEMPO_DEBUG; or set -gx TEMPO_DEBUG 0
set -q TEMPO_HOOK_ENABLED; or set -gx TEMPO_HOOK_ENABLED 1

# Internal variables
set -g _TEMPO_LAST_DIR ""

# Debug logging function
function _tempo_debug
    if test "$TEMPO_DEBUG" = "1"
        echo "[TEMPO DEBUG] $argv" >&2
    end
end

# Function to detect if a directory is a project
function _tempo_is_project
    set dir $argv[1]
    
    # Check for git repository
    if test -d "$dir/.git"
        return 0
    end
    
    # Check for .tempo marker
    if test -f "$dir/.tempo"
        return 0
    end
    
    # Check for common project files
    for file in package.json Cargo.toml pyproject.toml pom.xml Makefile CMakeLists.txt go.mod composer.json
        if test -f "$dir/$file"
            return 0
        end
    end
    
    return 1
end

# Function to find the project root
function _tempo_find_project_root
    set dir $argv[1]
    set original_dir $dir
    
    # Walk up the directory tree
    while test "$dir" != "/"
        if _tempo_is_project $dir
            echo $dir
            return 0
        end
        set dir (dirname $dir)
    end
    
    # If no project found, return the original directory if it's reasonable
    if test -w "$original_dir"; and not string match -qr '^/(usr|bin|lib|etc|var|tmp|proc|sys|dev)' "$original_dir"
        echo $original_dir
        return 0
    end
    
    return 1
end

# Function to send IPC message to tempo daemon
function _tempo_send_ipc
    set socket_path "$HOME/.tempo/daemon.sock"
    
    # Simple check if socket exists
    if not test -S "$socket_path"
        _tempo_debug "Daemon socket not found at $socket_path"
        return 1
    end
    
    # Use the tempo CLI as IPC client
    if test -n "$TEMPO_BIN"; and test -x "$TEMPO_BIN"
        $TEMPO_BIN $argv 2>/dev/null
    else
        _tempo_debug "tempo binary not found"
        return 1
    end
end

# Function to handle directory change
function _tempo_on_directory_change
    if test "$TEMPO_HOOK_ENABLED" != "1"
        return
    end
    
    set current_dir $PWD
    
    # Skip if we're in the same directory
    if test "$current_dir" = "$_TEMPO_LAST_DIR"
        return
    end
    
    _tempo_debug "Directory changed from '$_TEMPO_LAST_DIR' to '$current_dir'"
    
    # Find project root
    if set project_root (_tempo_find_project_root $current_dir)
        _tempo_debug "Found project at: $project_root"
        
        # Send project entered signal via CLI
        _tempo_send_ipc session start --project "$project_root" --context terminal
        
        # Export for other tools
        set -gx TEMPO_CURRENT_PROJECT $project_root
    else
        _tempo_debug "No project found for: $current_dir"
        set -e TEMPO_CURRENT_PROJECT
    end
    
    set -g _TEMPO_LAST_DIR $current_dir
end

# Function to handle shell exit
function _tempo_on_shell_exit --on-event fish_exit
    if test "$TEMPO_HOOK_ENABLED" = "1"; and test -n "$TEMPO_CURRENT_PROJECT"
        _tempo_debug "Shell exiting, leaving project: $TEMPO_CURRENT_PROJECT"
        _tempo_send_ipc session stop
    end
end

# Hook into directory changes
function __tempo_pwd_handler --on-variable PWD
    _tempo_on_directory_change
end

# Initialize for current directory
_tempo_on_directory_change

# Utility functions for manual control
function tempo-enable
    set -gx TEMPO_HOOK_ENABLED 1
    echo "✅ Tempo automatic tracking enabled"
    _tempo_on_directory_change
end

function tempo-disable
    set -gx TEMPO_HOOK_ENABLED 0
    echo "⏸️ Tempo automatic tracking disabled"
end

function tempo-status
    if test "$TEMPO_HOOK_ENABLED" = "1"
        echo "✅ Tempo automatic tracking is enabled"
        if test -n "$TEMPO_CURRENT_PROJECT"
            echo "   📂 Current project: $TEMPO_CURRENT_PROJECT"
        else
            echo "   💤 No active project"
        end
    else
        echo "⏸️ Tempo automatic tracking is disabled"
    end
    
    # Show daemon status
    if test -n "$TEMPO_BIN"; and test -x "$TEMPO_BIN"
        echo ""
        $TEMPO_BIN status
    end
end

function tempo-debug
    if test "$TEMPO_DEBUG" = "1"
        set -gx TEMPO_DEBUG 0
        echo "🔇 Tempo debug logging disabled"
    else
        set -gx TEMPO_DEBUG 1
        echo "🔊 Tempo debug logging enabled"
    end
end

# Print installation message
if test "$TEMPO_DEBUG" = "1"
    echo "🔄 Tempo shell hook loaded for Fish (debug mode)"
end