namespace StellaOps.Feedser.Source.Distro.RedHat.Configuration;

public sealed class RedHatOptions
{
    /// <summary>
    /// Name of the HttpClient registered for Red Hat Hydra requests.
    /// </summary>
    public const string HttpClientName = "redhat-hydra";

    /// <summary>
    /// Base API endpoint for Hydra security data requests.
    /// </summary>
    public Uri BaseEndpoint { get; set; } = new("https://access.redhat.com/hydra/rest/securitydata");

    /// <summary>
    /// Relative path for the advisory listing endpoint (returns summary rows with resource_url values).
    /// </summary>
    public string SummaryPath { get; set; } = "csaf.json";

    /// <summary>
    /// Number of summary rows requested per page when scanning for new advisories.
    /// </summary>
    public int PageSize { get; set; } = 200;

    /// <summary>
    /// Maximum number of summary pages to inspect within one fetch invocation.
    /// </summary>
    public int MaxPagesPerFetch { get; set; } = 5;

    /// <summary>
    /// Upper bound on individual advisories fetched per invocation (guards against unbounded catch-up floods).
    /// </summary>
    public int MaxAdvisoriesPerFetch { get; set; } = 800;

    /// <summary>
    /// Initial look-back window applied when no watermark exists (Red Hat publishes extensive history; we default to 30 days).
    /// </summary>
    public TimeSpan InitialBackfill { get; set; } = TimeSpan.FromDays(30);

    /// <summary>
    /// Optional overlap period re-scanned on each run to pick up late-published advisories.
    /// </summary>
    public TimeSpan Overlap { get; set; } = TimeSpan.FromDays(1);

    /// <summary>
    /// Timeout applied to individual Hydra document fetches.
    /// </summary>
    public TimeSpan FetchTimeout { get; set; } = TimeSpan.FromSeconds(60);

    /// <summary>
    /// Custom user-agent presented to Red Hat endpoints (kept short to satisfy Jetty header limits).
    /// </summary>
    public string UserAgent { get; set; } = "StellaOps.Feedser.RedHat/1.0";

    public void Validate()
    {
        if (BaseEndpoint is null || !BaseEndpoint.IsAbsoluteUri)
        {
            throw new InvalidOperationException("Red Hat Hydra base endpoint must be an absolute URI.");
        }

        if (string.IsNullOrWhiteSpace(SummaryPath))
        {
            throw new InvalidOperationException("Red Hat Hydra summary path must be configured.");
        }

        if (PageSize <= 0)
        {
            throw new InvalidOperationException("Red Hat Hydra page size must be positive.");
        }

        if (MaxPagesPerFetch <= 0)
        {
            throw new InvalidOperationException("Red Hat Hydra max pages per fetch must be positive.");
        }

        if (MaxAdvisoriesPerFetch <= 0)
        {
            throw new InvalidOperationException("Red Hat Hydra max advisories per fetch must be positive.");
        }

        if (InitialBackfill <= TimeSpan.Zero)
        {
            throw new InvalidOperationException("Red Hat Hydra initial backfill must be positive.");
        }

        if (Overlap < TimeSpan.Zero)
        {
            throw new InvalidOperationException("Red Hat Hydra overlap cannot be negative.");
        }

        if (FetchTimeout <= TimeSpan.Zero)
        {
            throw new InvalidOperationException("Red Hat Hydra fetch timeout must be positive.");
        }
    }
}
