using System.Net;
using KArtSell.Modules.ModelOperations.TradeExecution;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace KArtSell.Integration.Tests.TradeExecution;

public sealed class KisTradingHardOffTests
{
    [Fact]
    public async Task Every_kis_trade_operation_fails_before_any_http_call()
    {
        var handler = new CountingHandler();
        var service = new KisTradeExecutionService(new HttpClient(handler), NullLogger<KisTradeExecutionService>.Instance);
        var correlationId = Guid.NewGuid();

        await Assert.ThrowsAsync<KisTradingDisabledException>(() => service.ExecuteTradeAsync(Guid.NewGuid(), 1, 1m, correlationId));
        await Assert.ThrowsAsync<KisTradingDisabledException>(() => service.GetOrderStatusAsync("order", correlationId));
        await Assert.ThrowsAsync<KisTradingDisabledException>(() => service.CancelOrderAsync("order", "test", correlationId));
        await Assert.ThrowsAsync<KisTradingDisabledException>(() => service.ConfirmSettlementAsync("order", correlationId));

        Assert.Equal(0, handler.CallCount);
    }

    private sealed class CountingHandler : HttpMessageHandler
    {
        public int CallCount { get; private set; }

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            CallCount++;
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
        }
    }
}
