using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Repositories;
using adas_core.Test.Utilities;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using Options = Microsoft.Extensions.Options.Options;

namespace adas_core.Test.Repositories;

[TestFixture]
[Category("Integration")]
public class AdmissionRepositoryTest
{
    /// <summary>
    /// Initializes the integration test environment by resetting the admissions collection
    /// and seeding it with valid admission records to be used across the test fixture.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
    
            await IntegrationDb.Database.DropCollectionAsync("admissions");
            await IntegrationDb.Database.CreateCollectionAsync("admissions");
    
            _repository = new AdmissionRepository(_optionsApiSettings, IntegrationDb.Database);
    
            var admissions = new List<Admission>
            {
                TestUtilities.CreateValidAdmission(),
                TestUtilities.CreateValidAdmission(),
                TestUtilities.CreateValidAdmission()
            };
    
            foreach (var admission in admissions) await _repository.InsertOneAsync(admission);
        }

    private AdmissionRepository _repository;
    private IOptions<ApiSettings> _optionsApiSettings;

    private readonly ApiSettings _apiSettings = new()
    {
        Admissions = "admissions"
    };

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository.InsertOneAsync"/> successfully persists a valid admission so that it can subsequently be retrieved by its identifier.
    /// </summary>
    [Test]
        public async Task InsertOneAsync_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
    
            // Act
            await _repository.InsertOneAsync(admission);
    
            // Assert
            var result = await _repository.FindById(admission.Id);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result?.Id, Is.EqualTo(admission.Id));
        }

    /// <summary>
    /// Verifies that the repository successfully deletes an admission record, ensuring that the deleted admission can no longer be retrieved by its identifier.
    /// </summary>
    [Test]
        public async Task Delete_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            await _repository.InsertOneAsync(admission);
    
            // Act
            await _repository.Delete(admission.Id);
    
            // Assert
            var result = await _repository.FindById(admission.Id);
            Assert.That(result, Is.Null);
        }

    /// <summary>
    /// Verifies that <see cref="Admission"/> updates are persisted successfully by inserting an admission, modifying one of its fields, and asserting that the updated value is retrieved.
    /// </summary>
    [Test]
        public async Task Update_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            await _repository.InsertOneAsync(admission);
    
            // Modify the admission
            admission.DiagnosisAux = "Updated value";
    
            // Act
            await _repository.Update(admission);
    
            // Assert
            var updatedAdmission = await _repository.FindById(admission.Id);
            Assert.That(updatedAdmission, Is.Not.Null);
            Assert.That(updatedAdmission?.DiagnosisAux, Is.EqualTo(admission.DiagnosisAux));
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository.UpdateLocation"/> successfully updates the
    /// <c>PointOfCareId</c> of an existing admission to the specified new location identifier
    /// and that the change is persisted in the repository.
    /// </summary>
    [Test]
        public async Task UpdateLocation_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            var originalLocationId = ObjectId.GenerateNewId();
            var newLocationId = ObjectId.GenerateNewId();
    
            admission.PointOfCareId = originalLocationId;
            await _repository.InsertOneAsync(admission);
    
            // Act
            await _repository.UpdateLocation(admission.Id, newLocationId);
    
            // Assert
            var updatedAdmission = await _repository.FindById(admission.Id);
            Assert.That(updatedAdmission, Is.Not.Null);
            Assert.That(newLocationId, Is.EqualTo(updatedAdmission?.PointOfCareId));
        }

    /// <summary>
    /// Verifies that the repository successfully updates the patient information of an existing admission,
    /// ensuring the updated admission can be retrieved and reflects the new patient's details.
    /// </summary>
    [Test]
        public async Task UpdatePatient_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            var newPatient = new Person
            {
                LastName = "Doe",
                FirstName = "John",
                BirthDate = new DateTime(1990, 1, 1),
                Gender = PatientEnum.Gender.Male
            };
    
            await _repository.InsertOneAsync(admission);
    
            // Act
            await _repository.UpdatePatient(admission.Id, newPatient);
    
            // Assert
            var updatedAdmission = await _repository.FindById(admission.Id);
            Assert.That(updatedAdmission, Is.Not.Null);
            Assert.That(updatedAdmission?.Person, Is.Not.Null);
            Assert.That(updatedAdmission?.Person?.FirstName, Is.EqualTo(newPatient.FirstName));
        }

    /// <summary>
    /// Verifies that the repository's FindAll operation successfully retrieves all admissions, returning a non-empty collection of exactly three records.
    /// </summary>
    [Test]
        public async Task FindAll_Success()
        {
            // Arrange
    
    
            // Act
            var result = await _repository.FindAll();
    
            // Assert
            var admissions = result as Admission[] ?? result.ToArray();
            Assert.That(admissions, Is.Not.Null);
            Assert.That(admissions, Is.Not.Empty);
            Assert.That(admissions.Count(), Is.EqualTo(3));
        }

    /// <summary>
    /// Verifies that the repository successfully retrieves an admission by its unique identifier after it has been inserted.
    /// </summary>
    [Test]
        public async Task FindById_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            await _repository.InsertOneAsync(admission);
    
            // Act
            var result = await _repository.FindById(admission.Id);
    
            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(admission.Id, Is.EqualTo(result?.Id));
        }

    /// <summary>
    /// Verifies that the repository's <c>FindById</c> method returns <c>null</c> when queried with a non-existing identifier.
    /// </summary>
    [Test]
        public async Task FindById_NotFound()
        {
            // Arrange
            var nonExistingId = ObjectId.GenerateNewId();
    
            // Act
            var result = await _repository.FindById(nonExistingId);
    
            // Assert
            Assert.That(result, Is.Null);
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository.FindByNhc"/> successfully retrieves an admission record from the repository using its NHC identifier.
    /// Confirms that the returned record is not null and that the NHC of the retrieved admission matches the one used in the lookup.
    /// </summary>
    [Test]
        public async Task FindByNhc_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
            await _repository.InsertOneAsync(admission);
    
            // Act
            var result = await _repository.FindByNhc(admission.Nhc);
    
            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(admission.Nhc, Is.EqualTo(result?.Nhc));
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository"/>.<c>FindByNhc</c> returns <c>null</c> when the provided NHC does not exist in the data source.
    /// </summary>
    [Test]
        public async Task FindByNhc_NotFound()
        {
            // Arrange
            var nonExistingNhc = "non-existing-nhc";
    
            // Act
            var result = await _repository.FindByNhc(nonExistingNhc);
    
            // Assert
            Assert.That(result, Is.Null);
        }

    // [Test]
    // public async Task FindByLocation_Success()
    // {
    //     // Arrange
    //     var location = new PatientLocation
    //     {
    //         UnitName = "TestUnit",
    //         Bed = "TestBed",
    //         Room = "TestRoom"
    //     };
    //
    //     var admission1 = TestUtilities.CreateValidAdmission();
    //     admission1.PatientLocation = location;
    //
    //     var admission2 = TestUtilities.CreateValidAdmission();
    //     admission2.PatientLocation = location;
    //
    //     await _repository.InsertOneAsync(admission1);
    //     await _repository.InsertOneAsync(admission2);
    //
    //     // Act
    //     var result = await _repository.FindByLocation(location);
    //
    //     // Assert
    //     Assert.That(result, Is.Not.Null);
    //     Assert.That(result, Is.Not.Empty);
    //     Assert.That(result.Count, Is.EqualTo(2));
    //
    //     foreach (var admission in result)
    //     {
    //         Assert.That(admission.PatientLocation, Is.Not.Null);
    //         Assert.That(location.UnitName, Is.EqualTo(admission?.PatientLocation?.UnitName));
    //         Assert.That(location.Bed, Is.EqualTo(admission?.PatientLocation?.Bed));
    //         Assert.That(location.Room, Is.EqualTo(admission?.PatientLocation?.Room));
    //     }
    // }

    // [Test]
    // public async Task FindByOrigin_Success()
    // {
    //     // Arrange
    //     var origin = "TestOrigin";
    //     var admission1 = TestUtilities.CreateValidAdmission();
    //     admission1.Origin = TestUtilities.CreateValidOptionList();
    //
    //     var admission2 = TestUtilities.CreateValidAdmission();
    //     admission2.Origin = TestUtilities.CreateValidOptionList();
    //
    //     await _repository.InsertOneAsync(admission1);
    //     await _repository.InsertOneAsync(admission2);
    //
    //     // Act
    //     var result = await _repository.FindByOrigin(origin);
    //     var resultList = result?.ToList();
    //
    //     // Assert
    //     Assert.That(resultList, Is.Not.Null);
    //     Assert.That(resultList, Is.Not.Empty);
    //     Assert.That(resultList?.Count(), Is.EqualTo(2));
    // }

    /// <summary>
    /// Verifies that inserting a valid admission through the repository returns the same admission and persists it so it can be retrieved by its identifier.
    /// </summary>
    [Test]
        public async Task InsertOneAsyncAndReturn_Success()
        {
            // Arrange
            var admission = TestUtilities.CreateValidAdmission();
    
            // Act
            var result = await _repository.InsertOneAsyncAndReturn(admission);
    
            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.EqualTo(admission));
            var retrievedAdmission = await _repository.FindById(admission.Id);
            Assert.That(retrievedAdmission, Is.Not.Null);
            Assert.That(retrievedAdmission?.Id, Is.EqualTo(admission.Id));
        }


    /// <summary>
    /// Tests that the repository's <c>GetAdmissionByUnitIdWithOutPoC</c> method returns only the admissions matching the specified unit identifier
    /// while excluding admissions that have an associated point-of-care identifier.
    /// </summary>
    [Test]
        public async Task GetAdmissionByUnitIdWithOutPoC_Success()
        {
            // Arrange
            var unitId = ObjectId.GenerateNewId();
            var admission1 = TestUtilities.CreateValidAdmission();
            admission1.UnitId = unitId;
            admission1.PointOfCareId = null;
    
            var admission2 = TestUtilities.CreateValidAdmission();
            admission2.UnitId = ObjectId.GenerateNewId();
            admission2.PointOfCareId = ObjectId.GenerateNewId();
    
            await _repository.InsertOneAsync(admission1);
            await _repository.InsertOneAsync(admission2);
    
            // Act
            var result = await _repository.GetAdmissionByUnitIdWithOutPoC(unitId);
    
            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].UnitId, Is.EqualTo(unitId));
            Assert.That(result[0].PointOfCareId, Is.Null);
        }

    /// <summary>
    /// Verifies that <c>GetAdmissionByUnitIdWithOutPoC</c> returns an empty result set (instead of null) when no admissions match the provided unit identifier.
    /// </summary>
    [Test]
        public async Task GetAdmissionByUnitIdWithOutPoC_NoMatch()
        {
            // Arrange
            var unitId = ObjectId.GenerateNewId();
    
            // Act
            var result = await _repository.GetAdmissionByUnitIdWithOutPoC(unitId);
    
            // Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Empty);
        }

    /// <summary>
    /// Verifies that the repository's <c>FindByPointOfCareId</c> method successfully retrieves the admission matching the specified point of care identifier, returning a single result and excluding admissions associated with different point of care identifiers.
    /// </summary>
    [Test]
        public async Task FindByPointOfCareId_Success()
        {
            var pocId = ObjectId.GenerateNewId();
            var admission1 = TestUtilities.CreateValidAdmission();
            admission1.PointOfCareId = pocId;
    
            var admission2 = TestUtilities.CreateValidAdmission();
            admission2.PointOfCareId = ObjectId.GenerateNewId();
    
            await _repository.InsertOneAsync(admission1);
            await _repository.InsertOneAsync(admission2);
    
            var result = await _repository.FindByPointOfCareId(pocId);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].PointOfCareId, Is.EqualTo(pocId));
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository"/>.FindByPointOfCareId returns a non-null empty collection when no point of care matches the provided identifier.
    /// </summary>
    [Test]
        public async Task FindByPointOfCareId_NoMatch()
        {
            var pocId = ObjectId.GenerateNewId();
    
            var result = await _repository.FindByPointOfCareId(pocId);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Empty);
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository"/>.<c>FindByDiagnosis</c> successfully returns a non-null result when retrieving admissions matching the specified diagnosis value.
    /// </summary>
    [Test]
        public async Task FindByDiagnosis_Success()
        {
            var diagnosis = "SomeDiagnosis";
            var admission1 = TestUtilities.CreateValidAdmission();
            admission1.Diagnosis = TestUtilities.CreateValidOptionList();
    
            var admission2 = TestUtilities.CreateValidAdmission();
            admission2.Diagnosis = TestUtilities.CreateValidOptionList();
    
            await _repository.InsertOneAsync(admission1);
            await _repository.InsertOneAsync(admission2);
    
            var result = await _repository.FindByDiagnosis(diagnosis);
    
            Assert.That(result, Is.Not.Null);
        }

    /// <summary>
    /// Verifies that <see cref="AdmissionRepository"/>.<c>FindByDiagnosis</c> returns an empty collection when no entity matches the provided diagnosis.
    /// </summary>
    [Test]
        public async Task FindByDiagnosis_NoMatch()
        {
            var diagnosis = "NonExistentDiagnosis";
    
            var result = await _repository.FindByDiagnosis(diagnosis);
    
            Assert.That(result, Is.Empty);
        }
}