using MediatR;
using TakeoutSaaS.Application.App.Tenants.Contracts;
using TakeoutSaaS.Application.App.Tenants.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.Tenants.Handlers;

/// <summary>
/// 获取租户审核领取信息查询处理器。
/// </summary>
public sealed class GetTenantReviewClaimQueryHandler(ITenantRepository tenantRepository)
    : IRequestHandler<GetTenantReviewClaimQuery, TenantReviewClaimDto?>
{
    /// <inheritdoc />
    public async Task<TenantReviewClaimDto?> Handle(GetTenantReviewClaimQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询当前有效的审核领取记录
        var claim = await tenantRepository.GetActiveReviewClaimAsync(request.TenantId, cancellationToken);

        // 2. 未找到返回 null
        if (claim is null)
        {
            return null;
        }

        // 3. 转换为 DTO
        return new TenantReviewClaimDto
        {
            Id = claim.Id,
            TenantId = claim.TenantId,
            ClaimedBy = claim.ClaimedBy,
            ClaimedByName = claim.ClaimedByName,
            ClaimedAt = claim.ClaimedAt
        };
    }
}
