using Baya.Application.Contracts.Common;
using Baya.Application.Contracts.Persistence;
using Baya.Application.Models.Common;
using Mediator;

namespace Baya.Application.Features.Catalog.Commands.SetServiceCategoryActive;

internal sealed class SetServiceCategoryActiveCommandHandler(IUnitOfWork unitOfWork, ICacheService cache)
    : IRequestHandler<SetServiceCategoryActiveCommand, OperationResult<bool>>
{
    public async ValueTask<OperationResult<bool>> Handle(SetServiceCategoryActiveCommand request, CancellationToken cancellationToken)
    {
        var category = await unitOfWork.CatalogRepository.GetCategoryAsync(request.Id, cancellationToken);
        if (category is null)
            return OperationResult<bool>.NotFoundResult("Category not found.");

        category.IsActive = request.IsActive;

        await unitOfWork.CommitAsync();
        await CatalogCache.InvalidateAsync(cache, cancellationToken);

        return OperationResult<bool>.SuccessResult(true);
    }
}
