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 EditSequenceUnitTests : SequenceControllerBaseTest
{
    [SetUp]
    public override async Task Setup()
    {
        await base.Setup();
    }

    [Test]
    public void EditSequence_WhenNewSequenceNameAlreadyExists_ThrowsExistsException()
    {
        // Arrange
        const string errorText = "A sequence with this name already exists";

        var guid = new Guid("cdc17205-17ba-473b-87aa-c91509338d97");
        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.EditSequence(It.IsAny<AuditUserDetails>(), sequence, It.IsAny<CancellationToken>())).Throws(new ExistsException(errorText));

        // Assert
        Assert.ThrowsAsync<ExistsException>(async () =>
        {
            // Act
            var actualResult = await _sequenceController.EditSequence(sequence);
        });
    }

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

        var guid = new Guid("62cb2b8f-85ed-4c3a-a986-797270cfca57");
        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.EditSequence(It.IsAny<AuditUserDetails>(), sequence, It.IsAny<CancellationToken>())).Throws(new NotFoundException(errorText));

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

    [Test]
    public async Task EditSequence_WhenSequenceFound_ReturnsOK()
    {
        // Arrange
        var guid = new Guid("00339b4d-4ce8-4097-8b34-b645164c9926");
        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.EditSequence(sequence);

        // Assert
        _sequenceManagerMock.Verify(x => x.EditSequence(It.IsAny<AuditUserDetails>(), sequence, 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));
    }
}