#!/usr/bin/env bash

# strict mode
set -euo pipefail
IFS=$'\n\t'

# Script constants
readonly DOTFILES_ROOT="$HOME/.dotfiles"
readonly DOTFILES_BIN="$DOTFILES_ROOT/bin"

# Source helper functions
if [[ ! -f "$DOTFILES_BIN/helpers/functions.sh" ]]; then
    echo "Error: Required helper functions not found"
    exit 1
fi
source "$DOTFILES_BIN/helpers/functions.sh"

# Command functions
update() {
    local update_script="$DOTFILES_BIN/actions/update.sh"
    if [[ ! -x "$update_script" ]]; then
        printfe "%s\n" "red" "Error: Update script not found or not executable"
        return 1
    fi
    "$update_script" $@
}

hello() {
    local term_script="$DOTFILES_BIN/actions/hello.sh"
    if [[ ! -x "$term_script" ]]; then
        printfe "%s\n" "red" "Error: Terminal script not found or not executable"
        return 1
    fi
    "$term_script" "$@"
}

help() {
    local help_script="$DOTFILES_BIN/actions/help.sh"
    if [[ ! -x "$help_script" ]]; then
        printfe "%s\n" "red" "Error: Help script not found or not executable"
        return 1
    fi
    "$help_script" "$@"
}

secrets() {
    local secrets_script="$DOTFILES_BIN/actions/secrets.sh"
    if [[ ! -x "$secrets_script" ]]; then
        printfe "%s\n" "red" "Error: Secrets script not found or not executable"
        return 1
    fi
    "$secrets_script" "$@"
}

auto_start() {
    local auto_start_script="$DOTFILES_BIN/actions/auto-start.sh"
    if [[ ! -x "$auto_start_script" ]]; then
        printfe "%s\n" "red" "Error: Auto-start script not found or not executable"
        return 1
    fi
    "$auto_start_script" "$@"
}

ensure_git_hooks() {
    local hooks_dir="$DOTFILES_ROOT/.git/hooks"
    local target_link="$DOTFILES_BIN/actions/git"

    # Validate target directory exists
    if [[ ! -d "$target_link" ]]; then
        printfe "%s\n" "red" "Error: Git hooks source directory does not exist: $target_link"
        return 1
    fi

    # Handle existing symlink
    if [[ -L "$hooks_dir" ]]; then
        local current_link
        current_link=$(readlink "$hooks_dir")
        if [[ "$current_link" != "$target_link" ]]; then
            printfe "%s\n" "yellow" "Incorrect git hooks symlink found. Removing and recreating..."
            rm "$hooks_dir"
        else
            return 0
        fi
    fi

    # Handle existing directory
    if [[ -d "$hooks_dir" ]]; then
        printfe "%s\n" "yellow" "Removing existing hooks directory..."
        rm -rf "$hooks_dir"
    fi

    # Create new symlink
    if ln -s "$target_link" "$hooks_dir"; then
        printfe "%s\n" "green" "Git hooks successfully configured!"
    else
        printfe "%s\n" "red" "Failed to create git hooks symlink"
        return 1
    fi
}

main() {
    # Ensure we're in the correct directory
    if [[ ! -d "$DOTFILES_ROOT" ]]; then
        printfe "%s\n" "red" "Error: Dotfiles directory not found"
        exit 1
    fi

    # Setup git hooks
    ensure_git_hooks || exit 1

    # Parse commands
    case "${1:-help}" in
        update)  shift; update "$@" ;;
        help)    shift; help "$@" ;;
        hello)    shift; hello "$@" ;;
        secrets) shift; secrets "$@" ;;
        auto-start) shift; auto_start "$@" ;;
        *)       help ;;
    esac
}

main "$@"
