namespace Brizco.Repository.Handlers.Section;

public class GetSectionsQueryHandler : IRequestHandler<GetSectionsQuery, List<SectionSDto>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly ICurrentUserService _currentUserService;

    public GetSectionsQueryHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
    {
        _repositoryWrapper = repositoryWrapper;
        _currentUserService = currentUserService;
    }
    public async Task<List<SectionSDto>> Handle(GetSectionsQuery request, CancellationToken cancellationToken)
    {
        if (_currentUserService.ComplexId == null)
            throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
        if (!Guid.TryParse(_currentUserService.ComplexId, out Guid complexId))
            throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);

        var shifts = await _repositoryWrapper.SetRepository<Domain.Entities.Complex.Section>().TableNoTracking
            .Where(s=>s.ComplexId==complexId)
            .OrderByDescending(s => s.CreatedAt)
            .Skip(request.Page * 15).Take(15)
            .Select(SectionMapper.ProjectToSDto)
            .ToListAsync(cancellationToken);


        return shifts;
    }
}