using MongoDB.Bson.Serialization.Attributes;

namespace StellaOps.Scanner.Storage.Catalog;

public enum ArtifactDocumentType
{
    LayerBom,
    ImageBom,
    Diff,
    Index,
    Attestation,
}

public enum ArtifactDocumentFormat
{
    CycloneDxJson,
    CycloneDxProtobuf,
    SpdxJson,
    BomIndex,
    DsseJson,
}

[BsonIgnoreExtraElements]
public sealed class ArtifactDocument
{
    [BsonId]
    public string Id { get; set; } = string.Empty;

    [BsonElement("type")]
    public ArtifactDocumentType Type { get; set; }
        = ArtifactDocumentType.ImageBom;

    [BsonElement("format")]
    public ArtifactDocumentFormat Format { get; set; }
        = ArtifactDocumentFormat.CycloneDxJson;

    [BsonElement("mediaType")]
    public string MediaType { get; set; } = string.Empty;

    [BsonElement("bytesSha256")]
    public string BytesSha256 { get; set; } = string.Empty;

    [BsonElement("sizeBytes")]
    public long SizeBytes { get; set; }
        = 0;

    [BsonElement("immutable")]
    public bool Immutable { get; set; }
        = false;

    [BsonElement("refCount")]
    public long RefCount { get; set; }
        = 0;

    [BsonElement("rekor")]
    [BsonIgnoreIfNull]
    public RekorReference? Rekor { get; set; }
        = null;

    [BsonElement("createdAt")]
    public DateTime CreatedAtUtc { get; set; }
        = DateTime.UtcNow;

    [BsonElement("updatedAt")]
    public DateTime UpdatedAtUtc { get; set; }
        = DateTime.UtcNow;

    [BsonElement("ttlClass")]
    public string TtlClass { get; set; } = "default";
}

public sealed class RekorReference
{
    [BsonElement("uuid")]
    public string? Uuid { get; set; }
        = null;

    [BsonElement("index")]
    public long? Index { get; set; }
        = null;

    [BsonElement("url")]
    public string? Url { get; set; }
        = null;
}
