using adas_core.Domain.Models;
using adas_core.Domain.Models.AppSettings;
using adas_core.Infrastructure.Repositories;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using Options = Microsoft.Extensions.Options.Options;

namespace adas_core.Test.Repositories;

[TestFixture]
[Category("Integration")]
public class RecordingAlertArchiveRepositoryTest
{
    /// <summary>
    /// One-time test setup that prepares the archive collection for recording-alert integration tests by clearing it, creating a fresh instance of <see cref="RecordingAlertArchiveRepository"/>, and seeding it with two patient recording alerts (one matching the current patient with the current timestamp and one for a different patient dated ten days earlier), verifying that both records are persisted.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var recordingAlert1 = new PatientRecordingAlert
            {
                PatientId = PatientId,
                IsRecording = true,
                Time = Now
            };
    
            var recordingAlert2 = new PatientRecordingAlert
            {
                PatientId = ObjectId.GenerateNewId(),
                IsRecording = true,
                Time = Now.AddDays(-10)
            };
    
            await IntegrationDb.Database.DropCollectionAsync("archive_patients_recordingalerts");
            await IntegrationDb.Database.CreateCollectionAsync("archive_patients_recordingalerts");
    
            _repository = new RecordingAlertArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(recordingAlert1);
            await _repository.InsertOneAsync(recordingAlert2);
    
            Assert.That(await _repository.Collection.CountDocumentsAsync(_ => true), Is.EqualTo(2));
        }

    private RecordingAlertArchiveRepository _repository;

    private readonly ApiSettings _apiSettings = new()
    {
        ArchivePatientsRecordingalerts = "archive_patients_recordingalerts"
    };

    private IOptions<ApiSettings> _optionsApiSettings;

    private static readonly DateTime Now = DateTime.Now;
    private static readonly ObjectId PatientId = ObjectId.GenerateNewId();


    /// <summary>
    /// Verifies that the repository's <c>DeleteBeforeDate</c> operation removes all documents dated before the specified cutoff date, leaving only documents on or after that date.
    /// Expects more than one document in the collection initially and exactly one remaining after deletion.
    /// </summary>
    [Test]
        public async Task DeleteBeforeDate()
        {
            var result = await _repository.Collection.FindAsync(_ => true);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result.ToList(), Has.Count.GreaterThan(1));
    
            await _repository.DeleteBeforeDate(Now.AddDays(-1));
    
            var resultDelete = await _repository.Collection.FindAsync(_ => true);
    
            Assert.That(resultDelete, Is.Not.Null);
            Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
        }
}