using MediatR;
using TakeoutSaaS.Application.App.Merchants.Dto;
using TakeoutSaaS.Application.App.Merchants.Queries;
using TakeoutSaaS.Domain.Merchants.Repositories;

namespace TakeoutSaaS.Application.App.Merchants.Handlers;

/// <summary>
/// 获取商户详情查询处理器。
/// </summary>
public sealed class GetMerchantByIdQueryHandler(IMerchantRepository merchantRepository)
    : IRequestHandler<GetMerchantByIdQuery, MerchantDto?>
{
    /// <inheritdoc />
    public async Task<MerchantDto?> Handle(GetMerchantByIdQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询商户（跨租户）
        var merchant = await merchantRepository.FindByIdAsync(request.MerchantId, cancellationToken);
        if (merchant == null)
        {
            return null;
        }

        // 2. (空行后) 返回 DTO
        return new MerchantDto
        {
            Id = merchant.Id,
            TenantId = merchant.TenantId,
            BrandName = merchant.BrandName,
            BrandAlias = merchant.BrandAlias,
            LogoUrl = merchant.LogoUrl,
            Category = merchant.Category,
            ContactPhone = merchant.ContactPhone,
            ContactEmail = merchant.ContactEmail,
            Status = merchant.Status,
            JoinedAt = merchant.JoinedAt,
            CreatedAt = merchant.CreatedAt
        };
    }
}
