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

namespace StellaOps.Authority.Storage.Mongo.Documents;

/// <summary>
/// Represents a canonical Authority user persisted in MongoDB.
/// </summary>
[BsonIgnoreExtraElements]
public sealed class AuthorityUserDocument
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; } = ObjectId.GenerateNewId().ToString();

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

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

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

    [BsonElement("displayName")]
    [BsonIgnoreIfNull]
    public string? DisplayName { get; set; }

    [BsonElement("email")]
    [BsonIgnoreIfNull]
    public string? Email { get; set; }

    [BsonElement("disabled")]
    public bool Disabled { get; set; }

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

    [BsonElement("attributes")]
    public Dictionary<string, string?> Attributes { get; set; } = new(StringComparer.OrdinalIgnoreCase);

    [BsonElement("plugin")]
    [BsonIgnoreIfNull]
    public string? Plugin { get; set; }

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

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