using MongoDB.Driver;

namespace Guestlist;

public sealed class SetupStatusService(MongoDbContext db)
{
    private readonly IMongoCollection<AppSettingsDocument> _settings = db.Database.GetCollection<AppSettingsDocument>("app_settings");

    public async Task<bool> IsSetupCompleteAsync()
    {
        var settings = await _settings
            .Find(x => x.Id == "global")
            .FirstOrDefaultAsync();

        return settings?.IsSetupComplete == true;
    }

    public async Task MarkSetupCompleteAsync()
    {
        var document = new AppSettingsDocument
        {
            Id = "global",
            IsSetupComplete = true,
            CreatedAt = DateTime.UtcNow
        };

        await _settings.ReplaceOneAsync(
            x => x.Id == "global",
            document,
            new ReplaceOptions { IsUpsert = true });
    }
    
    public async Task ResetSetupAsync()
    {
        var document = new AppSettingsDocument
        {
            Id = "global",
            IsSetupComplete = false,
            CreatedAt = DateTime.UtcNow
        };

        await _settings.ReplaceOneAsync(
            x => x.Id == "global",
            document,
            new ReplaceOptions { IsUpsert = true });
    }
}