using System;
using Microsoft.Extensions.Logging.Abstractions;
using StellaOps.Feedser.Storage.Mongo.Exporting;

namespace StellaOps.Feedser.Storage.Mongo.Tests;

[Collection("mongo-fixture")]
public sealed class ExportStateStoreTests : IClassFixture<MongoIntegrationFixture>
{
    private readonly MongoIntegrationFixture _fixture;

    public ExportStateStoreTests(MongoIntegrationFixture fixture)
    {
        _fixture = fixture;
    }

    [Fact]
    public async Task UpsertAndFetchExportState()
    {
        var store = new ExportStateStore(_fixture.Database, NullLogger<ExportStateStore>.Instance);
        var record = new ExportStateRecord(
            Id: "json",
            BaseExportId: "base",
            BaseDigest: "sha-base",
            LastFullDigest: "sha-full",
            LastDeltaDigest: null,
            ExportCursor: "cursor",
            TargetRepository: "repo",
            ExporterVersion: "1.0",
            UpdatedAt: DateTimeOffset.UtcNow,
            Files: Array.Empty<ExportFileRecord>());

        var saved = await store.UpsertAsync(record, CancellationToken.None);
        Assert.Equal("json", saved.Id);
        Assert.Empty(saved.Files);

        var fetched = await store.FindAsync("json", CancellationToken.None);
        Assert.NotNull(fetched);
        Assert.Equal("sha-full", fetched!.LastFullDigest);
        Assert.Empty(fetched.Files);
    }
}
