using System;
using System.Collections.Generic;

namespace StellaOps.Feedser.Merge.Options;

/// <summary>
/// Provides the built-in precedence table used by the merge engine when no overrides are supplied.
/// </summary>
internal static class AdvisoryPrecedenceDefaults
{
    public static IReadOnlyDictionary<string, int> Rankings { get; } = CreateDefaultTable();

    private static IReadOnlyDictionary<string, int> CreateDefaultTable()
    {
        var table = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        // 0 – distro PSIRTs/OVAL feeds (authoritative for OS packages).
        Add(table, 0,
            "redhat",
            "ubuntu",
            "distro-ubuntu",
            "debian",
            "distro-debian",
            "suse",
            "distro-suse");

        // 1 – vendor PSIRTs (authoritative product advisories).
        Add(table, 1,
            "msrc",
            "vndr-msrc",
            "vndr-oracle",
            "vndr_oracle",
            "oracle",
            "vndr-adobe",
            "adobe",
            "vndr-apple",
            "apple",
            "vndr-cisco",
            "cisco",
            "vmware",
            "vndr-vmware",
            "vndr_vmware",
            "vndr-chromium",
            "chromium",
            "vendor");

        // 2 – ecosystem registries (OSS package maintainers).
        Add(table, 2,
            "ghsa",
            "osv",
            "cve");

        // 3 – regional CERT / ICS enrichment feeds.
        Add(table, 3,
            "jvn",
            "acsc",
            "cccs",
            "cert-fr",
            "certfr",
            "cert-in",
            "certin",
            "cert-cc",
            "certcc",
            "certbund",
            "cert-bund",
            "ru-bdu",
            "ru-nkcki",
            "kisa",
            "ics-cisa",
            "ics-kaspersky");

        // 4 – KEV / exploit catalogue annotations (flag only).
        Add(table, 4,
            "kev",
            "cisa-kev");

        // 5 – public registries (baseline data).
        Add(table, 5,
            "nvd");

        return table;
    }

    private static void Add(IDictionary<string, int> table, int rank, params string[] sources)
    {
        foreach (var source in sources)
        {
            if (string.IsNullOrWhiteSpace(source))
            {
                continue;
            }

            table[source] = rank;
        }
    }
}
