using adas_core.Application.Repositories.Interfaces;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace adas_core.Infrastructure.Repositories;

/// <summary>
/// Repository for managing ConfigUnits in MongoDB. Provides methods to retrieve and manipulate ConfigUnits data.
/// </summary>
public class ConfigUnitsRepository : MongoRepository<ConfigUnits>, IConfigUnitsRepository
{
    private readonly ApiSettings _apiSettings;

    /// <summary>
    /// Initializes a new instance of the ConfigUnitsRepository class with the specified API settings and MongoDB database.
    /// </summary>
    /// <param name="apiSettings">The API settings.</param>
    /// <param name="database">The MongoDB database.</param>
    /// <exception cref="ArgumentNullException">Thrown when apiSettings is null.</exception>
    public ConfigUnitsRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database) : base(database)
    {
        if (apiSettings == null) throw new ArgumentNullException(nameof(apiSettings));
        _apiSettings = apiSettings.Value;
    } //For testing


    /// <summary>
    /// Gets the name of the MongoDB collection for ConfigUnits. This method retrieves the collection name from the API settings, or defaults to "config_units" if not specified.
    /// </summary>
    /// <returns>The name of the MongoDB collection for ConfigUnits.</returns>
    public override string GetCollectionName()
    {
        return _apiSettings.ConfigUnits ?? "config_units";
    }

    /// <summary>
    /// Finds a ConfigUnits document by its unique identifier. This method queries the MongoDB collection for a document with the specified ID and returns it if found, or null if not found.
    /// </summary>
    /// <param name="id">The unique identifier of the ConfigUnits document.</param>
    /// <returns>The ConfigUnits document if found; otherwise, null.</returns>
    public async Task<ConfigUnits?> FindById(string id)
    {
        var resutl = await Collection.FindAsync(Builders<ConfigUnits>.Filter.Eq(x => x.Id, id));
        return resutl.FirstOrDefault();
    }
}