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

namespace DigitalData.UserManager.Infrastructure.Repositories
{
    public class GroupOfUserRepository<TDbContext> : CRUDRepository<GroupOfUser, int, TDbContext>, IGroupOfUserRepository
        where TDbContext : DbContext, IUserManagerDbContext
    {
        public GroupOfUserRepository(TDbContext dbContext) : base(dbContext, dbContext.GroupOfUsers)
        {
        }

        //TODO: making it public and having it in the interface is against Clean Architecture. Make it private
        public IQueryable<GroupOfUser> ReadByGroupId(int groupId)
        {
            return _dbSet.Where(mou => mou.GroupId == groupId);
        }

        private IQueryable<GroupOfUser> ReadByUsername(string userName)
        {
            return _dbSet.Where(gou => gou.User!.Username == userName).Include(gou => gou.Group);
        }

        public async Task<IEnumerable<GroupOfUser>> ReadByGroupUserIdAsync(int groupId, int userId)
        {
            return await _dbSet.Where(gou => gou.GroupId == groupId && gou.UserId == userId).ToListAsync();
        }

        public async Task<IEnumerable<GroupOfUser>> ReadAllAsyncWithGroup() => await _dbSet.Include(gou => gou.Group).ToListAsync();

        public async Task<IEnumerable<GroupOfUser>> ReadAllAsyncWithUser() => await _dbSet.Include(gou => gou.User).ToListAsync();

        public async Task<IEnumerable<GroupOfUser>> ReadAllAsyncWithGroupAndUser() => await _dbSet.Include(gou => gou.Group).Include(gou => gou.User).ToListAsync();

        public async Task<IEnumerable<GroupOfUser>> ReadByUsernameAsync(string username) => await ReadByUsername(username).ToListAsync();
    }
}