using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace StellaOps.Feedser.Storage.Mongo.Aliases;

[BsonIgnoreExtraElements]
internal sealed class AliasDocument
{
    [BsonId]
    public ObjectId Id { get; set; }

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

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

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

    [BsonElement("updatedAt")]
    public DateTime UpdatedAt { get; set; }
}

internal static class AliasDocumentExtensions
{
    public static AliasRecord ToRecord(this AliasDocument document)
    {
        ArgumentNullException.ThrowIfNull(document);
        var updatedAt = DateTime.SpecifyKind(document.UpdatedAt, DateTimeKind.Utc);
        return new AliasRecord(
            document.AdvisoryKey,
            document.Scheme,
            document.Value,
            new DateTimeOffset(updatedAt));
    }
}
