using System.Collections.Generic;
using System.Threading.Tasks;
using QuantEngine.Core.Domain;
using QuantEngine.Core.Interfaces;

namespace QuantEngine.Application.Services
{
    public class FormulaService
    {
        private readonly IPostgresqlHistoryStore _historyStore;

        public FormulaService(IPostgresqlHistoryStore historyStore)
        {
            _historyStore = historyStore;
        }

        public TimingDecisionResult ComputeTimingDecision(Dictionary<string, object> ctx)
            => FormulaEngine.ComputeTimingDecision(ctx);

        public SellDecisionResult ComputeSellDecision(Dictionary<string, object> ctx)
            => FormulaEngine.ComputeSellDecision(ctx);

        public FinalDecisionResult ComputeFinalDecision(Dictionary<string, object> ctx)
            => FormulaEngine.ComputeFinalDecision(ctx);

        public CashShortfallResult ComputeCashShortfallHarness(
            Dictionary<string, object> asResult,
            double totalAsset,
            Dictionary<string, object> cashFloorInfo,
            double mrsScore)
            => FormulaEngine.ComputeCashShortfallHarness(asResult, totalAsset, cashFloorInfo, mrsScore);

        public CashRecoveryPlanResult ComputeCashRecoveryOptimizer(
            List<Dictionary<string, object>> sellCandidates,
            double cashShortfallMinKrw)
            => FormulaEngine.ComputeCashRecoveryOptimizer(sellCandidates, cashShortfallMinKrw);

        public Task<int> AppendFormulaRunAsync(string formulaName, Dictionary<string, object?> payload)
            => _historyStore.AppendAsync($"formula_{formulaName}_history", payload);
    }
}
