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.FormsControllerUnitTests;

[TestFixture]
public class DeleteFormTemplateUnitTests : FormsControllerTestBase
{
    [SetUp]
    public override async Task Setup()
        => await base.Setup();

    [Test]
    public async Task DeleteFormTemplate_NormanConditions_Returns200OkObjResult()
    {
        //Arrange
        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("320951a7-ba70-419f-8263-411aec720d59"),
            Id = 5,
        };
        _formsManagerMock?.Setup(x => x.DeleteFormTemplateAsync(It.IsAny<AuditUserDetails>(), generalIdRef, It.IsAny<CancellationToken>())).Returns(Task.CompletedTask);

        //Act
        var res = await _formsController.DeleteForm(generalIdRef);

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

    [Test]
    public void DeleteFormTemplate_NotExistingId_ThrowsNotFoundException()
    {
        var idNum = 5;
        var guid = new Guid("58cc87a3-8819-45b9-91b4-9e0589e83b84");
        const string errorMessage = "Id error";

        var generalIdRef = new GeneralIdRef
        {
            Id = idNum,
            Guid = guid
        };

        _formsManagerMock?.Setup(x => x.DeleteFormTemplateAsync(It.IsAny<AuditUserDetails>(), It.Is<GeneralIdRef>(x => x.Id == idNum && x.Guid == guid), It.IsAny<CancellationToken>())).ThrowsAsync(new NotFoundException(errorMessage));

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