using System;
using System.Collections.Generic;

namespace StellaOps.Feedser.Source.Vndr.Msrc.Internal;

internal static class MsrcDocumentMetadata
{
    public static Dictionary<string, string> CreateMetadata(MsrcVulnerabilitySummary summary)
    {
        var metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
        {
            ["msrc.vulnerabilityId"] = summary.VulnerabilityId ?? summary.Id,
            ["msrc.id"] = summary.Id,
        };

        if (summary.LastModifiedDate.HasValue)
        {
            metadata["msrc.lastModified"] = summary.LastModifiedDate.Value.ToString("O");
        }

        if (summary.ReleaseDate.HasValue)
        {
            metadata["msrc.releaseDate"] = summary.ReleaseDate.Value.ToString("O");
        }

        if (!string.IsNullOrWhiteSpace(summary.CvrfUrl))
        {
            metadata["msrc.cvrfUrl"] = summary.CvrfUrl!;
        }

        if (summary.CveNumbers.Count > 0)
        {
            metadata["msrc.cves"] = string.Join(",", summary.CveNumbers);
        }

        return metadata;
    }

    public static Dictionary<string, string> CreateCvrfMetadata(MsrcVulnerabilitySummary summary)
    {
        var metadata = CreateMetadata(summary);
        metadata["msrc.cvrf"] = "true";
        return metadata;
    }
}
