using e_suite.API.Common.exceptions;

namespace e_suite.Modules.CustomFieldManagerUnitTests.CustomFieldManagerUnitTests;

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

    [Test]
    public async Task GetCustomField_GuidIdOnly_ReturnsCustomField()
    {
        var guid = Guid.NewGuid();
        var customField = new CustomField
        {
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Deleted = false,
            DefaultValue = "testtt",
            Guid = guid,
            Id = 15,
            Name = "testtName"
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        var idGetCustomIdField = new GeneralIdRef
        {
            Guid = guid,
            Id = null
        };
        var res = await _customFieldManager.GetFieldAsync(idGetCustomIdField, default);

        Assert.Multiple(() =>
        {
            Assert.That(res, Is.Not.Null);
            Assert.That(res.Name, Is.EqualTo("testtName"));
            Assert.That(res.FieldType, Is.EqualTo(eSuite.Core.CustomFields.FieldType.Text));
        });
    }

    [Test]
    public async Task GetCustomField_IdOnly_ReturnsCustomField()
    {
        var guid = Guid.NewGuid();
        var id = 15;
        var customField = new CustomField
        {
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Deleted = false,
            DefaultValue = "TT",
            Guid = guid,
            Id = 15,
            Name = "Second"
        };
        var idGetCustomIdField = new GeneralIdRef
        {
            Guid = null,
            Id = id
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);

        var res = await _customFieldManager.GetFieldAsync(idGetCustomIdField, default);

        Assert.Multiple(() =>
        {
            Assert.That(res, Is.Not.Null);
            Assert.That(res.DefaultValue, Is.EqualTo("TT"));
            Assert.That(res.Guid, Is.EqualTo(guid));
        });
    }

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

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

}