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> ReadByUserId(int userId)
        {
            return _dbSet.Where(gou => gou.User!.Id == userId).Include(gou => gou.Group);
        }

        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();
        }
        //TODO: Add -Async suffix at the end of async method names 
        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();

        public async Task<IEnumerable<GroupOfUser>> ReadByUserIdAsync(int userId) => await ReadByUserId(userId).ToListAsync();

        public async Task<IEnumerable<GroupOfUser>> ReadAsync(
            bool readOnly = true,
            bool withGroup = true, bool withUser = true,
            int? id = null, int? groupId = null, int? userId = null, string? username = null)
        {
            var query = readOnly ? _dbSet.AsNoTracking() : _dbSet.AsQueryable();

            if (withGroup)
                query = query.Include(gou => gou.Group);

            if (withUser)
                query = query.Include(gou => gou.User);

            if (id is not null)
                query = query.Where(gou => gou.Id == id);

            if (groupId is not null)
                query = query.Where(gou => gou.GroupId == groupId);

            if (userId is not null)
                query = query.Where(gou => gou.UserId == userId);

            if (username is not null)
                query = query.Where(gou => gou.User!.Username == username);

            return await query.ToListAsync();
        }
    }
}