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

namespace MTWorkHR.Infrastructure.Repositories
{
    public class UserTaskRepository : Repository<UserTask>, IUserTaskRepository
    {
        private readonly DbSet<UserTask> dbSet;

        public UserTaskRepository(HRDataContext context) : base(context)
        {
            dbSet = context.Set<UserTask>();

        }
        public async Task<UserTask> GetByIdWithAllChildren(long id)
        {
            return await dbSet
                .Include(x => x.TaskAttachments).ThenInclude(a => a.AttachmentType)
                .Include(x => x.TaskHistories)
                .Include(x => x.TaskStatus)
                .Include(x => x.Project)
                .FirstOrDefaultAsync(x => x.Id == id);
        }


        public async Task<UserTask> GetByUserIdWithAllChildren(string userId)
        {
            return await dbSet
                .Include(x => x.TaskAttachments).ThenInclude(a => a.AttachmentType)
                .Include(x => x.TaskHistories)
                .Include(x => x.TaskStatus)
                .Include(x => x.Project)
                .FirstOrDefaultAsync(x => x.AssignedUserId == userId);
        }

        public async Task<Tuple<IQueryable<UserTask>, int>> GetAllWithChildrenAsync()
        {
            var query = dbSet.Include(x => x.Project).Include(x => x.TaskStatus).Include(x => x.TaskAttachments)
                .Include(x => x.TaskHistories).AsQueryable();
            var total = await query.CountAsync();

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