using MediatR;
using FacebookService.API.Application.Dtos;
using FacebookService.Domain.AggregatesModel.ChatbotAggregate;

namespace FacebookService.API.Application.Queries;

/// <summary>
/// EN: Handler for GetAIChatbotConfigQuery.
/// VI: Handler cho GetAIChatbotConfigQuery.
/// </summary>
public class GetAIChatbotConfigQueryHandler : IRequestHandler<GetAIChatbotConfigQuery, AIChatbotConfigDto?>
{
    private readonly IAIChatbotConfigRepository _configRepository;

    public GetAIChatbotConfigQueryHandler(IAIChatbotConfigRepository configRepository)
    {
        _configRepository = configRepository ?? throw new ArgumentNullException(nameof(configRepository));
    }

    public async Task<AIChatbotConfigDto?> Handle(
        GetAIChatbotConfigQuery request,
        CancellationToken cancellationToken)
    {
        var config = await _configRepository.GetByShopIdAsync(request.ShopId, cancellationToken);

        if (config is null)
        {
            return null;
        }

        return new AIChatbotConfigDto(
            config.Id,
            config.ShopId,
            config.Provider.Name,
            config.Model,
            config.SystemPrompt,
            config.Temperature,
            config.MaxTokens,
            config.IsEnabled,
            config.CreatedAt,
            config.UpdatedAt
        );
    }
}
