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 GetFormUnitTest : FormsManagerUnitTestBase
{
    [SetUp]
    public async Task SetUp()
    {
        await base.Setup();
    }

    [Test]
    public async Task GetForm_NormalConditions_GetsObj()
    {
        //Arrange
        var firstFormName = "TestOne";
        var firstFormDefinition = "testtttOne";
        var secondFormName = "SomeNAme";
        var secondFormDefinition = "SOMESOMEDEFF";

        var formGuidOne = new Guid("4691197a-9508-4c41-962b-c87491e28256");
        var formGuidTwo = new Guid("b520642a-f2b7-4fac-a766-1b0b29346139");

        var form = new FormTemplate
        {
            Guid = formGuidOne,
            Deleted = false,
            Id = 1,
            Name = firstFormName,
        };
        var formVersion = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = firstFormDefinition,
            Id = 10,
            Guid = new Guid("f3bcd5a0-2782-477f-b809-0a1baf2d19c7"),
            TemplateId = 1,
            Template = form
        };
        form.Versions = new List<FormTemplateVersion> { formVersion };
        _fakeformRepository.FormTemplateVersions.Add(formVersion);
        _fakeformRepository.FormTemplates.Add(form);

        var formTwo = new FormTemplate
        {
            Guid = formGuidTwo,
            Deleted = false,
            Id = 2,
            Name = secondFormName,
        };
        var formVersionTwo = new FormTemplateVersion
        {
            Deleted = false,
            FormDefinition = secondFormDefinition,
            Id = 20,
            Guid = new Guid("4e591752-a8f6-4df9-9c6c-6214899b567d"),
            TemplateId = 2,
            Template = formTwo
        };
        formTwo.Versions = new List<FormTemplateVersion> { formVersionTwo };
        _fakeformRepository.FormTemplateVersions.Add(formVersionTwo);
        _fakeformRepository.FormTemplates.Add(formTwo);

        var generalId = new GeneralIdRef
        {
            Id = form.Id,
            Guid = form.Guid
        };

        var genralIdTwo = new GeneralIdRef
        {
            Id = formTwo.Id,
            Guid = formTwo.Guid
        };

        //Act
        var res = await _formManager.GetFormTemplateAsync(generalId, default);
        var resTwo = await _formManager.GetFormTemplateAsync(genralIdTwo, default);

        //Assert
        Assert.Multiple(() =>
        {

            Assert.That(res, Is.Not.Null);
            Assert.That(res.Name, Is.EqualTo(firstFormName));
            Assert.That(res.Definition, Is.EqualTo(firstFormDefinition));
            Assert.That(resTwo, Is.Not.Null);
            Assert.That(resTwo.Name, Is.EqualTo(secondFormName));
            Assert.That(resTwo.Definition, Is.EqualTo(secondFormDefinition));
        });
    }

    [Test]
    public void GetForm_NonExistentId_ThrowsFormNotFoundException()
    {
        Assert.ThrowsAsync<NotFoundException>(() => _formManager.GetFormTemplateAsync(new GeneralIdRef { Guid = null, Id = 0 }, default));
    }

    [Test]
    public void GetForm_NUllId_throwsException()
    {
        Assert.ThrowsAsync<NotFoundException>(() => _formManager.GetFormTemplateAsync(null!, default));
    }
}