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

namespace StellaOps.Authority.Storage.Mongo.Initialization;

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

        var indexModels = new[]
        {
            new CreateIndexModel<AuthorityScopeDocument>(
                Builders<AuthorityScopeDocument>.IndexKeys.Ascending(s => s.Name),
                new CreateIndexOptions { Name = "scope_name_unique", Unique = true })
        };

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