using QuantEngine.Core.Models;

namespace QuantEngine.Application.Services;

/// <summary>
/// Price Source 우선순위 결정 — Python _resolve_price_source 대응
/// KIS (우선) → Naver → JSON
/// </summary>
public class SourcePriorityResolver
{
    public (List<string> SourcePriority, Dictionary<string, object> Provenance) ResolveSourcePriority(
        string ticker,
        PriceSourceResult? kis,
        PriceSourceResult? naver,
        bool includeNaver = false,
        bool includeLiveKis = true)
    {
        var sourcePriority = new List<string> { "gathertradingdata_json" };
        var provenance = new Dictionary<string, object>
        {
            { "ticker", ticker },
            { "source_priority", new List<string>() }
        };

        // KIS 우선 (status OK만)
        if (includeLiveKis && kis?.Status == "OK")
        {
            sourcePriority.Insert(0, "kis_open_api");
            provenance["kis"] = kis;
        }

        // Naver 추가 (OK or DATA_MISSING)
        if (includeNaver && naver != null && (naver.Status == "OK" || naver.Status == "DATA_MISSING"))
        {
            sourcePriority.Add("naver_finance");
            provenance["naver"] = naver;
        }

        provenance["source_priority"] = sourcePriority;
        return (sourcePriority, provenance);
    }
}
