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

namespace adas_core.Infrastructure.Repositories;

/// <summary>
/// Represents a MongoDB repository responsible for managing archived patient recording alerts, exposing archive-specific data access operations through the recording alert archive repository contract.
/// </summary>
/// <typeparam name="PatientRecordingAlert">The type of the patient recording alert entity persisted in the archive.</typeparam>
public class RecordingAlertArchiveRepository : MongoRepository<PatientRecordingAlert>, IRecordingAlertArchiveRepository
{
    private readonly ApiSettings _apiSettings;

    public RecordingAlertArchiveRepository(IOptions<ApiSettings> apiSettings, IMongoDatabase database) : base(database)
    {
        if (apiSettings == null) throw new ArgumentNullException(nameof(apiSettings));
        _apiSettings = apiSettings.Value;
    } // To testing




    /// <summary>
    /// Gets the collection name for archive patients recording alerts, returning the configured value from API settings or a default name when no setting is provided.
    /// </summary>
    /// <returns>The configured collection name from <c>_apiSettings.ArchivePatientsRecordingalerts</c>, or the default "archive_patients_recordingalerts" if the setting is null.</returns>
    public override string GetCollectionName()
        {
            return _apiSettings.ArchivePatientsRecordingalerts ?? "archive_patients_recordingalerts";
        }

    /// <summary>
    /// Asynchronously inserts a new <see cref="PatientRecordingAlert"/> into the underlying collection.
    /// </summary>
    /// <param name="patientRecordingAlert">The patient recording alert document to persist.</param>
    public override async Task InsertOneAsync(PatientRecordingAlert patientRecordingAlert)
        {
            await Collection.InsertOneAsync(patientRecordingAlert);
        }

    /// <summary>
    /// Deletes all <see cref="PatientRecordingAlert"/> records whose <c>Time</c> is earlier than the specified cutoff date.
    /// </summary>
    /// <param name="date">The cutoff date; records with a time strictly before this value will be removed.</param>
    public async Task DeleteBeforeDate(DateTime date)
        {
            var filter = Builders<PatientRecordingAlert>.Filter.Lt(po => po.Time, date);
            await Collection.DeleteManyAsync(filter);
        }

    /// <summary>
    /// Bulk inserts a collection of patient recording alerts using a single batch operation and returns the number of documents successfully inserted.
    /// </summary>
    /// <param name="patientRecordingAlerts">The patient recording alerts to insert into the collection.</param>
    /// <returns>The number of patient recording alerts that were inserted.</returns>
    public async Task<long> InsertBatch(IEnumerable<PatientRecordingAlert> patientRecordingAlerts)
        {
            var writes = new List<WriteModel<PatientRecordingAlert>>();
            writes.AddRange(patientRecordingAlerts.Select(d => new InsertOneModel<PatientRecordingAlert>(d)));
    
            var bulkInsert = await Collection.BulkWriteAsync(writes);
    
            return bulkInsert.InsertedCount;
        }
}