namespace KArtSell.Modules.SignalEngine.Domain.Policies;

public sealed class HardImpairmentPolicy : ISellPolicy
{
    public int Priority => SellPolicyContract.HardImpairmentPriority;
    public string PolicyId => SellPolicyContract.HardImpairmentPolicyId;

    public SellPolicyResult Evaluate(SellDecisionInput input)
    {
        if (!input.HardImpairmentApproved)
        {
            return SellPolicyResult.NotApplicable(PolicyId, Priority, "HARD_IMPAIRMENT_NOT_APPROVED");
        }

        const decimal ratio = SellPolicyContract.HardImpairmentSellRatioOfLot;
        var decision = Create(input, ratio, SellAction.FullSell, "HARD_IMPAIRMENT", false);
        return SellPolicyResult.Applied(decision, ratio, false);
    }

    private SellDecision Create(
        SellDecisionInput input,
        decimal ratio,
        SellAction action,
        string reason,
        bool reentryEligible)
        => new(
            action,
            ratio,
            input.TargetSecurityPortfolioWeightAfter(ratio),
            PolicyId,
            Priority,
            reason,
            input.EvidenceId,
            input.DatasetId,
            input.ModelVersion,
            input.ConfigVersion,
            input.CodeSha,
            reentryEligible,
            Array.Empty<PolicyTraceEntry>());
}
