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

namespace Managing.Application.ManageBot;

public class ManualPositionCommandHandler : IRequestHandler<ManualPositionCommand, Position>
{
    private readonly IBotService _botService;

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

    public async Task<Position> Handle(ManualPositionCommand request, CancellationToken cancellationToken)
    {
        var bot = await _botService.GetBotByIdentifier(request.Identifier);
        if (bot == null)
        {
            throw new Exception($"Bot with identifier {request.Identifier} not found");
        }

        return await _botService.ClosePositionAsync(request.Identifier, request.PositionId);
    }
}