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 DeletePermissionCommandHandler(
    IPermissionRepository permissionRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<DeletePermissionCommand, bool>
{
    /// <summary>
    /// 处理删除权限请求。
    /// </summary>
    /// <param name="request">删除命令。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>执行结果。</returns>
    public async Task<bool> Handle(DeletePermissionCommand request, CancellationToken cancellationToken)
    {
        // 1. 获取租户上下文
        var tenantId = tenantProvider.GetCurrentTenantId();

        // 2. 删除权限
        await permissionRepository.DeleteAsync(request.PermissionId, tenantId, cancellationToken);
        await permissionRepository.SaveChangesAsync(cancellationToken);

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