namespace Brizco.Repository.Handlers.ShiftPlans;

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<ShiftPlan>()
            .TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id,cancellationToken);

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

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

        return true;
    }
}