using System.Text.Json.Serialization;

namespace StellaOps.Feedser.Models;

/// <summary>
/// Canonicalized CVSS metric details supporting deterministic serialization.
/// </summary>
public sealed record CvssMetric
{
    public static CvssMetric Empty { get; } = new("3.1", vector: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:N", baseScore: 0, baseSeverity: "none", provenance: AdvisoryProvenance.Empty);

    [JsonConstructor]
    public CvssMetric(string version, string vector, double baseScore, string baseSeverity, AdvisoryProvenance provenance)
    {
        Version = Validation.EnsureNotNullOrWhiteSpace(version, nameof(version));
        Vector = Validation.EnsureNotNullOrWhiteSpace(vector, nameof(vector));
        BaseSeverity = Validation.EnsureNotNullOrWhiteSpace(baseSeverity, nameof(baseSeverity)).ToLowerInvariant();
        BaseScore = Math.Round(baseScore, 1, MidpointRounding.AwayFromZero);
        Provenance = provenance ?? AdvisoryProvenance.Empty;
    }

    public string Version { get; }

    public string Vector { get; }

    public double BaseScore { get; }

    public string BaseSeverity { get; }

    public AdvisoryProvenance Provenance { get; }
}
