using Microsoft.Extensions.Options;

namespace DramaLing.Api.Models.Configuration;

public class ReplicateOptionsValidator : IValidateOptions<ReplicateOptions>
{
    public ValidateOptionsResult Validate(string? name, ReplicateOptions options)
    {
        var failures = new List<string>();

        if (string.IsNullOrEmpty(options.ApiKey))
        {
            failures.Add("Replicate API Key is required");
        }

        if (options.TimeoutSeconds < 60 || options.TimeoutSeconds > 600)
        {
            failures.Add("Timeout must be between 60 and 600 seconds");
        }

        if (string.IsNullOrEmpty(options.DefaultModel))
        {
            failures.Add("Default model must be specified");
        }

        if (!options.Models.ContainsKey(options.DefaultModel))
        {
            failures.Add($"Default model '{options.DefaultModel}' is not configured in Models section");
        }

        // 驗證模型配置
        foreach (var kvp in options.Models)
        {
            var modelName = kvp.Key;
            var config = kvp.Value;

            if (string.IsNullOrEmpty(config.Version))
            {
                failures.Add($"Model '{modelName}' must have a Version specified");
            }

            if (config.CostPerGeneration <= 0)
            {
                failures.Add($"Model '{modelName}' must have a positive CostPerGeneration");
            }

            if (config.DefaultWidth <= 0 || config.DefaultHeight <= 0)
            {
                failures.Add($"Model '{modelName}' must have positive default dimensions");
            }
        }

        if (failures.Any())
        {
            return ValidateOptionsResult.Fail(failures);
        }

        return ValidateOptionsResult.Success;
    }
}