public ResponseModel saveChatMessages([FromBody]  CustomerChatModel ChatMessageDetails)
        {
            ResponseModel objResponseModel = new ResponseModel();
            int           result           = 0;
            int           statusCode       = 0;
            string        statusMessage    = "";

            try
            {
                string       token        = Convert.ToString(Request.Headers["X-Authorized-Token"]);
                Authenticate authenticate = new Authenticate();
                authenticate = SecurityService.GetAuthenticateDataFromToken(_radisCacheServerAddress, SecurityService.DecryptStringAES(token));


                CustomerChatCaller customerChatCaller = new CustomerChatCaller();
                ChatMessageDetails.CreatedBy = !ChatMessageDetails.ByCustomer ? authenticate.UserMasterID : 0;
                result = customerChatCaller.SaveChatMessages(new CustomerChatService(_connectionString), ChatMessageDetails);

                statusCode    = result > 0 ? (int)EnumMaster.StatusCode.Success : (int)EnumMaster.StatusCode.RecordNotFound;
                statusMessage = CommonFunction.GetEnumDescription((EnumMaster.StatusCode)statusCode);


                objResponseModel.Status       = true;
                objResponseModel.StatusCode   = statusCode;
                objResponseModel.Message      = statusMessage;
                objResponseModel.ResponseData = result;
            }
            catch (Exception)
            {
                throw;
            }
            return(objResponseModel);
        }
        /// <summary>
        /// Save Chat messages
        /// </summary>
        /// <param name="CustomerChatModel"></param>
        /// <returns></returns>
        public int SaveChatMessages(CustomerChatModel ChatMessageDetails)
        {
            MySqlCommand cmd         = new MySqlCommand();
            int          resultCount = 0;

            try
            {
                if (conn != null && conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                cmd            = new MySqlCommand("SP_HSInsertChatDetails", conn);
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@_ChatID", ChatMessageDetails.ChatID);
                cmd.Parameters.AddWithValue("@_Message", string.IsNullOrEmpty(ChatMessageDetails.Message) ? "" : ChatMessageDetails.Message);
                cmd.Parameters.AddWithValue("@_ByCustomer", ChatMessageDetails.ByCustomer ? 1 : 2);
                cmd.Parameters.AddWithValue("@_Status", !ChatMessageDetails.ByCustomer ? 0 : 1);
                cmd.Parameters.AddWithValue("@_StoreManagerId", ChatMessageDetails.StoreManagerId);
                cmd.Parameters.AddWithValue("@_CreatedBy", ChatMessageDetails.CreatedBy);



                cmd.CommandType = CommandType.StoredProcedure;

                resultCount = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return(resultCount);
        }
        /// <summary>
        /// send Message To Customer
        /// </summary>
        /// <param name="ChatID"></param>
        /// <param name="MobileNo"></param>
        /// <param name="ProgramCode"></param>
        /// <param name="Messsage"></param>
        /// <param name="ClientAPIURL"></param>
        /// <param name="CreatedBy"></param>
        /// <returns></returns>
        public int SendMessageToCustomer(int ChatID, string MobileNo, string ProgramCode, string Message, string WhatsAppMessage, string ImageURL, string ClientAPIURL, int CreatedBy, int InsertChat)
        {
            MySqlCommand               cmd                    = new MySqlCommand();
            int                        resultCount            = 0;
            CustomerChatModel          ChatMessageDetails     = new CustomerChatModel();
            ClientCustomSendTextModel  SendTextRequest        = new ClientCustomSendTextModel();
            ClientCustomSendImageModel SendImageRequest       = new ClientCustomSendImageModel();
            string                     ClientAPIResponse      = string.Empty;
            string                     ClientImageAPIResponse = string.Empty;

            //bool isMessageSent = false;

            try
            {
                #region call client api for sending message to customer
                if (string.IsNullOrEmpty(ImageURL))
                {
                    SendTextRequest.To          = MobileNo;
                    SendTextRequest.textToReply = Message;
                    SendTextRequest.programCode = ProgramCode;

                    string JsonRequest = JsonConvert.SerializeObject(SendTextRequest);

                    ClientAPIResponse = CommonService.SendApiRequest(ClientAPIURL + "api/ChatbotBell/SendText", JsonRequest);
                }

                if (!string.IsNullOrEmpty(ImageURL))
                {
                    SendImageRequest.To          = MobileNo;
                    SendImageRequest.textToReply = WhatsAppMessage;
                    SendImageRequest.programCode = ProgramCode;
                    SendImageRequest.imageUrl    = ImageURL;

                    string JsonRequests = JsonConvert.SerializeObject(SendImageRequest);

                    ClientImageAPIResponse = CommonService.SendImageApiRequest(ClientAPIURL + "api/ChatbotBell/SendImage", JsonRequests);
                }

                //if (!string.IsNullOrEmpty(ClientAPIResponse))
                //{
                //    isMessageSent = Convert.ToBoolean(ClientAPIResponse);

                //    if (isMessageSent && ChatID > 0 && InsertChat.Equals(1))
                //    {
                //        ChatMessageDetails.ChatID = ChatID;
                //        ChatMessageDetails.Message = Message;
                //        ChatMessageDetails.ByCustomer = false;
                //        ChatMessageDetails.ChatStatus = 1;
                //        ChatMessageDetails.StoreManagerId = CreatedBy;
                //        ChatMessageDetails.CreatedBy = CreatedBy;

                //        resultCount = SaveChatMessages(ChatMessageDetails);

                //    }
                //}

                #endregion
            }
            catch (Exception)
            {
                throw;
            }

            return(resultCount);
        }
        /// <summary>
        /// send Recommendations To Customer
        /// </summary>
        /// <param name="customerID"></param>
        /// <param name="mobileNo"></param>
        /// <returns></returns>
        public int SendRecommendationsToCustomer(int TenantID, string Programcode, int CustomerID, string MobileNo, string ClientAPIURL, int CreatedBy)
        {
            MySqlCommand cmd = new MySqlCommand();
            int          resultCount = 0; int Chat_ID = 0;
            string       ProgramCode = string.Empty;
            List <CustomerRecommendatonModel> RecommendationsList = new List <CustomerRecommendatonModel>();
            DataSet ds = new DataSet();
            string  HtmlMessageContent = "<div class=\"card-body position-relative\"><div class=\"row\" style=\"margin: 0px; align-items: flex-end;\"><div class=\"col-md-2\"><img class=\"chat-product-img\" src=\"{0}\" alt=\"Product Image\" ></div><div class=\"col-md-10 bkcprdt\"><div><label class=\"chat-product-name\">Brand :{1}</label></div><div><label class=\"chat-product-code\">Category: {2}</label></div><div><label class=\"chat-product-code\">SubCategory: {3}</label></div><div><label class=\"chat-product-code\">Color: {4}</label></div><div><label class=\"chat-product-code\">Size: {5}</label></div><div><label class=\"chat-product-code\">Item Code: {6}</label></div><div><label class=\"chat-product-prize\"> Price : {7}</label></div><div><a href=\"{8}\" target=\"_blank\" class=\"chat-product-url\">{9}</a></div></div></div></div>";

            try
            {
                if (conn != null && conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }

                cmd            = new MySqlCommand("SP_HSGetRecomendationsByCustomerID", conn);
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@_TenantID", TenantID);
                cmd.Parameters.AddWithValue("@_prgCode", Programcode);
                cmd.Parameters.AddWithValue("@_CustomerID", CustomerID);
                cmd.Parameters.AddWithValue("@_MobileNo", MobileNo);

                cmd.CommandType = CommandType.StoredProcedure;

                MySqlDataAdapter da = new MySqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);

                if (ds != null && ds.Tables != null)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            CustomerRecommendatonModel obj = new CustomerRecommendatonModel()
                            {
                                Id          = Convert.ToInt32(dr["Id"]),
                                ItemCode    = dr["ItemCode"] == DBNull.Value ? string.Empty : Convert.ToString(dr["ItemCode"]),
                                Category    = dr["Category"] == DBNull.Value ? string.Empty : Convert.ToString(dr["Category"]),
                                SubCategory = dr["SubCategory"] == DBNull.Value ? string.Empty : Convert.ToString(dr["SubCategory"]),
                                Brand       = dr["Brand"] == DBNull.Value ? string.Empty : Convert.ToString(dr["Brand"]),
                                Color       = dr["Color"] == DBNull.Value ? string.Empty : Convert.ToString(dr["Color"]),
                                Size        = dr["Size"] == DBNull.Value ? string.Empty : Convert.ToString(dr["Size"]),
                                Price       = dr["Price"] == DBNull.Value ? "0" : Convert.ToString(dr["Price"]),
                                Url         = dr["Url"] == DBNull.Value ? string.Empty : Convert.ToString(dr["Url"]),
                                ImageURL    = dr["ImageURL"] == DBNull.Value ? string.Empty : Convert.ToString(dr["ImageURL"]),
                            };

                            RecommendationsList.Add(obj);
                        }
                    }

                    if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                    {
                        Chat_ID     = ds.Tables[1].Rows[0]["Chat_ID"] == System.DBNull.Value ? 0 : Convert.ToInt32(ds.Tables[1].Rows[0]["Chat_ID"]);
                        ProgramCode = ds.Tables[1].Rows[0]["prgCode"] == System.DBNull.Value ? string.Empty : Convert.ToString(ds.Tables[1].Rows[0]["prgCode"]);
                    }
                }

                if (RecommendationsList.Count > 0 && Chat_ID > 0 && !string.IsNullOrEmpty(ProgramCode))
                {
                    #region call client send text api for sending message to customer

                    foreach (CustomerRecommendatonModel RecObj in RecommendationsList)
                    {
                        string whatsAppContent = "Brand: " + RecObj.Brand + ", Category: " + RecObj.Category + ", Sub Category: " + RecObj.SubCategory + ", Color: " + RecObj.Color + ", Size: " +
                                                 RecObj.Size + ", Item Code: " + RecObj.ItemCode + ", Price: " + RecObj.Price + "  " + RecObj.Url;
                        resultCount = resultCount + SendMessageToCustomer(Chat_ID, MobileNo.Length > 10?MobileNo:"91" + MobileNo, ProgramCode, RecObj.Url, whatsAppContent, RecObj.ImageURL, ClientAPIURL, CreatedBy, 0);
                    }

                    #endregion



                    foreach (CustomerRecommendatonModel RecObj in RecommendationsList)
                    {
                        string messagecontent = string.Format(HtmlMessageContent, RecObj.ImageURL, RecObj.Brand, RecObj.Category, RecObj.SubCategory, RecObj.Color
                                                              , RecObj.Size, RecObj.ItemCode, RecObj.Price, RecObj.Url, RecObj.Url);

                        CustomerChatModel ChatMessageDetails = new CustomerChatModel();
                        ChatMessageDetails.ChatID         = Chat_ID;
                        ChatMessageDetails.Message        = messagecontent;
                        ChatMessageDetails.ByCustomer     = false;
                        ChatMessageDetails.ChatStatus     = 0;
                        ChatMessageDetails.StoreManagerId = CreatedBy;
                        ChatMessageDetails.CreatedBy      = CreatedBy;

                        resultCount = resultCount + SaveChatMessages(ChatMessageDetails);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                conn.Close();
            }
            return(resultCount);
        }
 public int SaveChatMessages(ICustomerChat customerChat, CustomerChatModel ChatMessageDetails)
 {
     _customerChat = customerChat;
     return(_customerChat.SaveChatMessages(ChatMessageDetails));
 }