using e_suite.API.Common.exceptions;
using e_suite.Database.Audit;
using eSuite.Core.Miscellaneous;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.CustomFieldControllerUnitTest;

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

    [Test]
    public void DeactivateCustomField_NotFound_Returns404()
    {
        //Arrange
        const string errorText = "There is no CustomField with this Id";

        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("220bd77a-e9d2-45aa-a50f-54a983cda0a6"),
            Id = 5,
        };
        _customFieldManagerMock?.Setup(x => x.DeleteFieldAsync(It.IsAny<AuditUserDetails>(), generalIdRef, default)).ThrowsAsync(new NotFoundException(errorText));

        //Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var res = await _customFieldController.DeleteField(generalIdRef);
        });
    }

    [Test]
    public async Task DeactivateCustomField_BouthId_Returns200()
    {
        //Arrange
        var genralId = new GeneralIdRef
        {
            Guid = new Guid("d558ef68-7685-4d8d-a8d2-ccdbdf1009c3"),
            Id = 5,
        };
        _customFieldManagerMock?.Setup(x => x.DeleteFieldAsync(It.IsAny<AuditUserDetails>(), genralId, default)).Returns(Task.CompletedTask);

        //Act
        var res = await _customFieldController.DeleteField(genralId);

        //Assert
        Assert.That(res.GetType(), Is.EqualTo(typeof(OkResult)));
    }

    [Test]
    public async Task EditCustomField_IdOnlyGuidId_Returns200()
    {
        //Arrange
        var guid = new Guid("7e73f9d8-7ce3-4723-978c-99244af7c350");
        var generalIdRef = new GeneralIdRef
        {
            Guid = guid,
            Id = null,
        };
        _customFieldManagerMock?.Setup(x => x.DeleteFieldAsync(It.IsAny<AuditUserDetails>(), It.Is<GeneralIdRef>(x => x.Guid == guid), default)).Returns(Task.CompletedTask);

        //Act
        var res = await _customFieldController.DeleteField(generalIdRef);

        //Assert
        Assert.That(res.GetType(), Is.EqualTo(typeof(OkResult)));
    }
}
