using MongoDB.Bson.Serialization.Attributes;

namespace StellaOps.Feedser.Storage.Mongo.JpFlags;

[BsonIgnoreExtraElements]
public sealed class JpFlagDocument
{
    [BsonId]
    [BsonElement("advisoryKey")]
    public string AdvisoryKey { get; set; } = string.Empty;

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

    [BsonElement("category")]
    [BsonIgnoreIfNull]
    public string? Category { get; set; }

    [BsonElement("vendorStatus")]
    [BsonIgnoreIfNull]
    public string? VendorStatus { get; set; }

    [BsonElement("recordedAt")]
    public DateTime RecordedAt { get; set; }
}

internal static class JpFlagDocumentExtensions
{
    public static JpFlagDocument FromRecord(JpFlagRecord record)
    {
        ArgumentNullException.ThrowIfNull(record);

        return new JpFlagDocument
        {
            AdvisoryKey = record.AdvisoryKey,
            SourceName = record.SourceName,
            Category = record.Category,
            VendorStatus = record.VendorStatus,
            RecordedAt = record.RecordedAt.UtcDateTime,
        };
    }

    public static JpFlagRecord ToRecord(this JpFlagDocument document)
    {
        ArgumentNullException.ThrowIfNull(document);

        return new JpFlagRecord(
            document.AdvisoryKey,
            document.SourceName,
            document.Category,
            document.VendorStatus,
            DateTime.SpecifyKind(document.RecordedAt, DateTimeKind.Utc));
    }
}
