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 OrderAllocationRepository : Repository<OrderAllocation>, IOrderAllocationRepository
    {
        private readonly DbSet<OrderAllocation> dbSet;
        public OrderAllocationRepository(HRDataContext context) : base(context)
        {
            dbSet = context.Set<OrderAllocation>();

        }
        public async Task<OrderAllocation> GetByIdWithAllChildren(long id)
        {
            return await dbSet
                .Include(x => x.OrderType)
                .FirstOrDefaultAsync(x => x.Id == id);
        }


     

        public async Task<bool> AllocationExists(string userId, long orderTypeId, long? leaveTypeId, long contractId, int period)
        {
            return await dbSet.AnyAsync(q => q.EmployeeId == userId
                                        && q.ContractId == contractId   
                                        && q.OrderTypeId == orderTypeId
                                      //  && q.Period == period
                                        && (!leaveTypeId.HasValue || leaveTypeId == 0 || leaveTypeId == q.LeaveTypeId));
        }

        public async Task<OrderAllocation> GetUserAllocations(string userId, long orderTypeId, long? leaveTypeId, long contractId, int period)
        {
            return await dbSet.FirstOrDefaultAsync(q => q.EmployeeId == userId
                                        && q.ContractId == contractId
                                        && q.OrderTypeId == orderTypeId
                                        //&& q.Period == period
                                        && (!leaveTypeId.HasValue || leaveTypeId == 0 || leaveTypeId == q.LeaveTypeId));
        }
    }
}
