#!/usr/bin/env bash

# day_deactivate: Deactivate the Daylily environment
# This script must be sourced, not executed directly.

# Ensure the script is sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    echo "Error: This script must be sourced, not executed directly."
    echo "Usage: source $0 [reset|--help]"
    return 1
fi

# Function to display usage information
usage() {
    echo "Usage: day-deactivate [activate|reset|--help]"
    echo ""
    echo "This script deactivates the Daylily environment by:"
    echo "  - Clearing the DAY_ROOT, DAY_PROFILE, and DAY_BIOME and other dyoainit set environment variables."
    echo "  - Attempting to deactivate any active DAYOA conda environments."
    echo "  - Restoring the original PATH and PS1 if they were modified."
    echo "  - Unaliasing Daylily CLI commands."
    echo ""
    echo "Options:"
    echo "  activate    Special case to partially deactivate the environment for day-activate."
    echo "  reset       Reset the command line prompt to default if it was not properly reset."
    echo "  --help      Display this help message."
    return 0
}

# Handle input arguments
case "$1" in
    reset|RESET)
        # Reset the command line prompt to default
        export PS1="[\h \A \W]\$ "
        reset
        return 0
        ;;
    -h|--help|help)
        usage
        return 0
        ;;
    activate)
        # Special case when called from day_activate
        # Do not unset DAY_ROOT, DAY_PROFILE, or DAY_BIOME
        # Proceed with minimal deactivation steps
        ;;
    ""|*)
        # Proceed with deactivation
        ;;
esac

# Restore original PATH if it was saved
if [[ -n "$ORIG_PATH" ]]; then
    export PATH="$ORIG_PATH"
    unset ORIG_PATH
fi

# Remove DAY_ROOT/bin from PATH if DAY_ROOT is set
if [[ -n "$DAY_ROOT" ]]; then
    export PATH="${PATH//:$DAY_ROOT\/bin/}"
    export PATH="${PATH//$DAY_ROOT\/bin:/}"
    export PATH="${PATH//$DAY_ROOT\/bin/}"
fi

# Unalias Daylily CLI commands
unalias day-deactivate 2>/dev/null
unalias day-build 2>/dev/null
unalias day-activate 2>/dev/null
unalias day-run 2>/dev/null
unalias dy-a 2>/dev/null
unalias dy-b 2>/dev/null
unalias dy-d 2>/dev/null
unalias dy-r 2>/dev/null

# Restore original PS1 if it was saved
if [[ -n "$ORIG_PS1" ]]; then
    export PS1="$ORIG_PS1"
    unset ORIG_PS1
else
    export PS1="[\h \A \W]\$ "
fi

# Reset the terminal to clear any residual settings
reset

# Unset environment variables unless called with 'activate'
if [[ "$1" != "activate" ]]; then
    unset DAY_ROOT
    unset DAY_PROFILE
    unset DAY_BIOME
    unset DAY_CONTACT_EMAIL
    unset DAY_PROFILE_DIR
    unset DAY_GENOME_BUILD
    unset DAY_REMOTE_EXE

fi

# Deactivate conda environment if conda is available
if command -v conda &> /dev/null; then
    conda deactivate || {
        echo "Warning: Failed to deactivate conda environment."
        echo "You may need to manually deactivate or clear environment variables."
    }
else
    echo "Conda is not available; skipping conda deactivation."
fi

# Final message
if [[ "$1" != "activate" ]]; then
    echo "Successfully cleared DAY_ROOT, DAY_BIOME, and DAY_PROFILE."
fi

# Suggest reset if needed
if [[ "$1" != "activate" && "$1" != "reset" ]]; then
    echo ""
    echo "If the deactivate command did not fully reset your shell prompt, try:"
    echo "  source dyoainit; day-deactivate reset"
fi

return 0
