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

namespace e_suite.Modules.FormsManagerUnitTests.FormsManagerUnitTests;

public class CreateFormUnitTest : FormsManagerUnitTestBase
{

    [SetUp]
    public async Task SetUp()
    {
        await base.Setup();
    }

    [Test]
    public void CreateForm_NormalConditionsAuteGenGuid_SaveChnges()
    {
        //Arrange
        var formDef = "SomeJsonDeffff";
        var formName = "NameFormTest";
        var createFormTemplate = new CreateFormTemplate
        {
            Definition = formDef,
            Guid = null,
            Name = formName
        };

        //Act
        _formManager.CreateFormTemplateAsync(auditResult, createFormTemplate, default).GetAwaiter().GetResult();
            
        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(_fakeformRepository.FormTemplates, Has.Count.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplates[0].Name, Is.EqualTo(formName));
            Assert.That(_fakeformRepository.FormTemplateVersions, Has.Count.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplateVersions[0].Version, Is.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplateVersions[0].FormDefinition, Is.EqualTo(formDef));
        });
    }

    [Test]
    public void CreateForm_NormalConditionsPredefindGuid_SaveChnges()
    {
        //Arrange
        var formGuid = Guid.NewGuid();
        var formDef = "SomeFormDef";
        var formName = "SomeFormName";
        var createFormTemplate = new CreateFormTemplate
        {
            Definition = formDef,
            Guid = formGuid,
            Name = formName
        };

        //Act
        _formManager.CreateFormTemplateAsync(auditResult, createFormTemplate, default).GetAwaiter().GetResult();
            
        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(_fakeformRepository.FormTemplates, Has.Count.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplates[0].Guid, Is.EqualTo(formGuid));
            Assert.That(_fakeformRepository.FormTemplates[0].Name, Is.EqualTo(formName));
            Assert.That(_fakeformRepository.FormTemplateVersions, Has.Count.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplateVersions[0].Version, Is.EqualTo(1));
            Assert.That(_fakeformRepository.FormTemplateVersions[0].FormDefinition, Is.EqualTo(formDef));
        });
    }

    [Test]
    public void CreateForm_WithExistingGuid_ThrowsException()
    {
        //Arrange
        var existingGuid = Guid.NewGuid();
        var formTemplate = new FormTemplate
        {
            Deleted = false,
            Guid = existingGuid,
            Name = "Some"
        };

        //Act
        _fakeformRepository.FormTemplates.Add(formTemplate);
            
        //Assert
        var createFormTempalate = new CreateFormTemplate
        {
            Guid = existingGuid,
            Name = "TEstst",
            Definition = "Defff"
        };
        Assert.ThrowsAsync<ExistsException>(() => _formManager.CreateFormTemplateAsync(auditResult, createFormTempalate, default));
    }
}