using Xunit;
using QuantEngine.Application.Services;
using QuantEngine.Core.Models;

namespace QuantEngine.Application.Tests;

public class DataNormalizationHelperTests
{
    [Theory]
    [InlineData("1234.56", 1234.56)]
    [InlineData("1,234.56", 1234.56)]
    [InlineData("1,234.56%", 1234.56)]
    [InlineData("", null)]
    [InlineData(null, null)]
    public void CoerceFloat_WithVariousFormats_ParsesCorrectly(string? input, double? expected)
    {
        var result = DataNormalizationHelper.CoerceFloat(input);
        Assert.Equal(expected, result);
    }
}

public class SourcePriorityResolverTests
{
    [Fact]
    public void ResolveSourcePriority_WithKisOk_PutsKisFirst()
    {
        var resolver = new SourcePriorityResolver();
        var kis = new PriceSourceResult { Status = "OK", Source = "kis" };

        var (priority, provenance) = resolver.ResolveSourcePriority("005930", kis, null, false, true);

        Assert.NotEmpty(priority);
        Assert.Equal("kis_open_api", priority[0]);
    }
}

public class PriceDataNormalizerTests
{
    [Fact]
    public void NormalizeCollectionRow_WithKisResult_ReturnsNormalized()
    {
        var normalizer = new PriceDataNormalizer(new SourcePriorityResolver());
        var row = new Dictionary<string, object> { { "Ticker", "005930" } };
        var kis = new PriceSourceResult { Status = "OK", CurrentPrice = 70000 };

        var (normalized, provenance) = normalizer.NormalizeCollectionRow(row, kis, null, false);

        Assert.Equal("005930", normalized["ticker"]);
        Assert.Equal(70000, normalized["current_price"]);
    }
}

public class GatherTradingDataParserTests
{
    [Fact]
    public void ParseGatherTradingData_WithJsonDocument_ReturnsRows()
    {
        var parser = new GatherTradingDataParser();
        var json = System.Text.Json.JsonDocument.Parse(@"
        {
            ""data"": {
                ""data_feed"": [{ ""Ticker"": ""005930"", ""Name"": ""삼성전자"" }]
            }
        }");

        var rows = parser.ParseGatherTradingData(json);

        Assert.Single(rows);
        Assert.True(rows[0].ContainsKey("Ticker"));
    }
}
