// using Modules.User.Application.Commands;
// using Modules.User.Application.Gateways;
// using Modules.User.Application.Models;
// using Modules.User.Database.Repositories;
// using System.Linq;
//
// namespace Modules.User.Database.GatewaysImplementations;
//
// public class UserGateway(UserRepository1 userRepository1, AccountRepository accountRepository) : IUserGateway
// {
//     public async Task<Guid> CreateUser(Account account, Application.Models.User user, string? avatarId)
//     {
//         if (string.IsNullOrWhiteSpace(user.NickName)) throw new Exception($"{nameof(Application.Models.User.NickName)} is not set");
//         var userId = await userRepository1.AddAsync(new Database.Entities.User
//         {
//             NickName = user.NickName,
//             FirstName = user.FirstName,
//             Patronymic = user.Patronymic,
//             LastName = user.LastName,
//             AvatarId = avatarId,
//             Account = new Database.Entities.Account
//             {
//                 Email = account.Email.Trim().ToLower(),
//                 HashedPassword = account.HashedPassword,
//             }
//         });
//         return userId;
//     }
//
//     public async Task<bool> Delete(Guid id) => await userRepository1.DeleteAsync(id);
//
//     public async Task<Account> GetAccount(string email)
//     {
//         var account = await accountRepository.GetFirstWhere(q => q.Email == email.Trim().ToLower());
//         return new Account
//         {
//             Id = account.Id,
//             Email = account.Email,
//             HashedPassword = account.HashedPassword,
//         };
//     }
//
//     public async Task<PagedData<UserInfoShort>> GetUsers(UserListFilter? filter)
//     {
//         var count = await userRepository1.GetFilteredCountAsync(filter?.HasAvatar, filter?.NickName,
//             filter?.LastOnlineFrom, filter?.LastOnlineTo, filter?.IsBanned);
//         var take = filter?.ItemsOnPage > 0 ? filter.ItemsOnPage : 10;
//         var pagesCount = (int)Math.Ceiling((decimal)count / take);
//         var page = filter?.Page ?? 1;
//         if (page < 1) page = 1;
//         if (page > pagesCount) page = pagesCount > 0 ? pagesCount : 1;
//         var skip = (page - 1) * take;
//         var users = await userRepository1.GetWhereAsync(filter?.HasAvatar, filter?.NickName,
//             filter?.LastOnlineFrom, filter?.LastOnlineTo, filter?.IsBanned, skip, take);
//         
//         return new PagedData<UserInfoShort>
//         {
//             Page = page,
//             ItemsOnPage = take,
//             ItemsCount = count,
//             PagesCount = pagesCount,
//             Items = users.Select(q => new UserInfoShort
//             {
//                 Id = q.Id,
//                 NickName = q.NickName,
//                 FirstName = q.FirstName,
//                 Patronymic = q.Patronymic,
//                 LastName = q.LastName,
//                 BirthDate = q.BirthDate,
//                 
//                 LanguageId = q.RegionalSettings?.LanguageId,
//                 Email = q.Account.Email,
//                 HasAvatar = !string.IsNullOrWhiteSpace(q.AvatarId),
//                 LastOnline = q.Account.Sessions.Count == 0 ? null : q.Account.Sessions.Select(q => q.LastUpdate).Max(),
//                 IsBanned = false,
//                 
//                 // Roles { get; init; } = [];
//             }).ToList()
//         };
//         
//     }
//
//     public async Task<Application.Models.User?> GetUser(Guid accountId, Guid? sessionId)
//     {
//         var user = await userRepository1.GetFirstOrDefaultWhereAsync(q => q.Account.Id == accountId &&
//             sessionId.HasValue ? q.Account.Sessions.Any(q => q.Id == sessionId.Value) : true);
//
//         return user == null ? null : new Application.Models.User
//         {
//             Id= user.Id,
//             AccountId = user.Account.Id,
//             SessionId = sessionId,
//             NickName = user.NickName,
//             FirstName = user.FirstName,
//             Patronymic = user.Patronymic,
//             LastName = user.LastName,
//             BirthDate = user.BirthDate,
//             LanguageId = user.RegionalSettings?.LanguageId,
//             Email = user.Account.Email,
//             IsAuthenticated = sessionId.HasValue,
//         };
//     }
//     
//     public async Task<string?> GetAvatarId(Guid userId)
//     {
//         var user = await userRepository1.GetFirstOrDefaultWhereAsync(q => q.Id == userId && !q.Deleted);
//         return user?.AvatarId;
//     }
//     
//     public async Task<Domain.Entities.User.User?> GetUser(Guid accountId)
//     {
//         var user = await userRepository1.GetFirstOrDefaultWhereAsync(q => q.Account.Id == accountId);
//
//         return user == null ? null : new Domain.Entities.User.User(new Domain.Models.User
//         {
//             Id = user.Id,
//             Account = new Domain.Models.Account
//             {
//                 Id = user.Account.Id,
//                 Email = user.Account.Email,
//                 HashedPassword = user.Account.HashedPassword,
//                 Sessions = user.Account.Sessions.Select(q => new Domain.Models.Session
//                 {
//                     Id = q.Id,
//                     AccountId = q.AccountId,
//                     RefreshToken = q.RefreshToken,
//                     ClientInfo = new()
//                     {
//                         UserAgent = q.ClientInfo.UserAgent,
//                         Country = q.ClientInfo.Country,
//                         Region = q.ClientInfo.Region,
//                     },
//                     ExpiredDate = q.ExpiredDate,
//                 })
//             },
//             AvatarId = user.AvatarId,
//             NickName = user.NickName,
//             FirstName = user.FirstName,
//             Patronymic = user.Patronymic,
//             LastName = user.LastName,
//             BirthDate = user.BirthDate,
//             LanguageId = user.RegionalSettings?.LanguageId,
//         });
//     }
//
//     public async Task<bool> IsExists(string email) =>
//         await accountRepository.AnyWhere(q => q.Email == email.Trim().ToLower());
//
//     public async Task<Account?> TryGetAccount(string email)
//     {
//         var account = await accountRepository.GetFirstOrDefaultWhere(q => q.Email == email.Trim().ToLower());
//         return account == null
//             ? null
//             : new Account
//             {
//                 Id = account.Id,
//                 Email = account.Email,
//                 HashedPassword = account.HashedPassword,
//             };
//     }
//
//     public async Task UpdateAccount(Guid accountId, Account account)
//     {
//         var dbAccount = await accountRepository.GetFirstWhere(q => q.Id == accountId && !q.Deleted);
//         dbAccount.HashedPassword = account.HashedPassword;
//         dbAccount.Email = account.Email;
//         await accountRepository.UpdateAccountAsync();
//     }
//
//     public async Task UpdateUser(Guid userId, Application.Models.User user, string? avatarId)
//     {
//         var dbUser = await userRepository1.GetFirstWhereAsync(q => q.Id == userId);
//         if (!string.IsNullOrWhiteSpace(user.NickName)) dbUser.NickName = user.NickName;
//         
//         //if (!string.IsNullOrWhiteSpace(user.FirstName)) dbUser.FirstName = user.FirstName;
//         //if (!string.IsNullOrWhiteSpace(user.Patronymic)) dbUser.Patronymic = user.Patronymic;
//         //if (!string.IsNullOrWhiteSpace(user.LastName)) dbUser.LastName = user.LastName;
//         //if (user.BirthDate.HasValue) dbUser.BirthDate = user.BirthDate;
//         
//         dbUser.FirstName = user.FirstName;
//         dbUser.Patronymic = user.Patronymic;
//         dbUser.LastName = user.LastName;
//         if (!dbUser.BirthDate.HasValue && user.BirthDate.HasValue) dbUser.BirthDate = user.BirthDate;
//         
//         dbUser.AvatarId = avatarId;
//         dbUser.RegionalSettings = user.LanguageId == null ? null : new Database.Entities.RegionalSettings
//         {
//             LanguageId = user.LanguageId
//         };
//         await userRepository1.UpdateUserAsync();
//     }
// }