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

namespace TakeoutSaaS.Application.App.SystemParameters.Handlers;

/// <summary>
/// 获取系统参数详情查询处理器。
/// </summary>
public sealed class GetSystemParameterByIdQueryHandler(ISystemParameterRepository repository)
    : IRequestHandler<GetSystemParameterByIdQuery, SystemParameterDto?>
{
    private readonly ISystemParameterRepository _repository = repository;

    /// <inheritdoc />
    public async Task<SystemParameterDto?> Handle(GetSystemParameterByIdQuery request, CancellationToken cancellationToken)
    {
        var parameter = await _repository.FindByIdAsync(request.ParameterId, cancellationToken);
        return parameter == null ? null : MapToDto(parameter);
    }

    private static SystemParameterDto MapToDto(Domain.SystemParameters.Entities.SystemParameter parameter) => new()
    {
        Id = parameter.Id,
        TenantId = parameter.TenantId,
        Key = parameter.Key,
        Value = parameter.Value,
        Description = parameter.Description,
        SortOrder = parameter.SortOrder,
        IsEnabled = parameter.IsEnabled,
        CreatedAt = parameter.CreatedAt,
        UpdatedAt = parameter.UpdatedAt
    };
}
