using FastEndpoints;
using KArtSell.Modules.SignalEngine.Domain;

namespace KArtSell.Modules.SignalEngine.Features.EvaluateResearchSellPolicy;

public sealed class Endpoint(SellPolicyChain policyChain) : Endpoint<Request, Response>
{
    public override void Configure()
    {
        Post("/internal/v1/research/sell-policy/evaluate");
        Roles("Quant", "System");
        Tags("ResearchOnly");
    }

    public override async Task HandleAsync(Request req, CancellationToken ct)
    {
        var decision = policyChain.Evaluate(new SellDecisionInput(
            req.PositionLotId,
            req.CycleId,
            req.EvidenceId,
            req.DatasetId,
            req.ModelVersion,
            req.ConfigVersion,
            req.CodeSha,
            req.AsOf,
            req.PublishedAtCutoff,
            req.CurrentSecurityPortfolioWeight,
            req.CurrentLotPortfolioWeight,
            req.StrategicCoreFloorWeight,
            req.HardImpairmentApproved,
            req.CapitalFloorBreached,
            req.SurvivalSellRatioOfLot,
            req.GapBelowFloorAtr,
            req.ConsecutiveCloseBreaches,
            req.CooldownSatisfied,
            req.ConcentrationSellRatioOfLot,
            req.OpportunityEdgeLowerBound,
            req.OpportunitySellRatioOfLot));

        await Send.OkAsync(new Response(
            decision.Action.ToString(),
            decision.SellRatioOfLot,
            decision.TargetSecurityPortfolioWeightAfter,
            decision.PolicyId,
            decision.ReasonCode,
            SellPolicyContract.DecisionContractVersion,
            SellPolicyContract.PolicyTraceSchemaVersion,
            decision.ReentryEligible,
            decision.PolicyTrace,
            "RESEARCH_CANDIDATE_NOT_PRODUCTION"), ct);
    }
}
