<?php

namespace common\logic\User\User\Service;

use common\helpers\GlobalParam;
use common\logic\AbstractService;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\User\Model\User;
use common\logic\UtilsInterface;

class UserUtils extends AbstractService implements UtilsInterface
{
    public function sendEmailSignup(User $user, Producer $producer)
    {
        \Yii::$app->mailer->compose(
            [
                'html' => 'signup-html',
                'text' => 'signup-text'
            ],
            [
                'user' => $user,
                'producer' => $producer
            ])
            ->setTo($user->email)
            ->setFrom([\Yii::$app->params['adminEmail'] => 'distrib'])
            ->setSubject('[Opendistrib] Inscription')
            ->send();
    }

    /**
     * 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 = Producer::findOne(GlobalParam::getCurrentProducerId());
            \Yii::$app->mailer->compose();
            $mail = \Yii::$app->mailer->compose(
                ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $user, 'producer' => $producer, 'password' => $password]
            )
                ->setTo($user->email)
                ->setFrom(['contact@opendistrib.net' => 'distrib'])
                ->setSubject('[Opendistrib] Inscription')
                ->send();
        }
    }
}