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

namespace DigitalData.UserManager.Infrastructure.Repositories
{
    public class ModuleOfUserRepository<TDbContext> : CRUDRepository<ModuleOfUser, int, TDbContext>, IModuleOfUserRepository
        where TDbContext : DbContext, IUserManagerDbContext
    {
        public ModuleOfUserRepository(TDbContext dbContext) : base(dbContext, dbContext.ModuleOfUsers)
        {
        }

        private IQueryable<ModuleOfUser> ReadByUser(string username)
        {
            return _dbSet.Where(mou => mou.User!.Username == username).Include(mou => mou.Module);
        }

        public IQueryable<ModuleOfUser> ReadByModuleId(int moduleId)
        {
            return _dbSet.Where(mou => mou.ModuleId == moduleId);
        }

        public async Task<IEnumerable<ModuleOfUser>> ReadByModelUserIdAsync(int moduleId, int userId)
        {
            return await _dbSet.Where(mou => mou.ModuleId == moduleId && mou.UserId == userId).ToListAsync();
        }

        public async Task<IEnumerable<ModuleOfUser>> ReadByUserAsync(string username) => await ReadByUser(username).ToListAsync();
    }
}