namespace KArtSell.Modules.ModelOperations.SellDecision;

public interface ISellPriorityRanker
{
    SellPriority RankByPolicy(decimal drawdown, decimal marginRatio, decimal concentration, decimal liquidity, int daysHeld);
    decimal CalculateScore(SellPriority priority, int fundAgeDays, decimal liquidityPercent);
}

public class SellPriorityRanker : ISellPriorityRanker
{
    public SellPriority RankByPolicy(decimal drawdown, decimal marginRatio, decimal concentration, decimal liquidity, int daysHeld)
    {
        // Immutable priority ranking logic (per business policy)
        if (drawdown <= -0.30m)
            return SellPriority.HardImpairment;

        if (marginRatio < 0.20m)
            return SellPriority.PortfolioSurvival;

        if (drawdown <= -0.10m)
            return SellPriority.DynamicProfitFloor;

        if (concentration > 0.25m)
            return SellPriority.Concentration;

        if (liquidity < 0.20m)
            return SellPriority.Liquidity;

        if (drawdown <= -0.05m)
            return SellPriority.OpportunityCost;

        return SellPriority.ReentryOption;
    }

    public decimal CalculateScore(SellPriority priority, int fundAgeDays, decimal liquidityPercent)
    {
        // Lower score = higher priority (sort ascending)
        decimal baseScore = priority switch
        {
            SellPriority.HardImpairment => 1000m,
            SellPriority.PortfolioSurvival => 500m,
            SellPriority.DynamicProfitFloor => 300m,
            SellPriority.Concentration => 200m,
            SellPriority.Liquidity => 200m,
            SellPriority.OpportunityCost => 100m,
            SellPriority.ReentryOption => 50m,
            _ => 0m
        };

        // Boosts (lower score = higher priority)
        decimal ageBoost = (fundAgeDays > 365) ? -50m : 0m;
        decimal liquidityBoost = (liquidityPercent < 0.20m) ? -25m : 0m;

        return baseScore + ageBoost + liquidityBoost;
    }
}
