using Managing.Application.Trading.Commands;
using Managing.Application.Trading.Handlers;
using Managing.Domain.Trades;
using Managing.Domain.Users;
using Moq;
using Xunit;
using static Managing.Common.Enums;

namespace Managing.Application.Tests;

public class PositionTests : BaseTests
{
    public PositionTests() : base()
    {
    }

    [Fact]
    public async Task Should_Open_Position()
    {
        var command = new OpenPositionRequest(
            "test",
            MoneyManagement,
            TradeDirection.Long,
            Ticker.ETH,
            PositionInitiator.User,
            DateTime.UtcNow,
            _account.User,
            100m,
            isForPaperTrading: false,
            signalIdentifier: new Guid().ToString());
        var handler = new OpenPositionCommandHandler(
            _exchangeService,
            _accountService.Object,
            _tradingService.Object);

        var position = await handler.Handle(command);

        Assert.NotNull(position);
    }

    [Fact]
    public async Task Shoud_Close_Position()
    {
        // var hexPositions = "[encoded data, 7937 characters]";
        // _ = new GetAccountPositioqwnInfoListOutputDTO().DecodeOutput(hexPositions).d
        //
        var openTrade = await _exchangeService.GetTrade(_account, "", Ticker.GMX);
        var position = new Position(Guid.NewGuid(), "", TradeDirection.Long, Ticker.GMX, MoneyManagement,
            PositionInitiator.User,
            DateTime.UtcNow, new User())
        {
            Open = openTrade
        };
        var command = new ClosePositionCommand(position);
        _ = _tradingService.Setup(m => m.GetPositionByIdentifierAsync(It.IsAny<Guid>())).ReturnsAsync(position);
        _ = _tradingService.Setup(m => m.GetPositionByIdentifierAsync(It.IsAny<Guid>())).ReturnsAsync(position);

        var handler = new ClosePositionCommandHandler(
            _exchangeService,
            _accountService.Object,
            _tradingService.Object);

        var closedPosition = await handler.Handle(command);
        Assert.NotNull(closedPosition);
    }
}