using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.Forms;
using e_suite.Database.Core.Tables.Printer;
using e_suite.Modules.SpecificationManager.UnitTests.Helpers;
using eSuite.Core.Miscellaneous;
using NUnit.Framework;

namespace e_suite.Modules.SpecificationManager.UnitTests;

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

    [Test]
    public void GetSpecification_WhenSpecificationMissing_ThrowsNotFoundException()
    {
        //Arrange
        var generalIdRef = new GeneralIdRef
        {
            Guid = new Guid("1326b676-e9a7-406a-a0ef-710b87290d57")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var result = await SpecificationManager.GetSpecification(generalIdRef, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Specification not found"));
    }

    [Test]
    public void GetSpecification_WhenSpecificationHasNoFormInstance_ReturnsNotFoundException()
    {
        //Arrange
        var specification = new Specification
        {
            Guid = new Guid("1326b676-e9a7-406a-a0ef-710b87290d57")
        };
        SpecificationManagerRepository.Specifications.Add(specification);

        var generalIdRef = new GeneralIdRef
        {
            Guid = specification.Guid
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var result = await SpecificationManager.GetSpecification(generalIdRef, CancellationToken.None);
        });
        Assert.That(actualResult!.Message, Is.EqualTo("Form instance not found"));
    }

    [Test]
    public async Task GetSpecification_WhenSpecificationLoadsCorrectly_ReturnsReadSpecification()
    {
        //Arrange
        var site = new Site
        {
            Id = 235423,
            Guid = new Guid("28df402e-5991-43d5-9342-72083f797d99"),
            Name = "Room of requirement"
        };

        var formInstance = new FormInstance()
        {
            Id = 12354,
            Guid = new Guid("1326b676-e9a7-406a-a0ef-710b87290d57")
        };
        FormRepository.FormInstances.Add(formInstance);

        var specification = new Specification
        {
            Guid = new Guid("1326b676-e9a7-406a-a0ef-710b87290d57"),
            FormInstanceId = formInstance.Id,
            Name = "My Specification",
            Site = site,
            SiteId = site.Id
        };
        SpecificationManagerRepository.Specifications.Add(specification);

        var generalIdRef = new GeneralIdRef
        {
            Guid = specification.Guid
        };

        //Act
        var result = await SpecificationManager.GetSpecification(generalIdRef, CancellationToken.None);

        //Assert
        Assert.Multiple(() =>
        {
            Assert.That(result, Is.Not.Null);
            Assert.That(result!.Guid, Is.EqualTo(specification.Guid));
            Assert.That(result.Id, Is.EqualTo(specification.Id));
            Assert.That(result.FormInstanceId.Id, Is.EqualTo(formInstance.Id));
            Assert.That(result.FormInstanceId.Guid, Is.EqualTo(formInstance.Guid));
            Assert.That(result.Name, Is.EqualTo(specification.Name));
            Assert.That(result.Site.Id, Is.EqualTo(site.Id));
            Assert.That(result.Site.Guid, Is.EqualTo(site.Guid));
        });
    }
}