# bash_handle_error
# -----------------
# Muestra un box de error formateado con contexto del fallo.
# Incluye script, linea, funcion y directorio.
#
# USO (sourced):
#   source bash_handle_error.sh
#   handle_error "BUILD_FAILED" "La compilacion fallo" "Verifica las dependencias"

SCRIPT_DIR_BASH_HANDLE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR_BASH_HANDLE/bash_colors.sh"
source "$SCRIPT_DIR_BASH_HANDLE/bash_log.sh"
bash_colors
bash_log_init

handle_error() {
    local error_code="$1"
    local error_description="$2"
    local solution="$3"

    echo ""
    echo -e "${RED}╔════════════════════════════════════════════════════════════╗${NC}"
    echo -e "${RED}║                    ${CROSS} ERROR DETECTADO                    ║${NC}"
    echo -e "${RED}╚════════════════════════════════════════════════════════════╝${NC}"
    echo ""
    echo -e "${RED}${BOLD}Error:${NC} ${error_description}"
    echo -e "${GRAY}Codigo: ${error_code}${NC}"
    echo ""

    if [ -n "$solution" ]; then
        echo -e "${YELLOW}${INFO} Solucion sugerida:${NC}"
        echo -e "   ${solution}"
        echo ""
    fi

    echo -e "${GRAY}${INFO} Informacion adicional:${NC}"
    echo -e "   ${GRAY}${BULLET} Script: ${BASH_SOURCE[2]:-desconocido}${NC}"
    echo -e "   ${GRAY}${BULLET} Linea: ${BASH_LINENO[1]:-desconocido}${NC}"
    echo -e "   ${GRAY}${BULLET} Funcion: ${FUNCNAME[2]:-main}${NC}"
    echo -e "   ${GRAY}${BULLET} Directorio: $(pwd)${NC}"
    echo -e "   ${GRAY}${BULLET} Usuario: $(whoami)${NC}"
    echo ""

    log "ERROR" "Code: $error_code | Description: $error_description | Script: ${BASH_SOURCE[2]} | Line: ${BASH_LINENO[1]}"

    return 1
}
