using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
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 EditFormTemplateUnitTests : FormsControllerTestBase
{
    [SetUp]
    public override Task Setup()
        => base.Setup();

    [Test]
    public async Task EditFormTemplate_NormalConditions_ReturnsOkObjectResult()
    {
        //Arrange
        var formGuid = new Guid("b8094034-0bf5-49b4-a691-a1875547ba63");
        var definition = "Definition";
        var name = "Name";

        var editModel = new EditFormTemplate
        {
            Id = new GeneralIdRef
            {
                Id = 5,
                Guid = formGuid
            },
            Definition = definition,
            Name = name,
        };

        //Act
        var res = await _formsController.EditFormTempalate(editModel);

        //Assert
        _formsManagerMock!.Verify(x => x.EditFormTemplateAsync(It.IsAny<AuditUserDetails>(), It.Is<EditFormTemplate>(f => f.Name == name && f.Definition == definition && f.Id.Guid == formGuid), It.IsAny<CancellationToken>()));
        Assert.That(res.GetType(), Is.EqualTo(typeof(OkResult)));
    }

    [Test]
    public void EditFormTemplate_NonExistingGeneralIdRef_ThrowsNotFoundException()
    {
        //Arrange
        var formGuid = new Guid("c758bc67-18c6-49ce-98e3-e19439bb7dbf");
        var id = 1;
        var errorMessage = "Tessst Error";
        var genralId = new GeneralIdRef
        {
            Guid = formGuid,
            Id = id
        };
        var editForm = new EditFormTemplate
        {
            Id = genralId
        };
        _formsManagerMock?.Setup(x => x.EditFormTemplateAsync(It.IsAny<AuditUserDetails>(), It.Is<EditFormTemplate>(f => f.Id.Guid == formGuid && f.Id.Id == id), It.IsAny<CancellationToken>())).ThrowsAsync(new NotFoundException(errorMessage));

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