using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.Forms;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.FormsManagerUnitTests.FormsManagerUnitTests;

public class DeleteFormUnitTest : FormsManagerUnitTestBase
{
    [SetUp]
    public async Task SetUp()
    {
        await base.Setup();
    }

    [Test]
    public async Task DeleteForm_NormalConditions_SaveChanges()
    {
        //Arrange
        var form = new FormTemplate
        {
            Deleted = false,
            Guid = Guid.NewGuid(),
            Name = "test",
            Id = 1
        };

        var formVersion = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = "Test",
            Guid = Guid.NewGuid(),
            Id = 1,
            Version = 0,
            TemplateId = 1,
        };

        _fakeformRepository.FormTemplates.Add(form);
        _fakeformRepository.FormTemplateVersions.Add(formVersion);

        var genralId = new GeneralIdRef
        {
            Id = 1
        };

        _fakeformRepository.FormTemplateVersions.Add(formVersion);
        _fakeformRepository.FormTemplates.Add(form);

        //Act
        await _formManager.DeleteFormTemplateAsync(auditResult, genralId, default);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(form.Deleted, Is.EqualTo(true));
            Assert.That(formVersion.Deleted, Is.EqualTo(true));
        });
    }

    [Test]
    public async Task DeleteForm_FormWithManyVersion_SaveChanges()
    {
        var form = new FormTemplate
        {
            Deleted = false,
            Guid = Guid.NewGuid(),
            Name = "test",
            Id = 1
        };

        var formVersion = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = "Test",
            Guid = Guid.NewGuid(),
            Id = 1,
            Version = 0,
            TemplateId = 1,
        };

        var formVersionTwo = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = "Test",
            Guid = Guid.NewGuid(),
            Id = 2,
            Version = 1,
            TemplateId = 1,
        };

        var formVersionThre = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = "Test",
            Guid = Guid.NewGuid(),
            Id = 3,
            Version = 2,
            TemplateId = 1,
        };

        var genralId = new GeneralIdRef
        {
            Id = 1
        };

        _fakeformRepository.FormTemplateVersions.AddRange(new FormTemplateVersion[] { formVersion, formVersionTwo, formVersionThre });
        _fakeformRepository.FormTemplates.Add(form);

        //Act
        await _formManager.DeleteFormTemplateAsync(auditResult, genralId, default);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(_fakeformRepository.FormTemplates[0].Deleted, Is.EqualTo(true));
            Assert.That(_fakeformRepository.FormTemplateVersions[0].Deleted, Is.EqualTo(true));
            Assert.That(formVersionTwo.Deleted, Is.EqualTo(true));
            Assert.That(formVersionThre.Deleted, Is.EqualTo(true));
        });
    }

    [Test]
    public void DeleteForm_IdDosentExists_ThrowsException()
    {
        Assert.ThrowsAsync<NotFoundException>(() => _formManager.DeleteFormTemplateAsync(auditResult, new GeneralIdRef { Id = 6 }, default));
    }
}