using e_suite.API.Common.exceptions;

namespace e_suite.Modules.CustomFieldManagerUnitTests.CustomFieldManagerUnitTests;

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

    [Test]
    public async Task GetCustomField_AllNormal_ReturnsCustomField()
    {
        var guid = Guid.NewGuid();
        var name = "testtName";
        var customField = new CustomField
        {
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Deleted = false,
            DefaultValue = "testtt",
            Guid = guid,
            Id = 15,
            Name = name,
        };
        _customFieldManager = new CustomFieldManager(_fakeCustomFieldRepository, _customFieldReferenceObjectRepository.Object, _customFieldHelper, PatchFactoryMock.Object);
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        var res = await _customFieldManager.GetFieldAsync(name,default);
        Assert.Multiple(() =>
        {
            Assert.That(res, Is.Not.Null);
            Assert.That(res.Guid, Is.EqualTo(guid));
        });

    }
    [Test]
    public void GetCustomField_WhenDeletedIsTrue_ThrowsFieldNotFoundException()
    {
        var guid = Guid.NewGuid();
        var id = 15;
        var name = "Second";
        var customField = new CustomField
        {
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Deleted = true,
            DefaultValue = "TT",
            Guid = guid,
            Id = id,
            Name = name
        };
        _customFieldRepository.Setup(r => r.GetByNameAsync(It.Is<string>(x => x == name), It.IsAny<CancellationToken>())).ReturnsAsync(customField);
        Assert.ThrowsAsync<NotFoundException>(() => _customFieldManager.GetFieldAsync(name,default));
    }

    [Test]
    public void GetCustomField_NullValueForNama_ThrowsFieldNotFoundException()
    {
        var guid = Guid.NewGuid();
        var id = 15;
        string name = "Second";
        var customField = new CustomField
        {
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Deleted = true,
            DefaultValue = "TT",
            Guid = guid,
            Id = id,
            Name = name
        };
        _customFieldRepository.Setup(r => r.GetByNameAsync(It.Is<string>(x => x == name), It.IsAny<CancellationToken>())).ReturnsAsync(customField);
        Assert.ThrowsAsync<NotFoundException>(() => _customFieldManager.GetFieldAsync((null as string)!,default));
    }
}
