<?php

declare(strict_types=1);

namespace Apexl\Tests\User\UnitTests;

use apexl\Config\ConfigurationMock;
use apexl\Io\modules\template\Services\TemplateService;
use apexl\Io\modules\user\services\ForgotPasswordEmailFactory;
use apexl\Io\modules\user\userModule;
use apexl\Io\services\HttpPaths;
use apexl\Io\services\Mailer;
use apexl\Io\TestInfrastructure\UnitTest;
use PHPUnit\Framework\Attributes\Test;
use function apexl\Io\container;

final class ForgotPasswordEmailFactoryTest extends UnitTest
{
    #[Test]
    public function makeEmail(): void
    {
        $fromEmail = $this->faker->email();
        $fromName = $this->faker->name();
        $frontEndDomain = $this->faker->domainName();

        $toName = $this->faker->name();
        $toEmail = $this->faker->email();
        $toLink = $this->faker->text();

        $config = new ConfigurationMock([
            'app.site.email_address' => $fromEmail,
            'app.site.name' => $fromName,
            'http.frontend_domain' => $frontEndDomain,
        ]);

        $templateService = container()->get(TemplateService::class);
        $templateService->loader->addDirectory(userModule::moduleDir('templates'));


        $factory = container()->make(ForgotPasswordEmailFactory::class, [
            'config' => $config,
            'templateService' => $templateService,
        ]);

        $httpPaths = container()->get(HttpPaths::class);

        $mailer = $factory->create($toName, $toEmail, $toLink);

        $this->assertInstanceOf(Mailer::class, $mailer);
        $this->assertEquals($fromEmail, $mailer->From);
        $this->assertEquals($fromName, $mailer->FromName);
        $toAddresses = $mailer->getToAddresses();
        $this->assertCount(1, $toAddresses);
        $this->assertEquals($toEmail, $toAddresses[0][0]);
        $this->assertEquals($toName, $toAddresses[0][1]);
        $this->assertStringContainsString($httpPaths->frontendUrl($toLink), $mailer->Body);
    }
}