using System;

namespace StellaOps.Excititor.Connectors.OCI.OpenVEX.Attest.Discovery;

public sealed record OciImageReference(string Registry, string Repository, string? Tag, string? Digest, string OriginalReference, string Scheme = "https")
{
    public string Canonical =>
        Digest is not null
            ? $"{Registry}/{Repository}@{Digest}"
            : Tag is not null
                ? $"{Registry}/{Repository}:{Tag}"
                : $"{Registry}/{Repository}";

    public bool HasDigest => !string.IsNullOrWhiteSpace(Digest);

    public bool HasTag => !string.IsNullOrWhiteSpace(Tag);

    public OciImageReference WithDigest(string digest)
    {
        if (string.IsNullOrWhiteSpace(digest))
        {
            throw new ArgumentException("Digest must be provided.", nameof(digest));
        }

        return this with { Digest = digest };
    }
}
