namespace ApiOPE.Configuration;

public sealed class InternalApiOptions
{
    public const string SectionName = "InternalApi";

    public string BaseUrl { get; set; } = "http://localhost:7093";

    public string ApiKey { get; set; } = string.Empty;

    public string ApiKeyHeaderName { get; set; } = "X-ApiOPE-Key";

    public int TimeoutSeconds { get; set; } = 20;
}

public static class InternalApiOptionsValidator
{
    public static bool IsValid(InternalApiOptions options)
    {
        return Uri.TryCreate(options.BaseUrl, UriKind.Absolute, out var baseUri) &&
               (baseUri.Scheme == Uri.UriSchemeHttp || baseUri.Scheme == Uri.UriSchemeHttps) &&
               string.IsNullOrEmpty(baseUri.UserInfo) &&
               string.IsNullOrEmpty(baseUri.Query) &&
               string.IsNullOrEmpty(baseUri.Fragment) &&
               !string.IsNullOrWhiteSpace(options.ApiKey) &&
               options.ApiKey.Length >= 32 &&
               string.Equals(options.ApiKeyHeaderName, "X-ApiOPE-Key", StringComparison.OrdinalIgnoreCase) &&
               options.TimeoutSeconds is >= 1 and <= 60;
    }
}
