public async Task<IHttpActionResult> GetUserVideo(int id)
        {
            try
            {
                var userId = User.Identity.GetUserId();
                var _User = _Uow._Users.GetAll(x => x.Id == userId).FirstOrDefault();

                var videosModel = new UserVideoModel();
                var video = await _Uow._Videos.GetAll(x => x.Id == id)
                    .Include(x => x.CategoryVideos)
                    .Include(x => x.CategoryVideos.Select(y => y.Category))
                    .Include(x => x.UserFavoriteVideos)
                    .FirstOrDefaultAsync();

            
                if (video == null)
                {
                    return NotFound();
                }

                if (_User.IsFreeUser.Value)
                {
                    if (!video.IsFreeVideo.Value)
                    {
                        return BadRequest();
                    }
                }

                if (_User.ExpirationDate != null && _User.ExpirationDate < DateTime.UtcNow && _User.IsFreeUser.Value)
                {
                    return BadRequest();
                }

                videosModel.BackgroundColor = video.BackgroundColor;
                videosModel.DateLive = video.DateLive;
                videosModel.Duration = video.Duration;
                videosModel.Id = video.Id;
                videosModel.Name = video.Name;
                videosModel.ReleaseYear = video.ReleaseYear;
                videosModel.VzaarVideoId = video.StandardVideoId;
                videosModel.ThumbnailURL = video.ThumbnailURL;
                videosModel.Description = video.Description;
                videosModel.TotalViews = await _Uow._UserVideoHistory.CountAsync(x => x.VideoId == video.Id);
                if (video.UserFavoriteVideos != null && video.UserFavoriteVideos.Count > 0)
                {
                    videosModel.IsFavoriteVideo = video.UserFavoriteVideos.Any(x => x.VideoId == id && x.UserId == userId);
                }
                if (video.CategoryVideos != null && video.CategoryVideos.Count > 0)
                {
                    foreach (var item in video.CategoryVideos)
                    {
                        videosModel.Categories.Add(new UserCategoryModel
                        {
                            Id = item.Category.Id,
                            Name = item.Category.Name
                        });
                    }

                }


                var lastWatched = await _Uow._UserVideoHistory
                    .GetAll(x => x.VideoId == id && x.UserId == userId)
                    .OrderByDescending(x => x.WatchDateTime)
                    .FirstOrDefaultAsync();

                if (lastWatched != null)
                {
                    videosModel.LastSeekTime = lastWatched.LastSeekTime.GetValueOrDefault();
                }

                //add video to user history
                var userHistory = new UserVideoHistory();
                userHistory.UserId = userId;
                userHistory.VideoId = id;
                userHistory.WatchDateTime = DateTime.UtcNow;
                _Uow._UserVideoHistory.Add(userHistory);
                await _Uow.CommitAsync();

                videosModel.WatchedVideoId = userHistory.Id;

                return Ok(videosModel);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }
        public async Task<IHttpActionResult> GetUserVideosHistory(int page = 1, int itemsPerPage = 20, string search = null)
        {
            try
            {
                var userId = User.Identity.GetUserId();
                var _User = _Uow._Users.GetAll(x => x.Id == userId).FirstOrDefault();
                var videosList = new List<UserVideoModel>();

                var videos = _Uow._UserVideoHistory.GetAll(x => x.UserId == userId)
                    .OrderByDescending(x => x.WatchDateTime)
                    .Include(x => x.Video)
                    .Select(x => x.Video)
                    .Include(x => x.CategoryVideos)
                    .Include(x => x.CategoryVideos.Select(y => y.Category));

                // searching
                if (!string.IsNullOrWhiteSpace(search))
                {
                    search = search.ToLower();
                    videos = videos.Where(x =>
                        x.Name.ToLower().Contains(search) ||
                        x.Description.ToLower().Contains(search));
                }

                int totalVideos = 0;
                totalVideos = await videos.CountAsync();

                //if (_User.IsFreeUser != null && _User.IsFreeUser.Value)
                //{
                //    videos = videos.Where(x => x.IsFreeVideo.Value);
                //    totalVideos = await videos.CountAsync(x => x.IsFreeVideo.Value);
                //}

                // paging
                var videosPaged = await videos.Skip((page - 1) * itemsPerPage).Take(itemsPerPage).ToListAsync();

                videosPaged.ForEach(x =>
                {
                    var uvm = new UserVideoModel();
                    uvm.BackgroundColor = x.BackgroundColor;
                    uvm.DateLive = x.DateLive;
                    uvm.Duration = x.Duration;
                    uvm.Id = x.Id;
                    uvm.Name = x.Name;
                    uvm.ReleaseYear = x.ReleaseYear;
                    uvm.VzaarVideoId = x.StandardVideoId;
                    uvm.ThumbnailURL = x.ThumbnailURL;
                    uvm.IsFavoriteVideo = (x.UserFavoriteVideos != null && x.UserFavoriteVideos.Any(y => y.VideoId == x.Id));
                    if (x.CategoryVideos != null && x.CategoryVideos.Count > 0)
                    {
                        x.CategoryVideos.ToList().ForEach(y =>
                        {
                            uvm.Categories.Add(new UserCategoryModel
                            {
                                Id = y.Category.Id,
                                Name = y.Category.Name
                            });
                        });
                    }
                    videosList.Add(uvm);
                });

                //var count = await _Uow._UserVideoHistory.CountAsync(x => x.UserId == userId);

                // json result
                var json = new
                {
                    count = totalVideos,
                    data = videosList,
                };

                return Ok(json);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

        }
        public async Task<IHttpActionResult> GetAllTimeTopVideos()
        {
            try
            {
                var videosList = new List<UserVideoModel>();

                var videos = await _Uow._Videos.GetAll(x => x.Active == true)
                    .Include(x => x.UserVideoHistories)
                    .Select(x => new
                    {
                        video = x,
                        totalViews = x.UserVideoHistories.Count
                    })
                    .OrderByDescending(x => x.totalViews)
                    .Select(x => x.video)
                    .Include(x=>x.CategoryVideos)
                    .Include(x => x.CategoryVideos.Select(y => y.Category))
                    .Take(5)
                    .ToListAsync();

                videos.ForEach(x =>
                {
                    var uvm = new UserVideoModel();
                    uvm.BackgroundColor = x.BackgroundColor;
                    uvm.DateLive = x.DateLive;
                    uvm.Duration = x.Duration;
                    uvm.Id = x.Id;
                    uvm.Name = x.Name;
                    uvm.ReleaseYear = x.ReleaseYear;
                    uvm.VzaarVideoId = x.StandardVideoId;
                    uvm.ThumbnailURL = x.ThumbnailURL;
                    if (x.CategoryVideos != null && x.CategoryVideos.Count > 0)
                    {
                        x.CategoryVideos.ToList().ForEach(y =>
                        {
                            uvm.Categories.Add(new UserCategoryModel
                            {
                                Id = y.Category.Id,
                                Name = y.Category.Name
                            });
                        });
                    }
                    videosList.Add(uvm);
                });

                // json result
                var json = new
                {
                    count = 5,
                    data = videosList,
                };

                return Ok(json);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }