using System.Text.Json.Serialization;

namespace StellaOps.Feedser.Models;

/// <summary>
/// Canonical external reference associated with an advisory.
/// </summary>
public sealed record AdvisoryReference
{
    public static AdvisoryReference Empty { get; } = new("https://invalid.local/", kind: null, sourceTag: null, summary: null, provenance: AdvisoryProvenance.Empty);

    [JsonConstructor]
    public AdvisoryReference(string url, string? kind, string? sourceTag, string? summary, AdvisoryProvenance provenance)
    {
        if (!Validation.LooksLikeHttpUrl(url))
        {
            throw new ArgumentException("Reference URL must be an absolute http(s) URI.", nameof(url));
        }

        Url = url;
        Kind = Validation.TrimToNull(kind);
        SourceTag = Validation.TrimToNull(sourceTag);
        Summary = Validation.TrimToNull(summary);
        Provenance = provenance ?? AdvisoryProvenance.Empty;
    }

    public string Url { get; }

    public string? Kind { get; }

    public string? SourceTag { get; }

    public string? Summary { get; }

    public AdvisoryProvenance Provenance { get; }
}
