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;

[TestFixture]
public class GetFormInstanceAsyncUnitTests : FormsManagerUnitTestBase
{

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

    [Test]
    public Task GetFormInstanceAsync_WhenInstanceDoesNotExists_ReturnsError()
    {
        //Arrange
        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("1b2f6e6a-b194-4e47-9482-98cee266a8ba")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            await _formManager.GetFormInstanceAsync(generalIdRef, CancellationToken.None);
        });

        Assert.That(actualResult.Message, Is.EqualTo("Unable to find form instance"));
        return Task.CompletedTask;
    }

    [Test]
    public async Task GetFormInstanceAsync_WhenInstanceExists_Returns()
    {
        //Arrange

        var formTemplate = new FormTemplate
        {
            Guid = new Guid("731a6698-55eb-4d92-bc1f-85246253a2f3"),
            Id = 42,
            Name = "Meaning of life the universe and everything",
            Deleted = false
        };

        var formTemplateVersion = new FormTemplateVersion
        {
            Guid = new Guid("ebc4e58e-dac0-454c-8e1b-51855934309d"),
            Id = 57,
            FormDefinition = "<p>The meaning of life the universe and everything is...</p>",
            Template = formTemplate,
            TemplateId = formTemplate.Id,
            Version = 1
        };

        var formTemplateNewVersion = new FormTemplateVersion
        {
            Guid = new Guid("7b848071-4508-4567-bcb8-8f3276fb2d68"),
            Id = 58,
            FormDefinition = "<p>The meaning of life the universe and everything is chocolate</p>",
            Template = formTemplate,
            TemplateId = formTemplate.Id,
            Version = 2
        };

        formTemplate.Versions.Add(formTemplateVersion);
        formTemplate.Versions.Add(formTemplateNewVersion);

        var forminstance = new FormInstance
        {
            Guid = new Guid("cdc03065-b0d8-498b-87a3-de5617de65bb"),
            Id = 10,
            FormTemplateVersion = formTemplateVersion,
            FormTemplateVersionId = formTemplateVersion.Id
        };

        _fakeformRepository.FormTemplates.Add(formTemplate);
        _fakeformRepository.FormTemplateVersions.Add(formTemplateVersion);
        _fakeformRepository.FormTemplateVersions.Add(formTemplateNewVersion);
        _fakeformRepository.FormInstances.Add(forminstance);

        var generalIdRef = new GeneralIdRef
        {
            Guid = forminstance.Guid
        };

        //Act
        var result = await _formManager.GetFormInstanceAsync(generalIdRef, CancellationToken.None);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(result, Is.Not.Null);
            Assert.That(result!.Guid, Is.EqualTo(forminstance.Guid));
            Assert.That(result.Id, Is.EqualTo(forminstance.Id));
            Assert.That(result.Definition, Is.EqualTo(formTemplateVersion.FormDefinition));
            Assert.That(result.Version, Is.EqualTo(formTemplateVersion.Version));
            Assert.That(result.UpdatedVersion, Is.EqualTo(formTemplateNewVersion.Version));
        });
    }
}