namespace Brizco.Repository.Handlers.ShiftPlan;

public class DeleteShiftPlanCommandHandler : IRequestHandler<DeleteShiftPlanCommand, bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public DeleteShiftPlanCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }

    public async Task<bool> Handle(DeleteShiftPlanCommand request, CancellationToken cancellationToken)
    {
        var shiftPlan = await _repositoryWrapper.SetRepository<Domain.Entities.Shift.ShiftPlan>()
            .TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id,cancellationToken);

        if (shiftPlan == null)
            throw new AppException("ShiftPlan not found", ApiResultStatusCode.NotFound);

        _repositoryWrapper.SetRepository<Domain.Entities.Shift.ShiftPlan>()
            .Delete(shiftPlan);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);

        return true;
    }
}