using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace StellaOps.Feedser.Models;

/// <summary>
/// Represents a vendor-supplied status tag for an affected package when a concrete version range is unavailable or supplementary.
/// </summary>
public sealed record AffectedPackageStatus
{
    [JsonConstructor]
    public AffectedPackageStatus(string status, AdvisoryProvenance provenance)
    {
        Status = AffectedPackageStatusCatalog.Normalize(status);
        Provenance = provenance ?? AdvisoryProvenance.Empty;
    }

    public string Status { get; }

    public AdvisoryProvenance Provenance { get; }
}

public sealed class AffectedPackageStatusEqualityComparer : IEqualityComparer<AffectedPackageStatus>
{
    public static AffectedPackageStatusEqualityComparer Instance { get; } = new();

    public bool Equals(AffectedPackageStatus? x, AffectedPackageStatus? y)
    {
        if (ReferenceEquals(x, y))
        {
            return true;
        }

        if (x is null || y is null)
        {
            return false;
        }

        return string.Equals(x.Status, y.Status, StringComparison.Ordinal)
            && EqualityComparer<AdvisoryProvenance>.Default.Equals(x.Provenance, y.Provenance);
    }

    public int GetHashCode(AffectedPackageStatus obj)
        => HashCode.Combine(obj.Status, obj.Provenance);
}
