using System.Collections.Concurrent;
using ZB.MOM.WW.CBDD.Bson.Schema;
using ZB.MOM.WW.CBDD.Core.CDC;
using ZB.MOM.WW.CBDD.Core.Collections;
using ZB.MOM.WW.CBDD.Core.Compression;
using ZB.MOM.WW.CBDD.Core.Transactions;

namespace ZB.MOM.WW.CBDD.Core.Storage;

/// <summary>
/// Storage port used by collection/index orchestration to avoid concrete engine coupling.
/// </summary>
internal interface IStorageEngine : IIndexStorage, IDisposable
{
    uint PageCount { get; }
    ChangeStreamDispatcher? Cdc { get; }
    CompressionOptions CompressionOptions { get; }
    CompressionService CompressionService { get; }
    CompressionTelemetry CompressionTelemetry { get; }

    bool IsPageLocked(uint pageId, ulong excludingTxId);
    void RegisterCdc(ChangeStreamDispatcher cdc);

    Transaction BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted);
    Task<Transaction> BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, CancellationToken ct = default);

    CollectionMetadata? GetCollectionMetadata(string name);
    void SaveCollectionMetadata(CollectionMetadata metadata);
    void RegisterMappers(IEnumerable<IDocumentMapper> mappers);

    List<BsonSchema> GetSchemas(uint rootPageId);
    uint AppendSchema(uint rootPageId, BsonSchema schema);

    ConcurrentDictionary<string, ushort> GetKeyMap();
    ConcurrentDictionary<ushort, string> GetKeyReverseMap();
    ushort GetOrAddDictionaryEntry(string key);
    void RegisterKeys(IEnumerable<string> keys);
}
