using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Delete;
using Moq;
using Xunit;

namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;

public class DeleteManyAsyncTests : GenericTestContext<MongoDbEraser>
{
    [Fact]
    public async Task WithEmptyDocuments_DeletesNothing()
    {
        // Arrange
        var documents = new List<TestDocument>(0);
        var token = new CancellationToken(true);

        var collection = MockOf<IMongoCollection<TestDocument>>();

        var dbContext = MockOf<IMongoDbContext>();
        dbContext
            .Setup(x => x.GetCollection<TestDocument>(null))
            .Returns(collection.Object);

        // Act
        var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);

        // Assert
        result.Should().Be(0);

        var idsToDelete = documents.Select(e => e.Id).ToArray();
        Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
        collection.Verify(
            x => x.DeleteManyAsync(
                It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
                token),
            Times.Never);
    }

    [Fact]
    public async Task WithDocumentsAndCancellationToken_DeletesMany()
    {
        // Arrange
        var count = Fixture.Create<long>();
        var documents = Fixture.CreateMany<TestDocument>().ToList();
        var token = new CancellationToken(true);

        var collection = MockOf<IMongoCollection<TestDocument>>();
        collection
            .Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync(new DeleteResult.Acknowledged(count));

        var dbContext = MockOf<IMongoDbContext>();
        dbContext
            .Setup(x => x.GetCollection<TestDocument>(null))
            .Returns(collection.Object);

        // Act
        var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);

        // Assert
        result.Should().Be(count);

        var idsToDelete = documents.Select(e => e.Id).ToArray();
        Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
        collection.Verify(
            x => x.DeleteManyAsync(
                It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
                token),
            Times.Once);
    }

    [Fact]
    public async Task WithPartitionDocumentsAndCancellationToken_DeletesMany()
    {
        // Arrange
        var count = Fixture.Create<long>();
        var partitionKey = Fixture.Create<string>();
        var documents = Fixture
            .Build<PartitionedTestDocument>()
            .With(x => x.PartitionKey, partitionKey)
            .CreateMany()
            .ToList();
        var token = new CancellationToken(true);

        var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
        collection
            .Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<PartitionedTestDocument>>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync(new DeleteResult.Acknowledged(count));

        var dbContext = MockOf<IMongoDbContext>();
        dbContext
            .Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
            .Returns(collection.Object);

        // Act
        var result = await Sut.DeleteManyAsync<PartitionedTestDocument, Guid>(documents, token);

        // Assert
        result.Should().Be(count);

        var idsToDelete = documents.Select(e => e.Id).ToArray();
        Expression<Func<PartitionedTestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
        collection.Verify(
            x => x.DeleteManyAsync(
                It.Is<FilterDefinition<PartitionedTestDocument>>(f => f.EquivalentTo(expectedFilter)),
                token),
            Times.Once);

        dbContext.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once);
    }

    [Fact]
    public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
    {
        // Arrange
        var count = Fixture.Create<long>();
        var id = Fixture.Create<Guid>();
        var partitionKey = Fixture.Create<string>();
        var token = new CancellationToken(true);

        var collection = MockOf<IMongoCollection<TestDocument>>();
        collection
            .Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync(new DeleteResult.Acknowledged(count));

        var dbContext = MockOf<IMongoDbContext>();
        dbContext
            .Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
            .Returns(collection.Object);

        Expression<Func<TestDocument, bool>> filter = d => d.Id == id;

        // Act
        var result = await Sut.DeleteManyAsync<TestDocument, Guid>(filter, partitionKey, token);

        // Assert
        result.Should().Be(count);
        collection.Verify(
            x => x.DeleteManyAsync(
                It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
                token),
            Times.Once);

        dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
    }
}
