using e_suite.API.Common.models;
using e_suite.Utilities.Pagination;
using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;

namespace eSuite.API.UnitTests.Controllers.FormsControllerUnitTests;

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

    [Test]
    public async Task GetFormTemplates_NormalConditions_ReturnsOkObjResult()
    {
        //Arrange
        var paginatedData = new PaginatedData<GetFormTemplate>
        {
            Count = 2,
            Data = new List<GetFormTemplate> { new() },
            Page = 1,
            PageSize = 10
        };

        _formsManagerMock?.Setup(x => x.GetFormTemplatesAsync(It.IsAny<Paging>(), It.IsAny<CancellationToken>())).ReturnsAsync(paginatedData);

        var paging = new Paging();

        //Act
        var res = await _formsController.GetFormTemplates(paging);

        //Assert
        Assert.That(res, Is.Not.Null);
        Assert.That(res?.GetType(), Is.EqualTo(typeof(OkObjectResult)));
    }

    [Test]
    public async Task GetFormTemplates_NormalConditions_ReturnsExpectedObjects()
    {
        //Arrange
        var formTemplateOne = new GetFormTemplate
        {
            Definition = "Test",
            Name = "Name"
        };
        var formTemplateTwo = new GetFormTemplate
        {
            Definition = "Test",
            Name = "Name"
        };
        var collection = new List<GetFormTemplate> { formTemplateOne, formTemplateTwo };

        var paginatedData = new PaginatedData<GetFormTemplate>
        {
            Count = 2,
            Data = collection,
            Page = 1,
            PageSize = 10
        };

        _formsManagerMock?.Setup(x => x.GetFormTemplatesAsync(It.IsAny<Paging>(), It.IsAny<CancellationToken>())).ReturnsAsync(paginatedData);
        var paging = new Paging();

        //Act
        var res = await _formsController.GetFormTemplates(paging);

        //Assert
        Assert.That(res?.GetType(), Is.EqualTo(typeof(OkObjectResult)));
        var objectResult = res as OkObjectResult;
        Assert.That(objectResult?.Value, Is.Not.Null);
        if (objectResult?.Value != null)
        {
            Assert.That(objectResult.Value.GetType(), Is.EqualTo(typeof(PaginatedData<GetFormTemplate>)));
            var formCollection = objectResult?.Value as PaginatedData<GetFormTemplate>;
            Assert.That(formCollection?.Count, Is.EqualTo(2));
        }
    }
}