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

namespace StellaOps.Authority.Storage.Mongo.Documents;

/// <summary>
/// Represents an OAuth token issued by Authority.
/// </summary>
[BsonIgnoreExtraElements]
public sealed class AuthorityTokenDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; } = ObjectId.GenerateNewId().ToString();

    [BsonElement("tokenId")]
    public string TokenId { get; set; } = Guid.NewGuid().ToString("N");

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

    [BsonElement("subjectId")]
    [BsonIgnoreIfNull]
    public string? SubjectId { get; set; }

    [BsonElement("clientId")]
    [BsonIgnoreIfNull]
    public string? ClientId { get; set; }

    [BsonElement("scope")]
    public List<string> Scope { get; set; } = new();

    [BsonElement("referenceId")]
    [BsonIgnoreIfNull]
    public string? ReferenceId { get; set; }

    [BsonElement("status")]
    public string Status { get; set; } = "valid";

    [BsonElement("payload")]
    [BsonIgnoreIfNull]
    public string? Payload { get; set; }

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

    [BsonElement("expiresAt")]
    [BsonIgnoreIfNull]
    public DateTimeOffset? ExpiresAt { get; set; }

    [BsonElement("revokedAt")]
    [BsonIgnoreIfNull]
    public DateTimeOffset? RevokedAt { get; set; }

    [BsonElement("revokedReason")]
    [BsonIgnoreIfNull]
    public string? RevokedReason { get; set; }

    [BsonElement("revokedReasonDescription")]
    [BsonIgnoreIfNull]
    public string? RevokedReasonDescription { get; set; }


    [BsonElement("devices")]
    [BsonIgnoreIfNull]
    public List<BsonDocument>? Devices { get; set; }

    [BsonElement("revokedMetadata")]
    [BsonIgnoreIfNull]
    public Dictionary<string, string?>? RevokedMetadata { get; set; }
}
