<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\hooks;

use apexl\Config\ConfigurationInterface;
use apexl\Io\includes\Entity;
use apexl\Io\interfaces\AbstractHook;
use apexl\Io\modules\notifier\exceptions\notifierException;
use apexl\Io\modules\user\entities\userEntity;
use apexl\Io\modules\user\Factories\NewUserNotifierFactory;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

final readonly class SendNewUserNotification implements AbstractHook
{
    public function __construct(
        private ConfigurationInterface $configuration,
        private NewUserNotifierFactory $newUserNotifierFactory
    ) {}

    public function hooks(): array
    {
        return [Entity::entityHook(Entity::HOOK__AFTER_FIRST_STORE, userEntity::class)];
    }

    /**
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws notifierException
     * @throws Exception
     */
    public function __invoke($data, userEntity $user): userEntity
    {
        if ($this->configuration->get('auth.send_new_user_notification')) {
            $this->newUserNotifierFactory->make($user)->send();
        }

        return $user;
    }
}
