using System;

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

public sealed class UbuntuOptions
{
    public const string HttpClientName = "feedser.ubuntu";
    public const int MaxPageSize = 20;

    /// <summary>
    /// Endpoint exposing the rolling JSON index of Ubuntu Security Notices.
    /// </summary>
    public Uri NoticesEndpoint { get; set; } = new("https://ubuntu.com/security/notices.json");

    /// <summary>
    /// Base URI where individual notice detail pages live.
    /// </summary>
    public Uri NoticeDetailBaseUri { get; set; } = new("https://ubuntu.com/security/");

    public TimeSpan FetchTimeout { get; set; } = TimeSpan.FromSeconds(45);

    public TimeSpan InitialBackfill { get; set; } = TimeSpan.FromDays(30);

    public TimeSpan ResumeOverlap { get; set; } = TimeSpan.FromDays(3);

    public int MaxNoticesPerFetch { get; set; } = 60;

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

    public string UserAgent { get; set; } = "StellaOps.Feedser.Ubuntu/0.1 (+https://stella-ops.org)";

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

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

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

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

        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 (IndexPageSize <= 0 || IndexPageSize > MaxPageSize)
        {
            throw new InvalidOperationException($"IndexPageSize must be between 1 and {MaxPageSize}.");
        }
    }
}
