using System;

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

public sealed class SuseOptions
{
    public const string HttpClientName = "feedser.suse";

    /// <summary>
    /// CSV index enumerating CSAF advisories with their last modification timestamps.
    /// </summary>
    public Uri ChangesEndpoint { get; set; } = new("https://ftp.suse.com/pub/projects/security/csaf/changes.csv");

    /// <summary>
    /// Base URI where individual CSAF advisories reside (filename appended verbatim).
    /// </summary>
    public Uri AdvisoryBaseUri { get; set; } = new("https://ftp.suse.com/pub/projects/security/csaf/");

    /// <summary>
    /// Maximum advisories to fetch per run to bound backfill effort.
    /// </summary>
    public int MaxAdvisoriesPerFetch { get; set; } = 40;

    /// <summary>
    /// Initial history window for first-time execution.
    /// </summary>
    public TimeSpan InitialBackfill { get; set; } = TimeSpan.FromDays(30);

    /// <summary>
    /// Overlap window applied when resuming to capture late edits.
    /// </summary>
    public TimeSpan ResumeOverlap { get; set; } = TimeSpan.FromDays(3);

    /// <summary>
    /// Optional delay between advisory detail fetches.
    /// </summary>
    public TimeSpan RequestDelay { get; set; } = TimeSpan.Zero;

    /// <summary>
    /// Custom user agent presented to SUSE endpoints.
    /// </summary>
    public string UserAgent { get; set; } = "StellaOps.Feedser.Suse/0.1 (+https://stella-ops.org)";

    /// <summary>
    /// Timeout override applied to HTTP requests (defaults to 60 seconds when unset).
    /// </summary>
    public TimeSpan FetchTimeout { get; set; } = TimeSpan.FromSeconds(45);

    public void Validate()
    {
        if (ChangesEndpoint is null || !ChangesEndpoint.IsAbsoluteUri)
        {
            throw new InvalidOperationException("SuseOptions.ChangesEndpoint must be an absolute URI.");
        }

        if (AdvisoryBaseUri is null || !AdvisoryBaseUri.IsAbsoluteUri)
        {
            throw new InvalidOperationException("SuseOptions.AdvisoryBaseUri must be an absolute URI.");
        }

        if (MaxAdvisoriesPerFetch <= 0 || MaxAdvisoriesPerFetch > 250)
        {
            throw new InvalidOperationException("MaxAdvisoriesPerFetch must be between 1 and 250.");
        }

        if (InitialBackfill < TimeSpan.Zero || InitialBackfill > TimeSpan.FromDays(365))
        {
            throw new InvalidOperationException("InitialBackfill must be between 0 and 365 days.");
        }

        if (ResumeOverlap < TimeSpan.Zero || ResumeOverlap > TimeSpan.FromDays(14))
        {
            throw new InvalidOperationException("ResumeOverlap must be between 0 and 14 days.");
        }

        if (FetchTimeout <= TimeSpan.Zero || FetchTimeout > TimeSpan.FromMinutes(5))
        {
            throw new InvalidOperationException("FetchTimeout must be positive and less than five minutes.");
        }

        if (RequestDelay < TimeSpan.Zero || RequestDelay > TimeSpan.FromSeconds(10))
        {
            throw new InvalidOperationException("RequestDelay must be between 0 and 10 seconds.");
        }
    }
}
