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

namespace DigitalData.UserManager.Infrastructure.Repositories
{
    public class ClientUserRepository<TDbContext> : CRUDRepository<ClientUser, int, TDbContext>, IClientUserRepository
        where TDbContext : DbContext, IUserManagerDbContext
    {
        public ClientUserRepository(TDbContext dbContext) : base(dbContext, dbContext.ClientUsers)
        {
        }

        public async Task<IEnumerable<ClientUser>> ReadAsync(bool readOnly = true, int? userId = null)
        {
            var query = readOnly ? _dbSet.AsNoTracking() : _dbSet.AsQueryable();

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

            return await query.ToListAsync();
        }
    }
}