using Managing.Application.Abstractions;
using Managing.Application.ManageBot.Commands;
using MediatR;

namespace Managing.Application.ManageBot
{
    /// <summary>
    /// Handler for updating trading bot configurations
    /// </summary>
    public class UpdateBotConfigCommandHandler : IRequestHandler<UpdateBotConfigCommand, bool>
    {
        private readonly IBotService _botService;

        public UpdateBotConfigCommandHandler(IBotService botService)
        {
            _botService = botService;
        }

        public async Task<bool> Handle(UpdateBotConfigCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.NewConfig == null)
                {
                    throw new ArgumentException("New configuration is required");
                }

                var bot = await _botService.GetBotByIdentifier(request.Identifier);

                if (bot == null)
                {
                    throw new Exception($"Bot with identifier {request.Identifier} not found");
                }

                return await _botService.UpdateBotConfiguration(request.Identifier, request.NewConfig);
            }
            catch (Exception ex)
            {
                throw new Exception($"Error updating bot configuration: {ex.Message}");
            }
        }
    }
} 