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;

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




    public override string GetCollectionName()
    {
        return _apiSettings.ArchivePatientsRecordingalerts ?? "archive_patients_recordingalerts";
    }

    public override async Task InsertOneAsync(PatientRecordingAlert patientRecordingAlert)
    {
        await Collection.InsertOneAsync(patientRecordingAlert);
    }

    public async Task DeleteBeforeDate(DateTime date)
    {
        var filter = Builders<PatientRecordingAlert>.Filter.Lt(po => po.Time, date);
        await Collection.DeleteManyAsync(filter);
    }

    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;
    }
}