using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json.Serialization;
using StellaOps.Vexer.Core;

namespace StellaOps.Vexer.Attestation.Models;

public sealed record VexAttestationPredicate(
    string ExportId,
    string QuerySignature,
    string ArtifactAlgorithm,
    string ArtifactDigest,
    VexExportFormat Format,
    DateTimeOffset CreatedAt,
    IReadOnlyList<string> SourceProviders,
    IReadOnlyDictionary<string, string> Metadata)
{
    public static VexAttestationPredicate FromRequest(
        VexAttestationRequest request,
        IReadOnlyDictionary<string, string>? metadata = null)
        => new(
            request.ExportId,
            request.QuerySignature.Value,
            request.Artifact.Algorithm,
            request.Artifact.Digest,
            request.Format,
            request.CreatedAt,
            request.SourceProviders,
            metadata is null ? ImmutableDictionary<string, string>.Empty : metadata.ToImmutableDictionary(StringComparer.Ordinal));
}

public sealed record VexInTotoSubject(
    string Name,
    IReadOnlyDictionary<string, string> Digest);

public sealed record VexInTotoStatement(
    [property: JsonPropertyName("_type")] string Type,
    string PredicateType,
    IReadOnlyList<VexInTotoSubject> Subject,
    VexAttestationPredicate Predicate)
{
    public static readonly string InTotoType = "https://in-toto.io/Statement/v0.1";
}
