namespace KArtSell.Modules.ModelOperations.SellDecision;

public interface IPboValidator
{
    (bool IsValid, string Reason) ValidatePboScore(decimal? score, decimal threshold);
}

public interface IDsrValidator
{
    (bool IsValid, string Reason) ValidateDsrMetric(decimal? metric, decimal threshold);
}

public interface IOosValidator
{
    (bool IsValid, string Reason) ValidateOosPerformance(string? oosPerformanceJson, decimal? baselineReturn);
}

public class PboValidator : IPboValidator
{
    public (bool IsValid, string Reason) ValidatePboScore(decimal? score, decimal threshold)
    {
        if (score == null)
            return (false, "PBO score not yet available from Phase 1 evidence");

        if (score >= threshold)
            return (true, $"PBO score {score:F4} >= threshold {threshold:F4}");

        return (false, $"PBO score {score:F4} < threshold {threshold:F4}. Backtest overfit risk too high.");
    }
}

public class DsrValidator : IDsrValidator
{
    public (bool IsValid, string Reason) ValidateDsrMetric(decimal? metric, decimal threshold)
    {
        if (metric == null)
            return (false, "DSR metric not yet available from Phase 1 evidence");

        if (metric >= threshold)
            return (true, $"DSR metric {metric:F4} >= threshold {threshold:F4}");

        return (false, $"DSR metric {metric:F4} < threshold {threshold:F4}. Daily Sharpe ratio insufficient.");
    }
}

public class OosValidator : IOosValidator
{
    public (bool IsValid, string Reason) ValidateOosPerformance(string? oosPerformanceJson, decimal? baselineReturn)
    {
        if (string.IsNullOrEmpty(oosPerformanceJson))
            return (false, "OOS performance data not yet available from Phase 1 evidence");

        if (baselineReturn == null)
            return (false, "Baseline return not defined for comparison");

        try
        {
            // Parse JSON for OOS return (simple extraction; real implementation would use JSON parser)
            if (!decimal.TryParse(oosPerformanceJson, out decimal oosReturn))
                return (false, "Failed to parse OOS performance JSON");

            if (oosReturn >= baselineReturn)
                return (true, $"OOS return {oosReturn:F4} >= baseline {baselineReturn:F4}");

            return (false, $"OOS return {oosReturn:F4} < baseline {baselineReturn:F4}. Model underperforms out-of-sample.");
        }
        catch (Exception ex)
        {
            return (false, $"Error validating OOS performance: {ex.Message}");
        }
    }
}
