using Microsoft.EntityFrameworkCore;
using MTWorkHR.Core.Entities;
using MTWorkHR.Core.IDto;
using MTWorkHR.Core.IRepositories;
using MTWorkHR.Infrastructure.Entities;
using MTWorkHR.Infrastructure.DBContext;

namespace MTWorkHR.Infrastructure.Repositories
{
    public class TeamUserRepository : Repository<TeamUser>, ITeamUserRepository
    {
        private readonly DbSet<TeamUser> dbSet;

        public TeamUserRepository(HRDataContext context) : base(context)
        {
            dbSet = context.Set<TeamUser>();

        }
        public virtual async Task<TeamUser> AddAsync(TeamUser entity)
        {
            await dbSet.AddAsync(entity);
            return entity;
        }

        public virtual async Task<IList<TeamUser>> AddRangeAsync(IList<TeamUser> entity)
        {
            await dbSet.AddRangeAsync(entity);
            return entity;
        }

        public virtual async Task DeleteAsync(TeamUser entity)
        {
            dbSet.Remove(entity);
        }

        public virtual async Task DeleteAsync(IEnumerable<TeamUser> entities)
        {
            dbSet.RemoveRange(entities);
        }


        public async Task<Tuple<IQueryable<Team>, int>> GetUserTeams(string userId)
        {
            var query = dbSet.Where(u=> u.AssignedUserId == userId).Select(t=> t.Team).AsQueryable();
            var total = await query.CountAsync();

            return new Tuple<IQueryable<Team>, int>(query, total);
        }


        public async Task<Tuple<IQueryable<TeamUser>, int>> GetAllWithChildrenAsync()
        {
            var query = dbSet.Include(x => x.Team)
                .AsQueryable();

            var total = await query.CountAsync();

            return new Tuple<IQueryable<TeamUser>, int>(query, total);
        }
    }
}
