using e_suite.API.Common.exceptions;

namespace e_suite.Modules.CustomFieldManagerUnitTests.CustomFieldManagerUnitTests;

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

    [Test]
    public void Deactivate_NoMachingId_ThrowsException()
    {
        var guid = Guid.NewGuid();
        var customField = new CustomField
        {
            DefaultValue = "asdasd",
            Deleted = false,
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Guid = guid,
            Name = "Test",
            Id = 5,
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        _customFieldManager = new CustomFieldManager(_fakeCustomFieldRepository, _customFieldReferenceObjectRepository.Object, _customFieldHelper, PatchFactoryMock.Object);
        Assert.ThrowsAsync<NotFoundException>(() => _customFieldManager.DeleteFieldAsync(auditResult, new GeneralIdRef { Guid = Guid.NewGuid() }, default));
    }

    [Test]
    public async Task Deactivate_NormalContidions_SaveChanges()
    {
        var guid = Guid.NewGuid();
        var customField = new CustomField
        {
            DefaultValue = "asdasd",
            Deleted = false,
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Guid = guid,
            Name = "Test",
            Id = 5,
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        await _customFieldManager.DeleteFieldAsync(auditResult, new GeneralIdRef { Guid = guid }, default);
        Assert.That(customField.Deleted, Is.EqualTo(true));
    }

    [Test]
    public void DeactivateNotexistingCustomfieldthrowsException()
    {
        _customFieldManager = new CustomFieldManager(_fakeCustomFieldRepository, _customFieldReferenceObjectRepository.Object, _customFieldHelper, PatchFactoryMock.Object);
        Assert.ThrowsAsync<NotFoundException>(() => _customFieldManager.DeleteFieldAsync(auditResult, new GeneralIdRef { Guid = Guid.NewGuid() }, default));
    }

    [Test]
    public void Deactivate_AlreadyDeactivatedField_ThrowsException()
    {
        var guid = Guid.NewGuid();
        var customField = new CustomField
        {
            DefaultValue = "asdasd",
            Deleted = true,
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Guid = guid,
            Name = "Test",
            Id = 5,
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        Assert.ThrowsAsync<NotFoundException>(() => _customFieldManager.DeleteFieldAsync(auditResult, new GeneralIdRef { Guid = guid }, default));
    }

    [Test]
    public async Task Deactivate_OnlyGivenByIdNoGuid_SaveChanges()
    {
        var guid = Guid.NewGuid();
        var customField = new CustomField
        {
            DefaultValue = "asdasd",
            Deleted = false,
            FieldType = eSuite.Core.CustomFields.FieldType.Text,
            Guid = guid,
            Name = "Test",
            Id = 5,
        };
        _fakeCustomFieldRepository.CustomFields.Add(customField);
        await _customFieldManager.DeleteFieldAsync(auditResult, new GeneralIdRef { Id = 5, Guid = null }, default);
        Assert.That(customField.Deleted, Is.EqualTo(true));
    }
}
