using DigitalData.Core.Infrastructure;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.UserManager.Infrastructure.Contracts;
using Microsoft.EntityFrameworkCore;

namespace DigitalData.UserManager.Infrastructure.Repositories
{
    public class UserRepository<TDbContext> : CRUDRepository<User, int, TDbContext>, IUserRepository
        where TDbContext : DbContext, IUserManagerDbContext
    {
        private readonly IModuleOfUserRepository _moduleOfUserRepo;

        private readonly IGroupOfUserRepository _groupOfUserRepo;

        private readonly IUserRepRepository _uRepRepo;

        private readonly IClientUserRepository _cUserRepo;

        public UserRepository(TDbContext dbContext, IModuleOfUserRepository moduleOfUserRepo, IGroupOfUserRepository groupOfUserRepo, IUserRepRepository userRepRepository, IClientUserRepository clientUserRepository) : base(dbContext, dbContext.Users)
        {
            _moduleOfUserRepo = moduleOfUserRepo;
            _groupOfUserRepo = groupOfUserRepo;
            _uRepRepo = userRepRepository;
            _cUserRepo = clientUserRepository;
        }

        public async Task<IEnumerable<User>> ReadByModuleIdAsync(int moduleId)
        {
            var mous = _moduleOfUserRepo.ReadByModuleId(moduleId).Select<ModuleOfUser, int>(mos => mos.UserId);
            return await _dbSet.Where<User>(u => mous.Contains(u.Id)).ToListAsync();
        }

        public async Task<IEnumerable<User>> ReadByGroupIdAsync(int groupId)
        {
            var gous = _groupOfUserRepo.ReadByGroupId(groupId).Select(gou => gou.UserId);
            return await _dbSet.Where<User>(u => gous.Contains(u.Id)).ToListAsync();
        }

        public async Task<IEnumerable<User>> ReadUnassignedByModuleIdAsync(int moduleId)
        {
            var mous = _moduleOfUserRepo.ReadByModuleId(moduleId).Select<ModuleOfUser, int>(mos => mos.UserId);
            return await _dbSet.Where<User>(u => !mous.Contains(u.Id)).ToListAsync();
        }

        public async Task<IEnumerable<User>> ReadUnassignedByGroupIdAsync(int groupId)
        {
            var gous = _groupOfUserRepo.ReadByGroupId(groupId).Select<GroupOfUser, int>(gou => gou.UserId);
            return await _dbSet.Where<User>(u => !gous.Contains(u.Id)).ToListAsync();
        }

        public async Task<User?> ReadByUsernameAsync(string username)
        {
            return await _dbSet.Where(user => user.Username == username).FirstOrDefaultAsync();
        }

        //TODO: instead of this implmenet .OnDelete(DeleteBehavior.ClientCascade) in DbContext
        public override async Task<bool> DeleteAsync(User user)
        {
            var mou = await _moduleOfUserRepo.ReadByUserAsync(user.Username);
            if (mou.Any())
                _dbContext.RemoveRange(mou);

            var gou = await _groupOfUserRepo.ReadByUsernameAsync(user.Username);
            if (gou.Any())
                _dbContext.RemoveRange(gou);

            var uRep_list = await _uRepRepo.ReadAllAsync(readOnly: false, userId: user.Id);
            if (uRep_list.Any())
                _dbContext.RemoveRange(uRep_list);

            uRep_list = await _uRepRepo.ReadAllAsync(readOnly: false, repUserId: user.Id);
            if (uRep_list.Any())
                _dbContext.RemoveRange(uRep_list);

            var cu_list = await _cUserRepo.ReadAsync(readOnly: false, userId: user.Id);
            if (cu_list.Any())
                _dbContext.RemoveRange(cu_list);

            return await base.DeleteAsync(user);
        }
    }
}