public async Task <IActionResult> DeleteComment([FromQuery] long id)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var comment = _commentRepository.FindById(id);
                if (comment.Replies.Count > 0)
                {
                    foreach (var reply in comment.Replies.ToList())
                    {
                        _commentRepository.Delete(reply.Id);
                    }
                }

                var deletedComment = _commentRepository.Delete(id);

                if (deletedComment != null)
                {
                    await _hubContext.Clients.All.SendAsync("SignalCommentDeletedReceived", ResponseGenerator.GenerateCommentResponse(deletedComment));
                }

                return(Ok(new { deletedComment = ResponseGenerator.GenerateCommentResponse(deletedComment) }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public IActionResult GetCommentsByCourseId([FromQuery] long courseId)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var _comments = _commentRepository.GetCommentsByCourseId(courseId)
                                .OrderByDescending(c => c.CommentDateTime)
                                .ToList();

                var comments = new List <object>();
                foreach (var comment in _comments)
                {
                    comments.Add(ResponseGenerator.GenerateCommentResponse(comment));
                }

                return(Ok(new { comments }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public async Task <IActionResult> UpdateComment([FromBody] Comment comment)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var commentToUpdate = _commentRepository.FindById(comment.Id);
                commentToUpdate.Text = comment.Text;
                var updatedComment = _commentRepository.Update(commentToUpdate);

                if (updatedComment != null)
                {
                    await _hubContext.Clients.All.SendAsync("SignalCommentUpdatedReceived", ResponseGenerator.GenerateCommentResponse(updatedComment));
                }

                return(Ok(new { updatedComment = ResponseGenerator.GenerateCommentResponse(updatedComment) }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public async Task <IActionResult> LikeComment([FromBody] Like like, [FromQuery] string action)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var comment = _commentRepository.FindById(like.CommentId.Value);
                var user    = await _userManager.FindByIdAsync(like.UserId);

                Like updatedLike = null;

                if (action == "like")
                {
                    var newLike = new Like()
                    {
                        Comment      = comment,
                        CommentId    = like.CommentId,
                        User         = user,
                        UserId       = user.Id,
                        UserFullName = $"{user.FirstName} {user.LastName}",
                        LikeDateTime = DateTime.Now
                    };

                    updatedLike = _likeRepository.Create(newLike);

                    var newNotification = new Notification()
                    {
                        Type     = "LIKE",
                        Text     = $"{user.FirstName} {user.LastName} Liked [ {comment.UserFullName}'s comment ]",
                        DateTime = DateTime.Now,
                        IsSeen   = false
                    };

                    var createdNotification = await _notificationRepository.Create(newNotification);
                }
                else if (action == "unlike")
                {
                    updatedLike = _likeRepository.Delete(like.CommentId.Value, like.UserId, "comment");

                    var newNotification = new Notification()
                    {
                        Type     = "LIKE",
                        Text     = $"{user.FirstName} {user.LastName} Unliked [ {comment.UserFullName}'s comment ]",
                        DateTime = DateTime.Now,
                        IsSeen   = false
                    };

                    var createdNotification = await _notificationRepository.Create(newNotification);
                }


                if (comment != null)
                {
                    var commentResponse = new Comment()
                    {
                        Id              = comment.Id,
                        Text            = comment.Text,
                        CommentId       = comment.CommentId,
                        UserId          = comment.UserId,
                        UserFullName    = comment.UserFullName,
                        UserGender      = comment.UserGender,
                        CourseId        = comment.CourseId,
                        CommentDateTime = comment.CommentDateTime,
                        Replies         = comment.Replies,
                        Likes           = comment.Likes
                    };

                    await _hubContext.Clients.All.SendAsync("SignalCommentLikeReceived", ResponseGenerator.GenerateCommentResponse(commentResponse));
                }

                return(Ok(new { comment = ResponseGenerator.GenerateCommentResponse(comment) }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public async Task <IActionResult> CreateComment([FromBody] Comment comment)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                var course = _courseRepository.FindById(comment.CourseId);
                var user   = await _userManager.FindByIdAsync(comment.UserId);

                var newComment = new Comment()
                {
                    User            = user,
                    UserId          = user.Id,
                    UserFullName    = $"{user.FirstName} {user.LastName}",
                    UserGender      = user.Gender,
                    Course          = course,
                    CourseId        = course.Id,
                    Text            = comment.Text,
                    CommentId       = comment.CommentId ?? null,
                    CommentDateTime = DateTime.Now,
                    Replies         = new List <Comment>(),
                    Likes           = new List <Like>()
                };

                var createdComment = _commentRepository.Create(newComment);

                if (createdComment != null)
                {
                    await _hubContext.Clients.All.SendAsync("SignalCommentReceived", ResponseGenerator.GenerateCommentResponse(createdComment));
                }

                if (createdComment.CommentId == null)
                {
                    var newNotification = new Notification()
                    {
                        Type     = "COMMENT",
                        Text     = $"{user.FirstName} {user.LastName} Commented on [ {course.Title_EN} ]",
                        DateTime = DateTime.Now,
                        IsSeen   = false
                    };

                    var createdNotification = await _notificationRepository.Create(newNotification);
                }
                else
                {
                    var newNotification = new Notification()
                    {
                        Type     = "COMMENT REPLY",
                        Text     = $"{user.FirstName} {user.LastName} Replied to a comment on [ {course.Title_EN} ]",
                        DateTime = DateTime.Now,
                        IsSeen   = false
                    };

                    var createdNotification = await _notificationRepository.Create(newNotification);
                }

                return(Ok(new { createdComment = ResponseGenerator.GenerateCommentResponse(createdComment) }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }