using e_suite.API.Common.exceptions;
using e_suite.Database.Core.Tables.Domain;
using e_suite.Database.Core.Tables.Mail;
using e_suite.Modules.MailTemplatesManager.UnitTests.Helpers;
using eSuite.Core.MailService;
using eSuite.Core.Miscellaneous;
using Moq;
using NUnit.Framework;

namespace e_suite.Modules.MailTemplatesManager.UnitTests;

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

    [Test]
    public void GetMailTemplate_WhenDomainMissing_ThrowsException()
    {
        //Arrange
        var domain = new GeneralIdRef
        {
            Guid = new Guid("9e6665ba-f38b-4051-9cdd-3d981859efb7")
        };

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var template = await MailTemplateManager.GetMailTemplate(domain, MailType.ConfirmEmailAddress, CancellationToken.None);
        });

        Assert.That(actualResult!.Message, Is.EqualTo("Unable to find domain"));
    }

    [Test]
    public void GetMailTemplate_MailTemplateDoesNotExist_ThrowsNotFoundException()
    {
        //Arrange
        var domain = new GeneralIdRef
        {
            Guid = new Guid("9e6665ba-f38b-4051-9cdd-3d981859efb7")
        };

        var dbDomain = new Domain
        {
            Id = 100,
            Guid = domain.Guid.Value,
            Name = "Testing domain"
        };

        DomainRepositoryMock.Setup(x => x.GetDomainById(domain, It.IsAny<CancellationToken>())).ReturnsAsync(dbDomain);

        //Assert
        var actualResult = Assert.ThrowsAsync<NotFoundException>(async () =>
        {
            //Act
            var template = await MailTemplateManager.GetMailTemplate(domain, MailType.ConfirmEmailAddress, CancellationToken.None);
        });

        Assert.That(actualResult!.Message, Is.EqualTo("Unable to find mail template"));
    }

    [Test]
    public async Task GetMailTemplate_MailTemplateFoundForDomain_ReturnsDomainMailTemplate()
    {
        //Arrange
        var dbDomain = new Domain
        {
            Id = 100,
            Guid = new Guid("c7645f28-c492-4bd5-9b2e-68b9a2eb3e39"),
            Name = "Testing domain"
        };

        var domain = new GeneralIdRef
        {
            Guid = dbDomain.Guid
        };

        var domainMailTemplate = new MailTemplate
        {
            MailType = MailType.ConfirmEmailAddress,
            Subject = "Confirm Email Address (Domain level)",
            TemplateDefinition = "This is the message Content"
        };

        DomainRepositoryMock.Setup(x => x.GetDomainById(domain, It.IsAny<CancellationToken>())).ReturnsAsync(dbDomain);
        MailTemplatesManagerRepositoryMock.Setup(x =>
            x.GetMailTemplate(dbDomain.Id, MailType.ConfirmEmailAddress, It.IsAny<CancellationToken>())).ReturnsAsync(domainMailTemplate);
        
        //Act
        var template = await MailTemplateManager.GetMailTemplate(domain, MailType.ConfirmEmailAddress, CancellationToken.None);

        //Assert
        Assert.That(template.IsOverridden, Is.True);
        Assert.That(template.Subject, Is.EqualTo(domainMailTemplate.Subject));
        Assert.That(template.TemplateDefinition, Is.EqualTo(domainMailTemplate.TemplateDefinition));
    }

    [Test]
    public async Task GetMailTemplate_MailTemplateFoundForDomain_ReturnsSystemMailTemplate()
    {
        //Arrange
        var dbDomain = new Domain
        {
            Id = 100,
            Guid = new Guid("c7645f28-c492-4bd5-9b2e-68b9a2eb3e39"),
            Name = "Testing domain"
        };

        var domain = new GeneralIdRef
        {
            Guid = dbDomain.Guid
        };

        var systemMailTemplate = new MailTemplate
        {
            MailType = MailType.ConfirmEmailAddress,
            Subject = "Confirm Email Address (System level)",
            TemplateDefinition = "This is the message Content"
        };

        DomainRepositoryMock.Setup(x => x.GetDomainById(domain, It.IsAny<CancellationToken>())).ReturnsAsync(dbDomain);
        MailTemplatesManagerRepositoryMock.Setup(x => x.GetMailTemplate(1, MailType.ConfirmEmailAddress, It.IsAny<CancellationToken>())).ReturnsAsync(systemMailTemplate);

        //Act
        var template = await MailTemplateManager.GetMailTemplate(domain, MailType.ConfirmEmailAddress, CancellationToken.None);

        //Assert
        Assert.That(template.IsOverridden, Is.False);
        Assert.That(template.Subject, Is.EqualTo(systemMailTemplate.Subject));
        Assert.That(template.TemplateDefinition, Is.EqualTo(systemMailTemplate.TemplateDefinition));
    }
}