using System;

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

public sealed class DebianOptions
{
    public const string HttpClientName = "feedser.debian";

    /// <summary>
    /// Raw advisory list published by the Debian security tracker team.
    /// Defaults to the Salsa Git raw endpoint to avoid HTML scraping.
    /// </summary>
    public Uri ListEndpoint { get; set; } = new("https://salsa.debian.org/security-tracker-team/security-tracker/-/raw/master/data/DSA/list");

    /// <summary>
    /// Base URI for advisory detail pages. Connector appends {AdvisoryId}.
    /// </summary>
    public Uri DetailBaseUri { get; set; } = new("https://security-tracker.debian.org/tracker/");

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

    /// <summary>
    /// Initial history window pulled on first run.
    /// </summary>
    public TimeSpan InitialBackfill { get; set; } = TimeSpan.FromDays(30);

    /// <summary>
    /// Resume overlap to accommodate late edits of existing advisories.
    /// </summary>
    public TimeSpan ResumeOverlap { get; set; } = TimeSpan.FromDays(2);

    /// <summary>
    /// Request timeout used for list/detail fetches unless overridden via HTTP client.
    /// </summary>
    public TimeSpan FetchTimeout { get; set; } = TimeSpan.FromSeconds(45);

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

    /// <summary>
    /// Custom user-agent for Debian tracker courtesy.
    /// </summary>
    public string UserAgent { get; set; } = "StellaOps.Feedser.Debian/0.1 (+https://stella-ops.org)";

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

        if (DetailBaseUri is null || !DetailBaseUri.IsAbsoluteUri)
        {
            throw new InvalidOperationException("Debian detail base URI must be an absolute URI.");
        }

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

        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.");
        }
    }
}
