using System;
using System.Diagnostics.CodeAnalysis;

namespace StellaOps.Feedser.Source.CertIn.Configuration;

public sealed class CertInOptions
{
    public static string HttpClientName => "source.certin";

    /// <summary>
    /// Endpoint returning a paginated list of recent advisories.
    /// </summary>
    public Uri AlertsEndpoint { get; set; } = new("https://www.cert-in.org.in/api/alerts", UriKind.Absolute);

    /// <summary>
    /// Size of the rolling fetch window.
    /// </summary>
    public TimeSpan WindowSize { get; set; } = TimeSpan.FromDays(30);

    /// <summary>
    /// Overlap applied to subsequent windows.
    /// </summary>
    public TimeSpan WindowOverlap { get; set; } = TimeSpan.FromDays(2);

    /// <summary>
    /// Maximum pages fetched per cycle.
    /// </summary>
    public int MaxPagesPerFetch { get; set; } = 5;

    /// <summary>
    /// Delay between successive HTTP requests.
    /// </summary>
    public TimeSpan RequestDelay { get; set; } = TimeSpan.FromMilliseconds(500);

    [MemberNotNull(nameof(AlertsEndpoint))]
    public void Validate()
    {
        if (AlertsEndpoint is null || !AlertsEndpoint.IsAbsoluteUri)
        {
            throw new InvalidOperationException("AlertsEndpoint must be an absolute URI.");
        }

        if (WindowSize <= TimeSpan.Zero)
        {
            throw new InvalidOperationException("WindowSize must be greater than zero.");
        }

        if (WindowOverlap < TimeSpan.Zero)
        {
            throw new InvalidOperationException("WindowOverlap cannot be negative.");
        }

        if (WindowOverlap >= WindowSize)
        {
            throw new InvalidOperationException("WindowOverlap must be smaller than WindowSize.");
        }

        if (MaxPagesPerFetch <= 0)
        {
            throw new InvalidOperationException("MaxPagesPerFetch must be positive.");
        }

        if (RequestDelay < TimeSpan.Zero)
        {
            throw new InvalidOperationException("RequestDelay cannot be negative.");
        }
    }
}
