using e_suite.Database.Core.Tables.Glossaries;
using e_suite.Database.Core.Tables.Sequences;
using eSuite.Core.CustomFields;

namespace e_suite.Modules.CustomFieldManagerUnitTests.CustomFieldHelperUnitTests;

[TestFixture]
public class TranslateToCustomFieldDefinitionAsyncUnitTests : CustomFieldsTestBase<object>
{
    [SetUp]
    public override async Task Setup() => await base.Setup();

    [Test]
    public async Task TranslateToCustomFieldDefinitionAsync_FieldTypeGlossary_ReturnsCorrectInformation()
    {
        //Arrange
        var customField = new CustomField
        {
            FieldType = FieldType.Glossary,
            Id = 42,
            Guid = new Guid("1dda3a6f-24cf-4904-9dbe-1394457e871c"),
            Name = "TestField",
            MinEntries = 0,
            MaxEntries = 1
        };

        var glossaryItem = new Glossary
        {
            Id = 34,
            Guid = new Guid("a4e32489-42be-4e71-9198-4740e01e1e56")
        };

        _fakeCustomFieldReferenceRepository.CustomFieldGlossaries.Add(new CustomFieldGlossary
            {
                CustomField = customField,
                CustomFieldId = customField.Id,
                Glossary = glossaryItem,
                GlossaryId = glossaryItem.Id,
            }
        );

        _fakeCustomFieldReferenceRepository.Glossaries.Add(glossaryItem);

        //Act
        var result = await _customFieldHelper.TranslateToCustomFieldDefinitionAsync(customField, CancellationToken.None);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(result, Is.Not.Null);
            Assert.That(result.FieldType, Is.EqualTo(FieldType.Glossary));
            Assert.That(result.Id, Is.EqualTo(42));
            Assert.That(result.Guid, Is.EqualTo(new Guid("1dda3a6f-24cf-4904-9dbe-1394457e871c")));
            Assert.That(result.Name, Is.EqualTo("TestField"));
            Assert.That(result.MinEntries, Is.EqualTo(0));
            Assert.That(result.MaxEntries, Is.EqualTo(1));
            Assert.That(result.RefElementId, Is.Not.Null);
            Assert.That(result.RefElementId!.Id, Is.EqualTo(34));
            Assert.That(result.RefElementId!.Guid, Is.EqualTo(new Guid("a4e32489-42be-4e71-9198-4740e01e1e56")));
        });
    }

    [Test]
    public async Task TranslateToCustomFieldDefinitionAsync_FieldTypeSequence_ReturnsCorrectInformation()
    {
        //Arrange
        var customField = new CustomField
        {
            FieldType = FieldType.Sequence,
            Id = 83,
            Guid = new Guid("8d8b56f6-df96-4f9a-b94f-51a45d14f5af"),
            Name = "TestSequenceField",
            MinEntries = 0,
            MaxEntries = 1
        };

        var sequenceItem = new Sequence
        {
            Id = 69,
            Guid = new Guid("73ffc36c-6479-4f6e-afa7-4d65f35d8d20")
        };

        _fakeCustomFieldReferenceRepository.CustomFieldSequences.Add(new CustomFieldSequence
            {
                CustomField = customField,
                CustomFieldId = customField.Id,
                Sequence = sequenceItem,
                SequenceId = sequenceItem.Id,
            }
        );

        _fakeCustomFieldReferenceRepository.Sequences.Add(sequenceItem);

        //Act
        var result = await _customFieldHelper.TranslateToCustomFieldDefinitionAsync(customField, CancellationToken.None);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(result, Is.Not.Null);
            Assert.That(result.FieldType, Is.EqualTo(FieldType.Sequence));
            Assert.That(result.Id, Is.EqualTo(83));
            Assert.That(result.Guid, Is.EqualTo(new Guid("8d8b56f6-df96-4f9a-b94f-51a45d14f5af")));
            Assert.That(result.Name, Is.EqualTo("TestSequenceField"));
            Assert.That(result.MinEntries, Is.EqualTo(0));
            Assert.That(result.MaxEntries, Is.EqualTo(1));
            Assert.That(result.RefElementId, Is.Not.Null);
            Assert.That(result.RefElementId!.Id, Is.EqualTo(69));
            Assert.That(result.RefElementId!.Guid, Is.EqualTo(new Guid("73ffc36c-6479-4f6e-afa7-4d65f35d8d20")));
        });
    }


    [Test]
    public async Task TranslateToCustomFieldDefinitionAsync_FieldTypeNumber_ReturnsCorrectInformation()
    {
        //Arrange
        var customField = new CustomField
        {
            FieldType = FieldType.Number,
            Id = 83,
            Guid = new Guid("f0175809-0b4e-4fe2-8fd1-c2bd9949b546"),
            Name = "TestNumberField",
            MinEntries = 0,
            MaxEntries = 1
        };

        _fakeCustomFieldReferenceRepository.CustomFieldNumber.Add( new CustomFieldNumber
        {
            CustomFieldId = customField.Id,
            CustomField = customField,
            MinimumValue = 10,
            MaximumValue = 100,
            Step = 5
        });

        //Act
        var result = await _customFieldHelper.TranslateToCustomFieldDefinitionAsync(customField, CancellationToken.None);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(result, Is.Not.Null);
            Assert.That(result.FieldType, Is.EqualTo(FieldType.Number));
            Assert.That(result.Id, Is.EqualTo(83));
            Assert.That(result.Guid, Is.EqualTo(customField.Guid));
            Assert.That(result.Name, Is.EqualTo(customField.Name));
            Assert.That(result.MinEntries, Is.EqualTo(0));
            Assert.That(result.MaxEntries, Is.EqualTo(1));
            Assert.That(result.RefElementId, Is.Null);
            Assert.That(result.Parameters, Is.EqualTo("{\"minValue\":\"1\",\"maxValue\":\"1\",\"step\":\"5\"}"));
        });
    }
}