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 DiagnosisArchiveRepositoryTest
{
    /// <summary>
    /// Initializes the test fixture by creating sample patient diagnosis records, resetting the
    /// archive collection in the integration database, and configuring the diagnosis archive
    /// repository used by the test suite.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var patientDiagnosis1 = new PatientDiagnosis
            {
                Id = Id,
                PatientId = PatientId,
                Time = Now.AddDays(-1),
                Code = "code1",
                DiagnosisSystem = "diagnosisSystem"
            };
    
            var patientDiagnosis2 = new PatientDiagnosis
            {
                Id = ObjectId.GenerateNewId(),
                PatientId = PatientId,
                Time = Now.AddDays(-2),
                Code = "code2",
                DiagnosisSystem = "diagnosisSystem"
            };
    
            var patientDiagnosis3 = new PatientDiagnosis
            {
                Id = ObjectId.GenerateNewId(),
                PatientId = PatientId,
                Time = Now.AddDays(-3),
                Code = "code3",
                DiagnosisSystem = "diagnosisSystem"
            };
    
            await IntegrationDb.Database.DropCollectionAsync("archive_patients_diagnosis");
            await IntegrationDb.Database.CreateCollectionAsync("archive_patients_diagnosis");
    
            _repository = new DiagnosisArchiveRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(patientDiagnosis1);
            await _repository.InsertOneAsync(patientDiagnosis2);
            await _repository.InsertOneAsync(patientDiagnosis3);
        }

    private DiagnosisArchiveRepository _repository;

    private readonly ApiSettings _apiSettings = new()
    {
        ArchivePatientsDiagnosis = "archive_patients_diagnosis"
    };

    private IOptions<ApiSettings> _optionsApiSettings;

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


    /// <summary>
    /// Verifies that the repository's <c>DeleteBeforeDate</c> operation correctly removes all records
    /// for a given patient that were created before the specified cutoff date, while preserving records
    /// created on or after that date.
    /// </summary>
    [Test]
        public async Task DeleteBeforeDate()
        {
            var result = await _repository.Collection.FindAsync(p => p.PatientId == PatientId);
    
            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(p => p.PatientId == PatientId);
    
            Assert.That(resultDelete, Is.Not.Null);
            Assert.That(resultDelete.ToList(), Has.Count.EqualTo(1));
        }
}