using System.Text.Json;

namespace QuantEngine.Application.Services;

public class GatherTradingDataParser
{
    public List<Dictionary<string, object>> ParseGatherTradingData(string jsonFilePath)
    {
        if (!File.Exists(jsonFilePath))
            return new();

        var jsonText = File.ReadAllText(jsonFilePath);
        return ParseGatherTradingData(JsonDocument.Parse(jsonText));
    }

    public List<Dictionary<string, object>> ParseGatherTradingData(JsonDocument json)
    {
        var rows = new List<Dictionary<string, object>>();
        var root = json.RootElement;

        // Extract data_feed
        if (root.TryGetProperty("data", out var dataElem) && dataElem.TryGetProperty("data_feed", out var feedElem))
        {
            var feedDict = new Dictionary<string, Dictionary<string, object>>();

            foreach (var item in feedElem.EnumerateArray())
            {
                if (item.TryGetProperty("Ticker", out var tickerElem))
                {
                    var ticker = tickerElem.GetString();
                    if (string.IsNullOrEmpty(ticker))
                        continue;

                    var row = new Dictionary<string, object>();
                    foreach (var prop in item.EnumerateObject())
                    {
                        row[prop.Name] = prop.Value.GetRawText();
                    }
                    feedDict[ticker] = row;
                }
            }

            // Merge with core_satellite
            if (dataElem.TryGetProperty("core_satellite", out var satElem))
            {
                foreach (var item in satElem.EnumerateArray())
                {
                    if (item.TryGetProperty("Ticker", out var tickerElem))
                    {
                        var ticker = tickerElem.GetString();
                        if (!string.IsNullOrEmpty(ticker) && feedDict.TryGetValue(ticker, out var row))
                        {
                            foreach (var prop in item.EnumerateObject())
                            {
                                if (!row.ContainsKey(prop.Name))
                                    row[prop.Name] = prop.Value.GetRawText();
                            }
                        }
                    }
                }
            }

            rows.AddRange(feedDict.Values);
        }

        return rows;
    }
}
