using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.CustomFields;
using e_suite.Database.Core.Tables.Forms;
using e_suite.Database.Core.Tables.Glossaries;
using e_suite.Modules.SpecificationManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.SpecificationManager.UnitTests;

[TestFixture]
public class GetTemplateForPrintSpecUnitTests : SpecificationManagerTestBase
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void GetTemplateForPrint_WhenGlossaryNotFound_ThrowsException()
    {
        //Arrange

        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("0285dfde-e130-4ed2-9c6b-64b139fb6477")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var template = await SpecificationManager.GetTemplateForPrintSpec(generalIdRef, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Template glossary item does not exist"));
    }

    [Test]
    public void GetTemplateForPrint_WhenGlossaryNotDefinedProperly_ThrowsException()
    {
        //Arrange

        var glossary = new Glossary()
        {
            Guid = new Guid("9929fec1-0fdb-4dd2-8a5d-4badc2e5a4b1")
        };
        GlossariesManagerRepository.Glossaries.Add(glossary);

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

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var template = await SpecificationManager.GetTemplateForPrintSpec(generalIdRef, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Glossary does not contain template reference"));
    }

    [Test]
    public void GetTemplateForPrint_WhenTemplateNotFound_ThrowsException()
    {
        //Arrange
        var glossary = new Glossary
        {
            Guid = new Guid("9929fec1-0fdb-4dd2-8a5d-4badc2e5a4b1"),
            CustomFieldValues =
            [
                new()
                {
                    CustomField = new CustomField
                    {
                        Guid = new Guid("{8D910089-3079-4A29-ABAD-8DDF82DB6DBB}")
                    },
                    Value = new Guid("065b2e4d-e324-4f0d-bec0-8e3a738b8ada").ToString()
                }
            ]
        };
        GlossariesManagerRepository.Glossaries.Add(glossary);

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

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var template = await SpecificationManager.GetTemplateForPrintSpec(generalIdRef, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Template does not exist"));
    }

    [Test]
    public async Task GetTemplateForPrint_WhenTemplateFound_ReturnsTemplateGeneralIdRef()
    {
        //Arrange
        var formTemplateGuid = new Guid("ab75d0f6-f018-468b-80a4-fb696228124b");

        var glossary = new Glossary
        {
            Guid = new Guid("9929fec1-0fdb-4dd2-8a5d-4badc2e5a4b1"),
            CustomFieldValues =
            [
                new()
                {
                    CustomField = new CustomField
                    {
                        Guid = new Guid("{8D910089-3079-4A29-ABAD-8DDF82DB6DBB}")
                    },
                    Value = formTemplateGuid.ToString()
                }
            ]
        };
        GlossariesManagerRepository.Glossaries.Add(glossary);

        var formTemplate = new FormTemplate
        {
            Guid = formTemplateGuid,
            Id = 872364
        };
        FormRepository.FormTemplates.Add(formTemplate);

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

        //Act
        var template = await SpecificationManager.GetTemplateForPrintSpec(generalIdRef, CancellationToken.None);

        //Assert
        Assert.That(template, Is.Not.Null);
        Assert.That(template!.Guid, Is.EqualTo(formTemplateGuid));
        Assert.That(template.Id, Is.EqualTo(formTemplate.Id));
    }
}