using MediatR;
using TakeoutSaaS.Application.Identity.Commands;
using TakeoutSaaS.Domain.Identity.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.Identity.Handlers;

/// <summary>
/// 删除菜单处理器。
/// </summary>
public sealed class DeleteMenuCommandHandler(
    IMenuRepository menuRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<DeleteMenuCommand, bool>
{
    /// <inheritdoc />
    public async Task<bool> Handle(DeleteMenuCommand request, CancellationToken cancellationToken)
    {
        // 1. 删除目标及可能的孤儿由外层保证
        var tenantId = tenantProvider.GetCurrentTenantId();
        await menuRepository.DeleteAsync(request.Id, tenantId, cancellationToken);

        // 2. 持久化
        await menuRepository.SaveChangesAsync(cancellationToken);

        // 3. 返回执行结果
        return true;
    }
}
