using Knot.Contracts.Conversations.Domain;
using MongoDB.Driver;

namespace Knot.Modules.Conversations.Infrastructure.Persistence.Mongo;

public sealed class UserFolderSettingsRepository : IUserFolderSettingsRepository
{
    private readonly IMongoCollection<UserFolderSettings> _collection;

    public UserFolderSettingsRepository(IMongoDatabase database)
    {
        _collection = database.GetCollection<UserFolderSettings>("user_folder_settings");
    }

    public async Task<UserFolderSettings?> GetByUserIdAsync(Guid userId, CancellationToken cancellationToken)
    {
        return await _collection.Find(s => s.UserId == userId).FirstOrDefaultAsync(cancellationToken);
    }

    public async Task UpdateAsync(UserFolderSettings settings, CancellationToken cancellationToken)
    {
        var options = new ReplaceOptions { IsUpsert = true };
        await _collection.ReplaceOneAsync(s => s.Id == settings.Id, settings, options, cancellationToken);
    }

    public async Task RemoveByUserIdAsync(Guid userId, CancellationToken cancellationToken)
    {
        await _collection.DeleteManyAsync(s => s.UserId == userId, cancellationToken);
    }
}
