<?php

namespace common\logic\User\User\Service;

use common\helpers\GlobalParam;
use common\logic\AbstractNotifier;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Ticket\Ticket\Model\Ticket;
use common\logic\User\User\Model\User;
use common\logic\ManagerInterface;

class UserNotifier extends AbstractNotifier implements ManagerInterface
{
    protected UserSolver $userSolver;

    public function loadDependencies(): void
    {
        parent::loadDependencies();
        $this->userSolver = $this->loadService(UserSolver::class);
    }

    public function sendEmailSignup(User $user, Producer $producer)
    {
        $this->mailer->sendFromSite(
            'Inscription',
            'signup',
            [
                'user' => $user,
                'producer' => $producer
            ],
            $user->email
        );
    }

    /**
     * Envoie un email de bienvenue à l'utilisateur lors de son inscription
     * via le backend du site.
     */
    public function sendMailWelcome(User $user, string $password): void
    {
        if (strlen($user->email)) {
            $producer = $this->getProducerContext();
            $this->mailer->sendFromProducer(
                'Inscription',
                'createUserAdmin',
                [
                    'user' => $user,
                    'producer' => $producer,
                    'password' => $password
                ],
                $user->email,
                $producer
            );
        }
    }

    public function sendMailNewTicketAdmin(Ticket $ticket)
    {
        $this->mailer->sendAdmin(
            'Nouveau ticket',
            'newTicketAdmin',
            [
                'ticket' => $ticket,
            ]
        );
    }

    public function sendMailNewPassword(User $user, string $password)
    {
        $producer = $this->getProducerContext();
        \Yii::$app->mailerService->sendFromProducer(
            'Nouveau mot de passe',
            'newPasswordUserAdmin',
            [
                'user' => $user,
                'producer' => $producer,
                'password' => $password,
            ],
            $user->email,
            $producer
        );
    }
}