using e_suite.API.Common.exceptions;
using e_suite.API.Common.models;
using e_suite.Database.Audit;
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 DeleteSequenceUnitTests : SequenceControllerBaseTest
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void DeleteSequence_WhenNotFound_ThrowsException()
    {
        // Arrange
        const string errorText = "Sequence not found.";

        var guid = new Guid("6461d22e-9abc-4563-8d30-cb6298cf2e10");
        var sequence = new Sequence
        {
            GeneralIdRef = new GeneralIdRef { Guid = guid },
            Name = $"sequence-{guid}",
            Pattern = "[0]",
            Increment = 1,
            RolloverType = Rollover.Continuous,
            Seed = 0
        };

        _sequenceManagerMock.Setup(x => x.DeleteSequence(It.IsAny<AuditUserDetails>(), sequence.GeneralIdRef, It.IsAny<CancellationToken>())).Throws(new NotFoundException(errorText));

        // Assert
        Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            // Act
            var actualResult = await _sequenceController.DeleteSequence(sequence.GeneralIdRef);
        });
    }

    [Test]
    public async Task DeleteSequence_WhenSequenceDeleted_ReturnsOK()
    {
        // Arrange
        var guid = new Guid("e88dc220-bdb6-4915-a31a-ad35ecbd1c83");
        var sequence = new Sequence
        {
            GeneralIdRef = new GeneralIdRef { Guid = guid },
            Name = $"sequence-{guid}",
            Pattern = "[0]",
            Increment = 1,
            RolloverType = Rollover.Continuous,
            Seed = 0
        };

        // Act
        var actualResult = await _sequenceController.DeleteSequence(sequence.GeneralIdRef);

        // Assert
        _sequenceManagerMock.Verify(x => x.DeleteSequence(It.IsAny<AuditUserDetails>(), sequence.GeneralIdRef, It.IsAny<CancellationToken>()), Times.Once());
        Assert.That(actualResult.GetType(), Is.EqualTo(typeof(OkResult)));
        var objectResult = actualResult as OkResult;
        Assert.That(objectResult?.StatusCode, Is.EqualTo(200));
    }
}