// <copyright file="NoLockBehavior.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

#pragma warning disable SA1309 // VAriables should not begin with underscore
#pragma warning disable CA1848 // VAriables should not begin with underscore

namespace Jellyfin.Database.Implementations.Locking;

using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

/// <summary>
/// Default lock behavior. Defines no explicit application locking behavior.
/// </summary>
public class NoLockBehavior : IEntityFrameworkCoreLockingBehavior
{
    private readonly ILogger<NoLockBehavior> _logger;

    /// <summary>
    /// Initializes a new instance of the <see cref="NoLockBehavior"/> class.
    /// </summary>
    /// <param name="logger">The Application logger.</param>
    public NoLockBehavior(ILogger<NoLockBehavior> logger)
    {
        this._logger = logger;
    }

    /// <inheritdoc/>
    public void OnSaveChanges(JellyfinDbContext context, Action saveChanges)
    {
        ArgumentNullException.ThrowIfNull(saveChanges);
        saveChanges();
    }

    /// <inheritdoc/>
    public void Initialise(DbContextOptionsBuilder optionsBuilder)
    {
        this._logger.LogInformation("The database locking mode has been set to: NoLock.");
    }

    /// <inheritdoc/>
    public async Task OnSaveChangesAsync(JellyfinDbContext context, Func<Task> saveChanges)
    {
        ArgumentNullException.ThrowIfNull(saveChanges);
        await saveChanges().ConfigureAwait(false);
    }
}
