using FluentValidation;

namespace KArtSell.Host.Features.ShadowRun;

public sealed class InitiateShadowRunValidator : AbstractValidator<InitiateShadowRunRequest>
{
    public InitiateShadowRunValidator()
    {
        RuleFor(x => x.ModelId)
            .NotEmpty()
            .WithMessage("ModelId is required");

        RuleFor(x => x.WindowStart)
            .NotEmpty()
            .WithMessage("WindowStart is required");

        RuleFor(x => x.WindowEnd)
            .NotEmpty()
            .GreaterThanOrEqualTo(x => x.WindowStart)
            .WithMessage("WindowEnd must be >= WindowStart");

        // Custom rule: window must span at least 250 days
        RuleFor(x => x)
            .Must(req => (req.WindowEnd.ToDateTime(TimeOnly.MinValue) - req.WindowStart.ToDateTime(TimeOnly.MinValue)).TotalDays >= 250)
            .WithMessage("Window must span at least 250 days (252 trading sessions)")
            .OverridePropertyName(nameof(InitiateShadowRunRequest.WindowEnd));

        RuleFor(x => x.PhaseFilter)
            .Must(pf => IsValidPhaseFilter(pf))
            .WithMessage("PhaseFilter must be: All, BullMarket, BearMarket, Sideways, or HighVolatility");
    }

    private static bool IsValidPhaseFilter(string phase) =>
        phase is "All" or "BullMarket" or "BearMarket" or "Sideways" or "HighVolatility";
}
