using MongoDB.Driver;
using StellaOps.Authority.Storage.Mongo.Documents;

namespace StellaOps.Authority.Storage.Mongo.Initialization;

internal sealed class AuthorityClientCollectionInitializer : IAuthorityCollectionInitializer
{
    public async ValueTask EnsureIndexesAsync(IMongoDatabase database, CancellationToken cancellationToken)
    {
        var collection = database.GetCollection<AuthorityClientDocument>(AuthorityMongoDefaults.Collections.Clients);

        var indexModels = new[]
        {
            new CreateIndexModel<AuthorityClientDocument>(
                Builders<AuthorityClientDocument>.IndexKeys.Ascending(c => c.ClientId),
                new CreateIndexOptions { Name = "client_id_unique", Unique = true }),
            new CreateIndexModel<AuthorityClientDocument>(
                Builders<AuthorityClientDocument>.IndexKeys.Ascending(c => c.Disabled),
                new CreateIndexOptions { Name = "client_disabled" })
        };

        await collection.Indexes.CreateManyAsync(indexModels, cancellationToken).ConfigureAwait(false);
    }
}
