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

namespace eSuite.API.UnitTests.Controllers.SequenceManagerControllerUnitTests;

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

    [Test]
    public async Task NextValue_Called_IssuesNextValueAndReturnsOK()
    {
        // Arrange
        var responses = new List<string> { "Order 001 from 12/10/2022" };

        var guid = new Guid("fbe77372-0f7e-4a93-ad8e-11426ea1562e");
        var sequence = new Sequence
        {
            GeneralIdRef = new GeneralIdRef
            {
                Guid = guid
            },
            Name = $"sequence-{guid}",
            Pattern = "Order [000] from {DD}/{MM}/{YYYY}",
            Increment = 1,
            RolloverType = Rollover.Continuous,
            Seed = 0
        };

        _sequenceManagerMock.Setup(x => x.NextValue(It.IsAny<AuditUserDetails>(), It.IsAny<GeneralIdRef>(), It.IsAny<CancellationToken>())).ReturnsAsync(responses);

        _sequenceManagerMock.Setup(x => x.NextValue(It.IsAny<AuditUserDetails>(), It.IsAny<GeneralIdRef>(), 1, It.IsAny<CancellationToken>())).ReturnsAsync(responses);

        // Act
        var generalId = new GeneralIdRef { Guid = sequence.GeneralIdRef.Guid };

        var sequenceIssueRequest = new SequenceIssueRequest
        {
            GeneralIdRef = generalId,
            Count = 1
        };

        var actualResult = await _sequenceController.NextValue(sequenceIssueRequest);

        // Assert
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkObjectResult)));

        var objectResult = actualResult as OkObjectResult;
        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
        Assert.That(objectResult?.Value, Is.Not.Null);
        Assert.That(objectResult!.Value!.GetType, Is.EqualTo(typeof(List<string>)));
    }
}