using MediatR;
using TakeoutSaaS.Application.App.TenantPackages.Commands;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.TenantPackages.Handlers;

/// <summary>
/// 删除租户套餐命令处理器（软删除）。
/// </summary>
public sealed class DeleteTenantPackageCommandHandler(ITenantPackageRepository tenantPackageRepository)
    : IRequestHandler<DeleteTenantPackageCommand, bool>
{
    /// <inheritdoc />
    public async Task<bool> Handle(
        DeleteTenantPackageCommand request,
        CancellationToken cancellationToken)
    {
        // 1. 查询套餐（带跟踪用于更新）
        var package = await tenantPackageRepository.GetByIdForUpdateAsync(request.TenantPackageId, cancellationToken);

        // 2. 如果不存在，返回 false
        if (package is null)
        {
            return false;
        }

        // 3. 软删除
        await tenantPackageRepository.SoftDeleteAsync(package, cancellationToken);

        // 4. 保存变更
        await tenantPackageRepository.SaveChangesAsync(cancellationToken);

        // 5. 返回成功
        return true;
    }
}
